summaryrefslogtreecommitdiff
path: root/.config/nvim/lua/extra.lua
blob: c299110b4f299d53ae1216e88067d9b2401ae476 (plain)
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
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
local function augroup(name)
    return vim.api.nvim_create_augroup('kyren-' .. name, { clear = true })
end

----------------------------------------------------------------------------
-- NOTE: Highlight when yanking text
----------------------------------------------------------------------------
vim.api.nvim_create_autocmd('TextYankPost', {
    group = augroup('highlight-yank'),
    callback = function()
        vim.highlight.on_yank()
    end,
})

vim.api.nvim_create_autocmd('BufEnter', {
    desc = 'Disable newlines on commented lines from continuing the comment',
    group = augroup('disable-comments-continuation'),
    callback = function()
        -- vim.opt_local.formatoptions:remove('r') -- no comments on enter
        vim.opt_local.formatoptions:remove('o') -- no comments on `o` or `O`
    end,
})

-- NOTE: highlight %v, %s etc in go string literals
vim.api.nvim_create_autocmd({ 'BufEnter', 'TextChanged', 'InsertLeave' }, {
    pattern = '*.go',
    callback = function()
        local query = vim.treesitter.query.parse('go', '(interpreted_string_literal) @string')
        local parser = vim.treesitter.get_parser(0, 'go')
        if not parser then
            return
        end
        local tree = parser:parse()[1]
        local root = tree:root()
        local bufnr = vim.api.nvim_get_current_buf()

        for id, node in query:iter_captures(root, bufnr, 0, -1) do
            if query.captures[id] == 'string' then
                local start_row, start_col, end_row, end_col = node:range()

                -- Get the text of the string literal
                local text = vim.api.nvim_buf_get_text(bufnr, start_row, start_col, end_row, end_col, {})[1]

                -- Highlight only the parts matching `%[a-z]`
                for match_start, match_end in text:gmatch('()%%[a-z]()') do
                    vim.api.nvim_buf_add_highlight(
                        bufnr,
                        -1,
                        '@lsp.type.formatSpecifier.go', -- Higlight group
                        start_row,
                        start_col + match_start - 1,
                        start_col + match_end - 1
                    )
                end
            end
        end
    end,
})

----------------------------------------------------------------------------
-- NOTE: Snacks snippet to notify LSP servers when renaming files in oil.nvim
----------------------------------------------------------------------------
vim.api.nvim_create_autocmd('User', {
    pattern = 'OilActionsPost',
    callback = function(event)
        if event.data.actions.type == 'move' then
            Snacks.rename.on_rename_file(event.data.actions.src_url, event.data.actions.dest_url)
        end
    end,
})

----------------------------------------------------------------------------
-- NOTE: Check if we need to reload the file when it changed
----------------------------------------------------------------------------
vim.api.nvim_create_autocmd({ 'FocusGained', 'TermClose', 'TermLeave' }, {
    group = augroup('checktime'),
    callback = function()
        if vim.o.buftype ~= 'nofile' then
            vim.cmd('checktime')
        end
    end,
})

----------------------------------------------------------------------------
-- NOTE: Close some filetypes with <q>
----------------------------------------------------------------------------
vim.api.nvim_create_autocmd('FileType', {
    group = augroup('close_with_q'),
    pattern = {
        'PlenaryTestPopup',
        'checkhealth',
        'dbout',
        'gitsigns-blame',
        'grug-far',
        'help',
        'lspinfo',
        'neotest-output',
        'neotest-output-panel',
        'neotest-summary',
        'notify',
        'qf',
        'startuptime',
        'tsplayground',
    },
    callback = function(event)
        vim.bo[event.buf].buflisted = false
        vim.schedule(function()
            vim.keymap.set('n', 'q', function()
                vim.cmd('close')
                pcall(vim.api.nvim_buf_delete, event.buf, { force = true })
            end, {
                buffer = event.buf,
                silent = true,
                desc = 'Quit buffer',
            })
        end)
    end,
})

----------------------------------------------------------------------------
-- NOTE: Format and organize imports on file save
----------------------------------------------------------------------------
vim.api.nvim_create_autocmd('BufWritePre', {
    group = augroup('java-auto-format'),
    pattern = '*.java',
    callback = function(event)
        if vim.g.disable_autoformat or vim.b[event.buf].disable_autoformat then
            return
        end

        require('jdtls').organize_imports()
        vim.lsp.buf.format({ async = false })
    end,
})

