Skip to content

Commit 600943f

Browse files
committed
fix: cache ftplugin on first load
This eliminates repeated reads of ftplugin implementation file on repeated filetype events. The only reliably persistent store available to ftplugin scripts that I can think of is the global table, which is why I'm using it to store loaded ftplugin "object" in. The prefix is a string gneerated by rolling a fair dice in order to make name collision highly improbable. There might be a more efficient way to do this. Please tear this commit apart if you know of it.
1 parent 2fe5303 commit 600943f

File tree

5 files changed

+90
-56
lines changed

5 files changed

+90
-56
lines changed

ftplugin/javascript.lua

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
1-
dofile(vim.fn.expand "<sfile>:h:h" .. "/utils/ftplugin-common.lua")
1+
local ftplugin = _G.nnyIAsk5fJtqzUaJ_typescript_tools_ftplugin
2+
3+
if not ftplugin then
4+
ftplugin = dofile(vim.fn.expand "<sfile>:h:h" .. "/utils/ftplugin-common.lua")
5+
_G.nnyIAsk5fJtqzUaJ_typescript_tools_ftplugin = ftplugin
6+
end
7+
8+
ftplugin.initialize()

ftplugin/javascriptreact.lua

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
1-
dofile(vim.fn.expand "<sfile>:h:h" .. "/utils/ftplugin-common.lua")
1+
local ftplugin = _G.nnyIAsk5fJtqzUaJ_typescript_tools_ftplugin
2+
3+
if not ftplugin then
4+
ftplugin = dofile(vim.fn.expand "<sfile>:h:h" .. "/utils/ftplugin-common.lua")
5+
_G.nnyIAsk5fJtqzUaJ_typescript_tools_ftplugin = ftplugin
6+
end
7+
8+
ftplugin.initialize()

ftplugin/typescript.lua

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
1-
dofile(vim.fn.expand "<sfile>:h:h" .. "/utils/ftplugin-common.lua")
1+
local ftplugin = _G.nnyIAsk5fJtqzUaJ_typescript_tools_ftplugin
2+
3+
if not ftplugin then
4+
ftplugin = dofile(vim.fn.expand "<sfile>:h:h" .. "/utils/ftplugin-common.lua")
5+
_G.nnyIAsk5fJtqzUaJ_typescript_tools_ftplugin = ftplugin
6+
end
7+
8+
ftplugin.initialize()

ftplugin/typescriptreact.lua

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
1-
dofile(vim.fn.expand "<sfile>:h:h" .. "/utils/ftplugin-common.lua")
1+
local ftplugin = _G.nnyIAsk5fJtqzUaJ_typescript_tools_ftplugin
2+
3+
if not ftplugin then
4+
ftplugin = dofile(vim.fn.expand "<sfile>:h:h" .. "/utils/ftplugin-common.lua")
5+
_G.nnyIAsk5fJtqzUaJ_typescript_tools_ftplugin = ftplugin
6+
end
7+
8+
ftplugin.initialize()

utils/ftplugin-common.lua

Lines changed: 58 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,70 @@
1-
if vim.fn.exists "g:disable_typescript_tools" == 1 then
2-
return
3-
end
4-
if vim.fn.exists "b:did_typescript_tools_ftplugin" == 1 then
5-
return
6-
end
7-
vim.b.did_typescript_tools_ftplugin = true
1+
local M = {}
82

9-
---@param name string
10-
---@param fn function
11-
local function create_command(name, fn)
12-
local command_completion = {
13-
nargs = "?",
14-
complete = function()
15-
return { "sync" }
16-
end,
17-
}
18-
vim.api.nvim_buf_create_user_command(0, name, function(cmd)
19-
local words = cmd.fargs
3+
M.initialize = function()
4+
if vim.fn.exists "g:disable_typescript_tools" == 1 then
5+
return
6+
end
7+
if vim.fn.exists "b:did_typescript_tools_ftplugin" == 1 then
8+
return
9+
end
10+
vim.b.did_typescript_tools_ftplugin = true
2011

21-
if #words == 1 and words[1] ~= "sync" then
22-
vim.notify("No such command", vim.log.levels.ERROR)
23-
return
24-
end
12+
---@param name string
13+
---@param fn function
14+
local function create_command(name, fn)
15+
local command_completion = {
16+
nargs = "?",
17+
complete = function()
18+
return { "sync" }
19+
end,
20+
}
21+
vim.api.nvim_buf_create_user_command(0, name, function(cmd)
22+
local words = cmd.fargs
2523

26-
fn(#words == 1)
27-
end, command_completion)
28-
end
24+
if #words == 1 and words[1] ~= "sync" then
25+
vim.notify("No such command", vim.log.levels.ERROR)
26+
return
27+
end
2928

30-
create_command("TSToolsOrganizeImports", function(is_sync)
31-
require("typescript-tools.api").organize_imports(is_sync)
32-
end)
29+
fn(#words == 1)
30+
end, command_completion)
31+
end
3332

34-
create_command("TSToolsSortImports", function(is_sync)
35-
require("typescript-tools.api").sort_imports(is_sync)
36-
end)
33+
create_command("TSToolsOrganizeImports", function(is_sync)
34+
require("typescript-tools.api").organize_imports(is_sync)
35+
end)
3736

38-
create_command("TSToolsRemoveUnusedImports", function(is_sync)
39-
require("typescript-tools.api").remove_unused_imports(is_sync)
40-
end)
37+
create_command("TSToolsSortImports", function(is_sync)
38+
require("typescript-tools.api").sort_imports(is_sync)
39+
end)
4140

42-
create_command("TSToolsGoToSourceDefinition", function(is_sync)
43-
require("typescript-tools.api").go_to_source_definition(is_sync)
44-
end)
41+
create_command("TSToolsRemoveUnusedImports", function(is_sync)
42+
require("typescript-tools.api").remove_unused_imports(is_sync)
43+
end)
4544

46-
create_command("TSToolsRemoveUnused", function(is_sync)
47-
require("typescript-tools.api").remove_unused(is_sync)
48-
end)
45+
create_command("TSToolsGoToSourceDefinition", function(is_sync)
46+
require("typescript-tools.api").go_to_source_definition(is_sync)
47+
end)
4948

50-
create_command("TSToolsAddMissingImports", function(is_sync)
51-
require("typescript-tools.api").add_missing_imports(is_sync)
52-
end)
49+
create_command("TSToolsRemoveUnused", function(is_sync)
50+
require("typescript-tools.api").remove_unused(is_sync)
51+
end)
5352

54-
create_command("TSToolsFixAll", function(is_sync)
55-
require("typescript-tools.api").fix_all(is_sync)
56-
end)
53+
create_command("TSToolsAddMissingImports", function(is_sync)
54+
require("typescript-tools.api").add_missing_imports(is_sync)
55+
end)
5756

58-
create_command("TSToolsRenameFile", function(is_sync)
59-
require("typescript-tools.api").rename_file(is_sync)
60-
end)
57+
create_command("TSToolsFixAll", function(is_sync)
58+
require("typescript-tools.api").fix_all(is_sync)
59+
end)
60+
61+
create_command("TSToolsRenameFile", function(is_sync)
62+
require("typescript-tools.api").rename_file(is_sync)
63+
end)
64+
65+
create_command("TSToolsFileReferences", function(is_sync)
66+
require("typescript-tools.api").file_references(is_sync)
67+
end)
68+
end
6169

62-
create_command("TSToolsFileReferences", function(is_sync)
63-
require("typescript-tools.api").file_references(is_sync)
64-
end)
70+
return M

0 commit comments

Comments
 (0)