Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into feat-custom-diagnostic
Browse files Browse the repository at this point in the history
  • Loading branch information
KostkaBrukowa committed Jul 15, 2024
2 parents df1f877 + 3ecf6b2 commit cf6d08f
Show file tree
Hide file tree
Showing 32 changed files with 519 additions and 32 deletions.
1 change: 1 addition & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ jobs:
"5.0",
"5.1",
"5.2",
"5.3",
]

steps:
Expand Down
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,13 @@ require("typescript-tools").setup {
-- by default code lenses are displayed on all referencable values and for some of you it can
-- be too much this option reduce count of them by removing member references from lenses
disable_member_code_lens = true,
-- JSXCloseTag
-- WARNING: it is disabled by default (maybe you configuration or distro already uses nvim-ts-autotag,
-- that maybe have a conflict if enable this feature. )
jsx_close_tag = {
enable = false,
filetypes = { "javascriptreact", "typescriptreact" },
}
},
}
```
Expand Down Expand Up @@ -201,6 +208,41 @@ require("typescript-tools").setup {
}
```

If you want to make `tsserver_format_options` or `tsserver_file_preferences` filetype dependant you
need to may set them as functions returning tables eg.

<details>
<summary>Example code here</summary>
<p>

```lua
require("typescript-tools").setup {
settings = {
...
tsserver_file_preferences = function(ft)
-- Some "ifology" using `ft` of opened file
return {
includeInlayParameterNameHints = "all",
includeCompletionsForModuleExports = true,
quotePreference = "auto",
...
}
end,
tsserver_format_options = function(ft)
-- Some "ifology" using `ft` of opened file
return {
allowIncompleteCompletions = false,
allowRenameOfImportPath = false,
...
}
end
},
}
```

</p>
</details>

