Skip to content

Commit dc4c380

Browse files
committed
neovim
1 parent 836aba7 commit dc4c380

3 files changed

Lines changed: 115 additions & 15 deletions

File tree

home/.config/nvim/init.lua

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ require('lazy').setup({
7171
'williamboman/mason.nvim',
7272
'williamboman/mason-lspconfig.nvim',
7373
'NeogitOrg/neogit',
74+
'nvim-neo-tree/neo-tree.nvim',
7475
'alaviss/nim.nvim',
7576
{'hrsh7th/nvim-cmp', dependencies = {'hrsh7th/cmp-buffer', 'hrsh7th/cmp-nvim-lsp'}},
7677
'terrortylor/nvim-comment',
@@ -79,7 +80,7 @@ require('lazy').setup({
7980
{'nvim-treesitter/nvim-treesitter', build = ':TSUpdate'},
8081
-- {'romgrk/nvim-treesitter-context', config = function() require('treesitter-context').setup() end},
8182
'nvim-treesitter/nvim-treesitter-textobjects',
82-
'pwntester/octo.nvim',
83+
-- 'pwntester/octo.nvim',
8384
{'stevearc/overseer.nvim', opts = {}},
8485
'nvim-treesitter/playground',
8586
{'nvim-telescope/telescope.nvim', dependencies = {'nvim-lua/plenary.nvim'}},
@@ -180,7 +181,6 @@ nmap('<leader>fc', '<cmd>cd %:p:h<cr>')
180181
nmap('<leader>fe', '<cmd>e ~/.config/nvim/init.lua<cr>')
181182
nmap('<leader>ff', '<cmd>Telescope find_files<cr>')
182183
nmap('<leader>fg', '<cmd>Telescope git_files<cr>')
183-
nmap('<leader>fk', '<cmd>Telescope keymaps<cr>')
184184
nmap('<leader>fr', '<cmd>Telescope oldfiles<cr>')
185185
-- <leader>g (fugitive)
186186
nmap('<leader>gb', '<cmd>Git blame<cr>')
@@ -209,6 +209,8 @@ nmap('<leader>pf', '<cmd>lua require("telescope.builtin").find_files({search_dir
209209
nmap('<leader>qq', '<cmd>quit<cr>')
210210
-- <leader>s (search)
211211
nmap('<leader>sd', '<cmd>lua require("telescope.builtin").live_grep({cwd=vim.fn.expand("%:p:h")})<cr>', 'Search directory')
212+
nmap('<leader>sh', '<cmd>Telescope help_tags<cr>')
213+
nmap('<leader>sk', '<cmd>Telescope keymaps<cr>')
212214
nmap('<leader>sp', '<cmd>lua require("telescope.builtin").live_grep({cwd=MyProject()})<cr>', 'Search project')
213215
nmap('<leader>ss', '<cmd>Telescope current_buffer_fuzzy_find<cr>', 'Search buffer')
214216
-- <leader>t (toggle & terminal)

home/.config/nvim/lua/lsp.lua

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
---Adapted from LazyVim util/lsp.lua
2+
local M = {}
3+
4+
M.words = {}
5+
M.words.enabled = false
6+
M.words.ns = vim.api.nvim_create_namespace("vim_lsp_references")
7+
8+
---@param opts? {enabled?: boolean}
9+
function M.words.setup(opts)
10+
opts = opts or {}
11+
if not opts.enabled then
12+
return
13+
end
14+
M.words.enabled = true
15+
local handler = vim.lsp.handlers["textDocument/documentHighlight"]
16+
vim.lsp.handlers["textDocument/documentHighlight"] = function(err, result, ctx, config)
17+
if not vim.api.nvim_buf_is_loaded(ctx.bufnr) then
18+
return
19+
end
20+
vim.lsp.buf.clear_references()
21+
return handler(err, result, ctx, config)
22+
end
23+
24+
M.on_supports_method("textDocument/documentHighlight", function(_, buf)
25+
vim.api.nvim_create_autocmd({ "CursorHold", "CursorHoldI", "CursorMoved", "CursorMovedI" }, {
26+
group = vim.api.nvim_create_augroup("lsp_word_" .. buf, { clear = true }),
27+
buffer = buf,
28+
callback = function(ev)
29+
if not require("lazyvim.plugins.lsp.keymaps").has(buf, "documentHighlight") then
30+
return false
31+
end
32+
33+
if not ({ M.words.get() })[2] then
34+
if ev.event:find("CursorMoved") then
35+
vim.lsp.buf.clear_references()
36+
else
37+
vim.lsp.buf.document_highlight()
38+
end
39+
end
40+
end,
41+
})
42+
end)
43+
end
44+
45+
---@return LspWord[] words, number? current
46+
function M.words.get()
47+
local cursor = vim.api.nvim_win_get_cursor(0)
48+
local current, ret = nil, {} ---@type number?, LspWord[]
49+
for _, extmark in ipairs(vim.api.nvim_buf_get_extmarks(0, M.words.ns, 0, -1, { details = true })) do
50+
local w = {
51+
from = { extmark[2] + 1, extmark[3] },
52+
to = { extmark[4].end_row + 1, extmark[4].end_col },
53+
}
54+
ret[#ret + 1] = w
55+
if cursor[1] >= w.from[1] and cursor[1] <= w.to[1] and cursor[2] >= w.from[2] and cursor[2] <= w.to[2] then
56+
current = #ret
57+
end
58+
end
59+
return ret, current
60+
end
61+
62+
---@param count number
63+
---@param cycle? boolean
64+
function M.words.jump(count, cycle)
65+
local words, idx = M.words.get()
66+
if not idx then
67+
return
68+
end
69+
idx = idx + count
70+
if cycle then
71+
idx = (idx - 1) % #words + 1
72+
end
73+
local target = words[idx]
74+
if target then
75+
vim.api.nvim_win_set_cursor(0, target.from)
76+
end
77+
end
78+
79+
return M

home/.config/nvim/lua/plugins.lua

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
local M = {}
2+
M.lsp = require'lsp'
3+
14
require'hop'.setup()
25

36
require('telescope').setup{
@@ -82,7 +85,11 @@ local on_attach = function(client, bufnr)
8285
end
8386
options = vim.tbl_extend('force', options, opts)
8487
end
85-
vim.api.nvim_buf_set_keymap(bufnr, mode, lhs, rhs, options)
88+
if type(opts) == 'string' then
89+
vim.api.nvim_buf_set_keymap(bufnr, mode, lhs, rhs, options)
90+
else
91+
vim.keymap.set(mode, lhs, rhs, {buffer=true})
92+
end
8693
end
8794
local function nmap(lhs, rhs, opts)
8895
map('n', lhs, rhs, opts)
@@ -125,23 +132,33 @@ local on_attach = function(client, bufnr)
125132
nmap('xd', '<cmd>CclsDerived<cr>')
126133
nmap('xi', '<cmd>lua vim.lsp.buf.implementation()<cr>', 'Implementation')
127134
nmap('xm', '<cmd>CclsMember<cr>', 'member')
135+
nmap('xn', function() M.lsp.words.jump(vim.v.count1) end, 'Next reference')
136+
nmap('xp', function() M.lsp.words.jump(-vim.v.count1) end, 'Prev reference')
128137
nmap('xt', '<cmd>lua vim.lsp.buf.type_definition()<cr>', 'Type definition')
129138
nmap('xv', '<cmd>CclsVars<cr>', 'vars')
130139

131-
if client.supports_method 'textDocument/documentHighlight' then
132-
vim.api.nvim_create_augroup('lsp_document_highlight', {clear = true})
133-
vim.api.nvim_clear_autocmds {buffer = bufnr, group = 'lsp_document_highlight'}
134-
vim.api.nvim_create_autocmd('CursorHold', {
135-
callback = vim.lsp.buf.document_highlight,
140+
if client.supports_method 'textDocument/codeLens' then
141+
vim.api.nvim_create_autocmd({'BufEnter'}, {
142+
group = vim.api.nvim_create_augroup('lsp_buf_' .. bufnr, {clear = true}),
136143
buffer = bufnr,
137-
group = 'lsp_document_highlight',
138-
desc = 'Document Highlight',
144+
callback = function(ev)
145+
vim.lsp.codelens.refresh({ bufnr = 0 })
146+
end,
139147
})
140-
vim.api.nvim_create_autocmd('CursorMoved', {
141-
callback = vim.lsp.buf.clear_references,
148+
end
149+
150+
if client.supports_method 'textDocument/documentHighlight' then
151+
vim.api.nvim_create_autocmd({'CursorHold', 'CursorHoldI', 'CursorMoved', 'CursorMovedI'}, {
152+
group = vim.api.nvim_create_augroup('lsp_word_' .. bufnr, {clear = true}),
142153
buffer = bufnr,
143-
group = 'lsp_document_highlight',
144-
desc = 'Clear All the References',
154+
callback = function(ev)
155+
if ev.event:find('CursorMoved') then
156+
vim.lsp.buf.clear_references()
157+
else
158+
vim.lsp.buf.document_highlight()
159+
end
160+
end,
161+
desc = 'Document Highlight',
145162
})
146163
end
147164

@@ -301,6 +318,8 @@ require('gitsigns').setup()
301318
local neogit = require('neogit')
302319
neogit.setup {}
303320

304-
require('octo').setup()
321+
-- require('octo').setup()
305322

306323
require('which-key').setup()
324+
325+
return M

0 commit comments

Comments
 (0)