Skip to content

Commit d250ccc

Browse files
better typescript support
1 parent a4d72a6 commit d250ccc

File tree

1 file changed

+53
-4
lines changed

1 file changed

+53
-4
lines changed

init.lua

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,22 @@ vim.keymap.set('n', '<C-l>', '<C-w><C-l>', { desc = 'Move focus to the right win
190190
vim.keymap.set('n', '<C-j>', '<C-w><C-j>', { desc = 'Move focus to the lower window' })
191191
vim.keymap.set('n', '<C-k>', '<C-w><C-k>', { desc = 'Move focus to the upper window' })
192192

193+
-- Define a function to easily set keymaps with description
194+
local function set_keymap(mode, lhs, rhs, desc)
195+
vim.api.nvim_set_keymap(mode, lhs, rhs, { noremap = true, silent = true, desc = desc })
196+
end
197+
198+
-- Typescript commands
199+
set_keymap('n', '<leader>cti', ':TSToolsOrganizeImports<CR>', 'Organize [I]mports')
200+
set_keymap('n', '<leader>cts', ':TSToolsSortImports<CR>', '[S]ort Imports')
201+
set_keymap('n', '<leader>ctu', ':TSToolsRemoveUnusedImports<CR>', 'Remove [U]nused Imports')
202+
set_keymap('n', '<leader>ctx', ':TSToolsRemoveUnused<CR>', 'Remove Unused Statements ([X])')
203+
set_keymap('n', '<leader>cta', ':TSToolsAddMissingImports<CR>', '[A]dd Missing Imports')
204+
set_keymap('n', '<leader>ctf', ':TSToolsFixAll<CR>', '[F]ix All Errors')
205+
set_keymap('n', '<leader>ctg', ':TSToolsGoToSourceDefinition<CR>', '[G]o to Source Definition')
206+
set_keymap('n', '<leader>ctr', ':TSToolsRenameFile<CR>', '[R]ename File')
207+
set_keymap('n', '<leader>ctR', ':TSToolsFileReferences<CR>', 'File [[R]]eferences')
208+
193209
-- [[ Basic Autocommands ]]
194210
-- See `:help lua-guide-autocommands`
195211

@@ -277,10 +293,11 @@ require('lazy').setup({
277293
'folke/which-key.nvim',
278294
event = 'VimEnter', -- Sets the loading event to 'VimEnter'
279295
config = function() -- This is the function that runs, AFTER loading
280-
require('which-key').setup()
296+
local wk = require 'which-key'
297+
wk.setup()
281298

282299
-- Document existing key chains
283-
require('which-key').register {
300+
wk.register {
284301
['<leader>c'] = { name = '[C]ode', _ = 'which_key_ignore' },
285302
['<leader>d'] = { name = '[D]ocument', _ = 'which_key_ignore' },
286303
['<leader>r'] = { name = '[R]ename', _ = 'which_key_ignore' },
@@ -290,9 +307,26 @@ require('lazy').setup({
290307
['<leader>h'] = { name = 'Git [H]unk', _ = 'which_key_ignore' },
291308
}
292309
-- visual mode
293-
require('which-key').register({
310+
wk.register({
294311
['<leader>h'] = { 'Git [H]unk' },
295312
}, { mode = 'v' })
313+
314+
wk.register({
315+
c = {
316+
t = {
317+
name = '[T]ypescript',
318+
i = 'Organize [I]mports',
319+
s = '[S]ort Imports',
320+
u = 'Remove [U]nused Imports',
321+
x = 'Remove Unused Statements ([X])',
322+
a = '[A]dd Missing Imports',
323+
f = '[F]ix All Errors',
324+
g = '[G]o to Source Definition',
325+
r = '[R]ename File',
326+
R = 'File [[R]]eferences',
327+
},
328+
},
329+
}, { prefix = '<leader>' })
296330
end,
297331
},
298332

@@ -423,6 +457,12 @@ require('lazy').setup({
423457
-- `neodev` configures Lua LSP for your Neovim config, runtime and plugins
424458
-- used for completion, annotations and signatures of Neovim apis
425459
{ 'folke/neodev.nvim', opts = {} },
460+
461+
{
462+
'pmizio/typescript-tools.nvim',
463+
dependencies = { 'nvim-lua/plenary.nvim', 'neovim/nvim-lspconfig' },
464+
opts = {},
465+
},
426466
},
427467
config = function()
428468
-- Brief aside: **What is LSP?**
@@ -607,7 +647,6 @@ require('lazy').setup({
607647
local ensure_installed = vim.tbl_keys(servers or {})
608648
vim.list_extend(ensure_installed, {
609649
'stylua', -- Used to format Lua code
610-
'tsserver',
611650
'lua_ls',
612651
})
613652
require('mason-tool-installer').setup { ensure_installed = ensure_installed }
@@ -620,10 +659,19 @@ require('lazy').setup({
620659
-- by the server configuration above. Useful when disabling
621660
-- certain features of an LSP (for example, turning off formatting for tsserver)
622661
server.capabilities = vim.tbl_deep_extend('force', {}, capabilities, server.capabilities or {})
662+
if server_name == 'tsserver' then
663+
return
664+
end
623665
require('lspconfig')[server_name].setup(server)
624666
end,
667+
['typescript-tools'] = function()
668+
require('typescript-tools').setup {}
669+
end,
625670
},
626671
}
672+
673+
-- Enable the `typescript-tools` plugin for TypeScript and JavaScript
674+
-- require('typescript-tools').setup {}
627675
end,
628676
},
629677

@@ -775,6 +823,7 @@ require('lazy').setup({
775823
{ name = 'nvim_lsp' },
776824
{ name = 'luasnip' },
777825
{ name = 'path' },
826+
{ name = 'nvim_lsp_signature_help' },
778827
},
779828
}
780829
end,

0 commit comments

Comments
 (0)