Skip to content

Commit

Permalink
move utils to lsp#utils#...
Browse files Browse the repository at this point in the history
  • Loading branch information
prabirshrestha committed Aug 12, 2017
1 parent 974614e commit 2802c39
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 49 deletions.
58 changes: 12 additions & 46 deletions autoload/lsp.vim
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ function! s:call_did_save(buf, server_name, result, cb) abort
endif

let l:server = s:servers[a:server_name]
let l:path = s:get_buffer_uri(a:buf)
let l:path = lsp#utils#get_buffer_uri(a:buf)
let l:buffers = l:server['buffers']
let l:buffer_info = l:buffers[l:path]

Expand Down Expand Up @@ -202,9 +202,9 @@ function! s:ensure_flush(buf, server_name, cb) abort
endfunction

function! s:ensure_start(buf, server_name, cb) abort
let l:path = s:get_buffer_path(a:buf)
let l:path = lsp#utils#get_buffer_path(a:buf)

if s:is_remote_uri(l:path)
if lsp#utils#is_remote_uri(l:path)
let l:msg = s:new_rpc_error('ignoring start server due to remote uri', { 'server_name': a:server_name, 'uri': l:path})
call lsp#log(l:msg)
call a:cb(l:msg)
Expand Down Expand Up @@ -238,7 +238,7 @@ function! s:ensure_start(buf, server_name, cb) abort

if l:lsp_id > 0
let l:server['lsp_id'] = l:lsp_id
let l:msg = s:new_rpc_success('started lsp server successfully', { 'server_name': a:server_name, 'lps_id': l:lsp_id })
let l:msg = s:new_rpc_success('started lsp server successfully', { 'server_name': a:server_name, 'lsp_id': l:lsp_id })
call lsp#log(l:msg)
call a:cb(l:msg)
else
Expand All @@ -259,7 +259,7 @@ function! s:ensure_init(buf, server_name, cb) abort
endif

if has_key(l:server, 'init_callbacks')
" waiting for initialize resposne
" waiting for initialize response
call add(l:server['init_callbacks'], a:cb)
let l:msg = s:new_rpc_success('waiting for lsp server to initialize', { 'server_name': a:server_name })
call lsp#log(l:msg)
Expand All @@ -272,11 +272,11 @@ function! s:ensure_init(buf, server_name, cb) abort
if has_key(l:server_info, 'root_uri')
let l:root_uri = l:server_info['root_uri'](l:server_info)
else
let l:root_uri = s:get_default_root_uri()
let l:root_uri = lsp#utils#get_default_root_uri()
endif

if empty(l:root_uri)
let l:msg = s:new_rpc_error('ignore initialization lsp server due to empty root_uri', { 'server_name': a:server_name, 'lsp_id': l:lps_id })
let l:msg = s:new_rpc_error('ignore initialization lsp server due to empty root_uri', { 'server_name': a:server_name, 'lsp_id': l:server['lsp_id'] })
call lsp#log(l:msg)
call a:cb(l:msg)
return
Expand All @@ -296,7 +296,7 @@ endfunction

function! s:ensure_changed(buf, server_name, cb) abort
let l:server = s:servers[a:server_name]
let l:path = s:get_buffer_uri(a:buf)
let l:path = lsp#utils#get_buffer_uri(a:buf)

let l:buffers = l:server['buffers']
let l:buffer_info = l:buffers[l:path]
Expand Down Expand Up @@ -332,7 +332,7 @@ endfunction

function! s:ensure_open(buf, server_name, cb) abort
let l:server = s:servers[a:server_name]
let l:path = s:get_buffer_uri(a:buf)
let l:path = lsp#utils#get_buffer_uri(a:buf)

if empty(l:path)
let l:msg = s:new_rpc_error('ignore open since not a valid uri', { 'server_name': a:server_name, 'path': l:path })
Expand Down Expand Up @@ -486,41 +486,11 @@ function! lsp#get_whitelisted_servers(...) abort
return l:active_servers
endfunction

function! s:get_default_root_uri() abort
return s:path_to_uri(getcwd())
endfunction

function! s:get_buffer_path(...) abort
return expand((a:0 > 0 ? '#' . a:1 : '%') . ':p')
endfunction

function! s:get_buffer_uri(...) abort
return s:path_to_uri(expand((a:0 > 0 ? '#' . a:1 : '%') . ':p'))
endfunction

