local M = {} local defaults = { server = nil, dap_adapter = nil, gcc_profile = nil, provider = nil, } local state = { config = {}, replay = nil } function M.setup(options) state.config = vim.tbl_extend("force", defaults, options or {}) end local function config() return state.config end local function absolute(path) return type(path) == "string" and vim.startswith(path, "/") end local function executable(name, explicit) local path = explicit or vim.fn.exepath(name) if path == nil or path == "" then return nil, name .. " is not on PATH" end if not absolute(path) then return nil, name .. " did not resolve to an absolute path" end return path end local function workspace_root(bufnr) local name = vim.api.nvim_buf_get_name(bufnr) if name == "" then return vim.fn.getcwd() end local markers = { ".git", "vbs.lock", "compile_commands.json", "build.cpp" } return vim.fs.root(name, markers) or vim.fn.fnamemodify(name, ":p:h") end local function client_for(bufnr) for _, client in ipairs(vim.lsp.get_clients({ bufnr = bufnr })) do if client.name == "VoidLanguageServer" then return client end end return nil end function M.start(bufnr) bufnr = bufnr or vim.api.nvim_get_current_buf() local server, err = executable("vls", config().server) if not server then return nil, err end local root = workspace_root(bufnr) local cmd = { server, "--stdio", "--workspace-root", root } if config().provider then vim.list_extend(cmd, { "--provider", config().provider }) end if config().gcc_profile then if not absolute(config().gcc_profile) then return nil, "gcc_profile must be an absolute path" end vim.list_extend(cmd, { "--gcc-profile", config().gcc_profile }) end local id = vim.lsp.start({ name = "VoidLanguageServer", cmd = cmd, root_dir = root }, { bufnr = bufnr }) if not id then return nil, "could not start VoidLanguageServer" end return vim.lsp.get_client_by_id(id) end local function request_sync(bufnr, method, params) bufnr = bufnr or vim.api.nvim_get_current_buf() local client = client_for(bufnr) if not client then return nil, "no VoidLanguageServer attached to this buffer" end local reply = client:request_sync(method, params, 5000, bufnr) if not reply then return nil, method .. " timed out" end if reply.err then return nil, vim.inspect(reply.err) end return reply.result end function M.status(bufnr) bufnr = bufnr or vim.api.nvim_get_current_buf() local client = client_for(bufnr) if not client then return nil, "no VoidLanguageServer attached to this buffer" end return request_sync(bufnr, "voidLanguageServer/providerStatus", { textDocument = { uri = vim.uri_from_bufnr(bufnr) }, workspaceUri = vim.uri_from_fname(client.config.root_dir), }) end function M.analysis_status(bufnr) bufnr = bufnr or vim.api.nvim_get_current_buf() return request_sync(bufnr, "voidLanguageServer/analysisStatus", { textDocument = { uri = vim.uri_from_bufnr(bufnr) }, }) end local function dap() local ok, module = pcall(require, "dap") if ok then return module end return nil end function M.replay(trace) if not absolute(trace) then return nil, "an absolute trace path is required" end if vim.fn.filereadable(trace) ~= 1 then return nil, "trace is not readable: " .. trace end local adapter, err = executable("vls-dap", config().dap_adapter) if not adapter then return nil, err end local dap_module = dap() if not dap_module then return nil, "nvim-dap is not installed" end local name = "vls-replay" dap_module.adapters[name] = { type = "executable", command = adapter, args = { "--replay", trace } } dap_module.configurations.cpp = dap_module.configurations.cpp or {} for index = #dap_module.configurations.cpp, 1, -1 do if dap_module.configurations.cpp[index].type == name then table.remove(dap_module.configurations.cpp, index) end end local configuration = { type = name, request = "launch", name = "VLS replay " .. vim.fn.fnamemodify(trace, ":t") } table.insert(dap_module.configurations.cpp, configuration) state.replay = { trace = trace } dap_module.run(configuration) return state.replay end local function session() local dap_module = dap() if not dap_module then return nil end return dap_module.session() end local function request(method, arguments) local active = session() if not active then return nil, "no active replay session" end local done, failure, body = false, nil, nil active:request(method, arguments, function(err, result) failure, body, done = err, result, true end) if not vim.wait(5000, function() return done end, 10) then return nil, method .. " timed out" end if failure then return nil, vim.inspect(failure) end return body end function M.session() local reply, err = request("vls/session") if not reply then return nil, err end state.replay = state.replay or {} state.replay.schemaVersion = reply.schemaVersion state.replay.sessionId = reply.sessionId state.replay.generation = reply.generation return reply end local function current() local active = state.replay if not active or not active.generation then return nil, "subscribe with :VlsSession first" end return active end function M.timeline(start, count) local active, err = current() if not active then return nil, err end local page, failure = request("vls/timeline", { schemaVersion = active.schemaVersion, sessionId = active.sessionId, generation = active.generation, start = start or 0, count = count or 20, }) if not page then return nil, failure end if page.sessionId ~= active.sessionId or page.generation ~= active.generation then return nil, "stale replay generation" end return page end function M.frames(start, count) local active, err = current() if not active then return nil, err end local page, failure = request("vls/frames", { schemaVersion = active.schemaVersion, sessionId = active.sessionId, generation = active.generation, start = start or 0, count = count or 20, }) if not page then return nil, failure end if page.sessionId ~= active.sessionId or page.generation ~= active.generation then return nil, "stale replay generation" end return page end function M.source() local active, err = current() if not active then return nil, err end local reply, failure = request("source", { sourceReference = 1 }) if not reply then return nil, failure end return reply.content end function M.stop() local dap_module = dap() if dap_module and dap_module.session() then dap_module.terminate() end state.replay = nil end function M.render(title, lines) local buffer = vim.api.nvim_create_buf(false, true) vim.api.nvim_buf_set_lines(buffer, 0, -1, false, lines) vim.bo[buffer].bufhidden = "wipe" vim.bo[buffer].filetype = "vls-inspection" vim.api.nvim_buf_set_name(buffer, title .. " " .. os.date("%H:%M:%S")) vim.cmd.split() vim.api.nvim_win_set_buf(0, buffer) return buffer end local function before(left, right) if left.line ~= right.line then return left.line > right.line end return left.character > right.character end -- Applies an LSP `changes` map transactionally. Every range is validated -- before any buffer is touched; an application failure restores the exact -- pre-edit lines and modified state of every buffer already changed. function M.apply_workspace_edit(changes) if type(changes) ~= "table" then return nil, "no workspace edit changes" end local plan = {} for uri, edits in pairs(changes) do if type(edits) ~= "table" or #edits == 0 then return nil, "empty edit list for " .. uri end local bufnr = vim.uri_to_bufnr(uri) if not vim.api.nvim_buf_is_loaded(bufnr) then vim.fn.bufload(bufnr) end local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false) local sorted = vim.deepcopy(edits) table.sort(sorted, function(left, right) return before(left.range.start, right.range.start) end) for _, edit in ipairs(sorted) do local first, last = edit.range.start, edit.range["end"] if first.line < 0 or first.line >= #lines or last.line < first.line or last.line >= #lines or first.character < 0 or last.character < 0 or first.character > #lines[first.line + 1] or (last.line == first.line and last.character < first.character) then return nil, "edit range is outside the buffer: " .. uri end if type(edit.newText) ~= "string" then return nil, "edit text is not a string: " .. uri end end plan[#plan + 1] = { bufnr = bufnr, uri = uri, lines = lines, modified = vim.bo[bufnr].modified, edits = sorted } end table.sort(plan, function(left, right) return left.uri < right.uri end) for _, item in ipairs(plan) do if not vim.bo[item.bufnr].modifiable then return nil, "buffer is not modifiable: " .. item.uri end end local applied = {} local ok, failure = pcall(function() for _, item in ipairs(plan) do applied[#applied + 1] = item for _, edit in ipairs(item.edits) do vim.api.nvim_buf_set_text(item.bufnr, edit.range.start.line, edit.range.start.character, edit.range["end"].line, edit.range["end"].character, { edit.newText }) end end end) if ok then return true end for _, item in ipairs(applied) do vim.api.nvim_buf_set_lines(item.bufnr, 0, -1, false, item.lines) vim.bo[item.bufnr].modified = item.modified end return nil, tostring(failure) end -- Requests a guarded rename from the server and applies it transactionally. function M.rename(new_name, bufnr) bufnr = bufnr or vim.api.nvim_get_current_buf() local client = client_for(bufnr) if not client then return nil, "no VoidLanguageServer attached to this buffer" end local cursor = vim.api.nvim_win_get_cursor(0) local reply = client:request_sync("textDocument/rename", { textDocument = { uri = vim.uri_from_bufnr(bufnr) }, position = { line = cursor[1] - 1, character = cursor[2] }, newName = new_name, }, 5000, bufnr) if not reply then return nil, "textDocument/rename timed out" end if reply.err then return nil, (reply.err.message or vim.inspect(reply.err)) end if not reply.result or not reply.result.changes then return nil, "the server returned no rename changes" end return M.apply_workspace_edit(reply.result.changes) end return M