dev (#1)
* dev * Improve sync design * Support buffer local mapping * tmp * tmp * tmp * tmp * tmp * tmp * tmp * tmp * tmp * tmp * tmp * tmp * tmp * tmp * tmp * tmp * tmp * tmp * tmp * tmp * tmp * tmp * tmp * tmp * tmp * tmp * tmp * tmp * tmp * tmp * tmp * tmp * stylua * tmp * tmp * tmp * tmp * tmp * tmp * tmp * integration * tmp * tmp * tmp * tmp * tmp * tmp * tmp * tmp * tmp * tmp * tmp * tmp * tmp * tmp * tmp * tmp * tmp * update * tmp * tmp * tmp * tmp * tmp * tmp * tmp * tmp * tmp * tmp * tmp * tmp * tmp
This commit is contained in:
84
lua/cmp/types/cmp.lua
Normal file
84
lua/cmp/types/cmp.lua
Normal file
@@ -0,0 +1,84 @@
|
||||
local cmp = {}
|
||||
|
||||
---@alias cmp.ConfirmBehavior "'insert'" | "'replace'"
|
||||
cmp.ConfirmBehavior = {}
|
||||
cmp.ConfirmBehavior.Insert = 'insert'
|
||||
cmp.ConfirmBehavior.Replace = 'replace'
|
||||
|
||||
---@alias cmp.ContextReason "'auto'" | "'manual'" | "'none'"
|
||||
cmp.ContextReason = {}
|
||||
cmp.ContextReason.Auto = 'auto'
|
||||
cmp.ContextReason.Manual = 'manual'
|
||||
cmp.ContextReason.None = 'none'
|
||||
|
||||
---@alias cmp.TriggerEvent "'InsertEnter'" | "'TextChanged'"
|
||||
cmp.TriggerEvent = {}
|
||||
cmp.TriggerEvent.InsertEnter = 'InsertEnter'
|
||||
cmp.TriggerEvent.TextChanged = 'TextChanged'
|
||||
|
||||
---@class cmp.ContextOption
|
||||
---@field public reason cmp.ContextReason|nil
|
||||
|
||||
---@class cmp.ConfirmOption
|
||||
---@field public behavior cmp.ConfirmBehavior
|
||||
|
||||
---@class cmp.SnippetExpansionParams
|
||||
---@field public body string
|
||||
---@field public insert_text_mode number
|
||||
|
||||
---@class cmp.Setup
|
||||
---@field public __call fun(c: cmp.ConfigSchema)
|
||||
---@field public buffer fun(c: cmp.ConfigSchema)
|
||||
---@field public global fun(c: cmp.ConfigSchema)
|
||||
|
||||
---@class cmp.CompletionRequest
|
||||
---@field public context cmp.Context
|
||||
---@field public option table
|
||||
---@field public offset number
|
||||
---@field public completion_context lsp.CompletionContext
|
||||
|
||||
---@class cmp.ConfigSchema
|
||||
---@field private revision number
|
||||
---@field public completion cmp.CompletionConfig
|
||||
---@field public documentation cmp.DocumentationConfig
|
||||
---@field public confirmation cmp.ConfirmationConfig
|
||||
---@field public sorting cmp.SortingConfig
|
||||
---@field public formatting cmp.FormattingConfig
|
||||
---@field public snippet cmp.SnippetConfig
|
||||
---@field public sources cmp.SourceConfig[]
|
||||
|
||||
---@class cmp.CompletionConfig
|
||||
---@field public autocomplete cmp.TriggerEvent[]
|
||||
---@field public completeopt string
|
||||
---@field public keyword_pattern string
|
||||
---@field public keyword_length number
|
||||
|
||||
---@class cmp.DocumentationConfig
|
||||
---@field public border string[]
|
||||
---@field public winhighlight string
|
||||
---@field public maxwidth number|nil
|
||||
---@field public maxheight number|nil
|
||||
|
||||
---@class cmp.ConfirmationConfig
|
||||
---@field public default_behavior cmp.ConfirmBehavior
|
||||
---@field public mapping table<string, cmp.ConfirmMappingConfig>
|
||||
|
||||
---@class cmp.ConfirmMappingConfig
|
||||
---@field behavior cmp.ConfirmBehavior
|
||||
---@field select boolean
|
||||
|
||||
---@class cmp.SortingConfig
|
||||
---@field public sort fun(entries: cmp.Entry[]): cmp.Entry[]
|
||||
|
||||
---@class cmp.FormattingConfig
|
||||
---@field public format fun(entry: cmp.Entry, suggeset_offset: number): vim.CompletedItem
|
||||
|
||||
---@class cmp.SnippetConfig
|
||||
---@field public expand fun(args: cmp.SnippetExpansionParams)
|
||||
|
||||
---@class cmp.SourceConfig
|
||||
---@field public name string
|
||||
---@field public opts table
|
||||
|
||||
return cmp
|
||||
|
||||
8
lua/cmp/types/init.lua
Normal file
8
lua/cmp/types/init.lua
Normal file
@@ -0,0 +1,8 @@
|
||||
local types = {}
|
||||
|
||||
types.cmp = require('cmp.types.cmp')
|
||||
types.lsp = require('cmp.types.lsp')
|
||||
types.vim = require('cmp.types.vim')
|
||||
|
||||
return types
|
||||
|
||||
205
lua/cmp/types/lsp.lua
Normal file
205
lua/cmp/types/lsp.lua
Normal file
@@ -0,0 +1,205 @@
|
||||
---@see https://microsoft.github.io/language-server-protocol/specifications/specification-current/
|
||||
---@class lsp
|
||||
local lsp = {}
|
||||
|
||||
lsp.Position = {}
|
||||
|
||||
---Convert lsp.Position to vim.Position
|
||||
---@param buf number|string
|
||||
---@param position lsp.Position
|
||||
---@return vim.Position
|
||||
lsp.Position.to_vim = function(buf, position)
|
||||
if not vim.api.nvim_buf_is_loaded(buf) then
|
||||
vim.fn.bufload(buf)
|
||||
end
|
||||
local lines = vim.api.nvim_buf_get_lines(buf, position.line, position.line + 1, false)
|
||||
if #lines > 0 then
|
||||
for i = position.character, 1, -1 do
|
||||
local s, v = pcall(function()
|
||||
return {
|
||||
row = position.line + 1,
|
||||
col = vim.str_byteindex(lines[1], i) + 1
|
||||
}
|
||||
end)
|
||||
if s then
|
||||
return v
|
||||
end
|
||||
end
|
||||
end
|
||||
return {
|
||||
row = position.line + 1,
|
||||
col = position.character + 1,
|
||||
}
|
||||
end
|
||||
|
||||
---Convert lsp.Position to vim.Position
|
||||
---@param buf number|string
|
||||
---@param position vim.Position
|
||||
---@return lsp.Position
|
||||
lsp.Position.to_lsp = function(buf, position)
|
||||
if not vim.api.nvim_buf_is_loaded(buf) then
|
||||
vim.fn.bufload(buf)
|
||||
end
|
||||
local lines = vim.api.nvim_buf_get_lines(buf, position.row - 1, position.row, false)
|
||||
if #lines > 0 then
|
||||
return {
|
||||
line = position.row - 1,
|
||||
character = vim.str_utfindex(lines[1], math.max(0, math.min(position.col - 1, #lines[1]))),
|
||||
}
|
||||
end
|
||||
return {
|
||||
line = position.row - 1,
|
||||
character = position.col - 1,
|
||||
}
|
||||
end
|
||||
|
||||
lsp.Range = {}
|
||||
|
||||
---Convert lsp.Position to vim.Position
|
||||
---@param buf number|string
|
||||
---@param range lsp.Range
|
||||
---@return vim.Range
|
||||
lsp.Range.to_vim = function(buf, range)
|
||||
return {
|
||||
start = lsp.Position.to_vim(buf, range.start),
|
||||
['end'] = lsp.Position.to_vim(buf, range['end']),
|
||||
}
|
||||
end
|
||||
|
||||
---Convert lsp.Position to vim.Position
|
||||
---@param buf number|string
|
||||
---@param range vim.Range
|
||||
---@return lsp.Range
|
||||
lsp.Range.to_lsp = function(buf, range)
|
||||
return {
|
||||
start = lsp.Position.to_lsp(buf, range.start),
|
||||
['end'] = lsp.Position.to_lsp(buf, range['end']),
|
||||
}
|
||||
end
|
||||
|
||||
---@alias lsp.CompletionTriggerKind "1" | "2" | "3"
|
||||
lsp.CompletionTriggerKind = {}
|
||||
lsp.CompletionTriggerKind.Invoked = 1
|
||||
lsp.CompletionTriggerKind.TriggerCharacter = 2
|
||||
lsp.CompletionTriggerKind.TriggerForIncompleteCompletions = 3
|
||||
|
||||
---@class lsp.CompletionContext
|
||||
---@field public triggerKind lsp.CompletionTriggerKind
|
||||
---@field public triggerCharacter string|nil
|
||||
|
||||
---@alias lsp.InsertTextFormat "1" | "2"
|
||||
lsp.InsertTextFormat = {}
|
||||
lsp.InsertTextFormat.PlainText = 1
|
||||
lsp.InsertTextFormat.Snippet = 2
|
||||
lsp.InsertTextFormat = vim.tbl_add_reverse_lookup(lsp.InsertTextFormat)
|
||||
|
||||
---@alias lsp.InsertTextMode "1" | "2"
|
||||
lsp.InsertTextMode = {}
|
||||
lsp.InsertTextMode.AsIs = 0
|
||||
lsp.InsertTextMode.AdjustIndentation = 1
|
||||
lsp.InsertTextMode = vim.tbl_add_reverse_lookup(lsp.InsertTextMode)
|
||||
|
||||
---@alias lsp.MarkupKind "'plaintext'" | "'markdown'"
|
||||
lsp.MarkupKind = {}
|
||||
lsp.MarkupKind.PlainText = 'plaintext'
|
||||
lsp.MarkupKind.Markdown = 'markdown'
|
||||
lsp.MarkupKind.Markdown = 'markdown'
|
||||
lsp.MarkupKind = vim.tbl_add_reverse_lookup(lsp.MarkupKind)
|
||||
|
||||
---@alias lsp.CompletionItemTag "1"
|
||||
lsp.CompletionItemTag = {}
|
||||
lsp.CompletionItemTag.Deprecated = 1
|
||||
lsp.CompletionItemTag = vim.tbl_add_reverse_lookup(lsp.CompletionItemTag)
|
||||
|
||||
---@alias lsp.CompletionItemKind "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" | "10" | "11" | "12" | "13" | "14" | "15" | "16" | "17" | "18" | "19" | "20" | "21" | "22" | "23" | "24" | "25"
|
||||
lsp.CompletionItemKind = {}
|
||||
lsp.CompletionItemKind.Text = 1
|
||||
lsp.CompletionItemKind.Method = 2
|
||||
lsp.CompletionItemKind.Function = 3
|
||||
lsp.CompletionItemKind.Constructor = 4
|
||||
lsp.CompletionItemKind.Field = 5
|
||||
lsp.CompletionItemKind.Variable = 6
|
||||
lsp.CompletionItemKind.Class = 7
|
||||
lsp.CompletionItemKind.Interface = 8
|
||||
lsp.CompletionItemKind.Module = 9
|
||||
lsp.CompletionItemKind.Property = 10
|
||||
lsp.CompletionItemKind.Unit = 11
|
||||
lsp.CompletionItemKind.Value = 12
|
||||
lsp.CompletionItemKind.Enum = 13
|
||||
lsp.CompletionItemKind.Keyword = 14
|
||||
lsp.CompletionItemKind.Snippet = 15
|
||||
lsp.CompletionItemKind.Color = 16
|
||||
lsp.CompletionItemKind.File = 17
|
||||
lsp.CompletionItemKind.Reference = 18
|
||||
lsp.CompletionItemKind.Folder = 19
|
||||
lsp.CompletionItemKind.EnumMember = 20
|
||||
lsp.CompletionItemKind.Constant = 21
|
||||
lsp.CompletionItemKind.Struct = 22
|
||||
lsp.CompletionItemKind.Event = 23
|
||||
lsp.CompletionItemKind.Operator = 24
|
||||
lsp.CompletionItemKind.TypeParameter = 25
|
||||
lsp.CompletionItemKind = vim.tbl_add_reverse_lookup(lsp.CompletionItemKind)
|
||||
|
||||
---@class lsp.CompletionList
|
||||
---@field public isIncomplete boolean
|
||||
---@field public items lsp.CompletionItem[]
|
||||
|
||||
---@alias lsp.CompletionResponse lsp.CompletionList|lsp.CompletionItem[]|nil
|
||||
|
||||
---@class lsp.MarkupContent
|
||||
---@field public kind lsp.MarkupKind
|
||||
---@field public value string
|
||||
|
||||
---@class lsp.Position
|
||||
---@field public line number
|
||||
---@field public character number
|
||||
|
||||
---@class lsp.Range
|
||||
---@field public start lsp.Position
|
||||
---@field public end lsp.Position
|
||||
|
||||
---@class lsp.Command
|
||||
---@field public title string
|
||||
---@field public command string
|
||||
---@field public arguments any[]|nil
|
||||
|
||||
---@class lsp.TextEdit
|
||||
---@field public range lsp.Range|nil
|
||||
---@field public newText string
|
||||
|
||||
---@class lsp.InsertReplaceTextEdit
|
||||
---@field public insert lsp.Range|nil
|
||||
---@field public replace lsp.Range|nil
|
||||
---@field public newText string
|
||||
|
||||
---@class lsp.CompletionItemLabelDetails
|
||||
---@field public parameters string|nil
|
||||
---@field public qualifier string|nil
|
||||
---@field public type string|nil
|
||||
|
||||
---@class lsp.CompletionItem
|
||||
---@field public label string
|
||||
---@field public labelDetails lsp.CompletionItemLabelDetails|nil
|
||||
---@field public kind lsp.CompletionItemKind|nil
|
||||
---@field public tags lsp.CompletionItemTag[]|nil
|
||||
---@field public detail string|nil
|
||||
---@field public documentation lsp.MarkupContent|string|nil
|
||||
---@field public deprecated boolean|nil
|
||||
---@field public preselect boolean|nil
|
||||
---@field public sortText string|nil
|
||||
---@field public filterText string|nil
|
||||
---@field public insertText string|nil
|
||||
---@field public insertTextFormat lsp.InsertTextFormat
|
||||
---@field public insertTextMode lsp.InsertTextMode
|
||||
---@field public textEdit lsp.TextEdit|lsp.InsertReplaceTextEdit|nil
|
||||
---@field public additionalTextEdits lsp.TextEdit[]
|
||||
---@field public commitCharacters string[]|nil
|
||||
---@field public command lsp.Command|nil
|
||||
---@field public data any|nil
|
||||
---
|
||||
---TODO: Should send the issue for upstream?
|
||||
---@field public word string|nil
|
||||
---@field public dup boolean|nil
|
||||
|
||||
return lsp
|
||||
|
||||
47
lua/cmp/types/lsp_spec.lua
Normal file
47
lua/cmp/types/lsp_spec.lua
Normal file
@@ -0,0 +1,47 @@
|
||||
local spec = require'cmp.utils.spec'
|
||||
local lsp = require'cmp.types.lsp'
|
||||
|
||||
describe('types.lsp', function ()
|
||||
before_each(spec.before)
|
||||
describe('Position', function ()
|
||||
vim.fn.setline('1', {
|
||||
'あいうえお',
|
||||
'かきくけこ',
|
||||
'さしすせそ',
|
||||
})
|
||||
local vim_position, lsp_position
|
||||
|
||||
vim_position = lsp.Position.to_vim('%', { line = 1, character = 3 })
|
||||
assert.are.equal(vim_position.row, 2)
|
||||
assert.are.equal(vim_position.col, 10)
|
||||
lsp_position = lsp.Position.to_lsp('%', vim_position)
|
||||
assert.are.equal(lsp_position.line, 1)
|
||||
assert.are.equal(lsp_position.character, 3)
|
||||
|
||||
vim_position = lsp.Position.to_vim('%', { line = 1, character = 0 })
|
||||
assert.are.equal(vim_position.row, 2)
|
||||
assert.are.equal(vim_position.col, 1)
|
||||
lsp_position = lsp.Position.to_lsp('%', vim_position)
|
||||
assert.are.equal(lsp_position.line, 1)
|
||||
assert.are.equal(lsp_position.character, 0)
|
||||
|
||||
vim_position = lsp.Position.to_vim('%', { line = 1, character = 5 })
|
||||
assert.are.equal(vim_position.row, 2)
|
||||
assert.are.equal(vim_position.col, 16)
|
||||
lsp_position = lsp.Position.to_lsp('%', vim_position)
|
||||
assert.are.equal(lsp_position.line, 1)
|
||||
assert.are.equal(lsp_position.character, 5)
|
||||
|
||||
-- overflow (lsp -> vim)
|
||||
vim_position = lsp.Position.to_vim('%', { line = 1, character = 6 })
|
||||
assert.are.equal(vim_position.row, 2)
|
||||
assert.are.equal(vim_position.col, 16)
|
||||
|
||||
-- overflow(vim -> lsp)
|
||||
vim_position.col = vim_position.col + 1
|
||||
lsp_position = lsp.Position.to_lsp('%', vim_position)
|
||||
assert.are.equal(lsp_position.line, 1)
|
||||
assert.are.equal(lsp_position.character, 5)
|
||||
end)
|
||||
end)
|
||||
|
||||
18
lua/cmp/types/vim.lua
Normal file
18
lua/cmp/types/vim.lua
Normal file
@@ -0,0 +1,18 @@
|
||||
---@class vim.CompletedItem
|
||||
---@field public word string
|
||||
---@field public abbr string|nil
|
||||
---@field public kind string|nil
|
||||
---@field public menu string|nil
|
||||
---@field public equal "1"|nil
|
||||
---@field public empty "1"|nil
|
||||
---@field public dup "1"|nil
|
||||
---@field public id any
|
||||
|
||||
---@class vim.Position
|
||||
---@field public row number
|
||||
---@field public col number
|
||||
|
||||
---@class vim.Range
|
||||
---@field public start vim.Position
|
||||
---@field public end vim.Position
|
||||
|
||||
Reference in New Issue
Block a user