Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions runtime/plugins/comment/comment.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}

Expand Down Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
11 changes: 8 additions & 3 deletions runtime/plugins/comment/help/comment.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,23 @@ 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`:

```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 deprecated.
**Use `comment.type` instead.**