@@ -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,34 +62,20 @@ ft["zig"] = "// %s"
6162ft [" zscript" ] = " // %s"
6263ft [" zsh" ] = " # %s"
6364
64- function updateCommentType (bp )
65- -- This is the first time doing comment in this bp
66- if bp .Settings [" commentfiletype" ] == nil then
67- -- If commenttype is not registered, use the comment table we have
68- if bp .Settings [" commenttype" ] == nil then
69- if ft [bp .Settings [" filetype" ]] ~= nil then
70- bp .Settings [" commenttype" ] = ft [bp .Settings [" filetype" ]]
71- else
72- bp .Settings [" commenttype" ] = " # %s"
73- end
74- -- Otherwise if the commenttype is registered, that means this is coming from the settings,
75- -- or set manually by the user. We should update our comment table
76- else
77- ft [bp .Settings [" filetype" ]] = bp .Settings [" commenttype" ]
65+ function updateCommentType (buf )
66+ -- NOTE: Don't use SetOptionNative() to set "comment.type",
67+ -- otherwise "comment.type" can't be reset by a "filetype" change.
68+ if buf .Settings [" comment.type" ] == " " then
69+ if buf .Settings [" commenttype" ] ~= nil then
70+ micro .InfoBar ():Error (" \" commenttype\" option has been renamed to \" comment.type\" " ,
71+ " , please update your configuration" )
7872 end
79- -- Update filetype
80- bp .Settings [" commentfiletype" ] = bp .Settings [" filetype" ]
8173
82- -- Otherwise, check if the filetype has changed manually. If so, use the comment table
83- elseif bp .Settings [" commentfiletype" ] ~= bp .Settings [" filetype" ] then
84- if ft [bp .Settings [" filetype" ]] ~= nil then
85- bp .Settings [" commenttype" ] = ft [bp .Settings [" filetype" ]]
74+ if ft [buf .Settings [" filetype" ]] ~= nil then
75+ buf .Settings [" comment.type" ] = ft [buf .Settings [" filetype" ]]
8676 else
87- bp .Settings [" commenttype " ] = " # %s"
77+ buf .Settings [" comment.type " ] = " # %s"
8878 end
89-
90- -- Update filetype
91- bp .Settings [" commentfiletype" ] = bp .Settings [" filetype" ]
9279 end
9380end
9481
@@ -105,7 +92,7 @@ function commentLine(bp, lineN, indentLen)
10592 updateCommentType (bp .Buf )
10693
10794 local line = bp .Buf :Line (lineN )
108- local commentType = bp .Buf .Settings [" commenttype " ]
95+ local commentType = bp .Buf .Settings [" comment.type " ]
10996 local sel = - bp .Cursor .CurSelection
11097 local curpos = - bp .Cursor .Loc
11198 local index = string.find (commentType , " %%s" ) - 1
@@ -131,7 +118,7 @@ function uncommentLine(bp, lineN, commentRegex)
131118 updateCommentType (bp .Buf )
132119
133120 local line = bp .Buf :Line (lineN )
134- local commentType = bp .Buf .Settings [" commenttype " ]
121+ local commentType = bp .Buf .Settings [" comment.type " ]
135122 local sel = - bp .Cursor .CurSelection
136123 local curpos = - bp .Cursor .Loc
137124 local index = string.find (commentType , " %%s" ) - 1
195182function comment (bp , args )
196183 updateCommentType (bp .Buf )
197184
198- local commentType = bp .Buf .Settings [" commenttype " ]
185+ local commentType = bp .Buf .Settings [" comment.type " ]
199186 local commentRegex = " ^%s*" .. commentType :gsub (" %%" ," %%%%" ):gsub (" %$" ," %$" ):gsub (" %)" ," %)" ):gsub (" %(" ," %(" ):gsub (" %?" ," %?" ):gsub (" %*" , " %*" ):gsub (" %-" , " %-" ):gsub (" %." , " %." ):gsub (" %+" , " %+" ):gsub (" %]" , " %]" ):gsub (" %[" , " %[" ):gsub (" %%%%s" , " (.*)" )
200187
201188 if bp .Cursor :HasSelection () then
@@ -221,6 +208,10 @@ function string.starts(String,Start)
221208 return string.sub (String ,1 ,string.len (Start ))== Start
222209end
223210
211+ function preinit ()
212+ config .RegisterCommonOption (" comment" , " type" , " " )
213+ end
214+
224215function init ()
225216 config .MakeCommand (" comment" , comment , config .NoComplete )
226217 config .TryBindKey (" Alt-/" , " lua:comment.comment" , false )
0 commit comments