----------------------------------------------------------------------------
-- NOTE: show unused zig variables as "DiagnosticUnnecessary" instead of error
----------------------------------------------------------------------------
local orig_underline_show = vim.diagnostic.handlers.underline.show
local custom_ns = vim.api.nvim_create_namespace('custom_unused_ns')

vim.diagnostic.handlers.underline.show = function(namespace, bufnr, diagnostics, opts)
    local normal_diags = {}
    local custom_diags = {}
    for _, diag in ipairs(diagnostics) do
        if diag.message:find('unused', 1, true) then
            table.insert(custom_diags, diag)
        else
            table.insert(normal_diags, diag)
        end
    end
    if #normal_diags > 0 and orig_underline_show ~= nil then
        orig_underline_show(namespace, bufnr, normal_diags, opts)
    end
    for _, diag in ipairs(custom_diags) do
        vim.highlight.range(
            bufnr,
            custom_ns,
            'DiagnosticUnnecessary',
            { diag.lnum, diag.col },
            { diag.end_lnum, diag.end_col },
            { inclusive = false }
        )
    end
end

local orig_underline_hide = vim.diagnostic.handlers.underline.hide

vim.diagnostic.handlers.underline.hide = function(namespace, bufnr)
    if orig_underline_hide ~= nil then
        orig_underline_hide(namespace, bufnr)
    end
    vim.api.nvim_buf_clear_namespace(bufnr, custom_ns, 0, -1)
end

-- NOTE: template for C/C++ files
vim.api.nvim_create_autocmd({ 'BufNewFile', 'BufEnter' }, {
    pattern = { '*.c', '*.h', '*.cpp' },
    callback = function()
        local buf = vim.api.nvim_get_current_buf()
        local line_count = vim.api.nvim_buf_line_count(buf)
        local is_empty = line_count == 1 and vim.api.nvim_buf_get_lines(buf, 0, 1, false)[1] == ''
        if not is_empty then
            return
        end

        local ext = vim.fn.expand('%:e')
        local cwd = vim.fn.getcwd()
        local project_name = vim.fn.fnamemodify(cwd, ':t')
        local copyright = nil
        local lines = nil

        if project_name == 'krypton' then
            copyright = {
                '// Copyright (c) 2026 Kyren <contact@kyren.codes>. All rights reserved.',
                '',
            }

            local c_template = {
                '#include "base.h"',
                '',
            }

            lines = {
                c = c_template,
                h = {
                    '#ifndef ' .. string.upper(vim.fn.expand('%:t:r')) .. '_H',
                    '#define ' .. string.upper(vim.fn.expand('%:t:r')) .. '_H',
                    '',
                    '#include "generated/' .. vim.fn.expand('%:t:r') .. '.meta.h"',
                    '',
                    '#endif',
                },
                cpp = c_template,
            }
        end

        if not lines then
            return
        end
        local content = lines[ext]

        if copyright then
            content = vim.list_extend(copyright, content)
        end

        vim.api.nvim_buf_set_lines(0, 0, -1, false, content)
    end,
})

----------------------------------------------------------------------------
-- NOTE: Function to highlight TODO/NOTE patterns
----------------------------------------------------------------------------
local todo_ns = vim.api.nvim_create_namespace('todo_highlight')
local groups = {
    todo = {
        keywords = { 'TODO', 'WIP' },
        hl = '@comment.todo.comment',
    },
    warning = {
        keywords = { 'WARN', 'WARNING', 'HACK', 'SECURITY', 'SECURE', 'IMPORTANT' },
        hl = '@comment.warning.comment',
    },
    error = {
        keywords = { 'FIX', 'FIXME', 'ERROR', 'UNSAFE', 'SAFETY' },
        hl = '@comment.error.comment',
    },
    perf = {
        keywords = { 'PERF', 'OPTIMIZE', 'STUDY' },
        hl = '@comment.perf.comment',
    },
    note = {
        keywords = { 'NOTE', 'INFO', 'DOCS', 'TEST' },
        hl = '@comment.note.comment',
    },
}
local scopes = {
    bracket = '@punctuation.bracket.comment',
    constant = '@constant.comment',
    delimiter = '@punctuation.delimiter.comment',
}

