From d40a952392468ed7c830c2ea19548cd15fe65293 Mon Sep 17 00:00:00 2001 From: Neko Box Coder Date: Thu, 22 Aug 2024 20:24:53 +0100 Subject: [PATCH 1/4] Updating comment plugin option and fixing comment option reset bug Updating comment plugin option to be comment.type Fixing unwanted comment option reset when switching buffers --- runtime/plugins/comment/comment.lua | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/runtime/plugins/comment/comment.lua b/runtime/plugins/comment/comment.lua index f86da945..50d63ee5 100644 --- a/runtime/plugins/comment/comment.lua +++ b/runtime/plugins/comment/comment.lua @@ -61,17 +61,15 @@ 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 + -- NOTE: Don't use SetOptionNative() to set "comment.type", + -- otherwise "comment.type" can't be reset by a "filetype" change. + if buf.Settings["comment.type"] == "" then if ft[buf.Settings["filetype"]] ~= nil then - buf:SetOptionNative("commenttype", ft[buf.Settings["filetype"]]) + buf.Settings["comment.type"] = ft[buf.Settings["filetype"]] else - buf:SetOptionNative("commenttype", "# %s") + buf.Settings["comment.type"] = "# %s" end - - last_ft = buf.Settings["filetype"] end end @@ -88,7 +86,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 +112,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 +176,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 +202,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) From b4920fb645e85624287cbd60c86c7ae07ec82cea Mon Sep 17 00:00:00 2001 From: Neko Box Coder Date: Thu, 22 Aug 2024 20:31:08 +0100 Subject: [PATCH 2/4] Added error message when using old comment option --- runtime/plugins/comment/comment.lua | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/runtime/plugins/comment/comment.lua b/runtime/plugins/comment/comment.lua index 50d63ee5..65cf6510 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 = {} @@ -62,6 +63,11 @@ ft["zscript"] = "// %s" ft["zsh"] = "# %s" function updateCommentType(buf) + if buf.Settings["commenttype"] ~= nil then + micro.InfoBar():Error("\"commenttype\" option has been updated to \"comment.type\"", + "instead, please update accordingly") + end + -- NOTE: Don't use SetOptionNative() to set "comment.type", -- otherwise "comment.type" can't be reset by a "filetype" change. if buf.Settings["comment.type"] == "" then From 7134d3f2724031e1054ecd510b0cef5382b77895 Mon Sep 17 00:00:00 2001 From: Neko Box Coder Date: Thu, 22 Aug 2024 21:46:42 +0100 Subject: [PATCH 3/4] Rewording error message for old comment option --- runtime/plugins/comment/comment.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/runtime/plugins/comment/comment.lua b/runtime/plugins/comment/comment.lua index 65cf6510..ead500bb 100644 --- a/runtime/plugins/comment/comment.lua +++ b/runtime/plugins/comment/comment.lua @@ -64,8 +64,8 @@ ft["zsh"] = "# %s" function updateCommentType(buf) if buf.Settings["commenttype"] ~= nil then - micro.InfoBar():Error("\"commenttype\" option has been updated to \"comment.type\"", - "instead, please update accordingly") + micro.InfoBar():Error("\"commenttype\" option has been renamed to \"comment.type\"", + ", please update your configuration") end -- NOTE: Don't use SetOptionNative() to set "comment.type", From 5dc41ce756e9df88e8143ecaf000a65e72b4d0e5 Mon Sep 17 00:00:00 2001 From: Neko Box Coder Date: Sat, 24 Aug 2024 18:10:13 +0100 Subject: [PATCH 4/4] Fixing old comment option error message being spammed --- runtime/plugins/comment/comment.lua | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/runtime/plugins/comment/comment.lua b/runtime/plugins/comment/comment.lua index ead500bb..4a016bfd 100644 --- a/runtime/plugins/comment/comment.lua +++ b/runtime/plugins/comment/comment.lua @@ -63,14 +63,14 @@ ft["zscript"] = "// %s" ft["zsh"] = "# %s" function updateCommentType(buf) - if buf.Settings["commenttype"] ~= nil then - micro.InfoBar():Error("\"commenttype\" option has been renamed to \"comment.type\"", - ", please update your configuration") - end - -- NOTE: Don't use SetOptionNative() to set "comment.type", -- otherwise "comment.type" can't be reset by a "filetype" change. if buf.Settings["comment.type"] == "" then + if buf.Settings["commenttype"] ~= nil then + micro.InfoBar():Error("\"commenttype\" option has been renamed to \"comment.type\"", + ", please update your configuration") + end + if ft[buf.Settings["filetype"]] ~= nil then buf.Settings["comment.type"] = ft[buf.Settings["filetype"]] else