-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.lua
874 lines (757 loc) · 24.9 KB
/
init.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
-- [email protected]; email me to say hi or if there are any questions
vim.g.mapleader = ','
vim.g.maplocalleader = ','
-- Install package manager
-- https://github.com/folke/lazy.nvim
-- `:help lazy.nvim.txt` for more info
local lazypath = vim.fn.stdpath 'data' .. '/lazy/lazy.nvim'
if not vim.loop.fs_stat(lazypath) then
vim.fn.system {
'git',
'clone',
'--filter=blob:none',
'https://github.com/folke/lazy.nvim.git',
'--branch=stable', -- latest stable release
lazypath,
}
end
vim.opt.rtp:prepend(lazypath)
-- NOTE: Here is where you install your plugins.
-- You can configure plugins using the `config` key.
--
-- You can also configure plugins after the setup call,
-- as they will be available in your neovim runtime.
require('lazy').setup({
-- NOTE: First, some plugins that don't require any configuration
-- Git related plugins
'tpope/vim-fugitive',
'tpope/vim-rhubarb',
-- surround delimiters
'tpope/vim-surround',
-- repeat delimiters
'tpope/vim-repeat',
-- Detect tabstop and shiftwidth automatically
'tpope/vim-sleuth',
'zbirenbaum/copilot.lua',
{
'f-person/git-blame.nvim',
config = function()
vim.g.gitblame_enabled = 1
vim.g.gitblame_message_template = ' <author> • <date> • <summary>'
vim.g.gitblame_date_format = '%r'
vim.g.gitblame_highlight_group = 'Comment'
end
},
-- NOTE: This is where your plugins related to LSP can be installed.
-- The configuration is done below. Search for lspconfig to find it below.
{
-- LSP Configuration & Plugins
'neovim/nvim-lspconfig',
dependencies = {
-- Automatically install LSPs to stdpath for neovim
{ 'williamboman/mason.nvim', config = true },
'williamboman/mason-lspconfig.nvim',
-- Useful status updates for LSP
-- NOTE: `opts = {}` is the same as calling `require('fidget').setup({})`
{ 'j-hui/fidget.nvim', tag = 'legacy', opts = {} },
-- Additional lua configuration, makes nvim stuff amazing!
'folke/neodev.nvim',
},
},
{ "catppuccin/nvim", name = "catppuccin", priority = 1000 },
{
-- Autocompletion
'hrsh7th/nvim-cmp',
dependencies = {
-- Snippet Engine & its associated nvim-cmp source
'L3MON4D3/LuaSnip',
'saadparwaiz1/cmp_luasnip',
-- Adds LSP completion capabilities
'hrsh7th/cmp-nvim-lsp',
-- nvim-cmp source for path
'hrsh7th/cmp-path',
-- nvim-cmp source for buffer words
'hrsh7th/cmp-buffer',
},
},
{
"zbirenbaum/copilot-cmp",
config = function()
require("copilot_cmp").setup()
end
},
-- Useful plugin to show you pending keybinds.
{
-- Adds git releated signs to the gutter, as well as utilities for managing changes
'lewis6991/gitsigns.nvim',
opts = {
-- See `:help gitsigns.txt`
signs = {
add = { text = '+' },
change = { text = '~' },
delete = { text = '_' },
topdelete = { text = '‾' },
changedelete = { text = '~' },
},
on_attach = function(bufnr)
vim.keymap.set('n', '<leader>gp', require('gitsigns').prev_hunk,
{ buffer = bufnr, desc = '[G]o to [P]revious Hunk' })
vim.keymap.set('n', '<leader>gn', require('gitsigns').next_hunk, { buffer = bufnr, desc = '[G]o to [N]ext Hunk' })
vim.keymap.set('n', '<leader>ph', require('gitsigns').preview_hunk, { buffer = bufnr, desc = '[P]review [H]unk' })
end,
},
},
{
-- Set lualine as statusline
'nvim-lualine/lualine.nvim',
-- See `:help lualine.txt`
opts = {
options = {
icons_enabled = false,
-- theme = 'onedark',
component_separators = '|',
section_separators = '',
},
},
},
{
-- Add indentation guides even on blank lines
'lukas-reineke/indent-blankline.nvim',
main = 'ibl',
opts = {
},
},
-- "gc" to comment visual regions/lines
{ 'numToStr/Comment.nvim', opts = {} },
-- Fuzzy Finder (files, lsp, etc)
{ 'nvim-telescope/telescope.nvim', branch = '0.1.x', dependencies = { 'nvim-lua/plenary.nvim' } },
-- Fuzzy Finder Algorithm which requires local dependencies to be built.
-- Only load if `make` is available. Make sure you have the system
-- requirements installed.
{
'nvim-telescope/telescope-fzf-native.nvim',
-- NOTE: If you are having trouble with this installation,
-- refer to the README for telescope-fzf-native for more instructions.
build = 'make',
cond = function()
return vim.fn.executable 'make' == 1
end,
},
{
-- Highlight, edit, and navigate code
'nvim-treesitter/nvim-treesitter',
dependencies = {
'nvim-treesitter/nvim-treesitter-textobjects',
},
build = ':TSUpdate',
},
{
},
}, {})
-- [[ Setting options ]]
-- See `:help vim.o`
-- NOTE: You can change these options as you wish!
-- Set highlight on search
vim.o.hlsearch = false
-- Make line numbers default
vim.wo.number = true
-- Enable mouse mode
vim.o.mouse = 'a'
-- Sync clipboard between OS and Neovim.
-- Remove this option if you want your OS clipboard to remain independent.
-- See `:help 'clipboard'`
vim.o.clipboard = 'unnamedplus'
-- Enable break indent
vim.o.breakindent = true
-- Save undo history
vim.o.undofile = true
-- Case-insensitive searching UNLESS \C or capital in search
vim.o.ignorecase = true
vim.o.smartcase = true
-- Keep signcolumn on by default
vim.wo.signcolumn = 'yes'
-- Decrease update time
-- vim.o.updatetime = 250
vim.o.timeoutlen = 1000
-- Set completeopt to have a better completion experience
vim.o.completeopt = 'menu,menuone,noselect'
-- NOTE: You should make sure your terminal supports this
vim.o.termguicolors = true
-- [[ Basic Keymaps ]]
-- Keymaps for better default experience
-- See `:help vim.keymap.set()` vim.keymap.set({ 'n', 'v' }, '<Space>', '<Nop>', { silent = true })
-- Remap for dealing with word wrap
vim.keymap.set('n', 'k', "v:count == 0 ? 'gk' : 'k'", { expr = true, silent = true })
vim.keymap.set('n', 'j', "v:count == 0 ? 'gj' : 'j'", { expr = true, silent = true })
-- [[ Highlight on yank ]]
-- See `:help vim.highlight.on_yank()`
local highlight_group = vim.api.nvim_create_augroup('YankHighlight', { clear = true })
vim.api.nvim_create_autocmd('TextYankPost', {
callback = function()
vim.highlight.on_yank()
end,
group = highlight_group,
pattern = '*',
})
--
--
-- Enable telescope fzf native, if installed
pcall(require('telescope').load_extension, 'fzf')
--
-- -- See `:help telescope.builtin` for more. Try to stick with official docs or vim internal help "ALMOST ALWAYS"
-- [[ Configure Treesitter ]]
-- See `:help nvim-treesitter`
require('nvim-treesitter.configs').setup {
-- Add languages to be installed here that you want installed for treesitter
ensure_installed = { 'c', 'cpp', 'go', 'lua', 'python', 'rust', 'tsx', 'typescript', 'vimdoc', 'vim' },
-- Autoinstall languages that are not installed. Defaults to false (but you can change for yourself!)
-- auto_install = false,
auto_install = true,
highlight = { enable = true },
indent = { enable = true },
incremental_selection = {
enable = true,
keymaps = {
init_selection = '<c-space>',
node_incremental = '<c-space>',
scope_incremental = '<c-s>',
node_decremental = '<M-space>',
},
},
textobjects = {
select = {
enable = true,
lookahead = true, -- Automatically jump forward to textobj, similar to targets.vim
keymaps = {
-- You can use the capture groups defined in textobjects.scm
['aa'] = '@parameter.outer',
['ia'] = '@parameter.inner',
['af'] = '@function.outer',
['if'] = '@function.inner',
['ac'] = '@class.outer',
['ic'] = '@class.inner',
},
},
move = {
enable = true,
set_jumps = true, -- whether to set jumps in the jumplist
goto_next_start = {
[']m'] = '@function.outer',
[']]'] = '@class.outer',
},
goto_next_end = {
[']M'] = '@function.outer',
[']['] = '@class.outer',
},
goto_previous_start = {
['[m'] = '@function.outer',
['[['] = '@class.outer',
},
goto_previous_end = {
['[M'] = '@function.outer',
['[]'] = '@class.outer',
},
},
swap = {
enable = true,
swap_next = {
['<leader>a'] = '@parameter.inner',
},
swap_previous = {
['<leader>A'] = '@parameter.inner',
},
},
},
}
-- [[ Configure LSP ]]
-- This function gets run when an LSP connects to a particular buffer.
local on_attach = function(_, bufnr)
-- NOTE: Remember that lua is a real programming language, and as such it is possible
-- to define small helper and utility functions so you don't have to repeat yourself
-- many times.
--
-- In this case, we create a function that lets us more easily define mappings specific
-- for LSP related items. It sets the mode, buffer and description for us each time.
local nmap = function(keys, func, desc)
if desc then
desc = 'LSP: ' .. desc
end
vim.keymap.set('n', keys, func, { buffer = bufnr, desc = desc })
end
nmap('<leader>rn', vim.lsp.buf.rename, '[R]e[n]ame')
nmap('<leader>ca', vim.lsp.buf.code_action, '[C]ode [A]ction')
nmap('gd', vim.lsp.buf.definition, '[G]oto [D]efinition')
nmap('gr', require('telescope.builtin').lsp_references, '[G]oto [R]eferences')
nmap('gI', vim.lsp.buf.implementation, '[G]oto [I]mplementation')
nmap('<leader>D', vim.lsp.buf.type_definition, 'Type [D]efinition')
nmap('<leader>ds', require('telescope.builtin').lsp_document_symbols, '[D]ocument [S]ymbols')
nmap('<leader>ws', require('telescope.builtin').lsp_dynamic_workspace_symbols, '[W]orkspace [S]ymbols')
-- See `:help K` for why this keymap
nmap('K', vim.lsp.buf.hover, 'Hover Documentation')
nmap('<C-k>', vim.lsp.buf.signature_help, 'Signature Documentation')
-- Lesser used LSP functionality
nmap('gD', vim.lsp.buf.declaration, '[G]oto [D]eclaration')
nmap('<leader>wa', vim.lsp.buf.add_workspace_folder, '[W]orkspace [A]dd Folder')
nmap('<leader>wr', vim.lsp.buf.remove_workspace_folder, '[W]orkspace [R]emove Folder')
nmap('<leader>wl', function()
print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
end, '[W]orkspace [L]ist Folders')
-- Create a command `:Format` local to the LSP buffer
vim.api.nvim_buf_create_user_command(bufnr, 'Format', function(_)
vim.lsp.buf.format()
end, { desc = 'Format current buffer with LSP' })
end
-- Enable the following language servers
-- Feel free to add/remove any LSPs that you want here. They will automatically be installed.
--
-- Add any additional override configuration in the following tables. They will be passed to
-- the `settings` field of the server config. You must look up that documentation yourself.
--
-- If you want to override the default filetypes that your language server will attach to you can
-- define the property 'filetypes' to the map in question.
local servers = {
-- clangd = {},
-- gopls = {},
-- pyright = {},
-- rust_analyzer = {},
-- tsserver = {},
-- html = { filetypes = { 'html', 'twig', 'hbs'} },
lua_ls = {
Lua = {
workspace = { checkThirdParty = false },
telemetry = { enable = false },
},
},
}
-- Setup neovim lua configuration
require('neodev').setup()
-- nvim-cmp supports additional completion capabilities, so broadcast that to servers
local capabilities = vim.lsp.protocol.make_client_capabilities()
capabilities = require('cmp_nvim_lsp').default_capabilities(capabilities)
-- Ensure the servers above are installed
local mason_lspconfig = require 'mason-lspconfig'
mason_lspconfig.setup {
ensure_installed = vim.tbl_keys(servers),
}
mason_lspconfig.setup_handlers {
function(server_name)
require('lspconfig')[server_name].setup {
capabilities = capabilities,
on_attach = on_attach,
settings = servers[server_name],
filetypes = (servers[server_name] or {}).filetypes,
}
end
}
-- [[ Configure nvim-cmp ]]
local cmp = require 'cmp'
local luasnip = require 'luasnip'
require('luasnip.loaders.from_vscode').lazy_load()
luasnip.config.setup {}
cmp.setup {
mapping = cmp.mapping.preset.insert {
['<C-n>'] = cmp.mapping.select_next_item(),
['<C-p>'] = cmp.mapping.select_prev_item(),
['<C-b>'] = cmp.mapping.scroll_docs(-4),
['<C-f>'] = cmp.mapping.scroll_docs(4),
['<C-e>'] = cmp.mapping.abort(),
['<C-y>'] = cmp.mapping.confirm({select = true}),
['<C-Space>'] = cmp.mapping.complete {},
['<CR>'] = cmp.mapping.confirm {
behavior = cmp.ConfirmBehavior.Replace,
select = true,
},
['<Tab>'] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_next_item()
elseif luasnip.expand_or_locally_jumpable() then
luasnip.expand_or_jump()
else
fallback()
end
end, { 'i', 's' }),
['<S-Tab>'] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_prev_item()
elseif luasnip.locally_jumpable(-1) then
luasnip.jump(-1)
else
fallback()
end
end, { 'i', 's' }),
},
sources = {
{name = 'copilot'},
{name = 'nvim_lsp'},
{name = 'luasnip'},
{name = 'buffer', min_length = 4},
{name = 'path', min_length = 4},
},
}
require("copilot").setup({
suggestion = { enabled = false },
panel = { enabled = false },
})
local has_words_before = function()
if vim.api.nvim_buf_get_option(0, "buftype") == "prompt" then return false end
local line, col = unpack(vim.api.nvim_win_get_cursor(0))
return col ~= 0 and vim.api.nvim_buf_get_text(0, line - 1, 0, line - 1, col, {})[1]:match("^%s*$") == nil
end
cmp.setup({
mapping = {
["<Tab>"] = vim.schedule_wrap(function(fallback)
if cmp.visible() and has_words_before() then
cmp.select_next_item({ behavior = cmp.SelectBehavior.Select })
else
fallback()
end
end),
},
})
-- Function to set colorscheme
function SetColorScheme()
vim.cmd('colorscheme catppuccin-mocha')
end
-- Call the function to set the colorscheme
SetColorScheme()
vim.cmd('set autoread')
vim.cmd('au SwapExists * let v:swapchoice = "e"')
-- restore cursor position
vim.api.nvim_create_autocmd({ "BufReadPost", "BufEnter" }, {
pattern = "*",
callback = function()
-- check if mark `"` exists and if it's within the range of the buffer
local mark = vim.fn.getpos('`"')
local line_count = vim.api.nvim_buf_line_count(0)
if mark[1] ~= -1 and mark[2] <= line_count then
-- if mark `"` is valid, move the cursor to its position and scroll the window
vim.api.nvim_exec('silent! normal! g`"zv', false)
end
end,
})
vim.cmd[[autocmd InsertEnter * :set tabstop=2 shiftwidth=2 expandtab]]
-- Shift text in visual mode1
vim.cmd([[
let s:save_cpo = &cpo
set cpo&vim
let s:pv_active = 1
function! PV_On ()
let s:pv_active = 1
endfunction
function! PV_Off ()
let s:pv_active = 0
endfunction
function PV_Toggle ()
let s:pv_active = !s:pv_active
endfunction
" When shifting, retain selection over multiple shifts...
silent! xmap <unique><silent><expr> > <SID>ShiftKeepingSelection(">")
silent! xmap <unique><silent><expr> < <SID>ShiftKeepingSelection("<")
" Hit <RETURN> to escape visual mode...
silent! xnoremap <unique><silent> <CR> <ESC>
" Hit ZZ to quit from within visual mode...
silent! xnoremap <unique><silent> ZZ <ESC>ZZ
" Allow selection to persist through an undo...
silent! xnoremap <unique><silent> u <ESC>ugv
silent! xnoremap <unique><silent> <C-R> <ESC><C-R>gv
function! s:ShiftKeepingSelection(cmd)
set nosmartindent
" No-op if plugin not active, or tab expansions are off...
if !s:pv_active || !&expandtab
return a:cmd . ":set smartindent\<CR>"
" Visual and Visual Line modes...
elseif mode() =~ '[vV]'
return a:cmd . ":set smartindent\<CR>gv"
" Visual block mode...
else
" Work out the adjustment for the way we're shifting...
let b:_pv_shift_motion
\ = &shiftwidth . (a:cmd == '>' ? "\<RIGHT>" : "\<LEFT>")
" Return instructions to implement the shift and reset selection...
return a:cmd . ":set smartindent\<CR>uM"
endif
endfunction
let &cpo = s:save_cpo
]])
-- Terminal stuff
vim.cmd([[ command! Term :botright sp | term ]])
vim.cmd([[ command! Termvsp :vertical sp | term ]])
vim.cmd([[
" Speed up viewport scrolling
nnoremap <C-e> 3<C-e>
nnoremap <C-y> 3<C-y>
" Magically build interim directories if necessary
" thanks to Author: Damian Conway
function! AskQuit (msg, options, quit_option)
if confirm(a:msg, a:options) == a:quit_option
exit
endif
endfunction
function! EnsureDirExists ()
let required_dir = expand("%:h")
if !isdirectory(required_dir)
call AskQuit("Parent directory '" . required_dir . "' doesn't exist.",
\ "&Create it\nor &Quit?", 2)
try
call mkdir( required_dir, 'p' )
catch
call AskQuit("Can't create '" . required_dir . "'",
\ "&Quit\nor &Continue anyway?", 1)
endtry
endif
endfunction
augroup AutoMkdir
autocmd!
autocmd BufNewFile * :call EnsureDirExists()
augroup END
" " Damian Conway's Blink function
nnoremap <silent> n n:call HLNext(0.1)<cr>
nnoremap <silent> N N:call HLNext(0.1)<cr>
function! HLNext (blinktime)
let target_pat = '\c\%#'.@/
let ring = matchadd('ErrorMsg', target_pat, 101)
redraw
exec 'sleep ' . float2nr(a:blinktime * 2000) . 'm'
call matchdelete(ring)
redraw
endfunction
" make the command mode less annoying
" Emacs(readline) binding is here
" start of line
cnoremap <C-A> <Home>
" back one character
cnoremap <C-B> <Left>
" delete character under cursor
cnoremap <C-D> <Del>
" end of line
cnoremap <C-E> <End>
" forward one character
cnoremap <C-F> <Right>
" recall newer command-line
cnoremap <C-N> <Down>
" recall previous (older) command-line
cnoremap <C-P> <Up>
" back one word
inoremap <expr> <C-B> getline('.')=~'^\s*$'&&col('.')>strlen(getline('.'))?"0\<Lt>C-D>\<Lt>Esc>kJs":"\<Lt>Left>"
" delete character under cursor
inoremap <expr> <C-D> col('.')>strlen(getline('.'))?"\<Lt>C-D>":"\<Lt>Del>"
" end of line
" forward one character
inoremap <expr> <C-F> col('.')>strlen(getline('.'))?"\<Lt>C-F>":"\<Lt>Right>"
" common type-mistakes
ab teh the
" Highlight Matched Parenthesis
hi MatchParen ctermbg=gray guibg=lightgray
" Better JUMP upwards and downwards
inoremap <C-k> <C-g>k
" remember, I have remapped <C-j> VIM's default Behavior
inoremap <C-j> <C-g>j
nnoremap Y "+Y
nnoremap y "+yy
xnoremap Y "+Y
xnoremap y "+y
function! HighlightAllOfWord(...)
if exists("a:1")
au CursorMoved * silent! exe printf('match Search /\<%s\>/', expand('<cword>'))
endif
if a:0 < 1
match none /\<%s\>/
endif
endfunction
command! -nargs=? HighlightAllOfWord call HighlightAllOfWord(<f-args>)
vnoremap J :m '>+1<CR>gv=gv
vnoremap K :m '<-2<CR>gv=gv
" STEAL FROM reactJS creator MACVIM box
function! s:VSplitIntoNextTab()
"there is only one window
if tabpagenr('$') == 1 && winnr('$') == 1
return
endif
"preparing new window
let l:tab_nr = tabpagenr('$')
let l:cur_buf = bufnr('%')
if tabpagenr() < tab_nr
close!
if l:tab_nr == tabpagenr('$')
tabnext
endif
vsp
else
close!
tabnew
endif
"opening current buffer in new window
exe "b".l:cur_buf
endfunc
function! s:VSplitIntoPrevTab()
"there is only one window
if tabpagenr('$') == 1 && winnr('$') == 1
return
endif
"preparing new window
let l:tab_nr = tabpagenr('$')
let l:cur_buf = bufnr('%')
if tabpagenr() != 1
close!
if l:tab_nr == tabpagenr('$')
tabprev
endif
vsp
else
close!
exe "0tabnew"
endif
"opening current buffer in new window
exe "b".l:cur_buf
endfunc
command! VSplitIntoPrevTab call s:VSplitIntoPrevTab()
command! VSplitIntoNextTab call s:VSplitIntoNextTab()
]])
local vim = vim
function load_large_file_async(filename, chunk_size)
chunk_size = chunk_size or 8192 -- default chunk size
local cmd = string.format("dd if=%s bs=%d", filename, chunk_size)
-- Handler for each chunk of data
local on_data = function(_, data, _)
-- Process each chunk of data here
-- For example, you can append it to a buffer
end
-- Handler for the end of the job
local on_exit = function(_, exit_code, _)
if exit_code == 0 then
print("File loaded successfully")
else
print("Error loading file")
end
end
-- Create an asynchronous job to read the file in chunks
vim.fn.jobstart(cmd, {
on_stdout = on_data,
on_stderr = on_data,
on_exit = on_exit,
})
end
-- https://github.com/f-person/git-blame.nvim
vim.g.gitblame_display_virtual_text = 0 -- Disable virtual text
local git_blame = require('gitblame')
require('lualine').setup({
sections = {
lualine_c = {
{ git_blame.get_current_blame_text, cond = git_blame.is_blame_text_available }
}
}
})
-- python symbolic representation
local function python_symbols(opts)
opts = opts or {}
local bufnr = vim.api.nvim_get_current_buf()
local file_extension = vim.fn.expand('%:e')
if file_extension ~= 'py' and file_extension ~= 'pyc' then
return
end
local parser = vim.treesitter.get_parser(bufnr, 'python')
local tree = parser:parse()[1]
local root = tree:root()
local query = vim.treesitter.query.parse('python', [[
(class_definition name: (identifier) @class)
(function_definition name: (identifier) @function)
(decorator) @decorator
(assignment left: (identifier) @variable)
(import_from_statement name: (dotted_name) @import)
(import_statement name: (dotted_name) @import)
(parameter name: (identifier) @parameter)
]])
local symbols = {classes = {}, functions = {}, variables = {}, decorators = {}, imports = {}, parameters = {}}
local positions = {}
local function get_scope(node)
local scope = {}
local parent = node:parent()
while parent do
local parent_type = parent:type()
if parent_type == "class_definition" then
table.insert(scope, 1, "Class: " .. vim.treesitter.get_node_text(parent:field("name")[1], bufnr))
elseif parent_type == "function_definition" then
table.insert(scope, 1, "Function: " .. vim.treesitter.get_node_text(parent:field("name")[1], bufnr))
end
parent = parent:parent()
end
if #scope == 0 then
return "Global"
else
return table.concat(scope, " > ")
end
end
local start_row, start_col, end_row, end_col = root:range()
for id, node in query:iter_captures(root, bufnr, start_row, end_row) do
local name = vim.treesitter.get_node_text(node, bufnr)
local row, col = node:start()
local node_type = query.captures[id] -- Capture name
local scope = get_scope(node)
local entry = {
value = name,
display = string.format("%s [%s]", name, scope),
ordinal = name,
loc = {row, col},
kind = node_type
}
if node_type == "class" then
table.insert(symbols.classes, entry)
elseif node_type == "function" then
table.insert(symbols.functions, entry)
elseif node_type == "variable" then
table.insert(symbols.variables, entry)
elseif node_type == "decorator" then
table.insert(symbols.decorators, entry)
elseif node_type == "import" then
table.insert(symbols.imports, entry)
elseif node_type == "parameter" then
table.insert(symbols.parameters, entry)
end
end
local flattened_symbols = {}
for _, class in ipairs(symbols.classes) do
table.insert(flattened_symbols, class)
end
for _, decorator in ipairs(symbols.decorators) do
table.insert(flattened_symbols, decorator)
end
for _, func in ipairs(symbols.functions) do
table.insert(flattened_symbols, func)
end
for _, variable in ipairs(symbols.variables) do
table.insert(flattened_symbols, variable)
end
for _, import in ipairs(symbols.imports) do
table.insert(flattened_symbols, import)
end
for _, param in ipairs(symbols.parameters) do
table.insert(flattened_symbols, param)
end
require('telescope.pickers').new(opts, {
prompt_title = 'Python Symbols',
finder = require('telescope.finders').new_table {
results = flattened_symbols,
entry_maker = function(entry)
return entry
end
},
sorter = require('telescope.config').values.generic_sorter(opts),
attach_mappings = function(prompt_bufnr, map)
local actions = require('telescope.actions')
local action_state = require('telescope.actions.state')
local function goto_location()
local selection = action_state.get_selected_entry()
actions.close(prompt_bufnr)
vim.api.nvim_win_set_cursor(0, {selection.loc[1] + 1, selection.loc[2]})
vim.cmd("normal! zz")
end
map('i', '<CR>', goto_location)
map('n', '<CR>', goto_location)
return true
end
}):find()
end
vim.api.nvim_create_user_command('PythonSymbols', function()
python_symbols()
end, {})
--