function RenderTodoHighlights(bufnr)
    vim.api.nvim_buf_clear_namespace(bufnr, todo_ns, 0, -1)
    local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)

    for i, line in ipairs(lines) do
        for _, cfg in pairs(groups) do
            for _, kw in ipairs(cfg.keywords) do
                for s, e in line:gmatch('()' .. kw .. '()') do
                    -- 1) highlight keyword
                    vim.api.nvim_buf_add_highlight(bufnr, todo_ns, cfg.hl, i - 1, s - 1, e - 1)

                    local next_char = line:sub(e, e)

                    -- 2) highlight (...) if it directly follows keyword
                    if next_char == '(' then
                        local ps = e
                        local pe = nil
                        for j = e + 1, math.min(#line, e + 100) do
                            if line:sub(j, j) == ')' then
                                pe = j
                                break
                            end
                        end

                        if pe then
                            vim.api.nvim_buf_add_highlight(bufnr, todo_ns, scopes.bracket, i - 1, ps - 1, ps)
                            vim.api.nvim_buf_add_highlight(bufnr, todo_ns, scopes.bracket, i - 1, pe - 1, pe)
                            if pe - ps > 1 then
                                vim.api.nvim_buf_add_highlight(bufnr, todo_ns, scopes.constant, i - 1, ps, pe - 1)
                            end

                            e = pe
                        end
                    end

                    -- -- 3) highlight ":" if directly after keyword or after parens
                    local colon_pos = nil
                    if line:sub(e, e) == ':' then
                        colon_pos = e
                    end

                    if colon_pos then
                        vim.api.nvim_buf_add_highlight(
                            bufnr,
                            todo_ns,
                            scopes.delimiter,
                            i - 1,
                            colon_pos - 1,
                            colon_pos
                        )
                    end
                end
            end
        end
    end
end

vim.api.nvim_create_autocmd({ 'VimEnter', 'BufRead', 'BufWinEnter', 'BufWritePost', 'TextChanged', 'TextChangedI' }, {
    nested = true,
    callback = function(args)
        local bufnr = args.buf
        RenderTodoHighlights(bufnr)
    end,
})

----------------------------------------------------------------------------
-- NOTE: Jump to a file location from a "path:line:column" in terminal buffers
----------------------------------------------------------------------------
function Jump_to_file_location(win, path, lnum, col, focus)
    if win then
        vim.api.nvim_win_call(win, function()
            vim.cmd('edit ' .. path)
            vim.api.nvim_win_set_cursor(0, { tonumber(lnum), tonumber(col) - 1 })
        end)
        if focus then
            vim.api.nvim_set_current_win(win)
        end
    else
        vim.cmd('edit ' .. path)
        vim.api.nvim_win_set_cursor(0, { tonumber(lnum), tonumber(col) - 1 })
    end
end

function Jump_to_file_location_at_cursor(focus)
    local line = vim.api.nvim_get_current_line()
    local path, lnum, col = line:match('([^:]+):(%d+):(%d+):')
    if not (path and lnum) then
        vim.cmd('normal! gd')
        return
    end

    local main_win = nil
    for _, win in ipairs(vim.api.nvim_list_wins()) do
        local buf = vim.api.nvim_win_get_buf(win)
        if vim.api.nvim_buf_get_option(buf, 'buftype') ~= 'terminal' then
            main_win = win
            break
        end
    end

    Jump_to_file_location(main_win, path, lnum, col, focus)
end

vim.api.nvim_create_autocmd('TermOpen', {
    callback = function(args)
        vim.api.nvim_buf_set_keymap(
            args.buf,
            'n',
            'gd',
            '<cmd>lua Jump_to_file_location_at_cursor(true)<CR>',
            { noremap = true, silent = true }
        )
        vim.api.nvim_buf_set_keymap(
            args.buf,
            'n',
            'go',
            '<cmd>lua Jump_to_file_location_at_cursor(false)<CR>',
            { noremap = true, silent = true }
        )
    end,
})

