]> Sergey Matveev's repositories - codecomm.git/commitdiff
vim9script
authorSergey Matveev <stargrave@stargrave.org>
Fri, 1 Jul 2022 15:57:39 +0000 (18:57 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Fri, 1 Jul 2022 15:57:39 +0000 (18:57 +0300)
autoload/codecomm.vim [new file with mode: 0644]
plugin/codecomm.vim

diff --git a/autoload/codecomm.vim b/autoload/codecomm.vim
new file mode 100644 (file)
index 0000000..8d39de7
--- /dev/null
@@ -0,0 +1,76 @@
+vim9script
+
+const SHA1Len = 40
+const Separator = "---------------------------------- >8 ----------------------------------"
+
+export def Do(firstline: number, lastline: number, gitDir: string)
+    if bufwinnr("CodeCommCommenting") != -1
+        echohl ErrorMsg | echomsg "Close already existing code commenting window first" | echohl None
+        return
+    endif
+    if bufexists("CodeCommCommenting") != 0
+        bdelete! CodeCommCommenting
+    endif
+    g:codecomm_count += 1
+
+    # Determine file's path inside repository
+    var path = expand("%:p")
+    path = substitute(path, gitDir[: -5], "", "")
+    path = substitute(path, "^.*\.git//", "", "")
+
+    # Header generation
+    var header: list<string>
+    if match(path, "/") ==# SHA1Len
+        header = add(header, path[: 8])
+        header = add(header, "|")
+        header = add(header, path[SHA1Len + 1 :])
+    else
+        header = add(header, path)
+    endif
+    var ready = [printf("-----#%2d [ %54S ]-----", g:codecomm_count, join(header, " "))]
+
+    # Collect enumerated selected code block's lines
+    var fmted: string
+    var line: string
+    for n in range(firstline, lastline)
+        fmted = printf("%4d", n)
+        line = getline(n)
+        if len(line) > 0 | fmted = fmted .. " " .. line | endif
+        ready = add(ready, fmted)
+    endfor
+    ready = add(ready, Separator)
+
+    # Place commented signs
+    sign define commented text=C texthl=Search
+    var cmd: string
+    for n in range(firstline, lastline)
+        cmd = ":sign place " .. n .. " line=" .. n
+        cmd ..= " name=commented buffer=" .. bufnr("%")
+        execute cmd
+    endfor
+
+    # Spawn a new small code commenting window nonbinded to file
+    new CodeCommCommenting
+    setlocal noreadonly noswapfile buftype=acwrite filetype=codecomm
+    append(0, ready)
+
+    # Separate codecomm_file consolidating function, called when buffer is saved
+    # autocmd! BufWriteCmd CodeCommCommenting
+    autocmd! BufWriteCmd CodeCommCommenting {
+        var ccprev = [" vim: filetype=codecomm", ""]
+        if filereadable(g:codecomm_file) | ccprev = readfile(g:codecomm_file) | endif
+        var ready = ccprev + getline(0, "$") + [""]
+        writefile(ready, g:codecomm_file)
+        setlocal nomodified
+        echohl MoreMsg | echomsg "Commented:" len(ready) "lines" | echohl None
+    }
+    normal zR
+    startinsert
+enddef
+
+export def Clear()
+    writefile([], g:codecomm_file)
+    g:codecomm_count = 0
+    execute "sign unplace * buffer=" .. bufnr("%")
+    echohl WarningMsg | echomsg "Comments are wiped" | echohl None
+enddef
index 8a38d898978f0fce56cd57ad0f1f1dcd93cc128b..1b6d1e1ed0eb849ddb057c2f65bf67f14167964a 100644 (file)
@@ -1,87 +1,23 @@
-" Code comments preparation helper
-" Maintainer: Sergey Matveev <stargrave@stargrave.org>
-" License: GNU General Public License version 3 of the License or later
+vim9script
 
-if exists("g:loaded_codecomm") | finish | endif
-let g:loaded_codecomm = 1
+# Code comments preparation helper
+# Maintainer: Sergey Matveev <stargrave@stargrave.org>
+# License: GNU General Public License version 3 of the License or later
+
+if exists("*codecomm#Do") | finish | endif
 
 if !exists("g:codecomm_file")
-    let tmpdir = getenv("TMPDIR")
-    if tmpdir == v:null | let tmpdir = "/tmp" | endif
-    let g:codecomm_file = tmpdir . "/" . "codecomm.txt"
+    g:codecomm_file = ((getenv("TMPDIR") == null) ? "/tmp" : getenv("TMPDIR")) ..
+        "/" .. "codecomm.txt"
 endif
 
-if !exists("g:codecomm_count") | let g:codecomm_count = 0 | endif
-
-function! s:CodeComm() range
-    let SHA1_LENGTH = 40
-    if bufwinnr("CodeCommCommenting") != -1
-        echohl ErrorMsg | echomsg "Close already existing code commenting window first" | echohl None
-        return
-    endif
-    if bufexists("CodeCommCommenting") != 0
-        bdelete! CodeCommCommenting
-    endif
-    let g:codecomm_count += 1
-    " Determine file's path inside repository
-    let path = expand("%:p")
-    let path = substitute(path, FugitiveExtractGitDir(getcwd())[:-5], "", "")
-    let path = substitute(path, "^.*\.git//", "", "")
-    " Header generation
-    let header = []
-    if match(path, "/") ==# SHA1_LENGTH
-        let header = add(header, path[:8])
-        let header = add(header, "|")
-        let header = add(header, path[SHA1_LENGTH+1:])
-    else
-        let header = add(header, path)
-    endif
-    let ready = [printf("-----#%2d [ %54S ]-----", g:codecomm_count, join(header, " "))]
-    " Collect enumerated selected code block's lines
-    for bufline_n in range(a:firstline, a:lastline)
-        let fmted = printf("%4d", bufline_n)
-        let line = getline(bufline_n)
-        if len(line) > 0 | let fmted = fmted . " " . line | endif
-        let ready = add(ready, fmted)
-    endfor
-    let ready = add(ready, "---------------------------------- >8 ----------------------------------")
-    " Place commented signs
-    sign define commented text=C texthl=Search
-    for linen in range(a:firstline, a:lastline)
-        let cmd = ":sign place " . linen . " line=" . linen
-        let cmd .= " name=commented buffer=" . bufnr("%")
-        execute cmd
-    endfor
-    " Spawn a new small code commenting window nonbinded to file
-    new CodeCommCommenting
-    setlocal noreadonly noswapfile buftype=acwrite filetype=codecomm
-    call append("^", ready)
-    " Separate codecomm_file consolidating function, called when buffer is saved
-    autocmd! BufWriteCmd CodeCommCommenting
-    function! s:AppendCC()
-        " Collect already written comments from file if it exists
-        let ccprev = [" vim: filetype=codecomm", ""]
-        if filereadable(g:codecomm_file)
-            let ccprev = readfile(g:codecomm_file)
-        endif
-        " Save all those consolidated data to file
-        let ready = ccprev + getline(0, "$") + [""]
-        call writefile(ready, g:codecomm_file)
-        setlocal nomodified
-        echohl MoreMsg | echomsg "Commented:" len(ready) "lines" | echohl None
-    endfunction
-    autocmd BufWriteCmd CodeCommCommenting call s:AppendCC()
-    normal zR
-    startinsert
-endfunction
+if !exists("g:codecomm_count") | g:codecomm_count = 0 | endif
 
-function! s:CodeCommClear()
-    call writefile([], g:codecomm_file)
-    let g:codecomm_count = 0
-    echohl WarningMsg | echomsg "Comments are wiped" | echohl None
+function CCRangeWrapper() range
+    call codecomm#Do(a:firstline, a:lastline, FugitiveExtractGitDir(getcwd()))
 endfunction
 
-command! CodeCommClear call <SID>CodeCommClear()
-command! CodeCommEdit :execute "edit " . g:codecomm_file
-command! -range CodeComm <line1>, <line2> call <SID>CodeComm()
-vnoremap <silent><Leader>cc :call <SID>CodeComm()<CR>
+command! CodeCommClear codecomm#Clear()
+command! CodeCommEdit :execute "edit " .. g:codecomm_file
+command! -range CodeComm <line1>, <line2> call <SID>CCRangeWrapper()
+vnoremap <silent><Leader>cc :call <SID>CCRangeWrapper()<CR>