summaryrefslogtreecommitdiff
path: root/test/lsp/test_did_save_event.vader
blob: 042a3ce2e19f839f84ab2fe7baf5236dddbc0d10 (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
Before:
  Save g:ale_lint_on_save
  Save g:ale_enabled
  Save g:ale_linters
  Save g:ale_run_synchronously

  call ale#test#SetDirectory('/testplugin/test/completion')
  call ale#test#SetFilename('dummy.txt')

  runtime autoload/ale/lsp.vim

  let g:ale_lint_on_save = 1
  let b:ale_enabled = 1
  let g:ale_lsp_next_message_id = 1
  let g:ale_run_synchronously = 1
  let g:message_list = []
  let g:Callback = ''

  function! LanguageCallback() abort
    return 'foobar'
  endfunction

  function! ProjectRootCallback() abort
    return expand('.')
  endfunction

  call ale#linter#Define('foobar', {
  \ 'name': 'dummy_linter',
  \ 'lsp': 'stdio',
  \ 'command': 'cat - > /dev/null',
  \ 'executable': has('win32') ? 'cmd' : 'echo',
  \ 'language_callback': 'LanguageCallback',
  \ 'project_root_callback': 'ProjectRootCallback',
  \ })
  let g:ale_linters = {'foobar': ['dummy_linter']}

  function! ale#linter#StartLSP(buffer, linter, callback) abort
    let g:Callback = a:callback

    return {
    \ 'connection_id': 347,
    \ 'project_root': '/foo/bar',
    \}
  endfunction

  " Replace the Send function for LSP, so we can monitor calls to it.
  function! ale#lsp#Send(conn_id, message, ...) abort
    call add(g:message_list, a:message)
  endfunction

After:
  Restore

  unlet! b:ale_enabled
  unlet! b:ale_linters
  unlet! g:Callback
  unlet! g:message_list

  delfunction LanguageCallback
  delfunction ProjectRootCallback

  call ale#test#RestoreDirectory()
  call ale#linter#Reset()

  " Stop any timers we left behind.
  " This stops the tests from failing randomly.
  call ale#completion#StopTimer()

  runtime autoload/ale/completion.vim
  runtime autoload/ale/lsp.vim

Given foobar (Some imaginary filetype):
  <contents>

Execute(Server should be notified on save):
  call ale#events#SaveEvent(bufnr(''))

  AssertEqual
  \ [
  \   [1, 'textDocument/didChange', {
  \     'textDocument': {
  \       'uri': ale#path#ToURI(expand('%:p')),
  \       'version': g:ale_lsp_next_version_id - 1,
  \     },
  \     'contentChanges': [{'text': join(getline(1, '$'), "\n") . "\n"}],
  \   }],
  \   [1, 'textDocument/didSave', {
  \     'textDocument': {
  \       'uri': ale#path#ToURI(expand('%:p')),
  \     },
  \   }],
  \ ],
  \ g:message_list

Execute(Server should be notified on change):
  call ale#events#FileChangedEvent(bufnr(''))

  AssertEqual
  \ [
  \   [1, 'textDocument/didChange', {
  \     'textDocument': {
  \       'uri': ale#path#ToURI(expand('%:p')),
  \       'version': g:ale_lsp_next_version_id - 1,
  \     },
  \     'contentChanges': [{'text': join(getline(1, '$'), "\n") . "\n"}],
  \   }],
  \ ],
  \ g:message_list