Page 1 of 1

Go Locate Import

Posted: Tue Oct 25, 2016 5:38 am
by jussij
This macro will try to locate the folder location of the Go import found on the current line.

Just place the cursor on the line containing the import and run the macro.

Code: Select all

--
--        Name: Go Locate Import Macro
--
--    Language: Lua Macro
--
-- Description: This macro will try to locate the folder of the import
--              found on the current.
--

function file_import(gopath, import)
    local file_name =  gopath .. "\\src\\" ..import

    -- clean the string
    file_name = string.gsub(file_name, "/", "\\")
    file_name = string.gsub(file_name, "\\\\", "\\")
    file_name = string.gsub(file_name, "\\\\", "\\")

    return find_drives_item(file_name)
end

function key_macro()
    -- get a copy of the current line
    local text = get_line_text()

    if string.len(text) > 0 then
        local import = string.match(text, '^%s+\"([%a%./]+)\"')

        if import == nil then
            import = string.match(text, '^import%s+\"([%a%./]+)\"')
        end

        if import ~= nil then
            local result = file_import(os.getenv("GOPATH"), import)

            if result == 0 then
                result = file_import("c:\\Go", import)
            end

            if result == 0 then
                result = file_import("d:\\Go", import)
            end

            if result == 0 then
                message("Import not found: '" .. import .. "'")
                beep()
            else
                -- return the focus to the active document
                ActivateDocument()
            end
        else
            message("No import details found on current line. Place the cursor on a line containing a Go import.")
        end
    else
        message("Place the cursor on a line containing a Go import.")
    end
end

key_macro() -- run the macro