Scite 自動完成任何語言

lua-users home
wiki

這是提供任何檔案類型自動完成的 SciTE 啟動腳本。它會在儲存、開啟和切換緩衝區時掃描檔案中的識別碼,並在自動完成中使用那些識別碼。它不會使用任何外部檔案來收集識別碼清單。

識別碼包括的內容由 IDENTIFIER_PATTERNS 中的樣式清單決定。這個範例允許識別碼中包含點號(例如建議整個「object.member.property」字串)和破折號(例如「lisp-or-css-identifier」)。

如果您在 SciTE 屬性中啟用自動完成,這個腳本可能無法正常運作。(應該可以直接針對某些檔案類型停用它。)

    -- Dynamically generate autocomplete lists from possible identifiers in any file.

    local IGNORE_CASE = true
    -- Number of chars to type before the autocomplete list appears:
    local MIN_PREFIX_LEN = 3
    -- Length of shortest word to add to the autocomplete list
    local MIN_IDENTIFIER_LEN = 6
    -- A list of string patterns for finding suggestions for the autocomplete menu.
    local IDENTIFIER_PATTERNS = {"[%a_][%w_]+", "[%a_][%w_.]*[%w_]", "[%a_][%w_-]*[%w_]"}


    local names = {}
    local notempty = next


    if IGNORE_CASE then
        normalize = string.lower
    else
        normalize = function(word) return word end
    end


    function buildNames()
        names = {}
        local text = editor:GetText()
        for i, pattern in ipairs(IDENTIFIER_PATTERNS) do
            for word in string.gmatch(text, pattern) do
                if string.len(word) >= MIN_IDENTIFIER_LEN then
                    names[word] = true
                end
            end
        end
    end


    function handleChar()
        if not editor:AutoCActive() then
            editor.AutoCIgnoreCase = IGNORE_CASE            
            local pos = editor.CurrentPos
            local startPos = editor:WordStartPosition(pos, true)
            local len = pos - startPos
            if len >= MIN_PREFIX_LEN then
                local prefix = editor:textrange(startPos, pos)
                local menuItems = {}
                for name, v in pairs(names) do
                    if normalize(string.sub(name, 1, len)) == normalize(prefix) then 
                        table.insert(menuItems, name)
                    end
                end
                if notempty(menuItems) then
                    table.sort(menuItems)
                    editor:AutoCShow(len, table.concat(menuItems, " "))
                end
            end
        end
    end


    -- Event handlers
    OnChar = handleChar
    OnSave       = buildNames
    OnSwitchFile = buildNames
    OnOpen       = buildNames
MartinStone?

很不幸地,如果關鍵字中包含像「.」這樣的字元,AutoCShow() 函式似乎無法運作...


RecentChanges · 偏好設定
編輯 · 歷史
最後編輯於 2011 年 5 月 11 日下午 4:46 GMT (差異)