Skip to content

Commit

Permalink
refactor: enforce version for vbufs
Browse files Browse the repository at this point in the history
  • Loading branch information
tris203 committed Nov 11, 2024
1 parent 2d7f9d6 commit a2cb16e
Show file tree
Hide file tree
Showing 14 changed files with 39 additions and 33 deletions.
7 changes: 4 additions & 3 deletions lua/rzls/documentstore.lua
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,15 @@ local function uri_to_path(uri)
end

---@param uri string
---@param version integer
---@param type razor.LanguageKind
---@param version integer | "any"?
---@return rzls.VirtualDocument | nil
function M.get_virtual_document(uri, version, type)
function M.get_virtual_document(uri, type, version)
local doc = virtual_documents[uri_to_path(uri)]
if type == razor.language_kinds.razor then
return doc
end
assert(version, "version is required for virtual documents")

---@type rzls.VirtualDocument
local virtual_document = doc and doc[type]
Expand All @@ -110,7 +111,7 @@ function M.get_virtual_document(uri, version, type)
return nil
end

if version == nil or virtual_document.host_document_version == version then
if version == "any" or virtual_document.host_document_version == version then
return virtual_document
end

Expand Down
4 changes: 2 additions & 2 deletions lua/rzls/handlers/completion.lua
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ return function(err, result, _ctx, _config)

local virtual_document = documentstore.get_virtual_document(
result.identifier.textDocumentIdentifier.uri,
result.identifier.version,
result.projectedKind
result.projectedKind,
result.identifier.version
)
assert(virtual_document, "No virtual document found")

Expand Down
4 changes: 2 additions & 2 deletions lua/rzls/handlers/completionitemresolve.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ return function(err, result, _ctx, _config)
if result.originatingKind == razor.language_kinds.csharp and result.completionItem.data.TextDocument ~= nil then
local virtual_document = documentstore.get_virtual_document(
result.identifier.textDocumentIdentifier.uri,
result.identifier.version,
result.originatingKind
result.originatingKind,
result.identifier.version
)
assert(virtual_document, "No virtual document found")

Expand Down
8 changes: 2 additions & 6 deletions lua/rzls/handlers/csharppulldiagnostics.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,8 @@ local empty_response = {}
---@param _ctx lsp.HandlerContext
---@param _config table
return function(_err, result, _ctx, _config)
local virtual_document = documentstore.get_virtual_document(
result.textDocument.uri,
---@diagnostic disable-next-line: undefined-field
result._razor_hostDocumentVersion,
razor.language_kinds.csharp
)
local virtual_document =
documentstore.get_virtual_document(result.textDocument.uri, razor.language_kinds.csharp, "any")
assert(virtual_document, "csharp document was not found")

local virtual_client = virtual_document:get_lsp_client()
Expand Down
4 changes: 2 additions & 2 deletions lua/rzls/handlers/htmlformatting.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ return function(err, result, _ctx, _config)

local virtual_document = documentstore.get_virtual_document(
result.textDocument.uri,
result._razor_hostDocumentVersion,
razor.language_kinds.html
razor.language_kinds.html,
result.hostDocumentVersion
)
assert(virtual_document, "Could not find html virtual document")

