为SciTE添加命令,自动添加注释(Lua)

SciTE官方文档对添加命令的说明如下:
1
2
3
4
5
6
7
8
9
10
command.name.number.filepattern
command.number.filepattern
command.is.filter.number.filepattern
command.subsystem.number.filepattern
command.save.before.number.filepattern
command.input.number.filepattern
command.replace.selection.number.filepattern
command.quiet.number.filepattern
command.mode.number.filepattern
command.shortcut.number.filepattern
Extra commands can be added to the Tools menu. For example to include the 'astyle' indenter, the properties file could contain
1
2
3
command.name.0.*.cc=Indent
command.0.*.cc=astyle -taO $(FileNameExt)
command.is.filter.0.*.cc=1
The first line defines the string that will appear in the Tools menu (immediately below 'Go'). The second line is the command string, similar to those of the compile, build, and go commands. The optional command.is.filter property states that the command modifies the current file so it may need to be read in after performing the command if load.on.activate is set.
If command.save.before is set to 1, SciTE automatically saves the file before execution. If it is set to 2, SciTE will not save the file, otherwise SciTE asks you. On Windows, the optional command.input property specifies text that will be piped to the command. This may reference other properties; for example, command.input.0.*.cc=$(CurrentSelection) would pipe the current selection to the command processes. The command.input property is only supported for subsystem 0 (command line programs).

The optional command.replace.selection can be used to specify that the command output should replace the current selection (or be inserted at the cursor location, if there is no selection). This property has three available settings: 0, the default, means do not replace the selection. 1 means replace the selection when the command finishes. 2 means replace the selection only if the command finishes with an exit code of 0. If the user cancels the command via "Tools / Stop Executing", the selection will not be replaced even in mode 1. Note, commands run asynchronously, so you are not prevented from modifying the document or even switching buffers while a command is running. However, please bear in mind that command.replace.selection will send the output to whatever window is active when the command completes.
A final command property that is currently supported only on windows is command.quiet. A value of 1 indicates that the command I/O should not be echoed to the output pane. This may be useful in combination with command.input and command.replace.selection.

The command.mode property is a comma-separated list of flags / settings. Each mode setting can have an argument, separated from the setting name by a colon. For most of these, the argument portion is optional; if the setting name appears without an argument, this works the same as "setting:yes". If a setting is included in the command.mode but also appears as a separate command property, the mode property will be overridden. Similarly, if a single setting appears more than once with different arguments, the last valid argument takes priority. The supported command.mode settings are:
1
2
3
4
5
6
filter - accepts keyword arguments yes and no
quiet - accepts keyword arguments yes and no
replaceselection - accepts yes, no, and auto
savebefore - accepts yes, no, and prompt
subsystem - console, windows, shellexec, lua, director, winhelp, htmlhelp
groupundo - yes or no
Currently, all of these except groupundo are based on individual properties with similar names, and so are not described separately here. The groupundo setting works with subsystem 3 (lua / director), and indicates that SciTE should treat any changes made by the command as a single undo action. A command that uses the groupundo setting should not change which buffer is active in the editor.
The command.shortcut property allows you to specify a keyboard shortcut for the command. By default, commands 0 to 9 have keyboard shortcuts Ctrl+0 to Ctrl+9 respectively, but this can be overridden. For commands numbered higher than 9, there is no default keyboard shortcut. The notation used to specify keyboard shortcuts is the same as for the user.shortcuts property, described elsewhere in this document.

If the text of a command starts with '*' then the Parameters dialog is displayed to prompt for parameters before executing the command. The initial '*' is not included in the command that is executed.

The command number can be in the range of 0 to 49. Command numbers 0 to 9 are assigned Ctrl+Number shortcuts. Internally these commands use IDs starting from 1100 (IDM_TOOLS) which can be used in user.shortcuts and user.context.menu as:
1
user.context.menu=Indent|1100|

