@@ -3,6 +3,7 @@ VERSION = "1.0.0"
33local util = import (" micro/util" )
44local config = import (" micro/config" )
55local buffer = import (" micro/buffer" )
6+ local micro = import (" micro" )
67
78local ft = {}
89
@@ -61,17 +62,21 @@ ft["zig"] = "// %s"
6162ft [" zscript" ] = " // %s"
6263ft [" zsh" ] = " # %s"
6364
64- local last_ft
65-
6665function 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
7681end
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
178183function 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
205210end
206211
212+ function preinit ()
213+ config .RegisterCommonOption (" comment" , " type" , " " )
214+ end
215+
207216function init ()
208217 config .MakeCommand (" comment" , comment , config .NoComplete )
209218 config .TryBindKey (" Alt-/" , " lua:comment.comment" , false )
0 commit comments