Skip to content

xzbdmw/colorful-menu.nvim

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

51 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

README

colorful-menu.nvim

image

Out of box, this plugin reconsturct completion_item and applies Treesitter highlight queries to produce richly colorized completion items with variable-size highlight ranges, somehow similar to lspkind.nvim.

Highly beta.

run nvim -u ~/.config/nvim/repro.lua ~/.config/nvim/repro.lua as a minimal reproduce template see repro.lua

Has built-in supports for rust-analyzer(rust), gopls(go), typescript-language-server/vtsls, lua-ls, clangd(C/CPP), intelephense(php), zls(zig), roslyn(C#), for any other language, default to directly apply treesitter highlight to label (feel free to open feature request for more languages).

Currently supports nvim-cmp and blink.cmp.

Installation

With lazy.nvim:

return {
    "xzbdmw/colorful-menu.nvim",
    config = function()
        -- You don't need to set these options.
        require("colorful-menu").setup({
            ls = {
                lua_ls = {
                    -- Maybe you want to dim arguments a bit.
                    arguments_hl = "@comment",
                },
                gopls = {
                    -- When true, label for field and variable will format like "foo: Foo"
                    -- instead of go's original syntax "foo Foo".
                    add_colon_before_type = false,
                },
                ["typescript-language-server"] = {
                    extra_info_hl = "@comment",
                },
                ["typescript-tools"] = {
                    extra_info_hl = "@comment",
                },
                ts_ls = {
                    extra_info_hl = "@comment",
                },
                tsserver = {
                    extra_info_hl = "@comment",
                },
                vtsls = {
                    extra_info_hl = "@comment",
                },
                ["rust-analyzer"] = {
                    -- Such as (as Iterator), (use std::io).
                    extra_info_hl = "@comment",
                },
                clangd = {
                    -- Such as "From <stdio.h>".
                    extra_info_hl = "@comment",
                },
                roslyn = {
                    extra_info_hl = "@comment",
                },

                -- If true, try to highlight "not supported" languages.
                fallback = true,
            },
            -- If the built-in logic fails to find a suitable highlight group,
            -- this highlight is applied to the label.
            fallback_highlight = "@variable",
            -- If provided, the plugin truncates the final displayed text to
            -- this width (measured in display cells). Any highlights that extend
            -- beyond the truncation point are ignored. Default 60.
            max_width = 60,
        })
    end,
}

call it in cmp:

formatting = {
    format = function(entry, vim_item)
        local highlights_info = require("colorful-menu").cmp_highlights(entry)

        -- if highlight_info==nil, which means missing ts parser, let's fallback to use default `vim_item.abbr`.
        -- What this plugin offers is two fields: `vim_item.abbr_hl_group` and `vim_item.abbr`.
        if highlights_info ~= nil then
            vim_item.abbr_hl_group = highlights_info.highlights
            vim_item.abbr = highlights_info.text
        end

        return vim_item
    end,
}

Or call it on blink.cmp

config = function()
    require("blink.cmp").setup({
        completion = {
            menu = {
                draw = {
                    -- We don't need label_description now because label and label_description are already
                    -- conbined together in label by colorful-menu.nvim.
                    columns = { { "kind_icon" }, { "label", gap = 1 } },
                    components = {
                        label = {
                            text = require("colorful-menu").blink_components_text,
                            highlight = require("colorful-menu").blink_components_highlight,
                        },
                    },
                },
            },
        },
    })
end

If you want to customize a bit more in those options, here is a more granular control approach:

Click to see
config = function()
    require("blink.cmp").setup({
        completion = {
            menu = {
                draw = {
                    -- We don't need label_description now because label and label_description are already
                    -- conbined together in label by colorful-menu.nvim.
                    columns = { { "kind_icon" }, { "label", gap = 1 } },
                    components = {
                        label = {
                            width = { fill = true, max = 60 },
                            text = function(ctx)
                                local highlights_info = require("colorful-menu").blink_highlights(ctx)
                                if highlights_info ~= nil then
                                    -- Or you want to add more item to label
                                    return highlights_info.label
                                else
                                    return ctx.label
                                end
                            end,
                            highlight = function(ctx)
                                local highlights = {}
                                local highlights_info = require("colorful-menu").blink_highlights(ctx)
                                if highlights_info ~= nil then
                                    highlights = highlights_info.highlights
                                end
                                for _, idx in ipairs(ctx.label_matched_indices) do
                                    table.insert(highlights, { idx, idx + 1, group = "BlinkCmpLabelMatch" })
                                end
                                -- Do something else
                                return highlights
                            end,
                        },
                    },
                },
            },
        },
    })
end

Screen

Note: you may want to set CmpItemAbbrMatch or BlinkCmpLabelMatch to only has bold style (without fg) to have the similar effect as blow images.

gopls

before:

image

after:

image
Click to see video https://github.com/user-attachments/assets/fe72a70b-28ec-460f-9b77-12c95bf74e2e

rust-analyzer

before:

image

after:

image
Click to see video https://github.com/user-attachments/assets/94cb79f0-b93f-4749-99b7-15eae3764f0f

clangd

before:

image

after:

image
Click to see video https://github.com/user-attachments/assets/725ea273-b598-4947-b189-f642fa51cf9b

lua_ls

before:

image

after:

image
Click to see video https://github.com/user-attachments/assets/725ea273-b598-4947-b189-f642fa51cf9b)](https://github.com/user-attachments/assets/1e5b1587-4374-49c3-88e7-1e8ed37b3210

typescript-language-server

before:

image

after:

image
Click to see video https://github.com/user-attachments/assets/07509e0c-8c7a-4895-8096-73343f85c583

intelephense(PHP) Thanks to @pnx

before:

image

after:

image

zls

before:

image

after:

image

roslyn(C#) Thanks to @seblj

before:

image

after:

image

Contributing

Feel free to open issues or submit pull requests if you encounter any bugs or have feature requests.

License

MIT License.

Credit

Zed for the initial idea of colorize.

@David van Munster for the pr which make this plugin possible.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages