Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Limit ftplugin to buffer and avoid early lua requires #1

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
- uses: actions/checkout@v3
- run: date +%F > todays-date
- name: Restore cache for today's nightly.
uses: actions/cache@v2
uses: actions/cache@v3
with:
path: _neovim
key: ${{ runner.os }}-x64-${{ hashFiles('todays-date') }}
Expand Down
2 changes: 1 addition & 1 deletion ftplugin/javascript.lua
Original file line number Diff line number Diff line change
@@ -1 +1 @@
require("typescript-tools.user_commands").setup_user_commands()
dofile(vim.fn.expand "<sfile>:h:h" .. "/utils/ftplugin-common.lua")
2 changes: 1 addition & 1 deletion ftplugin/javascriptreact.lua
Original file line number Diff line number Diff line change
@@ -1 +1 @@
require("typescript-tools.user_commands").setup_user_commands()
dofile(vim.fn.expand "<sfile>:h:h" .. "/utils/ftplugin-common.lua")
2 changes: 1 addition & 1 deletion ftplugin/typescript.lua
Original file line number Diff line number Diff line change
@@ -1 +1 @@
require("typescript-tools.user_commands").setup_user_commands()
dofile(vim.fn.expand "<sfile>:h:h" .. "/utils/ftplugin-common.lua")
2 changes: 1 addition & 1 deletion ftplugin/typescriptreact.lua
Original file line number Diff line number Diff line change
@@ -1 +1 @@
require("typescript-tools.user_commands").setup_user_commands()
dofile(vim.fn.expand "<sfile>:h:h" .. "/utils/ftplugin-common.lua")
38 changes: 21 additions & 17 deletions lua/typescript-tools/protocol/text_document/code_lens/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,30 @@ local M = {}
---@param lenses table
---@param implementations boolean|nil
local function convert_nodes_to_response(tree, query, text_document, lenses, implementations)
for _, match in query:iter_matches(tree:root(), vim.uri_to_bufnr(text_document.uri)) do
for id, node in pairs(match) do
for _, match in
query:iter_matches(tree:root(), vim.uri_to_bufnr(text_document.uri), 0, -1, { all = true })
do
for id, nodes in pairs(match) do
local name = query.captures[id]
local start_row, start_col, end_row, end_col = node:range()
for _, node in ipairs(nodes) do
local start_row, start_col, end_row, end_col = node:range()

if config.disable_member_code_lens and name == "member" then
goto continue
end
if config.disable_member_code_lens and name == "member" then
goto continue
end

table.insert(lenses, {
range = {
start = { line = start_row, character = start_col },
["end"] = { line = end_row, character = end_col },
},
data = {
textDocument = text_document,
implementations = implementations,
},
})
::continue::
table.insert(lenses, {
range = {
start = { line = start_row, character = start_col },
["end"] = { line = end_row, character = end_col },
},
data = {
textDocument = text_document,
implementations = implementations,
},
})
::continue::
end
end
end
end
Expand Down
64 changes: 0 additions & 64 deletions lua/typescript-tools/user_commands.lua

This file was deleted.

33 changes: 33 additions & 0 deletions tests/editor_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,37 @@ describe("Lsp request", function()
assert.is.same(vim.tbl_contains(lines, 'import { export1 } from "exports";'), true)
end
)

describe("ftplugin", function()
local commands = {
":TSToolsOrganizeImports",
":TSToolsOrganizeImports",
":TSToolsSortImports",
":TSToolsRemoveUnusedImports",
":TSToolsGoToSourceDefinition",
":TSToolsRemoveUnused",
":TSToolsAddMissingImports",
":TSToolsFixAll",
":TSToolsRenameFile",
":TSToolsFileReferences",
}

it("should not create usercommands in unhandled buffers", function()
utils.open_file "src/batch_code_actions.ts"
utils.wait_for_lsp_initialization()
for _, command in pairs(commands) do
assert.is.same(vim.fn.exists(command), 2)
end

utils.open_file "src/package.json"
for _, command in pairs(commands) do
assert.is.same(vim.fn.exists(command), 0)
end

vim.cmd ":set ft=typescriptreact"
for _, command in pairs(commands) do
assert.is.same(vim.fn.exists(command), 2)
end
end)
end)
end)
11 changes: 6 additions & 5 deletions tests/ts_project/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/ts_project/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"license": "MIT",
"devDependencies": {
"prettier": "2.8.8",
"typescript": "5.x"
"typescript": "^5.8.2"
},
"prettier": {
"singleQuote": true
Expand Down
64 changes: 64 additions & 0 deletions utils/ftplugin-common.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
if vim.fn.exists "g:disable_typescript_tools" == 1 then
return
end
if vim.fn.exists "b:did_typescript_tools_ftplugin" == 1 then
return
end
vim.b.did_typescript_tools_ftplugin = true

---@param name string
---@param fn function
local function create_command(name, fn)
local command_completion = {
nargs = "?",
complete = function()
return { "sync" }
end,
}
vim.api.nvim_buf_create_user_command(0, name, function(cmd)
local words = cmd.fargs

if #words == 1 and words[1] ~= "sync" then
vim.notify("No such command", vim.log.levels.ERROR)
return
end

fn(#words == 1)
end, command_completion)
end

create_command("TSToolsOrganizeImports", function(is_sync)
require("typescript-tools.api").organize_imports(is_sync)
end)

create_command("TSToolsSortImports", function(is_sync)
require("typescript-tools.api").sort_imports(is_sync)
end)

create_command("TSToolsRemoveUnusedImports", function(is_sync)
require("typescript-tools.api").remove_unused_imports(is_sync)
end)

create_command("TSToolsGoToSourceDefinition", function(is_sync)
require("typescript-tools.api").go_to_source_definition(is_sync)
end)

create_command("TSToolsRemoveUnused", function(is_sync)
require("typescript-tools.api").remove_unused(is_sync)
end)

create_command("TSToolsAddMissingImports", function(is_sync)
require("typescript-tools.api").add_missing_imports(is_sync)
end)

create_command("TSToolsFixAll", function(is_sync)
require("typescript-tools.api").fix_all(is_sync)
end)

create_command("TSToolsRenameFile", function(is_sync)
require("typescript-tools.api").rename_file(is_sync)
end)

create_command("TSToolsFileReferences", function(is_sync)
require("typescript-tools.api").file_references(is_sync)
end)