diff --git a/autoload/phpactor.vim b/autoload/phpactor.vim index 44e2712df1..5344e3a143 100644 --- a/autoload/phpactor.vim +++ b/autoload/phpactor.vim @@ -327,7 +327,7 @@ endfunction " Utility functions """"""""""""""""""""""" function! s:isOpenInCurrentWindow(filePath) - return expand('%:p') == a:filePath + return phpactor#_path() == a:filePath endfunction function! phpactor#_switchToBufferOrEdit(filePath) @@ -353,7 +353,15 @@ function! phpactor#_source() endfunction function! phpactor#_path() - return expand('%:p') + let l:path = expand('%:p') + + if filereadable(l:path) || stridx(l:path, '/') == 0 + return l:path + endif + + " todo if empty path + " + return printf('%s/%s', g:phpactorInitialCwd, l:path) endfunction function! s:getStartOffsetFromMark(mark, linewise) @@ -435,14 +443,53 @@ endfunction " RPC -->-->-->-->-->-- """"""""""""""""""""""" +function! s:searchDirectoryUpwardForRootPatterns(initialDirectory, workspaceRootPatterns, fallbackDirectory) + if index(g:phpactorGlobalRootPatterns, '/') < 0 + call add(g:phpactorGlobalRootPatterns, '/') + endif + + let l:directory = a:initialDirectory + + while index(g:phpactorGlobalRootPatterns, l:directory) < 0 + if s:directoryMatchesToPatterns(l:directory, a:workspaceRootPatterns) + return l:directory + endif + + let l:directory = fnamemodify(l:directory, ':h') + endwhile + + if index(g:phpactorGlobalRootPatterns, l:directory) >= 0 + let l:directory = a:fallbackDirectory + endif + + return l:directory +endfunction + +function s:directoryMatchesToPatterns(directory, patterns) abort + for l:pattern in a:patterns + if (filereadable(a:directory .'/'. l:pattern)) + return v:true + endif + endfor + + return v:false +endfunction + function! phpactor#rpc(action, arguments) " Remove any existing output in the message window execute ':redraw' let request = { "action": a:action, "parameters": a:arguments } - let cmd = g:phpactorPhpBin . ' ' . g:phpactorbinpath . ' rpc --working-dir=' . g:phpactorInitialCwd - let result = system(cmd, json_encode(request)) + let l:workspaceDir = s:searchDirectoryUpwardForRootPatterns( + \ fnamemodify(phpactor#_path(), ':h'), + \ g:phpactorProjectRootPatterns, + \ g:phpactorInitialCwd + \) + + let l:cmd = g:phpactorPhpBin . ' ' . g:phpactorbinpath . ' rpc --working-dir=' . l:workspaceDir + + let result = system(l:cmd, json_encode(request)) if (v:shell_error == 0) try diff --git a/composer.lock b/composer.lock index 6c0270b205..4373c6ffa3 100644 --- a/composer.lock +++ b/composer.lock @@ -1916,12 +1916,12 @@ "source": { "type": "git", "url": "https://github.com/phpactor/source-code-filesystem.git", - "reference": "72f53091d3692324ccde5d98cd9b666963884c9f" + "reference": "b1e11fdf14053becb37795f01bc5b9b48ea5871e" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpactor/source-code-filesystem/zipball/72f53091d3692324ccde5d98cd9b666963884c9f", - "reference": "72f53091d3692324ccde5d98cd9b666963884c9f", + "url": "https://api.github.com/repos/phpactor/source-code-filesystem/zipball/b1e11fdf14053becb37795f01bc5b9b48ea5871e", + "reference": "b1e11fdf14053becb37795f01bc5b9b48ea5871e", "shasum": "" }, "require": { @@ -1955,7 +1955,7 @@ } ], "description": "Filesystem library for working with source code files", - "time": "2019-12-04T19:21:48+00:00" + "time": "2020-03-18T11:45:50+00:00" }, { "name": "phpactor/source-code-filesystem-extension", @@ -5789,5 +5789,6 @@ "platform-dev": [], "platform-overrides": { "php": "7.2.5" - } + }, + "plugin-api-version": "1.1.0" } diff --git a/doc/phpactor.txt b/doc/phpactor.txt index bd7a3ee279..d74c85fef7 100644 --- a/doc/phpactor.txt +++ b/doc/phpactor.txt @@ -48,6 +48,13 @@ is to use the VIM inputlist. When jumping to a file location: if the target file open in a window, switch to that window instead of switching buffers. The default is false. + *g:phpactorProjectRootPatterns* +The list of files that determine workspace root directory if contained within + + *g:phpactorGlobalRootPatterns* +The list of directories that should not be considered as workspace root +directory (in addition to '/' which is always considered) + ============================================================================== COMPLETION *phpactor-completion* diff --git a/ftplugin/php.vim b/ftplugin/php.vim index 3e4e81ffaa..65cae72242 100644 --- a/ftplugin/php.vim +++ b/ftplugin/php.vim @@ -40,6 +40,16 @@ let g:phpactorInputListStrategy = get(g:, 'phpactorInputListStrategy', 'phpactor " to that window instead of switching buffers. The default is false. let g:phpactorUseOpenWindows = get(g:, 'phpactorUseOpenWindows', v:false) +"" +" The list of files that determine workspace root directory +" if contained within +let g:phpactorProjectRootPatterns = get(g:, 'phpactorProjectRootPatterns', ['composer.json', '.git', '.phpactor.json', '.phpactor.yml']) + +"" +" The list of directories that should not be considered as workspace root directory +" (in addition to '/' which is always considered) +let g:phpactorGlobalRootPatterns = get(g:, 'phpactorGlobalRootPatterns', ['/', '/home']) + if g:phpactorOmniAutoClassImport == v:true autocmd CompleteDone *.php call phpactor#_completeImportClass(v:completed_item) endif