----------------------------------------------------------------------------
-- NOTE: Compile a project by opening a terminal, inspired by Casey's workflow
----------------------------------------------------------------------------
function RunCommand(command)
    local x = 2
    local y = 1
    local filter = function(win)
        return vim.api.nvim_win_is_valid(win) and vim.api.nvim_win_get_config(win).relative == ''
    end

    local current = vim.api.nvim_get_current_win()
    if not filter(current) then
        return
    end
    local pos = vim.api.nvim_win_get_position(current)
    local is_build = nil
    -- if pos[x] == 0 then
    --     is_build = function(pos)
    --         return pos ~= 0
    --     end
    -- else
    --     is_build = function(pos)
    --         return pos == 0
    --     end
    -- end

    -- NOTE: for new monitor setup, temporary
    if pos[x] < 160 then
        is_build = function(pos_x)
            return pos_x > 160
        end
    else
        is_build = function(pos_x)
            return pos_x == 0
        end
    end
    -- vim.notify('Current window: ' .. vim.inspect(current) .. ' ' .. vim.inspect(pos), vim.log.levels.INFO)

    local windows = vim.tbl_filter(filter, vim.api.nvim_list_wins())
    -- vim.notify('Valid windows: ' .. vim.inspect(windows), vim.log.levels.INFO)

    local old_build_win = -1
    local new_build_win = -1
    for _, win in ipairs(windows) do
        local pos = vim.api.nvim_win_get_position(win)
        -- vim.notify('Window id: ' .. vim.inspect(win) .. ' Window pos: ' .. vim.inspect(pos), vim.log.levels.INFO)
        -- vim.notify(
        --     'is build: ' .. vim.inspect(is_build(pos[1])) .. ' y == 0: ' .. vim.inspect(pos[2] == 0),
        --     vim.log.levels.INFO
        -- )
        if vim.api.nvim_win_get_buf(win) == BuildTerminalBuf then
            old_build_win = win
        end
        if is_build(pos[x]) and pos[y] == 0 then
            new_build_win = win
            -- vim.notify('SET BUILD_WIN: ' .. vim.inspect(build_win), vim.log.levels.INFO)
        end
    end
    if old_build_win == new_build_win then
        old_build_win = -1
    end

    if new_build_win == -1 then
        vim.cmd('botright vsplit')
        -- vim.cmd('vertical resize ' .. math.floor(vim.o.columns * 0.395))
        -- vim.cmd('vertical resize ' .. math.floor(vim.o.columns * 0.46))
        new_build_win = vim.api.nvim_get_current_win()
        -- vim.notify('SET BUILD_WIN: ' .. vim.inspect(build_win), vim.log.levels.INFO)
    end

    if not filter(new_build_win) then
        return
    end
    -- local build_pos = vim.api.nvim_win_get_position(build_win)
    -- vim.notify(
    --     'Build ID: ' .. vim.inspect(build_win) .. ' Build pos: ' .. vim.inspect(build_pos),
    --     vim.log.levels.INFO
    -- )

    vim.api.nvim_set_current_win(new_build_win)

    local build_buf = vim.api.nvim_win_get_buf(new_build_win)
    if vim.api.nvim_win_get_buf(current) == BuildTerminalBuf then
        -- NOTE(kyren): switch buffers
        vim.api.nvim_win_set_buf(current, build_buf)
    end

    local old_terminal = BuildTerminalBuf
    BuildTerminalBuf = vim.api.nvim_create_buf(false, true)
    vim.bo[BuildTerminalBuf].filetype = 'build_terminal'
    vim.api.nvim_buf_call(BuildTerminalBuf, function()
        -- vim.fn.termopen(command, vim.empty_dict())
        -- local job_id = vim.fn.termopen(vim.o.shell)
        local job_id = vim.fn.jobstart(vim.o.shell, { term = true })
        if job_id > 0 then
            vim.fn.chansend(job_id, command)
        end
    end)

    local previous_buffer = vim.api.nvim_win_get_buf(new_build_win)
    vim.api.nvim_win_set_buf(new_build_win, BuildTerminalBuf)
    if old_build_win ~= -1 then
        vim.api.nvim_win_set_buf(old_build_win, previous_buffer)
    end

    if old_terminal then
        vim.api.nvim_buf_delete(old_terminal, { force = true })
    end

    vim.api.nvim_set_current_win(current)
end

vim.api.nvim_create_user_command('CompileClose', function()
    if BuildTerminalBuf ~= nil and vim.api.nvim_buf_is_valid(BuildTerminalBuf) then
        vim.api.nvim_buf_delete(BuildTerminalBuf, { force = true })
    end
end, {})

vim.keymap.set({ 'i', 'n', 'v' }, '<C-q>', '<cmd>CompileClose<cr><cmd>wqa<cr>')

