為SciTE添加命令,自動添加註釋(Lua)

來源:互聯網
上載者:User

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",如所示:

4.在Lua檔案中,效果如所示:

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.