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