If command.name is empty then no item is added to the Tools menu. This can be used for commands that are only in the context menu or user shortcuts.

在这里要为LuaForWindows自带的SciTE添加自动注释功能,参照《Using Lua With Scite》。步骤如下:
1.打开"SciTEGlobal.properties"文件,在文件末添加如下:

1
2
3
4
command.name.0.*=Auto AddComment
command.0.*=auto_AddComment
command.subsystem.0.*=3
command.mode.0.*=savebefore:no
2.打开“SciTE\scite-debug\extman.lua"文件,在文件末添加如下代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
local strCommentHeader =  "-------------------------------------------------------------------------------\r\n-- \r\n-------------------------------------------------------------------------------\r\n"
local patternCommentFun =  "function .+%((.*)%)"
local strCommentFun =  "-------------------------------------------------------------------------------\r\n-- \r\n"
local patternCommentTable =  " *([%w_]+) *=%s*(%b{})"
local strCommentTable =  "-------------------------------------------------------------------------------\r\n-- \r\n-- @class table\r\n"
-------------------------------------------------------------------------------
-- 自动添加Lua语言注释
function auto_AddComment()
     local nLine = editor:LineFromPosition(editor.CurrentPos)
     local nPosition = editor:PositionFromLine(nLine)
     -- 文件头注释
     if  0 == nLine  then
        editor:InsertText(nPosition, strCommentHeader)
        editor.CurrentPos = editor.LineEndPosition[ 1]
        editor:SetSel(editor.CurrentPos, editor.CurrentPos)
         return
     end
     -- 函数注释, 仅函数在同一行声明
     local strLineText = editor:textrange(nPosition, editor.LineEndPosition[nLine])
     local strParam =  string.match(strLineText, patternCommentFun)
     if  nil ~= strParam  then
         local tCommentFun = {strCommentFun}
         for k  in  string.gmatch(strParam,  "([%w_]+)[, ]*"do
            tCommentFun[#tCommentFun +  1] =  string. format( "-- @param %s \r\n", k)
         end
         local strComment =  table.concat(tCommentFun)
        editor:InsertText(editor:PositionFromLine(nLine), strComment)
        editor.CurrentPos = editor.LineEndPosition[nLine +  1]
        editor:SetSel(editor.CurrentPos, editor.CurrentPos)
         return
     end
     -- 表注释
     local strRangeText = editor:textrange(nPosition, editor.Length)
     local strTable, strField =  string.match(strRangeText, patternCommentTable)
     if  nil ~= strTable  then
         local tCommentTable = {strCommentTable}
        tCommentTable[#tCommentTable +  1] =  string. format( "-- @name %s\r\n", strTable)
         for k  in  string.gmatch(strField,  "([%w_]+) *= *"do
            tCommentTable[#tCommentTable +  1] =  string. format( "-- @field %s \r\n", k)
         end
         local strComment =  table.concat(tCommentTable)
        editor:InsertText(editor:PositionFromLine(nLine), strComment)
        editor.CurrentPos = editor.LineEndPosition[nLine +  1]
        editor:SetSel(editor.CurrentPos, editor.CurrentPos)
         return
     end
end
3.保存,重启SciTE,在菜单栏→"Tools"即可看到,新添加的命令" Auto AddComment",如下图所示:
为SciTE添加命令,自动添加注释(Lua)_第1张图片
4.在Lua文件中,效果如下图所示:
为SciTE添加命令,自动添加注释(Lua)_第2张图片
5.在所要注释的当前行,按下快捷键Ctrl+0,即可进行注释,经过这样注释之后的Lua代码可以通过 LuaDoc生成文档。若以上代码有问题,或是有更好的方法,请通知我以便改正。


参考资料:
1.http://www.scintilla.org/SciTEDoc.html
2.http://www.scintilla.org/SciTELua.html
3.http://lua-users.org/wiki/UsingLuaWithScite

你可能感兴趣的:(properties,command,lua,Parameters,tools,keyboard)