]> Sergey Matveev's repositories - vim-lsp.git/commitdiff
feat: Add custom completion kind
authorD4yvid <kkd4yv1d@gmail.com>
Tue, 11 Apr 2023 15:28:14 +0000 (12:28 -0300)
committerD4yvid <kkd4yv1d@gmail.com>
Tue, 11 Apr 2023 15:28:14 +0000 (12:28 -0300)
autoload/lsp/completion.vim
autoload/lsp/options.vim
doc/lsp.txt

index dddacacfb38b9f2de90a8e5089d222f6994d7077..b76070662754c1f8c0470dc7abf98f5762373361 100644 (file)
@@ -10,6 +10,34 @@ import './textedit.vim'
 # per-filetype omni-completion enabled/disabled table
 var ftypeOmniCtrlMap: dict<bool> = {}
 
+var defaultKinds: dict<string> = {
+  '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<string> = ['',
-               '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.
index b3b8dfbff13c4ce7e4cde8443695f10d8bd5b2d9..f69365feb9d86e06cf99c9b862896981cdc302be 100644 (file)
@@ -59,7 +59,11 @@ export var lspOptions: dict<any> = {
   # 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
index 0cc668970517c1afc48f13b47ffc49a031e69c7f..87b8592f4c4cecd6dd96fcdfb2e1814807a4ce22 100644 (file)
@@ -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: