Jump to content

MouseHoverCallTips [11/24/2023]


Recommended Posts

Mmm. Seems like your assuming user functions are highlighted by default. However this might not be the case as its a separate feature. CQ not linked to the API part.

(guessing AdmiralAlkex might have no highlight active on his user functions.)

function MouseHoverCallTips:OnDwellStart(position, word)
    -- ...
    elseif (style == 14 or style == 15) then -- user defined functions and com methods
        directory = props["SciteDefaultHome"] .. "apiau3.user.calltips.api"
    -- ...
Edited by MvGulik

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Link to comment
Share on other sites

I don't see what you mean. Could you elaborate?

like _GUICtrlListView_Create().

Mmm. Seems like your assuming user functions are highlighted by default. However this might not be the case as its a separate feature. CQ not linked to the API part.

(guessing AdmiralAlkex might have no highlight active on his user functions.)

function MouseHoverCallTips:OnDwellStart(position, word)
    -- ...
    elseif (style == 14 or style == 15) then -- user defined functions and com methods
        directory = props["SciteDefaultHome"] .. "\\api\\au3.user.calltips.api"
    -- ...
I don't understand.
Link to comment
Share on other sites

like _GUICtrlListView_Create().

...

I don't understand.

Aha. I was locked on local home made user UDF functions. (CQ: overlooking/ignoring the AutoIt-User-UDF's part.)

---

Now handles call tips for user defined functions and for com methods -- assuming that you have them defined.

Roger. Did not sink in at first. Edited by MvGulik

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Link to comment
Share on other sites

This is super nice.

You think you could add tip for every other functions? For tip to show the first line of the definition.

For example if I have function defined as:

Func ABC(ByRef vVar, sSomething = "DEF", iSomethingElse = 456)
     ;...
EndFunc
...displayed tip would be
ABC(ByRef vVar, sSomething = "DEF", iSomethingElse = 456)

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

Thanks!

Ok, so you mean to create call tips from functions which are not defined in an api file? I'll definitely look into that.

Edit: I may be able to modify AutoItGotoDefinition.lua to do this. No, that's going above and beyond what is needed.

Edited by LaCastiglione
Link to comment
Share on other sites

Tsk tsk. Hard coded paths and not resilient if said paths are missing.

Here's the fixed code. Changes are:

  • get_function_calltip() now checks if the file actually opened. If it doesn't it returns an empty string.
  • OnDwellStart() re-written and simplified. It now uses the api.$(au3) property which contains a full list of all API files. This removes the hard-coded files (Which were not present on my system). It iterates each API found until a match is located (or not). The property api.$(au3) should be standard on all installations.
  • I also renamed onstartUp() to onstartup() since Lua is case-sensitive. Your onstartUp() was never called. However, for reasons I don't know calling initialize_mouse_dwell() still doesn't work from onstartup(). It only seems to work if called from OnSwitchFile(). I suggest removing onstartup() since it isn't necessary.
MouseHoverCallTips = EventClass:new(Common)

--------------------------------------------------------------------------------
-- OnStartUp
--
--------------------------------------------------------------------------------
function MouseHoverCallTips:OnStartup()
    self:initialize_mouse_dwell()
    return false
end

--------------------------------------------------------------------------------
-- OnSwitchFile
--
--------------------------------------------------------------------------------
function MouseHoverCallTips:OnSwitchFile(path)
    self:initialize_mouse_dwell()
    return false
end

--------------------------------------------------------------------------------
-- initialize_mouse_dwell
--
--------------------------------------------------------------------------------
function MouseHoverCallTips:initialize_mouse_dwell()
    --if (self:IsAu3File(path)) then
        local mousehover_calltips_dwelltime = tonumber(props["mousehover.calltips.dwelltime"])
        if (mousehover_calltips_dwelltime == nil) then mousehover_calltips_dwelltime = 700 end -- default
        scite.SendEditor(SCI_SETMOUSEDWELLTIME, mousehover_calltips_dwelltime)
    --end

    return true
end

--------------------------------------------------------------------------------
-- IsAu3File
--
--------------------------------------------------------------------------------
function MouseHoverCallTips:IsAu3File(path)
    if (string.lower(string.sub(path, -4)) == string.lower(string.sub(props["au3"], -4))) then
        return true
    end

    return false
end

--------------------------------------------------------------------------------
-- find_instance
--
--------------------------------------------------------------------------------
function MouseHoverCallTips:find_instance(source, pattern)
    local function find_first_instance(source, pattern, acc)
        if (source == '') then return 0 end

        if (pattern == '') then return 0 end

        if (acc == string.len(source)) then return 0 end

        if (string.sub(source, acc, acc) == pattern) then
            return acc
        else
            acc = acc + 1
            return find_first_instance(source, pattern, acc)
        end
    end

    return find_first_instance(source, pattern, 1)
end

--------------------------------------------------------------------------------
-- get_func_def
--
--------------------------------------------------------------------------------
function MouseHoverCallTips:get_func_def(line, word, style)
    local var = string.gsub(line, " (", '(', 1)

    local position = self:find_instance(var, '(')

    if (position ~= 0) then
        local func_name = string.sub(var, 1, position - 1)

        if (style == 14) then func_name = string.gsub(func_name, "_AutoItObject_", '') end

        func_name = string.lower(func_name)

        word = string.lower(word)

        if (func_name == word) then return string.gsub(line, "%)(.)", "%)n%1") end -- changed: was adding a trailing empty line in some cases.
    end

    return nil
end

--------------------------------------------------------------------------------
-- get_function_calltip
--
--------------------------------------------------------------------------------
function MouseHoverCallTips:get_function_calltip(directory, style, word)
    local file = io.open(directory, 'r')
    if file == nil then return "" end
    local function_calltip = ''

    for line in file:lines() do
        -- filter out constants
        if (style == 4) then
            local string_end = string.sub(line, -2)

            for i = 1, 4 do
                if (string_end == '?' .. i) then break end
            end
        end

        function_calltip = self:get_func_def(line, word, style)

        if (function_calltip ~= nil) then break end
    end

    file:close()

    return function_calltip
end

--------------------------------------------------------------------------------
-- OnDwellStart
--
--------------------------------------------------------------------------------
function MouseHoverCallTips:OnDwellStart(position, word)
    if (word == "") then
        if (scite.SendEditor(SCI_CALLTIPACTIVE)) then scite.SendEditor(SCI_CALLTIPCANCEL) end
        return true
    end

    local style = scite.SendEditor(SCI_GETSTYLEAT, position)

    if (style == 4 or style == 14 or style == 15) then
        local directories = props["api.$(au3)"]

        if (directories ~= nil) then
            for directory in directories:gmatch("([^;rn]+)") do
                local function_calltip = self:get_function_calltip(directory, style, word)
                if (function_calltip ~= nil) then scite.SendEditor(SCI_CALLTIPSHOW, position, function_calltip) end
            end
        end
    end
    return true
end
Edited by Valik
Link to comment
Share on other sites

I concur. It displays an empty calltip though it does get the calltip from the api though.

Ok, changing this line in get_function_calltip from '' to nil works: local function_calltip = nil

Link to comment
Share on other sites

Oops, that was a last second untested change and it exposed a second problem. The first problem is that I returned "" from get_function_calltip() instead of nil but then tested for nil. The second problem (and why this happened) was the iterative loop never terminated on a successful match. So it would find the right match, show it, then it would search the next file, receive an empty string and display that and so forth.

Here is a fixed version. I also added in debugging code. It's off by default but can be quickly toggled to provide diagnostics in the future.

MouseHoverCallTips = EventClass:new(Common)

-- If you are experiencing problems with this script then set the following
-- variable to true to enable diagnostic messages.
MouseHoverCallTips.Debug = false

--------------------------------------------------------------------------------
-- OnSwitchFile
--
--------------------------------------------------------------------------------
function MouseHoverCallTips:OnSwitchFile(path)
    self:initialize_mouse_dwell()
    return false
end

--------------------------------------------------------------------------------
-- initialize_mouse_dwell
--
--------------------------------------------------------------------------------
function MouseHoverCallTips:initialize_mouse_dwell()
    --if (self:IsAu3File(path)) then
        local mousehover_calltips_dwelltime = tonumber(props["mousehover.calltips.dwelltime"])
        if (mousehover_calltips_dwelltime == nil) then mousehover_calltips_dwelltime = 700 end -- default
        scite.SendEditor(SCI_SETMOUSEDWELLTIME, mousehover_calltips_dwelltime)
    --end

    return true
end

--------------------------------------------------------------------------------
-- IsAu3File
--
--------------------------------------------------------------------------------
function MouseHoverCallTips:IsAu3File(path)
    if (string.lower(string.sub(path, -4)) == string.lower(string.sub(props["au3"], -4))) then
        return true
    end

    return false
end

--------------------------------------------------------------------------------
-- find_instance
--
--------------------------------------------------------------------------------
function MouseHoverCallTips:find_instance(source, pattern)
    local function find_first_instance(source, pattern, acc)
        if (source == '') then return 0 end

        if (pattern == '') then return 0 end

        if (acc == string.len(source)) then return 0 end

        if (string.sub(source, acc, acc) == pattern) then
            return acc
        else
            acc = acc + 1
            return find_first_instance(source, pattern, acc)
        end
    end

    return find_first_instance(source, pattern, 1)
end

--------------------------------------------------------------------------------
-- get_func_def
--
--------------------------------------------------------------------------------
function MouseHoverCallTips:get_func_def(line, word, style)
    local var = string.gsub(line, " (", '(', 1)

    local position = self:find_instance(var, '(')

    if (position ~= 0) then
        local func_name = string.sub(var, 1, position - 1)

        if (style == 14) then func_name = string.gsub(func_name, "_AutoItObject_", '') end

        func_name = string.lower(func_name)

        word = string.lower(word)

        if (func_name == word) then return string.gsub(line, "%)(.)", "%)n%1") end -- changed: was adding a trailing empty line in some cases.
    end

    return nil
end

--------------------------------------------------------------------------------
-- get_function_calltip
--
--------------------------------------------------------------------------------
function MouseHoverCallTips:get_function_calltip(directory, style, word)
    local file = io.open(directory, 'r')
    if file == nil then return nil end
    local function_calltip = nil

    for line in file:lines() do
        -- filter out constants
        if (style == 4) then
            local string_end = string.sub(line, -2)

            for i = 1, 4 do
                if (string_end == '?' .. i) then break end
            end
        end

        function_calltip = self:get_func_def(line, word, style)

        if (function_calltip ~= nil) then break end
    end

    file:close()

    return function_calltip
end

--------------------------------------------------------------------------------
-- OnDwellStart
--
--------------------------------------------------------------------------------
function MouseHoverCallTips:OnDwellStart(position, word)
    if (word == "") then
        if (scite.SendEditor(SCI_CALLTIPACTIVE)) then scite.SendEditor(SCI_CALLTIPCANCEL) end
        return true
    end

    local style = scite.SendEditor(SCI_GETSTYLEAT, position)

    if (style == 4 or style == 14 or style == 15) then
        local directories = props["api.$(au3)"]

        if (directories ~= nil) then
            self:DebugPrint("API files: " .. directories)
            for directory in directories:gmatch("([^;rn]+)") do
                self:DebugPrint("Searching API file: " .. directory)
                local function_calltip = self:get_function_calltip(directory, style, word)
                if (function_calltip ~= nil) then
                    self:DebugPrint("Found calltip in API file: " .. directory)
                    scite.SendEditor(SCI_CALLTIPSHOW, position, function_calltip)
                    return true
                end
            end
        else
            self:DebugPrint("The api.$(au3) is missing or empty.")
        end
    else
        self:DebugPrint("Unsupported style detected.")
    end
    return true
end

Edit: Oops, it was the line mentioned above. Returning "" when the file didn't open was wrong, too, though.

Edited by Valik
Link to comment
Share on other sites

Now if someone would implement trancexx suggestion this would be really awesome.

Ohhh yeah, kinda forgot about that. Thanks.

Edit: Aren't a list of functions kept somewhere in a lua table?

Edited by LaCastiglione
Link to comment
Share on other sites

If you are referring to showing calltips for all functions then don't bother. I already have code to mostly do that. It's unfinished and not linked to this feature, obviously. I've had it for years and at this point I don't even remember what's not finished about it. I should probably just publish it.

Link to comment
Share on other sites

  • jaberwacky changed the title to MouseHoverCallTips [11/24/2023]

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...