Skip to content

Commit

Permalink
Merge branch 'master' into feature/221-v2
Browse files Browse the repository at this point in the history
  • Loading branch information
KostkaBrukowa committed Aug 9, 2024
2 parents 9776e74 + f8c2e0b commit 24bd6fe
Show file tree
Hide file tree
Showing 14 changed files with 80 additions and 28 deletions.
15 changes: 6 additions & 9 deletions lua/typescript-tools/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ local a = require "plenary.async"
local uv = require "plenary.async.uv_async"
local au = require "plenary.async.util"
local c = require "typescript-tools.protocol.constants"
local plugin_config = require "typescript-tools.config"
local async = require "typescript-tools.async"
local utils = require "typescript-tools.utils"

Expand Down Expand Up @@ -141,18 +140,16 @@ end
---@param callback fun(params: table, result: table)|nil
function M.request_diagnostics(callback)
local text_document = vim.lsp.util.make_text_document_params()
local client = vim.lsp.get_active_clients {
name = plugin_config.plugin_name,
bufnr = vim.uri_to_bufnr(text_document.uri),
}
local bufnr = vim.uri_to_bufnr(text_document.uri)
local typescript_client = get_typescript_client(bufnr)

if #client == 0 then
if typescript_client == nil then
return
end

vim.lsp.buf_request(0, c.CustomMethods.Diagnostic, {
typescript_client.request(c.CustomMethods.Diagnostic, {
textDocument = text_document,
}, callback)
}, callback, bufnr)
end

---@param is_sync boolean
Expand Down Expand Up @@ -212,7 +209,7 @@ end
---
---@param codes integer[]
function M.filter_diagnostics(codes)
vim.tbl_add_reverse_lookup(codes)
utils.add_reverse_lookup(codes)
return function(err, res, ctx, config)
local filtered = {}
for _, diag in ipairs(res.diagnostics) do
Expand Down
2 changes: 1 addition & 1 deletion lua/typescript-tools/autocommands/code_lens.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function M.setup_code_lens_autocmds()
return
end

pcall(vim.lsp.codelens.refresh)
pcall(vim.lsp.codelens.refresh, { bufnr = e.buf })
end,
group = augroup,
})
Expand Down
2 changes: 1 addition & 1 deletion lua/typescript-tools/autocommands/common.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ local plugin_config = require "typescript-tools.config"

local M = {}

M.extensions_pattern = { "*.js", "*.mjs", "*.jsx", "*.ts", "*.tsx", "*.mts" }
M.extensions_pattern = { "*.js", "*.mjs", "*.jsx", "*.ts", "*.tsx", "*.mts", "*.vue" }

---@param callback function
---@param augroup number
Expand Down
4 changes: 2 additions & 2 deletions lua/typescript-tools/autocommands/diagnostics.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ function M.setup_diagnostic_autocmds(dispatchers)
request_diagnostics_debounced()

api.nvim_create_autocmd("InsertEnter", {
pattern = M.extensions_pattern,
pattern = common.extensions_pattern,
callback = function(e)
proto_utils.publish_diagnostics(dispatchers, vim.uri_from_bufnr(e.buf), {})
end,
group = augroup,
})

api.nvim_create_autocmd({ "BufEnter", "InsertLeave", "TextChanged" }, {
pattern = M.extensions_pattern,
pattern = common.extensions_pattern,
callback = request_diagnostics_debounced,
group = augroup,
})
Expand Down
5 changes: 3 additions & 2 deletions lua/typescript-tools/protocol/module_mapper.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local c = require "typescript-tools.protocol.constants"
local utils = require "typescript-tools.utils"

