From 14ae8e394e18e165e8733602f4f01ff2b51acc9d Mon Sep 17 00:00:00 2001 From: Neko Box Coder Date: Thu, 15 May 2025 01:11:06 +0100 Subject: [PATCH] Minor fix for comment plugin and migrating to use "comment.type" option Fixing comment plugin not using user settings when overriding default setting, Migrating comment plugin to use "comment.type" option instead --- runtime/plugins/comment/comment.lua | 31 ++++++++++++++++--------- runtime/plugins/comment/help/comment.md | 11 ++++++--- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/runtime/plugins/comment/comment.lua b/runtime/plugins/comment/comment.lua index f86da945..11e135ae 100644 --- a/runtime/plugins/comment/comment.lua +++ b/runtime/plugins/comment/comment.lua @@ -3,6 +3,7 @@ VERSION = "1.0.0" local util = import("micro/util") local config = import("micro/config") local buffer = import("micro/buffer") +local micro = import("micro") local ft = {} @@ -61,17 +62,21 @@ ft["zig"] = "// %s" ft["zscript"] = "// %s" ft["zsh"] = "# %s" -local last_ft - function updateCommentType(buf) - if buf.Settings["commenttype"] == nil or (last_ft ~= buf.Settings["filetype"] and last_ft ~= nil) then - if ft[buf.Settings["filetype"]] ~= nil then - buf:SetOptionNative("commenttype", ft[buf.Settings["filetype"]]) + -- NOTE: Using DoSetOptionNative to avoid LocalSettings[option] = true + -- so that "comment.type" can be reset by a "filetype" change to default. + if (buf.Settings["comment.type"] == "") then + -- NOTE: This won't get triggered if a filetype is change via `setlocal filetype` + -- since it is not registered with `RegisterGlobalOption()`` + if buf.Settings["commenttype"] ~= nil then + buf:DoSetOptionNative("comment.type", buf.Settings["commenttype"]) else - buf:SetOptionNative("commenttype", "# %s") + if (ft[buf.Settings["filetype"]] ~= nil) then + buf:DoSetOptionNative("comment.type", ft[buf.Settings["filetype"]]) + else + buf:DoSetOptionNative("comment.type", "# %s") + end end - - last_ft = buf.Settings["filetype"] end end @@ -88,7 +93,7 @@ function commentLine(bp, lineN, indentLen) updateCommentType(bp.Buf) local line = bp.Buf:Line(lineN) - local commentType = bp.Buf.Settings["commenttype"] + local commentType = bp.Buf.Settings["comment.type"] local sel = -bp.Cursor.CurSelection local curpos = -bp.Cursor.Loc local index = string.find(commentType, "%%s") - 1 @@ -114,7 +119,7 @@ function uncommentLine(bp, lineN, commentRegex) updateCommentType(bp.Buf) local line = bp.Buf:Line(lineN) - local commentType = bp.Buf.Settings["commenttype"] + local commentType = bp.Buf.Settings["comment.type"] local sel = -bp.Cursor.CurSelection local curpos = -bp.Cursor.Loc local index = string.find(commentType, "%%s") - 1 @@ -178,7 +183,7 @@ end function comment(bp, args) updateCommentType(bp.Buf) - local commentType = bp.Buf.Settings["commenttype"] + local commentType = bp.Buf.Settings["comment.type"] local commentRegex = "^%s*" .. commentType:gsub("%%","%%%%"):gsub("%$","%$"):gsub("%)","%)"):gsub("%(","%("):gsub("%?","%?"):gsub("%*", "%*"):gsub("%-", "%-"):gsub("%.", "%."):gsub("%+", "%+"):gsub("%]", "%]"):gsub("%[", "%["):gsub("%%%%s", "(.*)") if bp.Cursor:HasSelection() then @@ -204,6 +209,10 @@ function string.starts(String,Start) return string.sub(String,1,string.len(Start))==Start end +function preinit() + config.RegisterCommonOption("comment", "type", "") +end + function init() config.MakeCommand("comment", comment, config.NoComplete) config.TryBindKey("Alt-/", "lua:comment.comment", false) diff --git a/runtime/plugins/comment/help/comment.md b/runtime/plugins/comment/help/comment.md index 74054cd1..88550458 100644 --- a/runtime/plugins/comment/help/comment.md +++ b/runtime/plugins/comment/help/comment.md @@ -80,10 +80,10 @@ but it is only available for certain filetypes: * zsh: `# %s` If your filetype is not available here, you can simply modify -the `commenttype` option: +the `comment.type` option: ``` -set commenttype "/* %s */" +set comment.type "/* %s */" ``` Or in your `settings.json`: @@ -91,7 +91,12 @@ Or in your `settings.json`: ```json { "*.c": { - "commenttype": "/* %s */" + "comment.type": "/* %s */" } } ``` + +`commenttype` (without the dot) is the legacy option that is +superseded by `comment.type`. `commenttype` is still supported +but will get deprecated in the future. +**Use `comment.type` instead.**