-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathmessage.vim
165 lines (141 loc) · 5.12 KB
/
message.vim
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
" Author: w0rp <[email protected]>
" Description: Language Server Protocol message implementations
"
" Messages in this movie will be returned in the format
" [is_notification, method_name, params?]
"
" All functions which accept line and column arguments expect them to be 1-based
" (the same format as being returned by getpos() and friends), those then
" will be converted to 0-based as specified by LSP.
let g:ale_lsp_next_version_id = 1
" The LSP protocols demands that we send every change to a document, including
" undo, with incrementing version numbers, so we'll just use one incrementing
" ID for everything.
function! ale#lsp#message#GetNextVersionID() abort
" Use the current ID
let l:id = g:ale_lsp_next_version_id
" Increment the ID variable.
let g:ale_lsp_next_version_id += 1
" When the ID overflows, reset it to 1. By the time we hit the initial ID
" again, the messages will be long gone.
if g:ale_lsp_next_version_id < 1
let g:ale_lsp_next_version_id = 1
endif
return l:id
endfunction
function! ale#lsp#message#Initialize(root_path, initialization_options) abort
" TODO: Define needed capabilities.
" NOTE: rootPath is deprecated in favour of rootUri
return [0, 'initialize', {
\ 'processId': getpid(),
\ 'rootPath': a:root_path,
\ 'capabilities': {},
\ 'initializationOptions': a:initialization_options,
\ 'rootUri': ale#path#ToURI(a:root_path),
\}]
endfunction
function! ale#lsp#message#Initialized() abort
return [1, 'initialized', {}]
endfunction
function! ale#lsp#message#Shutdown() abort
return [0, 'shutdown']
endfunction
function! ale#lsp#message#Exit() abort
return [1, 'exit']
endfunction
function! ale#lsp#message#DidOpen(buffer, language_id) abort
let l:lines = getbufline(a:buffer, 1, '$')
return [1, 'textDocument/didOpen', {
\ 'textDocument': {
\ 'uri': ale#path#ToURI(expand('#' . a:buffer . ':p')),
\ 'languageId': a:language_id,
\ 'version': ale#lsp#message#GetNextVersionID(),
\ 'text': join(l:lines, "\n") . "\n",
\ },
\}]
endfunction
function! ale#lsp#message#DidChange(buffer) abort
let l:lines = getbufline(a:buffer, 1, '$')
" For changes, we simply send the full text of the document to the server.
return [1, 'textDocument/didChange', {
\ 'textDocument': {
\ 'uri': ale#path#ToURI(expand('#' . a:buffer . ':p')),
\ 'version': ale#lsp#message#GetNextVersionID(),
\ },
\ 'contentChanges': [{'text': join(l:lines, "\n") . "\n"}]
\}]
endfunction
function! ale#lsp#message#DidSave(buffer) abort
return [1, 'textDocument/didSave', {
\ 'textDocument': {
\ 'uri': ale#path#ToURI(expand('#' . a:buffer . ':p')),
\ },
\}]
endfunction
function! ale#lsp#message#DidClose(buffer) abort
return [1, 'textDocument/didClose', {
\ 'textDocument': {
\ 'uri': ale#path#ToURI(expand('#' . a:buffer . ':p')),
\ },
\}]
endfunction
let s:COMPLETION_TRIGGER_INVOKED = 1
let s:COMPLETION_TRIGGER_CHARACTER = 2
function! ale#lsp#message#Completion(buffer, line, column, trigger_character) abort
let l:message = [0, 'textDocument/completion', {
\ 'textDocument': {
\ 'uri': ale#path#ToURI(expand('#' . a:buffer . ':p')),
\ },
\ 'position': {'line': a:line - 1, 'character': a:column - 1},
\}]
if !empty(a:trigger_character)
let l:message[2].context = {
\ 'triggerKind': s:COMPLETION_TRIGGER_CHARACTER,
\ 'triggerCharacter': a:trigger_character,
\}
endif
return l:message
endfunction
function! ale#lsp#message#Definition(buffer, line, column) abort
return [0, 'textDocument/definition', {
\ 'textDocument': {
\ 'uri': ale#path#ToURI(expand('#' . a:buffer . ':p')),
\ },
\ 'position': {'line': a:line - 1, 'character': a:column - 1},
\}]
endfunction
function! ale#lsp#message#TypeDefinition(buffer, line, column) abort
return [0, 'textDocument/typeDefinition', {
\ 'textDocument': {
\ 'uri': ale#path#ToURI(expand('#' . a:buffer . ':p')),
\ },
\ 'position': {'line': a:line - 1, 'character': a:column - 1},
\}]
endfunction
function! ale#lsp#message#References(buffer, line, column) abort
return [0, 'textDocument/references', {
\ 'textDocument': {
\ 'uri': ale#path#ToURI(expand('#' . a:buffer . ':p')),
\ },
\ 'position': {'line': a:line - 1, 'character': a:column - 1},
\ 'context': {'includeDeclaration': v:false},
\}]
endfunction
function! ale#lsp#message#Symbol(query) abort
return [0, 'workspace/symbol', {
\ 'query': a:query,
\}]
endfunction
function! ale#lsp#message#Hover(buffer, line, column) abort
return [0, 'textDocument/hover', {
\ 'textDocument': {
\ 'uri': ale#path#ToURI(expand('#' . a:buffer . ':p')),
\ },
\ 'position': {'line': a:line - 1, 'character': a:column - 1},
\}]
endfunction
function! ale#lsp#message#DidChangeConfiguration(buffer, config) abort
return [1, 'workspace/didChangeConfiguration', {
\ 'settings': a:config,
\}]
endfunction