Expand Down
4 changes: 2 additions & 2 deletions lua/rzls/handlers/providesemantictokensrange.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ local empty_response = {}
return function(_err, result, _ctx, _config)
local vd = documentstore.get_virtual_document(
result.textDocument.uri,
result.requiredHostDocumentVersion,
razor.language_kinds.csharp
razor.language_kinds.csharp,
result.requiredHostDocumentVersion
)
if not vd then
return { tokens = empty_response, hostDocumentSyncVersion = result.requiredHostDocumentVersion }, nil
Expand Down
2 changes: 1 addition & 1 deletion lua/rzls/roslyn_handlers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ local function roslyn_razor_provideDynamicFileHandler(_err, result, _ctx, _confi
if result.razorDocument == nil then
return nil, vim.lsp.rpc.rpc_response_error(-32602, "Missing razorDocument")
end
local vd = documentstore.get_virtual_document(result.razorDocument.uri, 0, razor.language_kinds.csharp)
local vd = documentstore.get_virtual_document(result.razorDocument.uri, razor.language_kinds.csharp, "any")
if not vd then
return nil, vim.lsp.rpc.rpc_response_error(-32600, "Could not find requested document")
end
Expand Down
6 changes: 3 additions & 3 deletions lua/rzls/server/methods/definition.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ return function(params)
local razor_bufnr = vim.uri_to_bufnr(params.textDocument.uri)
local razor_docname = vim.api.nvim_buf_get_name(razor_bufnr)

local rvd = documentstore.get_virtual_document(razor_docname, 0, razor.language_kinds.razor)
local rvd = documentstore.get_virtual_document(razor_docname, razor.language_kinds.razor)
assert(rvd, "Could not find virtual document")
local client = rvd:get_lsp_client()
assert(client, "Could not find Razor Client")
Expand All @@ -24,8 +24,8 @@ return function(params)

local virtual_document = documentstore.get_virtual_document(
vim.uri_from_bufnr(razor_bufnr),
language_query_response.result.hostDocumentVersion,
language_query_response.result.kind
language_query_response.result.kind,
language_query_response.result.hostDocumentVersion
)
assert(virtual_document)

Expand Down
6 changes: 3 additions & 3 deletions lua/rzls/server/methods/hover.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ return function(params)
local razor_bufnr = vim.uri_to_bufnr(params.textDocument.uri)
local razor_docname = vim.api.nvim_buf_get_name(razor_bufnr)

local rvd = documentstore.get_virtual_document(razor_docname, 0, razor.language_kinds.razor)
local rvd = documentstore.get_virtual_document(razor_docname, razor.language_kinds.razor)
assert(rvd, "Could not find virtual document")
local client = rvd:get_lsp_client()
assert(client, "Could not find Razor Client")
Expand All @@ -24,8 +24,8 @@ return function(params)

local virtual_document = documentstore.get_virtual_document(
vim.uri_from_bufnr(razor_bufnr),
language_query_response.result.hostDocumentVersion,
language_query_response.result.kind
language_query_response.result.kind,
language_query_response.result.hostDocumentVersion
)
assert(virtual_document)

Expand Down
6 changes: 3 additions & 3 deletions lua/rzls/server/methods/references.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ return function(params)
local razor_bufnr = vim.uri_to_bufnr(params.textDocument.uri)
local razor_docname = vim.api.nvim_buf_get_name(razor_bufnr)

local rvd = documentstore.get_virtual_document(razor_docname, 0, razor.language_kinds.razor)
local rvd = documentstore.get_virtual_document(razor_docname, razor.language_kinds.razor)
assert(rvd, "Could not find virtual document")
local client = rvd:get_lsp_client()
assert(client, "Could not find Razor Client")
Expand All @@ -24,8 +24,8 @@ return function(params)

local virtual_document = documentstore.get_virtual_document(
vim.uri_from_bufnr(razor_bufnr),
language_query_response.result.hostDocumentVersion,
language_query_response.result.kind
language_query_response.result.kind,
language_query_response.result.hostDocumentVersion
)
assert(virtual_document)

Expand Down
8 changes: 6 additions & 2 deletions lua/rzls/server/methods/rename.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ return function(params)
local razor_bufnr = vim.uri_to_bufnr(params.textDocument.uri)
local razor_docname = vim.api.nvim_buf_get_name(razor_bufnr)

local rvd = documentstore.get_virtual_document(razor_docname, 0, razor.language_kinds.razor)
local rvd = documentstore.get_virtual_document(razor_docname, razor.language_kinds.razor)
assert(rvd, "Could not find virtual document")
local client = rvd:get_lsp_client()
assert(client, "Could not find Razor Client")
Expand All @@ -37,7 +37,11 @@ return function(params)
return nil
end

local csvd = documentstore.get_virtual_document(razor_docname, 0, razor.language_kinds.csharp)
local csvd = documentstore.get_virtual_document(
razor_docname,
razor.language_kinds.csharp,
language_query_response.result.hostDocumentVersion
)
assert(csvd, "Could not find virtual document")

local roslyn_client = csvd:get_lsp_client()
Expand Down
8 changes: 6 additions & 2 deletions lua/rzls/server/methods/signaturehelp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ return function(params)
local razor_bufnr = vim.uri_to_bufnr(params.textDocument.uri)
local razor_docname = vim.api.nvim_buf_get_name(razor_bufnr)

local rvd = documentstore.get_virtual_document(razor_docname, 0, razor.language_kinds.razor)
local rvd = documentstore.get_virtual_document(razor_docname, razor.language_kinds.razor)
assert(rvd, "Could not find virtual document")
local client = rvd:get_lsp_client()
assert(client, "Could not find Razor Client")
Expand All @@ -25,7 +25,11 @@ return function(params)
local lsp_client = vim.lsp.get_clients({ name = razor.lsp_names[language_query_response.result.kind] })[1]
assert(lsp_client, "Could not find LSP Client for response type: " .. language_query_response.result.kind)

local vd = documentstore.get_virtual_document(razor_docname, 0, language_query_response.result.kind)
local vd = documentstore.get_virtual_document(
razor_docname,
language_query_response.result.kind,
language_query_response.result.hostDocumentVersion
)
assert(vd, "Could not find virtual document from projection result")

---@type lsp.SignatureHelpParams
Expand Down
3 changes: 2 additions & 1 deletion tests/rzls/documentstore_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ describe("documentstore", function()
local init_buf = vim.api.nvim_list_bufs()
documentstore.register_vbufs(init_buf[1])
for _, lang in pairs({ 1, 2 }) do
local doc = documentstore.get_virtual_document(full_path, 0, lang)
local doc = documentstore.get_virtual_document(full_path, lang, 0)
assert(doc, "Could not find virtual document")
eq(doc.kind, lang)
end
local bufs = vim.api.nvim_list_bufs()
Expand Down
2 changes: 1 addition & 1 deletion tests/rzls/virtual_document_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ local eq = assert.are.same
describe("virtual document", function()
local vd
local path = "tests/rzls/fixtures/vdtest.razor__virtual.html"
local full_path = vim.loop.cwd() .. "/" .. path
local full_path = "file://" .. vim.loop.cwd() .. "/" .. path
vim.cmd.edit({ args = { path } })
local ls = vim.fn.getbufinfo({ buflisted = 1 })
local bufnr = ls[1].bufnr
Expand Down

0 comments on commit a2cb16e

Please sign in to comment.