From: D4yvid Date: Tue, 11 Apr 2023 15:28:14 +0000 (-0300) Subject: feat: Add custom completion kind X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=b6969ba0d52dd8d7da6def3a77c9f26eb3531b60;p=vim-lsp.git feat: Add custom completion kind --- diff --git a/autoload/lsp/completion.vim b/autoload/lsp/completion.vim index dddacac..b760706 100644 --- a/autoload/lsp/completion.vim +++ b/autoload/lsp/completion.vim @@ -10,6 +10,34 @@ import './textedit.vim' # per-filetype omni-completion enabled/disabled table var ftypeOmniCtrlMap: dict = {} +var defaultKinds: dict = { + 'Text': 't', + 'Method': 'm', + 'Function': 'f', + 'Constructor': 'C', + 'Field': 'F', + 'Variable': 'v', + 'Class': 'c', + 'Interface': 'i', + 'Module': 'M', + 'Property': 'p', + 'Unit': 'u', + 'Value': 'V', + 'Enum': 'e', + 'Keyword': 'k', + 'Snippet': 'S', + 'Color': 'C', + 'File': 'f', + 'Reference': 'r', + 'Folder': 'F', + 'EnumMember': 'E', + 'Contant': 'd', + 'Struct': 's', + 'Event': 'E', + 'Operator': 'o', + 'TypeParameter': 'T' +} + # Returns true if omni-completion is enabled for filetype 'ftype'. # Otherwise, returns false. def LspOmniComplEnabled(ftype: string): bool @@ -24,36 +52,45 @@ enddef # Map LSP complete item kind to a character def LspCompleteItemKindChar(kind: number): string var kindMap: list = ['', - 't', # Text - 'm', # Method - 'f', # Function - 'C', # Constructor - 'F', # Field - 'v', # Variable - 'c', # Class - 'i', # Interface - 'M', # Module - 'p', # Property - 'u', # Unit - 'V', # Value - 'e', # Enum - 'k', # Keyword - 'S', # Snippet - 'C', # Color - 'f', # File - 'r', # Reference - 'F', # Folder - 'E', # EnumMember - 'd', # Contant - 's', # Struct - 'E', # Event - 'o', # Operator - 'T' # TypeParameter - ] + 'Text', + 'Method', + 'Function', + 'Constructor', + 'Field', + 'Variable', + 'Class', + 'Interface', + 'Module', + 'Property', + 'Unit', + 'Value', + 'Enum', + 'Keyword', + 'Snippet', + 'Color', + 'File', + 'Reference', + 'Folder', + 'EnumMember', + 'Contant', + 'Struct', + 'Event', + 'Operator', + 'TypeParameter' + ] + if kind > 25 return '' endif - return kindMap[kind] + + var kindName = kindMap[kind] + var kindValue = defaultKinds[kindName] + + if opt.lspOptions.customCompletionKinds && opt.lspOptions.completionKinds->has_key(kindName) + kindValue = opt.lspOptions.completionKinds[kindName] + endif + + return kindValue enddef # Remove all the snippet placeholders from 'str' and return the value. diff --git a/autoload/lsp/options.vim b/autoload/lsp/options.vim index b3b8dfb..f69365f 100644 --- a/autoload/lsp/options.vim +++ b/autoload/lsp/options.vim @@ -59,7 +59,11 @@ export var lspOptions: dict = { # Use a floating menu to show the code action menu instead of asking for input usePopupInCodeAction: false, # ShowReferences in a quickfix list instead of a location list` - useQuickfixForLocations: false + useQuickfixForLocations: false, + # Enable support for custom completion kinds + customCompletionKinds: false, + # A dictionary with all completion kinds that you want to customize + completionKinds: {} } # set the LSP plugin options from the user provided option values diff --git a/doc/lsp.txt b/doc/lsp.txt index 0cc6689..87b8592 100644 --- a/doc/lsp.txt +++ b/doc/lsp.txt @@ -444,7 +444,7 @@ snippetSupport |Boolean| option. Enable snippet completion support. Need a snippet completion plugin like vim-vsnip. By default this is set to false. *lsp-opt-ultisnipsSupport* -ultisnipsSupport |Boolean| option. Enable SirVer/ultisnips support. +ultisnipsSupport |Boolean| option. Enable SirVer/ultisnips support. Need a snippet completion plugin SirVer/ultisnips. By default this is set to false. *lsp-opt-usePopupInCodeAction* @@ -452,6 +452,12 @@ usePopupInCodeAction |Boolean| option. When using the |:LspCodeAction| command to display the code action for the current line, use a popup menu instead of echoing. By default this is set to false. + *lsp-opt-customCompletionKinds* +customCompletionKinds |Boolean| option. If you set this to true, you can set + custom completion kinds using the option completionKinds. + *lsp-opt-completionKinds* +completionKinds |Dictionary| option. Here you can set custom completion + kinds. To view all completion kind names, use :h lsp-custom-kinds For example, to disable the automatic placement of signs for the LSP diagnostic messages, you can add the following line to your .vimrc file: > @@ -1222,4 +1228,64 @@ The callback function should accept a Dict argument. The Dict argument contains the LSP Command interface fields. Refer to the LSP specification for more information about the "Command" interface. +============================================================================== +15. Custom LSP Completion Kinds *lsp-custom-kinds* + +When a completion popup is triggered, the LSP client will use a default kind +list to show in the completion "kind" section, to customize it, you need to +use the option *lsp-opt-customCompletionKinds* and set all custom kinds in the +option *lsp-opt-completionKinds* . There is a table with all default LSP kinds: + + Kind Name | Value +------------------------|-------------------- + Text | t + Method | m + Function | f + Constructor | C + Field | F + Variable | v + Class | c + Interface | i + Module | M + Property | p + Unit | u + Value | V + Enum | e + Keyword | k + Snippet | S + Color | C + File | f + Reference | r + Folder | F + EnumMember | E + Contant | d + Struct | s + Event | E + Operator | o + TypeParameter | T + +For example, if you want to change the "Method" kind to the kind "method()": + +> + vim9script + + LspOptionsSet({ + customCompletionKinds: true, + completionKinds: { + "Method": "method()" + } + }) +< + +In the completion popup, will show something like this: +> + + var file = new File() + + file.cre + | create method() | + | createIfNotExists method() | + | ... | + + vim:tw=78:ts=8:noet:ft=help:norl: