-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvim9functions.vim
More file actions
99 lines (76 loc) · 2.99 KB
/
vim9functions.vim
File metadata and controls
99 lines (76 loc) · 2.99 KB
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
vim9script
def MoveCocWordsToCSpell()
var coc_json = expand("$MYVIMDIR/coc-settings.json")
var cspell_json = expand("$MYVIMDIR/cSpell.json")
# Check if jq is installed
if system('command -v jq') == ""
echohl ErrorMsg | echom "jq not found. Please install jq first." | echohl None
return
endif
# Extract words from coc-settings.json
var words = systemlist('jq -r ".\"cSpell.userWords\"[]" ' .. shellescape(coc_json))
if empty(words)
words = systemlist('jq -r ".\"userWords\"[]" ' .. shellescape(coc_json))
endif
if v:shell_error != 0 || empty(words)
echohl WarningMsg | echom "No words found in cSpell.userWords or JSON syntax error." | echohl None
return
endif
# Ensure cSpell.json exists
if !filereadable(cspell_json)
writefile(['{ "words": [] }'], cspell_json)
endif
# Merge words into cSpell.json
var add_words_cmd = 'jq --indent 2 --argjson newWords ' .. shellescape(json_encode(words)) ..
' ''.words += $newWords | .words |= unique'' ' ..
shellescape(cspell_json) .. ' > ' .. shellescape(cspell_json .. ".tmp") .. ' && mv ' ..
shellescape(cspell_json .. ".tmp") .. ' ' .. shellescape(cspell_json)
system(add_words_cmd)
if v:shell_error != 0
echohl ErrorMsg | echom "Failed to update cSpell.json" | echohl None
return
endif
# Remove cSpell.userWords from coc-settings.json
var remove_words_cmd = 'jq "del(.\"cSpell.userWords\")" ' ..
shellescape(coc_json) .. ' > ' .. shellescape(coc_json .. ".tmp") .. ' && mv ' ..
shellescape(coc_json .. ".tmp") .. ' ' .. shellescape(coc_json)
system(remove_words_cmd)
if v:shell_error == 0
echom "Moved words to cSpell.json and cleaned coc-settings.json!"
else
echohl ErrorMsg | echom "Error cleaning coc-settings.json" | echohl None
endif
enddef
command! MoveWords call MoveCocWordsToCSpell()
def AddWordToCSpell(file: string)
var word = expand('<cword>')
if word == ''
echo 'No word under cursor.'
return
endif
if !filereadable(file)
echo 'File not found: ' .. file
return
endif
# Read file and parse JSON
var json_text = join(readfile(file), "\n")
var data = json_decode(json_text)
# Helper: append if not present
def AddUnique(list: list<string>, item: string)
if index(list, item) < 0
add(list, item)
endif
enddef
AddUnique(data.words, word)
AddUnique(data.userWords, word)
# Encode JSON (raw)
var new_json = json_encode(data)->split("\n")
writefile(new_json, file)
# Reformat using jq if available
if executable('jq')
system('jq . ' .. shellescape(file) .. ' > ' .. shellescape(file .. '.tmp'))
system('mv ' .. shellescape(file .. '.tmp') .. ' ' .. shellescape(file))
endif
echo 'Added "' .. word .. '" to cSpell dictionary.'
enddef
command! -nargs=0 CSpellAddWord call AddWordToCSpell('/home/jan/.vim/cSpell.json')