Skip to content

Commit ef5c482

Browse files
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
1 parent cc195b6 commit ef5c482

File tree

2 files changed

+28
-14
lines changed

2 files changed

+28
-14
lines changed

runtime/plugins/comment/comment.lua

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ VERSION = "1.0.0"
33
local util = import("micro/util")
44
local config = import("micro/config")
55
local buffer = import("micro/buffer")
6+
local micro = import("micro")
67

78
local ft = {}
89

@@ -61,17 +62,21 @@ ft["zig"] = "// %s"
6162
ft["zscript"] = "// %s"
6263
ft["zsh"] = "# %s"
6364

64-
local last_ft
65-
6665
function updateCommentType(buf)
67-
if buf.Settings["commenttype"] == nil or (last_ft ~= buf.Settings["filetype"] and last_ft ~= nil) then
68-
if ft[buf.Settings["filetype"]] ~= nil then
69-
buf:SetOptionNative("commenttype", ft[buf.Settings["filetype"]])
66+
-- NOTE: Using DoSetOptionNative to avoid LocalSettings[option] = true
67+
-- so that "comment.type" can be reset by a "filetype" change to default.
68+
if (buf.Settings["comment.type"] == "") then
69+
-- NOTE: This won't get triggered if a filetype is change via `setlocal filetype`
70+
-- since it is not registered with `RegisterGlobalOption()``
71+
if buf.Settings["commenttype"] ~= nil then
72+
buf:DoSetOptionNative("comment.type", buf.Settings["commenttype"])
7073
else
71-
buf:SetOptionNative("commenttype", "# %s")
74+
if (ft[buf.Settings["filetype"]] ~= nil) then
75+
buf:DoSetOptionNative("comment.type", ft[buf.Settings["filetype"]])
76+
else
77+
buf:DoSetOptionNative("comment.type", "# %s")
78+
end
7279
end
73-
74-
last_ft = buf.Settings["filetype"]
7580
end
7681
end
7782

@@ -88,7 +93,7 @@ function commentLine(bp, lineN, indentLen)
8893
updateCommentType(bp.Buf)
8994

9095
local line = bp.Buf:Line(lineN)
91-
local commentType = bp.Buf.Settings["commenttype"]
96+
local commentType = bp.Buf.Settings["comment.type"]
9297
local sel = -bp.Cursor.CurSelection
9398
local curpos = -bp.Cursor.Loc
9499
local index = string.find(commentType, "%%s") - 1
@@ -114,7 +119,7 @@ function uncommentLine(bp, lineN, commentRegex)
114119
updateCommentType(bp.Buf)
115120

116121
local line = bp.Buf:Line(lineN)
117-
local commentType = bp.Buf.Settings["commenttype"]
122+
local commentType = bp.Buf.Settings["comment.type"]
118123
local sel = -bp.Cursor.CurSelection
119124
local curpos = -bp.Cursor.Loc
120125
local index = string.find(commentType, "%%s") - 1
@@ -178,7 +183,7 @@ end
178183
function comment(bp, args)
179184
updateCommentType(bp.Buf)
180185

181-
local commentType = bp.Buf.Settings["commenttype"]
186+
local commentType = bp.Buf.Settings["comment.type"]
182187
local commentRegex = "^%s*" .. commentType:gsub("%%","%%%%"):gsub("%$","%$"):gsub("%)","%)"):gsub("%(","%("):gsub("%?","%?"):gsub("%*", "%*"):gsub("%-", "%-"):gsub("%.", "%."):gsub("%+", "%+"):gsub("%]", "%]"):gsub("%[", "%["):gsub("%%%%s", "(.*)")
183188

184189
if bp.Cursor:HasSelection() then
@@ -204,6 +209,10 @@ function string.starts(String,Start)
204209
return string.sub(String,1,string.len(Start))==Start
205210
end
206211

212+
function preinit()
213+
config.RegisterCommonOption("comment", "type", "")
214+
end
215+
207216
function init()
208217
config.MakeCommand("comment", comment, config.NoComplete)
209218
config.TryBindKey("Alt-/", "lua:comment.comment", false)

runtime/plugins/comment/help/comment.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,18 +80,23 @@ but it is only available for certain filetypes:
8080
* zsh: `# %s`
8181

8282
If your filetype is not available here, you can simply modify
83-
the `commenttype` option:
83+
the `comment.type` option:
8484

8585
```
86-
set commenttype "/* %s */"
86+
set comment.type "/* %s */"
8787
```
8888

8989
Or in your `settings.json`:
9090

9191
```json
9292
{
9393
"*.c": {
94-
"commenttype": "/* %s */"
94+
"comment.type": "/* %s */"
9595
}
9696
}
9797
```
98+
99+
`commenttype` was the previous option name that was replaced by `comment.type`.
100+
101+
While `commenttype` is still supported, we recommend switching to `comment.type` instead as
102+
`commenttype` can get deprecated in the future.

0 commit comments

Comments
 (0)