Skip to content
This repository has been archived by the owner on Mar 25, 2020. It is now read-only.

Commit

Permalink
Working on improving callback setup
Browse files Browse the repository at this point in the history
  • Loading branch information
TJ DeVries committed Dec 21, 2016
1 parent 4b1f7ef commit 7413c1d
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 13 deletions.
4 changes: 4 additions & 0 deletions autoload/langserver/api/textDocument.vim
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ function! langserver#api#textDocument#definition(request) abort
\ {},
\ )
endfunction

function! langserver#api#textDocument#completion(request) abort

endfunction
8 changes: 6 additions & 2 deletions autoload/langserver/client.vim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
let s:lsp_clients = {} " { id, opts, req_seq, on_notifications: { request, on_notification }, stdout: { max_buffer_size, buffer, next_token, current_content_length, current_content_type } }
let s:lsp_clients = {}
let s:lsp_token_type_contentlength = 'content-length'
let s:lsp_token_type_contenttype = 'content-type'
let s:lsp_token_type_message = 'message'
Expand All @@ -14,11 +14,14 @@ function! s:_on_lsp_stdout(id, data, event) abort
" \ 'id': l:lsp_client_id,
" \ 'opts': a:opts,
" \ 'req_seq': 0,
" \ 'on_notifications': {},
" \ 'lock': langserver#lock#semaphore(),
" \ 'request_notifications': {},
" \ 'stdout': {
" \ 'max_buffer_size': l:max_buffer_size,
" \ 'buffer': '',
" \ 'next_token': s:lsp_token_type_contentlength,
" \ 'current_content_length': ,
" \ 'current_content_type': ,
" \ },
" \ }
let l:client = s:lsp_clients[a:id]
Expand Down Expand Up @@ -126,6 +129,7 @@ function! s:lsp_start(opts) abort
\ 'id': l:lsp_client_id,
\ 'opts': a:opts,
\ 'req_seq': 0,
\ 'lock': langserver#lock#semaphore(),
\ 'on_notifications': {},
\ 'stdout': {
\ 'max_buffer_size': l:max_buffer_size,
Expand Down
5 changes: 4 additions & 1 deletion autoload/langserver/hover.vim
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ function! langserver#hover#request() abort
return langserver#client#send(langserver#util#get_lsp_id(), {
\ 'method': 'textDocument/hover',
\ 'params': langserver#util#get_text_document_position_params(),
\ 'on_notification': {
\ 'callback': function('langserver#hover#callback'),
\ },
\ })
endfunction

Expand All @@ -38,7 +41,7 @@ function! langserver#hover#display(range, data) abort

echo l:hover_string

return timer_start(5000, function('s:delete_highlight'))
return timer_start(3000, function('s:delete_highlight'))
endfunction

function! s:delete_highlight() abort
Expand Down
34 changes: 26 additions & 8 deletions autoload/langserver/lock.vim
Original file line number Diff line number Diff line change
@@ -1,28 +1,46 @@
let s:unlocked_id = -1

function! s:dict_lock() dict
let self.locked = v:true
function! s:dict_lock(job_id) dict
if self.locked == v:false
let self.locked = v:true
let self.id = a:job_id
return v:true
else
return v:false
endif
endfunction

function! s:dict_unlock() dict
let self.id = s:unlocked_id
let self.locked = v:false
endfunction

function! s:set_id(job_id) dict
let self.id = a:job_id
endfunction

function! s:get_id() dict
return self.id
endfunction

function! s:take_lock(job_id) dict
" If we're locked, kill the other job
" And set ourselves to be the current job.
if self.locked
" TODO: Don't actually want to stop the whole server...
" I just want to stop the current request. Maybe a send cancel request
" would be good enough. We will see.
" call langserver#job#stop(self.id)
call self.unlock()
endif

call self.lock(a:job_id)
endfunction

function! langserver#lock#semaphore() abort
let l:ret = {
\ 'id': -1,
\ 'id': s:unlocked_id,
\ 'locked': v:false,
\ }
let l:ret.lock = function('s:dict_lock')
let l:ret.unlock = function('s:dict_unlock')
let l:ret.set_id = function('s:set_id')
let l:ret.get_id = function('s:get_id')
let l:ret.take_lock = function('s:take_lock')
return l:ret
endfunction
11 changes: 9 additions & 2 deletions tests/test_lock.vader
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@ Execute (Test Lock):

AssertEqual v:false, my_lock.locked

call my_lock.lock()
call my_lock.lock(1)
AssertEqual v:true, my_lock.locked

call my_lock.unlock()
AssertEqual v:false, my_lock.locked

AssertEqual -1, my_lock.get_id()

call my_lock.set_id(1)
call my_lock.lock(1)
AssertEqual 1, my_lock.get_id()

let set_result = my_lock.lock(2)
AssertEqual v:false, set_result
AssertEqual 1, my_lock.get_id()

call my_lock.take_lock(2)
AssertEqual 2, my_lock.get_id()

0 comments on commit 7413c1d

Please sign in to comment.