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