]> Sergey Matveev's repositories - codecomm.git/blob - autoload/codecomm.vim
vim9script
[codecomm.git] / autoload / codecomm.vim
1 vim9script
2
3 const SHA1Len = 40
4 const Separator = "---------------------------------- >8 ----------------------------------"
5
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
9         return
10     endif
11     if bufexists("CodeCommCommenting") != 0
12         bdelete! CodeCommCommenting
13     endif
14     g:codecomm_count += 1
15
16     # Determine file's path inside repository
17     var path = expand("%:p")
18     path = substitute(path, gitDir[: -5], "", "")
19     path = substitute(path, "^.*\.git//", "", "")
20
21     # Header generation
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 :])
27     else
28         header = add(header, path)
29     endif
30     var ready = [printf("-----#%2d [ %54S ]-----", g:codecomm_count, join(header, " "))]
31
32     # Collect enumerated selected code block's lines
33     var fmted: string
34     var line: string
35     for n in range(firstline, lastline)
36         fmted = printf("%4d", n)
37         line = getline(n)
38         if len(line) > 0 | fmted = fmted .. " " .. line | endif
39         ready = add(ready, fmted)
40     endfor
41     ready = add(ready, Separator)
42
43     # Place commented signs
44     sign define commented text=C texthl=Search
45     var cmd: string
46     for n in range(firstline, lastline)
47         cmd = ":sign place " .. n .. " line=" .. n
48         cmd ..= " name=commented buffer=" .. bufnr("%")
49         execute cmd
50     endfor
51
52     # Spawn a new small code commenting window nonbinded to file
53     new CodeCommCommenting
54     setlocal noreadonly noswapfile buftype=acwrite filetype=codecomm
55     append(0, ready)
56
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)
64         setlocal nomodified
65         echohl MoreMsg | echomsg "Commented:" len(ready) "lines" | echohl None
66     }
67     normal zR
68     startinsert
69 enddef
70
71 export def Clear()
72     writefile([], g:codecomm_file)
73     g:codecomm_count = 0
74     execute "sign unplace * buffer=" .. bufnr("%")
75     echohl WarningMsg | echomsg "Comments are wiped" | echohl None
76 enddef