----------------------------------------------------------------------------
-- NOTE: Jump to next error based on compile()
----------------------------------------------------------------------------

function JumpToDiagnostic(direction, is_error, is_warning)
    vim.defer_fn(function()
        vim.cmd('w')
    end, 10)

    vim.defer_fn(function()
        -- vim.notify("Running JumpToError after delay", "warn")

        if BuildTerminalBuf == nil or not vim.api.nvim_buf_is_valid(BuildTerminalBuf) then
            -- vim.notify("BuildTerminalBuf not ready", "warn")
            return
        end

        local warning_parsers = {
            function(line)
                -- Clang: ABS:LINE:COL: warning: ...
                local file, lnum = line:match('^(.-):(%d+):%d+:%s+warning:')
                if file and lnum then
                    return { file = file, line = tonumber(lnum) }
                end
            end,
        }

        local error_parsers = {
            function(line)
                -- Clang: ABS:LINE:COL: error: ...
                local file, lnum = line:match('^(.-):(%d+):%d+:%s+error:')
                if file and lnum then
                    return { file = file, line = tonumber(lnum) }
                end
            end,

            function(line)
                -- MSVC: PATH(LINE): error ...
                -- Z:\home\kyren\projects\krypton\src\krypton\libkrypton.c(22): error C2220: ...
                -- Z:\home\kyren\projects\krypton\src\krypton\libkrypton.c(22): warning C4018: ...
                local file, lnum, kind = line:match('^(.-)%((%d+)%)%:%s*(%a+)')
                if file and lnum and kind then
                    kind = kind:lower()

                    -- Convert Z:\home\... to /home/...
                    -- Only if it matches the expected WSL mount pattern
                    local wsl_path = file:gsub('^Z:\\', '/'):gsub('\\', '/')

                    return {
                        file = wsl_path,
                        line = tonumber(lnum),
                        kind = kind,
                    }
                end
                return nil
            end,
        }

        local lines = vim.api.nvim_buf_get_lines(BuildTerminalBuf, 0, -1, false)
        -- vim.notify('Build buffer line count: ' .. #lines, 'warn')

        local diagnostics = {}
        for _, line in ipairs(lines) do
            if is_warning then
                for _, f in ipairs(warning_parsers) do
                    local warning = f(line)
                    if warning then
                        table.insert(diagnostics, warning)
                        break
                    end
                end
            end

            if is_error then
                for _, f in ipairs(error_parsers) do
                    local error = f(line)
                    if error then
                        table.insert(diagnostics, error)
                        break
                    end
                end
            end
        end

        -- vim.notify('Diagnostics count: ' .. #diagnostics, 'warn')
        if #diagnostics == 0 then
            return
        end

        local win = 0
        local current_file = vim.api.nvim_buf_get_name(win)
        local filtered = {}
        for _, e in ipairs(diagnostics) do
            if e.file == current_file then
                table.insert(filtered, e)
            end
        end

        -- vim.notify("Filtered errors: " .. #filtered, "warn")
        if #filtered == 0 then
            return
        end

        table.sort(filtered, function(a, b)
            return a.line < b.line
        end)

        local cursor_line = vim.api.nvim_win_get_cursor(win)[1]

        if direction == 1 then
            for _, e in ipairs(filtered) do
                if e.line > cursor_line then
                    vim.api.nvim_win_set_cursor(win, { e.line, 0 })
                    return
                end
            end
            vim.api.nvim_win_set_cursor(win, { filtered[1].line, 0 })
        else
            for i = #filtered, 1, -1 do
                if filtered[i].line < cursor_line then
                    vim.api.nvim_win_set_cursor(win, { filtered[i].line, 0 })
                    return
                end
            end
            vim.api.nvim_win_set_cursor(win, { filtered[#filtered].line, 0 })
        end
    end, 0) -- in milliseconds

    vim.defer_fn(function()
        local build_task = vim.g.workspace_tasks['build']
        if build_task and build_task.cmd then
            RunCommand(build_task.cmd)
        else
            vim.notify('vim.g.workspace_tasks["build"].cmd missing', 'warn')
        end
    end, 100) -- in milliseconds
end

function JumpToNextError()
    return JumpToDiagnostic(1, true, false)
end

function JumpToPrevError()
    return JumpToDiagnostic(-1, true, false)
end

function JumpToNextWarning()
    return JumpToDiagnostic(1, false, true)
end

function JumpToPrevWarning()
    return JumpToDiagnostic(-1, false, true)
end

----------------------------------------------------------------------------
-- NOTE: Run arbitrary code (if trusted) when opening a project
----------------------------------------------------------------------------

local init_trusted_paths = {
    '/home/kyren/projects/krypton',
    '/home/kyren/projects/krypton/src/langstats',
}

function Run_nvim_lua()
    -- NOTE(kyren): I use cwd instead of partial path for better security
    local cwd = vim.fn.getcwd()

    if vim.tbl_contains(init_trusted_paths, cwd) then
        local init_path = cwd .. '/.nvim.lua'
        if vim.fn.filereadable(init_path) == 1 then
            vim.cmd('source ' .. init_path)
        end
    end
end

vim.api.nvim_create_autocmd({ 'VimEnter', 'BufWritePost' }, {
    callback = function()
        vim.schedule(function()
            vim.wait(100)
            Run_nvim_lua()
        end)
    end,
})

----------------------------------------------------------------------------
-- NOTE: utility to create a new "k memory"
----------------------------------------------------------------------------
local function create_and_or_open_memory()
    vim.ui.input({ prompt = 'Enter title: ' }, function(title)
        if not title or title == '' then
            return
        end

        local sanitized_title = title:gsub('%s+', '-')
        local cmd = { 'k', 'memory', sanitized_title, '--output-path', '--edit=false', '--no-rebuild-self' }
        local output_lines = {}
        vim.notify('Memory command: ' .. vim.inspect(cmd))

        vim.fn.jobstart(cmd, {
            stdout_buffered = true,
            stderr_buffered = true,

            on_stdout = function(_, data)
                if data then
                    for _, line in ipairs(data) do
                        if line ~= '' then
                            table.insert(output_lines, line)
                        end
                    end
                end
            end,

            on_stderr = function(_, data)
                if data then
                    for _, line in ipairs(data) do
                        if line ~= '' then
                            table.insert(output_lines, line)
                        end
                    end
                end
            end,

            on_exit = function(_, exit_code)
                if exit_code == 0 then
                    local target_file = output_lines[#output_lines]
                    vim.notify(vim.inspect(output_lines))

                    if target_file and target_file ~= '' then
                        -- Trim any trailing carriage returns or spaces
                        target_file = target_file:gsub('%s+$', '')

                        vim.schedule(function()
                            vim.cmd('edit ' .. vim.fn.fnameescape(target_file))
                        end)
                    else
                        vim.notify('Error: CLI succeeded but stdout was empty!', vim.log.levels.ERROR)
                    end
                else
                    vim.api.nvim_err_writeln("Error: 'k memory' failed with exit code " .. exit_code)
                end
            end,
        })
    end)
end

-- 5. Create the Neovim user command (:MyCliCreate)
vim.api.nvim_create_user_command('KMemory', create_and_or_open_memory, {})

----------------------------------------------------------------------------
-- NOTE: Generate java template
----------------------------------------------------------------------------

local function generate_java_boilerplate()
    local buf_name = vim.api.nvim_buf_get_name(0)

    if
        buf_name == ''
        or vim.bo.filetype ~= 'java'
        or vim.api.nvim_buf_line_count(0) > 1
        or vim.api.nvim_buf_get_lines(0, 0, 1, false)[1] ~= ''
    then
        return
    end

    local filepath = buf_name
    -- Find where the standard Java source roots begin
    local source_root = filepath:match('.*/src/main/java/(.*)') or filepath:match('.*/src/test/java/(.*)')
    if not source_root then
        return -- Not inside a standard Java project directory structure
    end

    -- Separate the file name from the package directory paths
    local package_path = source_root:match('(.*)/.*%.java$')
    local class_name = source_root:match('.*/(.*)%.java$') or source_root:match('(.*)%.java$')
    if not class_name then
        return
    end

    local lines = {}

    --  Generate the package declaration if it's not in the default root package
    if package_path then
        local package_name = package_path:gsub('/', '.')
        table.insert(lines, 'package ' .. package_name .. ';')
        table.insert(lines, '')
    end

    -- Generate the standard class declaration boilerplate
    table.insert(lines, 'public final class ' .. class_name .. ' {')
    table.insert(lines, '    ')
    table.insert(lines, '}')

    vim.api.nvim_buf_set_lines(0, 0, -1, false, lines)
    local line = 4
    local column = 4
    pcall(vim.api.nvim_win_set_cursor, 0, { line, column }) -- put cursor inside the class
end

vim.api.nvim_create_autocmd({ 'BufNewFile', 'BufEnter' }, {
    pattern = '*.java',
    callback = generate_java_boilerplate,
})

----------------------------------------------------------------------------
-- NOTE: Show NullAway Jspecify diagnostics inside nvim
----------------------------------------------------------------------------

local nullaway_ns = vim.api.nvim_create_namespace('nullaway_diagnostics')

-- TOGGLE: When true, isolates "[NullAway]" errors and strips the "[NullAway]"
--         tag prefix from the inline diagnostic text.
local strip_nullaway_prefix = true

local debounce_timer = nil
local active_job_id = nil
local DEBOUNCE_MS = 800

vim.api.nvim_create_autocmd({ 'TextChanged', 'TextChangedI', 'BufEnter', 'BufWritePost' }, {
    pattern = '*.java',
    callback = function()
        -- vim.notify("[NullAway] Event triggered, managing build window...", vim.log.levels.DEBUG)

        local bufnr = vim.api.nvim_get_current_buf()
        local current_file = vim.api.nvim_buf_get_name(bufnr)

        local gradlew_search = vim.fs.find('gradlew', { upward = true, path = vim.fs.dirname(current_file) })
        if #gradlew_search == 0 then
            return
        end

        local gradlew_path = gradlew_search[1]
        local project_root = vim.fs.dirname(gradlew_path)

        if debounce_timer then
            debounce_timer:stop()
            debounce_timer:close()
            debounce_timer = nil
        end

        debounce_timer = vim.uv.new_timer()
        debounce_timer:start(
            DEBOUNCE_MS,
            0,
            vim.schedule_wrap(function()
                if active_job_id then
                    vim.fn.jobstop(active_job_id)
                    active_job_id = nil
                end

                local diagnostics = {}

                active_job_id = vim.fn.jobstart({ gradlew_path, 'compileJava', '--console=plain' }, {
                    cwd = project_root,
                    stdout_buffered = true,
                    stderr_buffered = true,

                    on_stderr = function(_, data)
                        if not data then
                            return
                        end

                        local function process_entry(index)
                            local line = data[index]

                            if line:match('^%s') then
                                return
                            end
                            if not line:find('error: %[NullAway%]') then
                                return
                            end

                            local file_path, lnum_str, msg = line:match('^(.-):(%d+):%s+error:%s+(.*)')
                            if not (file_path and lnum_str and msg) then
                                return
                            end
                            if vim.fs.normalize(file_path) ~= vim.fs.normalize(current_file) then
                                return
                            end

                            -- Clean up the diagnostic string based on the toggle configuration
                            if strip_nullaway_prefix then
                                msg = msg:gsub('^%[NullAway%]%s*', '')
                            end

                            local col = 0
                            local end_col = nil

                            for offset = 1, 3 do
                                local lookahead = data[index + offset]
                                if lookahead then
                                    local indent = lookahead:match('^(%s*)%^')
                                    if indent then
                                        col = #indent
                                        local param_token = msg:match("parameter '([^']+)'")
                                        if param_token then
                                            end_col = col + #param_token
                                        end
                                        break
                                    end
                                end
                            end

                            table.insert(diagnostics, {
                                lnum = tonumber(lnum_str) - 1,
                                col = col,
                                end_col = end_col,
                                severity = vim.diagnostic.severity.ERROR,
                                message = msg,
                                source = 'NullAway',
                            })
                        end

                        for i = 1, #data do
                            process_entry(i)
                        end
                    end,

                    on_exit = function(job_id, exit_code)
                        if job_id ~= active_job_id then
                            return
                        end
                        active_job_id = nil

                        vim.schedule(function()
                            vim.diagnostic.reset(nullaway_ns, bufnr)
                            if #diagnostics > 0 then
                                vim.diagnostic.set(nullaway_ns, bufnr, diagnostics)
                            -- vim.notify("[NullAway] Live check placed " .. #diagnostics .. " error(s).", vim.log.levels.INFO)
                            else
                                -- vim.notify("[NullAway] Live check clean.", vim.log.levels.INFO)
                            end
                        end)
                    end,
                })
            end)
        )
    end,
})

----------------------------------------------------------------------------
-- NOTE: LSP hover
----------------------------------------------------------------------------
require('lsphover')

----------------------------------------------------------------------------
-- NOTE: Global and per-project Scratch buffers
----------------------------------------------------------------------------

local global_scratch_path = '$HOME/personal/grimoire/scratch.md'
local workspace_scratch_path = '.scratch.md'

local function open_scratch_buffer(is_global)
    local scratch_file = is_global and global_scratch_path or vim.fn.fnamemodify(workspace_scratch_path, ':p')
    scratch_file = vim.fn.expand(scratch_file)

    -- Open the file in a new tab
    vim.cmd('tabedit ' .. vim.fn.fnameescape(scratch_file))
    vim.bo.filetype = 'markdown' -- syntax highlighting

    -- Map `q` in normal mode to save and close the tab (buffer-local)
    vim.keymap.set('n', 'q', function()
        vim.cmd('silent! write')
        vim.cmd('tabclose')
    end, { buffer = true, silent = true })

    -- Enable auto-saving on text change or leaving the buffer
    vim.api.nvim_create_autocmd({ 'TextChanged', 'TextChangedI', 'BufLeave' }, {
        buffer = 0,
        callback = function()
            vim.cmd('silent! write')
        end,
    })
end

local function open_scratch_buffer_map(is_global)
    return function()
        open_scratch_buffer(is_global)
    end
end

vim.keymap.set('n', '<leader>go', open_scratch_buffer_map(true))
vim.keymap.set('n', '<leader>so', open_scratch_buffer_map(false))

----------------------------------------------------------------------------
-- NOTE: Insert commit msg into commit buffer in neogit
----------------------------------------------------------------------------

local group = vim.api.nvim_create_augroup('NeogitCommitMessage', { clear = true })

vim.api.nvim_create_autocmd('FileType', {
    group = group,
    pattern = 'gitcommit',
    callback = function(args)
        local buf = args.buf

        local line = vim.api.nvim_buf_get_lines(buf, 0, 1, false)[1]
        if line ~= '' then
            return
        end

        local path = vim.fn.fnamemodify(workspace_scratch_path, ':p')
        local ok, lines = pcall(vim.fn.readfile, path)
        if not ok then
            return
        end
        local scratch_bufnr = vim.fn.bufnr(path)
        if scratch_bufnr ~= -1 and vim.api.nvim_buf_is_loaded(scratch_bufnr) then
            lines = vim.api.nvim_buf_get_lines(scratch_bufnr, 0, -1, false)
        end

        -- vim.notify('path:' .. path)
        -- vim.notify('file:' .. vim.inspect(vim.fn.readfile(path)))
        -- vim.notify('lines:' .. vim.inspect(lines))

        local commit_lines = {}
        for _, line in ipairs(lines) do
            if line:find('---', 1, true) then
                break
            end
            table.insert(commit_lines, line)
        end

        -- vim.notify('commit_lines:' .. vim.inspect(commit_lines))

        if #commit_lines > 0 then
            vim.api.nvim_buf_set_lines(buf, 0, 1, false, commit_lines)
            -- Place cursor at end of first line
            vim.api.nvim_win_set_cursor(0, { 1, #commit_lines[#commit_lines] })
        end
    end,
})

----------------------------------------------------------------------------
-- NOTE:
----------------------------------------------------------------------------

vim.api.nvim_create_autocmd('FileType', {
    pattern = 'markdown',
    callback = function(args)
        local cwd = vim.fn.getcwd()
        local project = vim.fn.expand('$HOME/projects/WorldBuilding/')

        -- if cwd == project or cwd:sub(1, #project + 1) == project .. '/' then
        vim.opt_local.concealcursor = 'n'
        vim.opt_local.conceallevel = 3
        -- end
    end,
})

vim.keymap.set('n', '<leader>me', function()
    if vim.wo.conceallevel == 0 then
        vim.wo.concealcursor = 'n'
        vim.opt.conceallevel = 2
    else
        vim.opt.concealcursor = ''
        vim.opt.conceallevel = 0
    end
end, { desc = 'Toggle Markdown Edit Mode (conceal)' })

----------------------------------------------------------------------------
-- NOTE:
----------------------------------------------------------------------------