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

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
tjdevries committed Nov 23, 2016
2 parents 84fb86b + 3c94634 commit 97ee0c1
Show file tree
Hide file tree
Showing 14 changed files with 379 additions and 147 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ Shim for the language server protocol developed by Microsoft. The protocol can b

## Configuration

First you need to install a language server. An example of installing one might be:

```shell
$ go get github.com/sourcegraph/go-langserver/langserver/cmd/langserver-go
```

A more complete set of language servers can be found here: https://github.com/Microsoft/language-server-protocol/wiki/Protocol-Implementations

You will need to put this somewhere that is sourced on startup.

```vim
Expand Down
9 changes: 9 additions & 0 deletions autoload/langserver/api/textDocument.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
""
function! langserver#api#textDocument#definition(request) abort
return langserver#goto#goto_defintion(
\ langserver#util#get_lsp_id(),
\ a:request.result.uri,
\ a:request.result.range,
\ {},
\ )
endfunction
12 changes: 3 additions & 9 deletions autoload/langserver/callbacks.vim
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ function! langserver#callbacks#on_stdout(id, data, event) abort
endfunction

function! langserver#callbacks#on_stderr(id, data, event) abort
echom 'stderr ...'
call langserver#log#response(a:id, a:data, a:event)

echom string(a:data)
echom '...stderr'
endfunction

function! langserver#callbacks#on_exit(id, status, event) abort
Expand All @@ -31,14 +27,12 @@ function! langserver#callbacks#on_notification(id, data, event) abort
call langserver#hover#callback(a:id, a:data, a:event)
elseif l:last_topic ==? 'textDocument/didOpen'
call langserver#documents#callback_did_open(a:id, a:data, a:event)
elseif l:last_topic ==? 'initialize'
call langserver#initialize#callback(a:id, a:data, a:event)
elseif l:last_topic ==? 'workspace/symbol'
call langserver#symbol#workspace#callback(a:id, a:data, a:event)
else
" if langserver#extension#command#callback(a:id, a:data, a:event)
" call langserver#log#log('debug', 'Handled: ' . l:last_topic)
" else
call langserver#log#log('warning', 'LAST REQUEST: ' . l:last_topic, v:true)
" endif
call langserver#log#log('warning', 'No callback registered for: ' . l:last_topic, v:true)
endif
elseif has_key(a:data, 'request')
echom 'notification...'
Expand Down
23 changes: 23 additions & 0 deletions autoload/langserver/client.vim
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ let s:lsp_text_document_sync_kind_incremental = 2

function! s:_on_lsp_stdout(id, data, event) abort
if has_key(s:lsp_clients, a:id)
" let s:lsp_clients[l:lsp_client_id] = {
" \ 'id': l:lsp_client_id,
" \ 'opts': a:opts,
" \ 'req_seq': 0,
" \ 'on_notifications': {},
" \ 'stdout': {
" \ 'max_buffer_size': l:max_buffer_size,
" \ 'buffer': '',
" \ 'next_token': s:lsp_token_type_contentlength,
" \ },
" \ }
let l:client = s:lsp_clients[a:id]

let l:client.stdout.buffer .= join(a:data, "\n")
Expand Down Expand Up @@ -146,6 +157,10 @@ function! s:lsp_send_request(id, opts) abort " opts = { method, params?, on_noti
let l:msg.params = a:opts.params
endif

if has_key(a:opts, 'result')
let l:msg.result = a:opts.result
endif

let l:json = json_encode(l:msg)
let l:req_data = 'Content-Length: ' . len(l:json) . "\r\n\r\n" . l:json

Expand Down Expand Up @@ -175,6 +190,14 @@ function! s:lsp_get_last_request_id(id) abort
endfunction

function! s:lsp_is_error(notification) abort
" if type(a:notification) != type({})
" return v:true
" endif

" if !has_key(a:notification, 'response') || !has_key(a:notification.response, 'result')
" return v:true
" endif

return has_key(a:notification, 'error')
endfunction

Expand Down
29 changes: 25 additions & 4 deletions autoload/langserver/default.vim
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,31 @@ let s:langserver_executabe = 'langserver-go'

""
" Get the default command for starting the server
function! langserver#default#cmd() abort
if has_key(g:langserver_executables, &filetype)
return g:langserver_executables[&filetype]['cmd']
function! langserver#default#cmd(...) abort
if a:0 > 0
let l:filetype_key = langserver#util#get_executable_key(a:1)
else
return [-1]
let l:filetype_key = langserver#util#get_executable_key(&filetype)
endif

let l:bad_cmd = [-1]

if has_key(g:langserver_executables, l:filetype_key)
" Has to be uppercase because of function naming
" Sorry for mixed case :/
let l:TmpCmd = g:langserver_executables[l:filetype_key]['cmd']

if type(l:TmpCmd) == type([])
return l:TmpCmd
elseif type(l:TmpCmd) == type(function('tr'))
let l:result = l:TmpCmd()
if type(l:result) == type([])
return l:result
endif
endif
endif

" If we didn't return anything, there was an error.
echoerr 'Please consult the documentation for how to configure the langserver'
return l:bad_cmd
endfunction
7 changes: 3 additions & 4 deletions autoload/langserver/extension/command.vim
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ function! langserver#extension#command#callback(id, data, event) abort

call langserver#client#send(a:id, {
\ 'req_id': a:data.request.id,
\ 'method': 'fs/readFile',
\ 'params': {
\ 'result': l:response,
\ },
\ 'method': l:method,
\ 'params': a:data.request.params,
\ 'result': l:response,
\ })
return v:true
endfunction
9 changes: 8 additions & 1 deletion autoload/langserver/extension/fs.vim
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,12 @@
function! langserver#extension#fs#readFille(filename) abort
echom a:filename

return join(readfile(a:filename), "\n")
" let l:text = join(readfile(a:filename), "\n")
" echo system(['base64', join(readfile('/home/tj/test/lsp.py'), "\n")])
if filereadable(a:filename)
return system(['base64', a:filename])
else
call langserver#log#log('error', 'Unable to read file: ' . a:filename)
return ''
endif
endfunction
17 changes: 13 additions & 4 deletions autoload/langserver/goto.vim
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@ function! langserver#goto#callback(id, data, event) abort
if has_key(a:data, 'response')
if type(a:data['response']['result']) == type([])
let l:parsed_data = a:data['response']['result'][0]
else
let l:parsed_data = a:data['response']['result']
elseif type(a:data['response']['result']) == type({})
" Check if we have an empty dictionary
if empty(a:data['response']['result'])
call langserver#log#log('warning', 'No definition found for: ' . string(a:data['request']), v:true)
return
else
let l:parsed_data = a:data['response']['result']
endif
endif
else
return
Expand Down Expand Up @@ -77,8 +83,11 @@ function! langserver#goto#goto_defintion(name, uri, range_dict, options) abort
" \ a:range_dict['start']['line'] + 1,
" \ a:range_dict['start']['character'] + 1,
" \ )
execute(printf('norm! %sG%s|',
let l:action = printf('norm! %sG%s|',
\ a:range_dict['start']['line'] + 1,
\ a:range_dict['start']['character'] + 1,
\ ))
\ )
execute(l:action)

return l:action
endfunction
15 changes: 14 additions & 1 deletion autoload/langserver/initialize.vim
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ function! langserver#initialize#get_client_capabilities() abort
return {}
endfunction

function! langserver#initialize#callback(id, data, event) abort
if langserver#client#is_error(a:data.response)
call langserver#log#log('error',
\ 'Could not connect to: ' . string(a:id) . "\n" .
\ 'Message was: ' . string(a:data),
\ v:true,
\ )
else
call langserver#initialize#response(a:id, a:data.response.result)
call langserver#log#log('info', 'Succesfully connected to: ' . string(a:id), v:true)
endif
endfunction

""
" Handle the response of the server.
" This message details the capabilities of the language server.
Expand All @@ -47,7 +60,7 @@ function! langserver#initialize#response(name, response) abort

if has_key(a:response, 'completionProvider')
let l:complete_opt_resolve = get(a:response['completionProvider'], 'resolveProvider', v:false)
let l:complete_opt_trigger = get(a:resposne['completionProvider'], 'triggerCharacters', [])
let l:complete_opt_trigger = get(a:response['completionProvider'], 'triggerCharacters', [])
call langserver#capabilities#set_completion_provider(a:name, l:complete_opt_resolve, l:complete_opt_trigger)
endif

Expand Down
Loading

0 comments on commit 97ee0c1

Please sign in to comment.