Scite 清理文档空白

lua-users home
wiki

这些是 BruceDodson 用于清理文档空白的工具命令。只是一些简单的脚本,已经包含在我的安装程序中。

脚本

function stripTrailingSpaces(reportNoMatch)
    local count = 0
    local fs,fe = editor:findtext("[ \\t]+$", SCFIND_REGEXP)
    if fe then
        repeat
            count = count + 1
            editor:remove(fs,fe)
            fs,fe = editor:findtext("[ \\t]+$", SCFIND_REGEXP, fs)
        until not fe
        print("Removed trailing spaces from " .. count .. " line(s).")
    elseif reportNoMatch then
        print("Document was clean already; nothing to do.")
    end
    return count
end

function fixIndentation(reportNoMatch)
    local tabWidth = editor.TabWidth
    local count = 0
    if editor.UseTabs then
        -- for each piece of indentation that includes at least one space
        for m in editor:match("^[\\t ]* [\\t ]*", SCFIND_REGEXP) do
            -- figure out the indentation size
            local indentSize = editor.LineIndentation[editor:LineFromPosition(m.pos)]
            local spaceCount = math.mod(indentSize, tabWidth)
            local tabCount = (indentSize - spaceCount) / tabWidth
            local fixedIndentation = string.rep('\t', tabCount) .. string.rep(' ', spaceCount)

            if fixedIndentation ~= m.text then
                m:replace(fixedIndentation)
                count = count + 1
            end
        end
    else
        -- for each piece of indentation that includes at least one tab
        for m in editor:match("^[\\t ]*\t[\\t ]*", SCFIND_REGEXP) do
            -- just change all of the indentation to spaces
            m:replace(string.rep(' ', editor.LineIndentation[editor:LineFromPosition(m.pos)]))
            count = count + 1
        end
    end
    if count > 0 then
        print("Fixed indentation for " .. count .. " line(s).")
    elseif reportNoMatch then
        print("Document was clean already; nothing to do.")
    end
    return count
end

function cleanDocWhitespace()
    local trailingSpacesCount = stripTrailingSpaces(false)
    local fixedIndentationCount = fixIndentation(false)

    if (fixedIndentationCount == 0) and (trailingSpacesCount == 0) then
        print("Document was clean already; nothing to do.")
    end
end

配置

    #This is the same as the next two together.
    #Probably comment out the others if you uncomment this.
    #command.name.31.*=Clean Document Whitespace
    #command.mode.31.*=subsystem:lua,savebefore:no,groupundo
    #command.shortcut.31.*=Alt+Shift+I
    #command.31.*=cleanDocWhitespace
    
    command.name.31.*=Fix Indentation
    command.mode.31.*=subsystem:lua,savebefore:no,groupundo
    command.shortcut.31.*=Alt+Shift+I
    command.31.*=fixIndentation
    
    command.name.32.*=Strip Trailing Spaces
    command.mode.32.*=subsystem:lua,savebefore:no,groupundo
    command.shortcut.32.*=Alt+Shift+S
    command.32.*=stripTrailingSpaces

最近更改 · 偏好设置
编辑 · 历史记录
最后编辑于 2006 年 8 月 31 日下午 7:50 GMT (差异)