Scite Auto Complete Any Language

lua-users home
wiki

This is a SciTE startup script that provides auto-completion for any file type. It scans the file for identifiers on save, open and buffer switch and uses those identifiers in auto-completion. It doesn't use any external files for lists of identifiers.

What constitutes an identifier is determined by the list of patterns in IDENTIFIER_PATTERNS. This example allows identifiers to include dots (e.g. to suggest a whole "object.member.property" string) and dashes, (e.g. "lisp-or-css-identifier").

This script may not behave well if you have auto-completion enabled in SciTE properties. (It would probably be straightforward to make it disable itself for certain file types.)

    -- 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?

Unfortunately the AutoCShow() function seems not to work if keywords contain characters like '.'...


RecentChanges · preferences
edit · history
Last edited May 11, 2011 4:46 pm GMT (diff)