if has('win32') || has('win64')
function! s:path_to_uri(path) abort
return s:is_remote_uri(a:path) ? a:path : 'file:///' . substitute(a:path, '\', '/', 'g')
endfunction
else
function! s:path_to_uri(path) abort
return s:is_remote_uri(a:path) ? a:path : 'file://' . a:path
endfunction
endif

if has('win32') || has('win64')
function! lsp#uri_to_path(uri) abort
return substitute(a:uri[len('file:///'):], '/', '\\', 'g')
endfunction
else
function! lsp#uri_to_path(uri) abort
return a:uri[len('file://'):]
endfunction
endif

function! s:get_text_document(buf, buffer_info) abort
return {
\ 'uri': s:get_buffer_uri(a:buf),
\ 'uri': lsp#utils#get_buffer_uri(a:buf),
\ 'languageId': &filetype,
\ 'version': a:buffer_info['version'],
\ 'text': join(getline(a:buf, '$'), "\n"),
Expand All @@ -529,7 +499,7 @@ endfunction

function! lsp#get_text_document_identifier(...) abort
let l:buf = a:0 > 0 ? a:1 : bufnr('%')
return { 'uri': s:get_buffer_uri(l:buf) }
return { 'uri': lsp#utils#get_buffer_uri(l:buf) }
endfunction

function! lsp#get_position(...) abort
Expand All @@ -538,15 +508,11 @@ endfunction

function! s:get_text_document_identifier(buf, buffer_info) abort
return {
\ 'uri': s:get_buffer_uri(a:buf),
\ 'uri': lsp#utils#get_buffer_uri(a:buf),
\ 'version': a:buffer_info['version'],
\ }
endfunction

function! s:is_remote_uri(uri) abort
return a:uri =~# '^\w\+::' || a:uri =~# '^\w\+://'
endfunction

function! lsp#send_request(server_name, request) abort
let l:Cb = has_key(a:request, 'on_notification') ? a:request['on_notification'] : function('s:Noop')
let l:request = copy(a:request)
Expand Down
2 changes: 1 addition & 1 deletion autoload/lsp/ui/vim.vim
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ function! s:apply_workspace_edits(workspace_edits) abort
endfunction

function! s:apply_text_edits(uri, text_edits) abort
let l:path = lsp#uri_to_path(a:uri)
let l:path = lsp#utils#uri_to_path(a:uri)
let l:cmd = 'edit ' . l:path
for l:text_edit in a:text_edits
let l:start_line = l:text_edit['range']['start']['line'] + 1
Expand Down
4 changes: 2 additions & 2 deletions autoload/lsp/ui/vim/utils.vim
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function! lsp#ui#vim#utils#locations_to_loc_list(result) abort
if !empty(l:locations) " some servers also return null so check to make sure it isn't empty
for l:location in l:locations
if s:is_file_uri(l:location['uri'])
let l:path = lsp#uri_to_path(l:location['uri'])
let l:path = lsp#utils#uri_to_path(l:location['uri'])
let l:line = l:location['range']['start']['line'] + 1
let l:col = l:location['range']['start']['character'] + 1
call add(l:list, {
Expand Down Expand Up @@ -60,7 +60,7 @@ function! lsp#ui#vim#utils#symbols_to_loc_list(result) abort
for l:symbol in a:result['response']['result']
let l:location = l:symbol['location']
if s:is_file_uri(l:location['uri'])
let l:path = lsp#uri_to_path(l:location['uri'])
let l:path = lsp#utils#uri_to_path(l:location['uri'])
let l:bufnr = bufnr(l:path)
let l:line = l:location['range']['start']['line'] + 1
let l:col = l:location['range']['start']['character'] + 1
Expand Down
77 changes: 77 additions & 0 deletions autoload/lsp/utils.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
function! lsp#utils#is_remote_uri(uri) abort
return a:uri =~# '^\w\+::' || a:uri =~# '^\w\+://'
endfunction

if has('win32') || has('win64')
function! lsp#utils#path_to_uri(path) abort
if empty(a:path)
return a:path
else
return lsp#utils#is_remote_uri(a:path) ? a:path : 'file:///' . substitute(a:path, '\', '/', 'g')
endif
endfunction
else
function! lsp#utils#path_to_uri(path) abort
if empty(a:path)
return a:path
else
return lsp#utils#is_remote_uri(a:path) ? a:path : 'file://' . a:path
endif
endfunction
endif

if has('win32') || has('win64')
function! lsp#utils#uri_to_path(uri) abort
return substitute(a:uri[len('file:///'):], '/', '\\', 'g')
endfunction
else
function! lsp#utils#uri_to_path(uri) abort
return a:uri[len('file://'):]
endfunction
endif

function! lsp#utils#get_default_root_uri() abort
return lsp#utils#path_to_uri(getcwd())
endfunction

function! lsp#utils#get_buffer_path(...) abort
return expand((a:0 > 0 ? '#' . a:1 : '%') . ':p')
endfunction

function! lsp#utils#get_buffer_uri(...) abort
return lsp#utils#path_to_uri(expand((a:0 > 0 ? '#' . a:1 : '%') . ':p'))
endfunction

" Find a nearest to a `path` parent directory `directoryname` by traversing the filesystem upwards
function! lsp#utils#find_nearest_parent_directory(path, directoryname) abort
let l:relative_path = finddir(a:directoryname, a:path . ';')

if !empty(l:relative_path)
return fnamemodify(l:relative_path, ':p')
else
return ''
endif
endfunction

" Find a nearest to a `path` parent filename `filename` by traversing the filesystem upwards
function! lsp#utils#find_nearest_parent_file(path, filename) abort
let l:relative_path = findfile(a:filename, a:path . ';')

if !empty(l:relative_path)
return fnamemodify(l:relative_path, ':p')
else
return ''
endif
endfunction

" Find a nearest to a `path` parent filename `filename` by traversing the filesystem upwards
function! lsp#utils#find_nearest_parent_file_directory(path, filename) abort
let l:path = lsp#utils#find_nearest_parent_file(a:path, a:filename)

if !empty(l:path)
return fnamemodify(l:path, ':p:h')
else
return ''
endif
endfunction

0 comments on commit 2802c39

Please sign in to comment.