local remapped_methods = {
[c.LspMethods.CompletionResolve] = "text_document.completion.resolve",
Expand All @@ -11,14 +12,14 @@ local remapped_methods = {
[c.CustomMethods.Diagnostic] = "text_document.custom_diagnostic",
[c.CustomMethods.BatchCodeActions] = "text_document.code_action.batch",
[c.LspMethods.CodeLensResolve] = "text_document.code_lens.resolve",
[c.CustomMethods.ConfigurePlugin] = "text_document.configure_plugin",
[c.CustomMethods.ConfigurePlugin] = "configure_plugin",
[c.CustomMethods.JsxClosingTag] = "text_document.jsx_close_tag",
[c.CustomMethods.FileReferences] = "text_document.file_references",
[c.CustomMethods.SaveTo] = "text_document.save_to",
}

local noop_methods = { c.LspMethods.DidSave }
vim.tbl_add_reverse_lookup(noop_methods)
utils.add_reverse_lookup(noop_methods)

local M = {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ end
---@type TsserverProtocolHandler
function M.handler(request, _, params)
local text_document = params.textDocument
local ok, parser = pcall(ts.get_parser)
local bufnr = vim.uri_to_bufnr(text_document.uri)
local ok, parser = pcall(ts.get_parser, bufnr)

if not ok then
vim.notify_once(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ local stylecheck_diagnostics = {
-- not all code paths return a value
7030,
}
vim.tbl_add_reverse_lookup(stylecheck_diagnostics)
utils.add_reverse_lookup(stylecheck_diagnostics)

--- @param diagnostic table
--- @return DiagnosticSeverity
Expand Down Expand Up @@ -83,7 +83,7 @@ end

--- @return string[]
local function get_attached_buffers()
local client = vim.lsp.get_active_clients({ name = plugin_config.plugin_name })[1]
local client = utils.get_clients({ name = plugin_config.plugin_name })[1]

if client then
local attached_bufs = {}
Expand Down
5 changes: 4 additions & 1 deletion lua/typescript-tools/protocol/text_document/did_open.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ local function configure(params)

local bo = vim.bo[vim.uri_to_bufnr(text_document.uri)]
local tab_size = bo.tabstop or 2
local indent_size = bo.shiftwidth or tab_size
local indent_size = bo.shiftwidth
if indent_size == 0 or not indent_size then
indent_size = tab_size
end
local convert_tabs_to_spaces = bo.expandtab or true
local new_line_character = get_eol_chars(bo)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ local utils = require "typescript-tools.protocol.utils"

local M = {}

local islist = vim.islist or vim.tbl_islist

---@type TsserverProtocolHandler
function M.handler(request, response, params)
local text_document = params.textDocument
Expand All @@ -18,7 +20,13 @@ function M.handler(request, response, params)
-- tsserver protocol reference:
-- https://github.com/microsoft/TypeScript/blob/503604c884bd0557c851b11b699ef98cdb65b93b/lib/protocol.d.ts#L2584
local body = coroutine.yield()
body = vim.tbl_islist(body) and body or { body }

if body == nil or body.success == false then
response(nil)
return
end

body = islist(body) and body or { body }

response(vim.tbl_map(function(it)
return utils.convert_tsserver_call_hierarchy_item_to_lsp(it)
Expand Down
18 changes: 15 additions & 3 deletions lua/typescript-tools/protocol/text_document/semantic_tokens.lua
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,26 @@ local function transform_spans(spans, lines_lengths)
---@type number
local previous_offset = 0

-- @vue/typescript-plugin inserts some its own spans at the end of the spans array so we need to sort them
-- because we calculate offset from last span and they need to be in order
local sorted_spans = {}

for i = 1, #spans, 3 do
table.insert(sorted_spans, { spans[i], spans[i + 1], spans[i + 2] })
end

table.sort(sorted_spans, function(a, b)
return a[1] < b[1]
end)

for _, span in ipairs(sorted_spans) do
-- ts-server sends us a packed array that contains 3 elements per 1 token:
-- 1. the start offset of the token
-- 2. length of the token
-- 3. token type & modifier packed into a bitset
local token_start_offset = spans[i]
local token_length = spans[i + 1]
local token_type_bit_set = spans[i + 2]
local token_start_offset = span[1]
local token_length = span[2]
local token_type_bit_set = span[3]

-- unpack the modifier and type: https://github.com/microsoft/TypeScript/blob/main/src/services/classifier2020.ts#L45
local token_modifier = bit.band(token_type_bit_set, token_encoding_modifier_mask)
Expand Down
8 changes: 6 additions & 2 deletions lua/typescript-tools/rpc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@ local autocommands = require "typescript-tools.autocommands"
local custom_handlers = require "typescript-tools.custom_handlers"
local request_router = require "typescript-tools.request_router"
local internal_commands = require "typescript-tools.internal_commands"
local utils = require "typescript-tools.utils"

local M = {}

---@param dispatchers Dispatchers
---@return LspInterface
function M.start(dispatchers)
local tsserver_syntax = Tsserver.new("syntax", dispatchers)
local modified_dispatchers = vim.deepcopy(dispatchers)
modified_dispatchers.on_exit = utils.run_once(dispatchers.on_exit) -- INFO: multiple calls to on_exit causes errors in nvim lsp

local tsserver_syntax = Tsserver.new("syntax", modified_dispatchers)
local tsserver_semantic = nil
if plugin_config.separate_diagnostic_server then
tsserver_semantic = Tsserver.new("semantic", dispatchers)
tsserver_semantic = Tsserver.new("semantic", modified_dispatchers)
end

autocommands.setup_autocommands(dispatchers)
Expand Down
1 change: 1 addition & 0 deletions lua/typescript-tools/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
---@field notification fun(method: string, params: table)
---@field server_request fun(method: string, params: table): nil, table
---@field on_exit fun(code: number, signal: number)
---@field on_error fun(code: number, ...: unknown)

---@alias LspCallback fun(err: any, result: any)

Expand Down
29 changes: 27 additions & 2 deletions lua/typescript-tools/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,15 @@ function M.is_nightly()
return type(v) ~= "boolean" and v ~= nil or v
end

function M.get_clients(filter)
local get_clients = vim.lsp.get_clients or vim.lsp.get_active_clients
return get_clients(filter)
end

---@param bufnr integer
---@return lsp.Client|nil
function M.get_typescript_client(bufnr)
local get_clients = M.is_nightly() and vim.lsp.get_clients or vim.lsp.get_active_clients
local clients = get_clients {
local clients = M.get_clients {
name = plugin_config.plugin_name,
bufnr = bufnr,
}
Expand Down Expand Up @@ -104,4 +108,25 @@ function M.list_contains(list, value)
return false
end

--- @param tbl table
function M.add_reverse_lookup(tbl)
local keys = vim.tbl_keys(tbl)
for _, k in ipairs(keys) do
local v = tbl[k]
tbl[v] = k
end
end

-- Returns a function that only runs the given function once.
--- @param func function
function M.run_once(func)
local ran = false
return function(...)
if not ran then
ran = true
return func(...)
end
end
end

return M
2 changes: 1 addition & 1 deletion tests/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ end
---@param capability string
---@return boolean
function M.supports_capability(capability)
return not not vim.lsp.get_active_clients({
return not not vim.lsp.get_clients({
name = require("typescript-tools.config").plugin_name,
})[1].server_capabilities[capability]
end
Expand Down

0 comments on commit 24bd6fe

Please sign in to comment.