Skip to content

Limit ftplugin to buffer and avoid early lua requires #332

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
@@ -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') }}
9 changes: 8 additions & 1 deletion ftplugin/javascript.lua
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
require("typescript-tools.user_commands").setup_user_commands()
local ftplugin = _G.nnyIAsk5fJtqzUaJ_typescript_tools_ftplugin

if not ftplugin then
ftplugin = dofile(vim.fn.expand "<sfile>:h:h" .. "/utils/ftplugin-common.lua")
_G.nnyIAsk5fJtqzUaJ_typescript_tools_ftplugin = ftplugin
end

ftplugin.initialize()
9 changes: 8 additions & 1 deletion ftplugin/javascriptreact.lua
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
require("typescript-tools.user_commands").setup_user_commands()
local ftplugin = _G.nnyIAsk5fJtqzUaJ_typescript_tools_ftplugin

if not ftplugin then
ftplugin = dofile(vim.fn.expand "<sfile>:h:h" .. "/utils/ftplugin-common.lua")
_G.nnyIAsk5fJtqzUaJ_typescript_tools_ftplugin = ftplugin
end

ftplugin.initialize()
9 changes: 8 additions & 1 deletion ftplugin/typescript.lua
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
require("typescript-tools.user_commands").setup_user_commands()
local ftplugin = _G.nnyIAsk5fJtqzUaJ_typescript_tools_ftplugin

if not ftplugin then
ftplugin = dofile(vim.fn.expand "<sfile>:h:h" .. "/utils/ftplugin-common.lua")
_G.nnyIAsk5fJtqzUaJ_typescript_tools_ftplugin = ftplugin
end

ftplugin.initialize()
9 changes: 8 additions & 1 deletion ftplugin/typescriptreact.lua
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
require("typescript-tools.user_commands").setup_user_commands()
local ftplugin = _G.nnyIAsk5fJtqzUaJ_typescript_tools_ftplugin

if not ftplugin then
ftplugin = dofile(vim.fn.expand "<sfile>:h:h" .. "/utils/ftplugin-common.lua")
_G.nnyIAsk5fJtqzUaJ_typescript_tools_ftplugin = ftplugin
end

ftplugin.initialize()
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
@@ -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
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
@@ -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)
70 changes: 70 additions & 0 deletions utils/ftplugin-common.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
local M = {}

M.initialize = function()
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)
end

return M