diff --git a/autoload/phpactor.vim b/autoload/phpactor.vim index 152dd1e522..f72f7349d9 100644 --- a/autoload/phpactor.vim +++ b/autoload/phpactor.vim @@ -314,23 +314,22 @@ endfunction """"""""""""""""""""""" " Utility functions """"""""""""""""""""""" +function! s:isOpenInCurrentWindow(filePath) + return expand('%:p') == a:filePath +endfunction function! phpactor#_switchToBufferOrEdit(filePath) - if expand('%:p') == a:filePath - " filePath is currently open + if s:isOpenInCurrentWindow(a:filePath) return v:false endif let bufferNumber = bufnr(a:filePath . '$') - if (bufferNumber == -1) - exec ":edit " . a:filePath - return v:true - endif - - exec ":buffer " . bufferNumber + let command = (bufferNumber == -1) + \ ? ":edit " . a:filePath + \ : ":buffer " . bufferNumber - return v:true + exec command endfunction function! phpactor#_offset() @@ -434,9 +433,9 @@ function! phpactor#rpc(action, arguments) let result = system(cmd, json_encode(request)) if (v:shell_error == 0) - try + try let response = json_decode(result) - catch + catch throw "Could not parse response from Phpactor: " . v:exception endtry @@ -510,27 +509,17 @@ function! phpactor#_rpc_dispatch(actionName, parameters) if a:actionName == "open_file" let changedFileOrWindow = v:true - if a:parameters['target'] == 'focused_window' - let changedFileOrWindow = phpactor#_switchToBufferOrEdit(a:parameters['path']) - - if a:parameters['force_reload'] == v:true - exec "e!" - endif - endif - - if a:parameters['target'] == 'vsplit' - exec ":vsplit " . a:parameters['path'] - endif - - if a:parameters['target'] == 'hsplit' - exec ":split " . a:parameters['path'] - endif + call s:openFileInSelectedTarget( + \ a:parameters["path"], + \ a:parameters["target"], + \ get(a:parameters, "use_open_window", g:phpactorUseOpenWindows), + \ a:parameters["force_reload"] + \ ) - if a:parameters['target'] == 'new_tab' - exec ":tabnew " . a:parameters['path'] + if a:parameters["target"] == 'focused_window' + let changedFileOrWindow = !s:isOpenInCurrentWindow(a:parameters["path"]) endif - if (a:parameters['offset']) let keepjumps = changedFileOrWindow ? 'keepjumps' : '' @@ -653,6 +642,41 @@ function! phpactor#_rpc_dispatch(actionName, parameters) throw "Do not know how to handle action '" . a:actionName . "'" endfunction +function! s:openFileInSelectedTarget(filePath, target, useOpenWindow, forceReload) + let bufferNumber = bufnr(a:filePath . "$") + if v:true == a:useOpenWindow && -1 != bufferNumber + let firstWindowId = get(win_findbuf(bufferNumber), 0, v:null) + + if v:null != firstWindowId + call win_gotoid(firstWindowId) + return + endif + endif + + if a:target == 'focused_window' + call phpactor#_switchToBufferOrEdit(a:filePath) + if v:true == a:forceReload + exec "e!" + endif + return + endif + + if a:target == 'vsplit' + exec ":vsplit " . a:filePath + return + endif + + if a:target == 'hsplit' + exec ":split " . a:filePath + return + endif + + if a:target == 'new_tab' + exec ":tabnew " . a:filePath + return + endif +endfunction + function! phpactor#_rpc_dispatch_input_handler(Next, parameters, parameterName, result) let a:parameters[a:parameterName] = a:result diff --git a/doc/phpactor.txt b/doc/phpactor.txt index 89e80dbd69..31c1bb5478 100644 --- a/doc/phpactor.txt +++ b/doc/phpactor.txt @@ -44,6 +44,11 @@ use the VIM quick-fix list. Function to use when presenting a user with a choice of options. The default is to use the VIM inputlist. + *g:phpactorUseOpenWindows* +When jump to the line of a file displayed in any existing window reuse this +window to avoid have more than one view of the same file. The default is +false. + ============================================================================== COMPLETION *phpactor-completion* diff --git a/ftplugin/php.vim b/ftplugin/php.vim index 24397340f5..7f7f8ba0a0 100644 --- a/ftplugin/php.vim +++ b/ftplugin/php.vim @@ -9,43 +9,37 @@ let g:phpactorInitialCwd = getcwd() let g:phpactorCompleteLabelTruncateLength=50 let g:_phpactorCompletionMeta = {} -if !exists('g:phpactorPhpBin') - "" - " Path to the PHP binary used by Phpactor - let g:phpactorPhpBin = 'php' -endif - -if !exists('g:phpactorBranch') - "" - " The Phpactor branch to use when calling @command(PhpactorUpdate) - let g:phpactorBranch = 'master' -endif - -if !exists('g:phpactorOmniAutoClassImport') - "" - " Automatically import classes when using VIM native omni-completion - let g:phpactorOmniAutoClassImport = v:true -endif - -if !exists('g:phpactorCompletionIgnoreCase') - "" - " Ignore case when suggestion completion results - let g:phpactorCompletionIgnoreCase = 1 -endif - -if !exists('g:phpactorQuickfixStrategy') - "" - " Function to use when populating a list of code references. The default - " is to use the VIM quick-fix list. - let g:phpactorQuickfixStrategy = 'phpactor#quickfix#vim' -endif - -if !exists('g:phpactorInputListStrategy') - "" - " Function to use when presenting a user with a choice of options. The default - " is to use the VIM inputlist. - let g:phpactorInputListStrategy = 'phpactor#input#list#inputlist' -endif +"" +" Path to the PHP binary used by Phpactor +let g:phpactorPhpBin = get(g:, 'phpactorPhpBin', 'php') + +"" +" The Phpactor branch to use when calling @command(PhpactorUpdate) +let g:phpactorBranch = get(g:, 'phpactorBranch', 'master') + +"" +" Automatically import classes when using VIM native omni-completion +let g:phpactorOmniAutoClassImport = get(g:, 'phpactorOmniAutoClassImport', v:true) + +"" +" Ignore case when suggestion completion results +let g:phpactorCompletionIgnoreCase = get(g:, 'phpactorCompletionIgnoreCase', 1) + +"" +" Function to use when populating a list of code references. The default +" is to use the VIM quick-fix list. +let g:phpactorQuickfixStrategy = get(g:, 'phpactorQuickfixStrategy', 'phpactor#quickfix#vim') + +"" +" Function to use when presenting a user with a choice of options. The default +" is to use the VIM inputlist. +let g:phpactorInputListStrategy = get(g:, 'phpactorInputListStrategy', 'phpactor#input#list#inputlist') + +"" +" When jump to the line of a file displayed in any existing window +" reuse this window to avoid have more than one view of the same file. +" The default is false. +let g:phpactorUseOpenWindows = get(g:, 'phpactorUseOpenWindows', v:false) if g:phpactorOmniAutoClassImport == v:true autocmd CompleteDone *.php call phpactor#_completeImportClass(v:completed_item) @@ -53,4 +47,3 @@ endif " vim: et ts=4 sw=4 fdm=marker -