]> Sergey Matveev's repositories - codecomm.git/blobdiff - autoload/codecomm.vim
vim9script
[codecomm.git] / autoload / 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