local lsp = vim.lsp local function virtual_text_document_handler(uri, res, client) if not res then return nil end local lines = vim.split(res.result, '\n') local bufnr = vim.uri_to_bufnr(uri) local current_buf = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false) if #current_buf ~= 0 then return nil end vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines) vim.api.nvim_set_option_value('readonly', true, { buf = bufnr }) vim.api.nvim_set_option_value('modified', false, { buf = bufnr }) vim.api.nvim_set_option_value('modifiable', false, { buf = bufnr }) lsp.buf_attach_client(bufnr, client.id) end local function virtual_text_document(uri, client) local params = { textDocument = { uri = uri, }, } local result = client:request_sync('deno/virtualTextDocument', params) virtual_text_document_handler(uri, result, client) end local function denols_handler(err, result, ctx, config) if not result or vim.tbl_isempty(result) then return nil end local client = vim.lsp.get_client_by_id(ctx.client_id) for _, res in pairs(result) do local uri = res.uri or res.targetUri if uri:match '^deno:' then if client then virtual_text_document(uri, client) end res['uri'] = uri res['targetUri'] = uri end end lsp.handlers[ctx.method](err, result, ctx, config) end local function buf_cache(bufnr, client) local params = { command = 'deno.cache', arguments = { {}, vim.uri_from_bufnr(bufnr) }, } client:request('workspace/executeCommand', params, function(err, _result, ctx) if err then local uri = ctx.params.arguments[2] vim.notify('cache command failed for ' .. vim.uri_to_fname(uri), vim.log.levels.ERROR) end end, bufnr) end return { cmd = { 'deno', 'lsp' }, cmd_env = { NO_COLOR = true }, filetypes = { 'javascript', 'javascriptreact', 'typescript', 'typescriptreact', 'json', 'jsonc', 'markdown', }, root_markers = { 'deno.json', 'deno.jsonc' }, workspace_required = true, settings = { deno = { enable = true, suggest = { imports = { hosts = { ['https://deno.land'] = true, }, }, }, }, }, handlers = { ['textDocument/definition'] = denols_handler, ['textDocument/typeDefinition'] = denols_handler, ['textDocument/references'] = denols_handler, }, on_attach = function(client, bufnr) local user_command = require 'std.user_command' user_command.buffer(bufnr).DenolsCache 'Cache a module and all of its dependencies.' { nargs = 0 } (function() local clients = vim.lsp.get_clients { bufnr = 0, name = 'denols' } if #clients > 0 then buf_cache(0, clients[#clients]) end end) end, commands = { DenolsCache = { function() local clients = vim.lsp.get_clients { bufnr = 0, name = 'denols' } if #clients > 0 then buf_cache(0, clients[#clients]) end end, description = 'Cache a module and all of its dependencies.', }, }, _test = { denols_handler = denols_handler, buf_cache = buf_cache, }, }