Skip to content
This repository was archived by the owner on Oct 13, 2021. It is now read-only.

Used stdlib signature_help function#189

Merged
haorenW1025 merged 1 commit intonvim-lua:masterfrom
Xuyuanp:master
Sep 3, 2020
Merged

Used stdlib signature_help function#189
haorenW1025 merged 1 commit intonvim-lua:masterfrom
Xuyuanp:master

Conversation

@Xuyuanp
Copy link
Copy Markdown
Contributor

@Xuyuanp Xuyuanp commented Sep 2, 2020

These deleted codes are most implemented in stdlib. DRY.

(There are some bugs in stdlib. See neovim/neovim#12832)

Additionally, using stdlib made it possible to customize the floating window display.

for example. I set my signature help callback as following

local signature_help_callback = function(_, _, result)
    local util = vim.lsp.util
    if not (result and result.signatures and #result.signatures > 0) then
        return { 'No signature available' }
    end
    local active_signature = result.activeSignature or 0
    if active_signature >= #result.signatures then
        active_signature = 0
    end
    local signature = result.signatures[active_signature + 1]
    if not signature then
        return { 'No signature available' }
    end

    local highlights = {}
    if result.activeParameter or signature.parameters then
        local active_parameter = result.activeParameter or 0
        local parameter = signature.parameters[active_parameter + 1]
        if parameter and parameter.label then
            local label_type = type(parameter.label)
            if label_type == "string" then
                local l, r = string.find(signature.label, parameter.label, 1, true)
                if l and r then
                    highlights = {l, r + 1}
                end
            elseif label_type == "table" then
                local l, r = unpack(parameter.label)
                highlights = {l + 1, r}
            end
        end
    end

    local filetype = vim.api.nvim_buf_get_option(0, "filetype")
    if filetype and type(signature.label) == "string" then
        signature.label = string.format("```%s\n%s\n```", filetype, signature.label)
    end

    local lines = util.convert_signature_help_to_markdown_lines(result)
    lines = util.trim_empty_lines(lines)
    if vim.tbl_isempty(lines) then
        return { 'No signature available' }
    end
    local bufnr, winnr = util.fancy_floating_markdown(lines, {
        pad_left = 1, pad_right = 1
    })
    if #highlights > 0 then
        vim.api.nvim_buf_add_highlight(bufnr, -1, 'Underlined', 0, highlights[1], highlights[2]+1)
    end
    util.close_preview_autocmd({"CursorMoved", "CursorMovedI", "BufHidden", "BufLeave"}, winnr)
end

And add this line to on_attach function

client.callbacks["textDocument/signatureHelp"] = signature_help_callback

The signature help window will from this
微信图片_20200902234818
to this
微信图片_20200902234812

@haorenW1025
Copy link
Copy Markdown
Collaborator

Yeah this is probably better for now. Thanks for the fix!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants