summaryrefslogtreecommitdiff
path: root/test/lsp/test_closing_documents.vader
blob: e6e114155f3915fda840261f71c7fff6508d81b1 (plain)
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
166
167
168
169
170
171
172
173
174
175
176
Before:
  runtime autoload/ale/lsp.vim

  let g:message_list = []

  function! MarkAllConnectionsInitialized() abort
    for l:conn in values(ale#lsp#GetConnections())
      let l:conn.initialized = 1
    endfor
  endfunction

  function! MarkDocumentOpened() abort
    for l:conn in values(ale#lsp#GetConnections())
      let l:conn.open_documents[bufnr('')] = 1
    endfor
  endfunction

  function! ale#lsp#Send(conn_id, message) abort
    let l:connections = ale#lsp#GetConnections()

    if !l:connections[a:conn_id].initialized
        throw 'LSP server not initialized yet!'
    endif

    call add(g:message_list, [a:conn_id] + a:message)
  endfunction

  call ale#lsp#ResetConnections()

After:
  unlet! g:message_list
  delfunction MarkAllConnectionsInitialized
  delfunction MarkDocumentOpened

  call ale#lsp#ResetConnections()

  runtime autoload/ale/lsp.vim

Execute(No errors should be thrown if the connection is not initialized):
  call ale#lsp#Register('command', '/foo', {})
  call MarkDocumentOpened()

  call ale#engine#Cleanup(bufnr(''))
  AssertEqual [], g:message_list

Execute(No messages should be sent if the document wasn't opened):
  call ale#lsp#Register('command', '/foo', {})
  call MarkAllConnectionsInitialized()

  call ale#engine#Cleanup(bufnr(''))
  AssertEqual [], g:message_list

Execute(A message should be sent if the document was opened):
  call ale#lsp#Register('command', '/foo', {})
  call MarkAllConnectionsInitialized()

  call ale#lsp#OpenDocument('command:/foo', bufnr(''), 'lang')
  call ale#engine#Cleanup(bufnr(''))
  " We should only send the message once.
  call ale#engine#Cleanup(bufnr(''))

  AssertEqual
  \ [
  \   ['command:/foo', 1, 'textDocument/didOpen', {
  \     'textDocument': {
  \       'uri': ale#path#ToURI(expand('%:p')),
  \       'version': g:ale_lsp_next_version_id - 1,
  \       'languageId': 'lang',
  \       'text': "\n",
  \     },
  \   }],
  \   ['command:/foo', 1, 'textDocument/didClose', {
  \     'textDocument': {
  \       'uri': ale#path#ToURI(expand('%:p')),
  \     },
  \   }],
  \ ],
  \ g:message_list

Execute(A message should be sent if the document was opened for tsserver):
  call ale#lsp#Register('command', '/foo', {})
  call ale#lsp#MarkConnectionAsTsserver('command:/foo')

  call ale#lsp#OpenDocument('command:/foo', bufnr(''), 'lang')
  call ale#engine#Cleanup(bufnr(''))
  " We should only send the message once.
  call ale#engine#Cleanup(bufnr(''))

  AssertEqual
  \ [
  \   ['command:/foo', 1, 'ts@open', {'file': expand('%:p')}],
  \   ['command:/foo', 1, 'ts@close', {'file': expand('%:p')}],
  \ ],
  \ g:message_list

Execute(Re-opening and closing the documents should work):
  call ale#lsp#Register('command', '/foo', {})
  call MarkAllConnectionsInitialized()

  call ale#lsp#OpenDocument('command:/foo', bufnr(''), 'lang')
  call ale#engine#Cleanup(bufnr(''))
  call ale#lsp#OpenDocument('command:/foo', bufnr(''), 'lang')
  call ale#engine#Cleanup(bufnr(''))

  AssertEqual
  \ [
  \   ['command:/foo', 1, 'textDocument/didOpen', {
  \     'textDocument': {
  \       'uri': ale#path#ToURI(expand('%:p')),
  \       'version': g:ale_lsp_next_version_id - 2,
  \       'languageId': 'lang',
  \       'text': "\n",
  \     },
  \   }],
  \   ['command:/foo', 1, 'textDocument/didClose', {
  \     'textDocument': {
  \       'uri': ale#path#ToURI(expand('%:p')),
  \     },
  \   }],
  \   ['command:/foo', 1, 'textDocument/didOpen', {
  \     'textDocument': {
  \       'uri': ale#path#ToURI(expand('%:p')),
  \       'version': g:ale_lsp_next_version_id - 1,
  \       'languageId': 'lang',
  \       'text': "\n",
  \     },
  \   }],
  \   ['command:/foo', 1, 'textDocument/didClose', {
  \     'textDocument': {
  \       'uri': ale#path#ToURI(expand('%:p')),
  \     },
  \   }],
  \ ],
  \ g:message_list

Execute(Messages for closing documents should be sent to each server):
  call ale#lsp#Register('command', '/foo', {})
  call ale#lsp#Register('command', '/bar', {})
  call MarkAllConnectionsInitialized()

  call ale#lsp#OpenDocument('command:/foo', bufnr(''), 'lang')
  call ale#lsp#OpenDocument('command:/bar', bufnr(''), 'lang')
  call ale#engine#Cleanup(bufnr(''))
  " We should only send the message once.
  call ale#engine#Cleanup(bufnr(''))

  AssertEqual
  \ [
  \   ['command:/foo', 1, 'textDocument/didOpen', {
  \     'textDocument': {
  \       'uri': ale#path#ToURI(expand('%:p')),
  \       'version': g:ale_lsp_next_version_id - 2,
  \       'languageId': 'lang',
  \       'text': "\n",
  \     },
  \   }],
  \   ['command:/bar', 1, 'textDocument/didOpen', {
  \     'textDocument': {
  \       'uri': ale#path#ToURI(expand('%:p')),
  \       'version': g:ale_lsp_next_version_id - 1,
  \       'languageId': 'lang',
  \       'text': "\n",
  \     },
  \   }],
  \   ['command:/bar', 1, 'textDocument/didClose', {
  \     'textDocument': {
  \       'uri': ale#path#ToURI(expand('%:p')),
  \     },
  \   }],
  \   ['command:/foo', 1, 'textDocument/didClose', {
  \     'textDocument': {
  \       'uri': ale#path#ToURI(expand('%:p')),
  \     },
  \   }],
  \ ],
  \ g:message_list