The default values for `preferences` and `format_options` are in [this file](https://github.com/pmizio/typescript-tools.nvim/blob/master/lua/typescript-tools/config.lua#L17)

#### 💅 `styled-components` support
Expand Down Expand Up @@ -249,6 +291,7 @@ This plugin provides several custom user commands (they are only applied to curr
[source definition](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-7.html#go-to-source-definition)
(available since TS v4.7)
- `TSToolsRenameFile` - allow to rename current file and apply changes to connected files
- `TSToolsFileReferences` - find files that reference the current file (available since TS v4.2)

## Supported LSP methods

Expand Down
84 changes: 82 additions & 2 deletions lua/typescript-tools/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ end
---@param type string|nil - lsp method to send the diagnostic: c.CustomMethods.Diagnostic or c.LspMethods.Diagnostic
function M.request_diagnostics(callback, type)
local text_document = vim.lsp.util.make_text_document_params()
local client = vim.lsp.get_active_clients {
local client = utils.get_clients {
name = plugin_config.plugin_name,
bufnr = vim.uri_to_bufnr(text_document.uri),
}
Expand Down Expand Up @@ -213,7 +213,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 All @@ -227,4 +227,84 @@ function M.filter_diagnostics(codes)
end
end

---JsxClosingTag feature impl
---@param bufnr integer
---@param params table
---@param cb fun()
---@param pre_request_id integer|nil
function M.jsx_close_tag(bufnr, params, cb, pre_request_id)
local typescript_client = get_typescript_client(bufnr)
if typescript_client == nil then
return nil
end
if pre_request_id ~= nil then
typescript_client.cancel_request(pre_request_id)
end
local changedtick = vim.api.nvim_buf_get_var(bufnr, "changedtick")

local _, request_id = typescript_client.request(
c.CustomMethods.JsxClosingTag,
params,
---@param data { newText: string, caretOffset: number }
function(err, data)
if
err ~= nil
or data == nil
or vim.tbl_isempty(data)
or bufnr ~= vim.api.nvim_get_current_buf()
or changedtick ~= vim.api.nvim_buf_get_var(bufnr, "changedtick")
then
return
end

vim.lsp.util.apply_text_edits({
{
range = {
start = params.position,
["end"] = params.position,
},
newText = data.newText,
},
}, bufnr, "utf-8")

vim.api.nvim_win_set_cursor(0, { params.position.line + 1, params.position.character })

cb()
end,
bufnr
)

return request_id
end

---@param is_sync boolean
function M.file_references(is_sync)
a.void(function()
local client = utils.get_typescript_client(0)

if not client then
return
end

local err, result = async.buf_request_isomorphic(
is_sync,
0,
c.CustomMethods.FileReferences,
{ textDocument = vim.lsp.util.make_text_document_params() }
)

vim.lsp.handlers[c.LspMethods.Reference](err, result, { client_id = client.id })
end)()
end

---@param tmpfile string
function M.save_snapshot_to(tmpfile)
async.buf_request_isomorphic(
true,
0,
c.CustomMethods.SaveTo,
{ textDocument = vim.lsp.util.make_text_document_params(), tmpfile = tmpfile }
)
end

return M
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: 5 additions & 0 deletions lua/typescript-tools/autocommands/init.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local diagnostics = require "typescript-tools.autocommands.diagnostics"
local code_lens = require "typescript-tools.autocommands.code_lens"
local config = require "typescript-tools.config"
local jsx_close_tag = require "typescript-tools.autocommands.jsx_close_tag"

local M = {}

Expand All @@ -11,6 +12,10 @@ function M.setup_autocommands(dispatchers)
if config.code_lens ~= config.code_lens_mode.off then
code_lens.setup_code_lens_autocmds()
end

if config.jsx_close_tag.enable then
jsx_close_tag.setup_jsx_close_tag_autocmds()
end
end

return M
67 changes: 67 additions & 0 deletions lua/typescript-tools/autocommands/jsx_close_tag.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
local api = vim.api

local common = require "typescript-tools.autocommands.common"
local plugin_config = require "typescript-tools.config"
local plugin_api = require "typescript-tools.api"
local list_contains = require("typescript-tools.utils").list_contains

local M = {}

function M.setup_jsx_close_tag_autocmds()
local augroup = vim.api.nvim_create_augroup("TypescriptToolsJSXCloseTagGroup", { clear = true })

common.create_lsp_attach_augcmd(function()
local changing = false
local request_id = nil
local prev_line = nil
local prev_line_text = ""

api.nvim_create_autocmd({ "TextChangedI" }, {
pattern = common.extensions_pattern,
callback = function()
if not list_contains(plugin_config.jsx_close_tag.filetypes, vim.bo.filetype) then
return
end
if changing then
changing = false
return
end
local params = vim.lsp.util.make_position_params(0, "utf-8")
local bufnr = vim.api.nvim_get_current_buf()
local line, character = params.position.line, params.position.character
local line_text = vim.api.nvim_buf_get_text(0, line, 0, line, character, {})[1]

local is_after_delete = prev_line == line
and string.len(prev_line_text) >= string.len(line_text)

prev_line_text = line_text
prev_line = line

if is_after_delete then
return
end

local line_words = vim.split(line_text, " ")

if #line_words == 0 then
return
end

local last_word = line_words[#line_words]

local last_char = string.sub(last_word, #last_word)

if last_char ~= ">" then
return
end

request_id = plugin_api.jsx_close_tag(bufnr, params, function()
changing = true
request_id = nil
end, request_id)
end,
})
end, augroup)
end

return M
15 changes: 15 additions & 0 deletions lua/typescript-tools/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
---@field expose_as_code_action ("fix_all"| "add_missing_imports"| "remove_unused" | "remove_unused_imports")[]
---@field include_completions_with_insert_text boolean
---@field code_lens code_lens_mode
---@field jsx_close_tag { enable: boolean, filetypes: string[] }
---@field disable_member_code_lens boolean
local M = {}
local __store = {}
Expand Down Expand Up @@ -137,6 +138,7 @@ function M.load_settings(settings)
},
["settings.code_lens"] = { settings.code_lens, "string", true },
["settings.disable_member_code_lens"] = { settings.disable_member_code_lens, "boolean", true },
["settings.jsx_close_tag"] = { settings.jsx_close_tag, "table", true },
}

__store = vim.tbl_deep_extend("force", __store, settings)
Expand Down Expand Up @@ -188,6 +190,19 @@ function M.load_settings(settings)
if not M.code_lens_mode[settings.code_lens] then
__store.code_lens = M.code_lens_mode.off
end

local default_jsx_filetypes = { "javascriptreact", "typescriptreact" }

if not settings.jsx_close_tag then
__store.jsx_close_tag = {
enable = false,
filetypes = default_jsx_filetypes,
}
end

if settings.jsx_close_tag and not settings.jsx_close_tag.filetypes then
__store.jsx_close_tag.filetypes = default_jsx_filetypes
end
end

setmetatable(M, {
Expand Down
3 changes: 3 additions & 0 deletions lua/typescript-tools/protocol/constants.lua
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ return {
CustomDiagnostic = "typescriptTools/customDiagnostic",
BatchCodeActions = "typescriptTools/batchCodeActions",
ConfigurePlugin = "typescriptTools/configurePlugin",
JsxClosingTag = "typescriptTools/jsxClosingTag",
FileReferences = "typescriptTools/fileReferences",
SaveTo = "typescriptTools/saveTo",
},
TsserverEvents = {
ProjectLoadingStart = "projectLoadingStart",
Expand Down
8 changes: 6 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 @@ -12,11 +13,14 @@ local remapped_methods = {
[c.CustomMethods.CustomDiagnostic] = "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
1 change: 1 addition & 0 deletions lua/typescript-tools/protocol/progress.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ local function create_message(event, message)
value = {
kind = event,
title = message,
message = message,
},
}
end
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
Loading

0 comments on commit cf6d08f

Please sign in to comment.