Browse Source

feat(neovim): add `pandoctrinated` formatter

Joe 5 tháng trước cách đây
mục cha
commit
f95c6b9e30

+ 29 - 0
.config/nvim/custom/pandoctrinated/lua/pandoctrinated/init.lua

@@ -0,0 +1,29 @@
+return {
+    pandoc_format_docstring = function()
+        local api, fn, bo = vim.api, vim.fn, vim.bo
+
+        local start_lnum = fn.search('"""', "bnc")
+        local end_lnum = fn.search('"""', "nc")
+        if start_lnum == 0 or end_lnum == 0 then
+            return
+        end
+
+        local content = table.concat(
+                api.nvim_buf_get_lines(0, start_lnum - 1, end_lnum, false), "\n")
+            :gsub('^%s*"""', ""):gsub('"""%s*$', "")
+
+        local cols = (bo.textwidth > 0) and bo.textwidth or 80
+        local cmd = string.format(
+            "pandoc --from=commonmark --to=commonmark --wrap=auto --columns=%d",
+            cols)
+        local result = fn.system(cmd, content)
+
+        if vim.v.shell_error ~= 0 then
+            return print("Pandoc Error: " .. result)
+        end
+
+        local new_lines = vim.split('"""' .. fn.trim(result) .. '"""', "\n",
+            { trimempty = false })
+        api.nvim_buf_set_lines(0, start_lnum - 1, end_lnum, false, new_lines)
+    end
+}

+ 13 - 0
.config/nvim/lua/plugins/command-palette.lua

@@ -92,5 +92,18 @@ return {
             "Save Formatter", "Configure automatic formatters", formatter.picker }
             "Save Formatter", "Configure automatic formatters", formatter.picker }
         })
         })
     end
     end
+}, {
+    dir = vim.fn.stdpath("config") .. "/custom/pandoctrinated",
+    name = "pandoctrinated",
+    dependencies = { "command-palette" },
+    enabled = true,
+    config = function(_, _)
+        local pandoctrinated = require("pandoctrinated")
+        vim.keymap.set("n", "Sfd", pandoctrinated.pandoc_format_docstring)
+        require("command-palette").add({ {
+            "Pandoctrinated", "Format \"\"\"docstring\"\"\" with `pandoc`",
+            pandoctrinated.pandoc_format_docstring }
+        })
+    end
 }
 }
 }
 }