5 const Separator = "---------------------------------- >8 ----------------------------------"
7 export def Do(firstline: number, lastline: number, gitDir: string)
8 if bufwinnr("CodeCommCommenting") != -1
9 echohl ErrorMsg | echomsg "Close already existing code commenting window first" | echohl None
12 if bufexists("CodeCommCommenting") != 0
13 bdelete! CodeCommCommenting
17 # Determine file's path inside repository
18 var path = expand("%:p")
19 path = substitute(path, gitDir[: -5], "", "")
20 path = substitute(path, "^.*\.git//", "", "")
23 var header: list<string>
24 if match(path, "/") ==# SHA1Len
25 header = add(header, path[: 8])
26 header = add(header, "|")
27 header = add(header, path[SHA1Len + 1 :])
28 elseif match(path, "/") ==# SHA256Len
29 header = add(header, path[: 8])
30 header = add(header, "|")
31 header = add(header, path[SHA256Len + 1 :])
33 header = add(header, path)
35 var ready = [printf("-----#%2d [ %54S ]-----", g:codecomm_count, join(header, " "))]
37 # Collect enumerated selected code block's lines
40 for n in range(firstline, lastline)
41 fmted = printf("%4d", n)
43 if len(line) > 0 | fmted = fmted .. " " .. line | endif
44 ready = add(ready, fmted)
46 ready = add(ready, Separator)
48 # Place commented signs
49 sign define commented text=C texthl=Search
51 for n in range(firstline, lastline)
52 cmd = ":sign place " .. n .. " line=" .. n
53 cmd ..= " name=commented buffer=" .. bufnr("%")
57 # Spawn a new small code commenting window nonbinded to file
58 new CodeCommCommenting
59 setlocal noreadonly noswapfile buftype=acwrite filetype=codecomm
62 # Separate codecomm_file consolidating function, called when buffer is saved
63 # autocmd! BufWriteCmd CodeCommCommenting
64 autocmd! BufWriteCmd CodeCommCommenting {
65 var ccprev = [" vim: filetype=codecomm", ""]
66 if filereadable(g:codecomm_file) | ccprev = readfile(g:codecomm_file) | endif
67 var ready = ccprev + getline(0, "$") + [""]
68 writefile(ready, g:codecomm_file)
70 echohl MoreMsg | echomsg "Commented:" len(ready) "lines" | echohl None
77 writefile([], g:codecomm_file)
79 execute "sign unplace * buffer=" .. bufnr("%")
80 echohl WarningMsg | echomsg "Comments are wiped" | echohl None