Merge pull request #104 from someboddy/add-doc-and-symbol-location-support-to-vim-plugin

Add doc and symbol location support to vim plugin
This commit is contained in:
Hackerpilot 2014-01-31 12:43:14 -08:00
commit 8b0b96868d
3 changed files with 52 additions and 4 deletions

View File

@ -56,6 +56,12 @@ Use `DCDclearCache` to clear the DCD server cache.
When the server is running, use `CTRL`+`x` `CTRL`+`o` in a D buffer to use DCD When the server is running, use `CTRL`+`x` `CTRL`+`o` in a D buffer to use DCD
completion. completion.
When the server is running, use the `DCDdoc` to print the doc-string of symbol
under the cursor.
When the server is running, use the `DCDsymbolLocation` to print jump to the
declaration of the symbol under the cursor.
Conflicts Conflicts
========= =========
This plugin conflicts with the DScanner plugin, as both use the `dcomplete` This plugin conflicts with the DScanner plugin, as both use the `dcomplete`

View File

@ -1,6 +1,8 @@
"The completion function "The completion function
function! dcomplete#Complete(findstart,base) function! dcomplete#Complete(findstart,base)
if a:findstart if a:findstart
"Vim temporarily deletes the current identifier from the file
let b:currentLineText=getline('.')
"We might need it for paren completion: "We might need it for paren completion:
let b:closingParenExists=getline('.')[col('.')-1:-1]=~'^\s*)' let b:closingParenExists=getline('.')[col('.')-1:-1]=~'^\s*)'
@ -15,7 +17,7 @@ function! dcomplete#Complete(findstart,base)
let parenPos=searchpos("(","bn",line('.')) let parenPos=searchpos("(","bn",line('.'))
if parenPos[0] if parenPos[0]
if getline('.')[parenPos[1]:col('.')-2]=~'^\s*\w*$' if getline('.')[parenPos[1]:col('.')-2]=~'^\s*\w*$'
let b:completionColumn=parenPos[1] let b:completionColumn=parenPos[1]+1
return parenPos[1] return parenPos[1]
endif endif
endif endif
@ -35,7 +37,10 @@ function! dcomplete#Complete(findstart,base)
else else
let b:base=a:base let b:base=a:base
"Run DCD "Run DCD
let l:prevCurrentLineText=getline('.')
call setline('.',b:currentLineText)
let scanResult=s:runDCDToGetAutocompletion() let scanResult=s:runDCDToGetAutocompletion()
call setline('.',l:prevCurrentLineText)
"Split the result text to lines. "Split the result text to lines.
let resultLines=split(scanResult,"\n") let resultLines=split(scanResult,"\n")
let b:res=resultLines let b:res=resultLines
@ -94,15 +99,48 @@ endfunction
"Run DCD to get autocompletion results "Run DCD to get autocompletion results
function! s:runDCDToGetAutocompletion() function! s:runDCDToGetAutocompletion()
return s:runDCDOnBufferBytePosition(line2byte('.')+b:completionColumn-2,'')
endfunction
"Run DCD on the current position in the buffer
function! dcomplete#runDCDOnCurrentBufferPosition(args)
return s:runDCDOnBufferBytePosition(line2byte('.')+col('.')-1,a:args)
endfunction
"Find where the symbol under the cursor is declared and jump there
function! dcomplete#runDCDtoJumpToSymbolLocation()
let l:scanResult=split(s:runDCDOnBufferBytePosition(line2byte('.')+col('.')-1,'--symbolLocation'),"\n")[0]
let l:resultParts=split(l:scanResult,"\t")
if 2!=len(l:resultParts)
echo 'Not found!'
return
endif
if l:resultParts[0]!='stdin'
execute 'edit '.l:resultParts[0]
endif
let l:symbolByteLocation=str2nr(l:resultParts[1])
if l:symbolByteLocation<1
echo 'Not found!'
return
endif
execute 'goto '.(l:symbolByteLocation+1)
endfunction
"Run DCD on the current buffer with the supplied position
function! s:runDCDOnBufferBytePosition(bytePosition,args)
let l:tmpFileName=tempname() let l:tmpFileName=tempname()
"Save the temp file in unix format for better reading of byte position. "Save the temp file in unix format for better reading of byte position.
let l:oldFileFormat=&fileformat let l:oldFileFormat=&fileformat
set fileformat=unix set fileformat=unix
let l:bytePosition=line2byte('.')+b:completionColumn-1 silent exec "write ".l:tmpFileName
exec "write ".l:tmpFileName
let &fileformat=l:oldFileFormat let &fileformat=l:oldFileFormat
let scanResult=system(dcomplete#DCDclient().' --cursorPos '.l:bytePosition.' <'.shellescape(l:tmpFileName)) let scanResult=system(dcomplete#DCDclient().' '.a:args.' --cursorPos='.a:bytePosition.' <'.shellescape(l:tmpFileName))
if v:shell_error
throw scanResult
endif
call delete(l:tmpFileName) call delete(l:tmpFileName)
return scanResult return scanResult
endfunction endfunction

View File

@ -8,6 +8,10 @@ else
\ ' '.dcomplete#globImportPath([<f-args>]).' > /dev/null &' \ ' '.dcomplete#globImportPath([<f-args>]).' > /dev/null &'
endif endif
command! -buffer -nargs=? DCD execute '!'.dcomplete#DCDclient().' '.<q-args> command! -buffer -nargs=? DCD execute '!'.dcomplete#DCDclient().' '.<q-args>
command! -buffer -nargs=? DCDonCurrentBufferPosition echo dcomplete#runDCDOnCurrentBufferPosition(<q-args>)
command! -buffer DCDstopServer DCD --shutdown command! -buffer DCDstopServer DCD --shutdown
command! -buffer -nargs=+ -complete=dir DCDaddPath execute 'DCD '.dcomplete#globImportPath([<f-args>]) command! -buffer -nargs=+ -complete=dir DCDaddPath execute 'DCD '.dcomplete#globImportPath([<f-args>])
command! -buffer DCDclearCache DCD --clearCache command! -buffer DCDclearCache DCD --clearCache
command! -buffer DCDdoc DCDonCurrentBufferPosition --doc
command! -buffer DCDsymbolLocation call dcomplete#runDCDtoJumpToSymbolLocation()