phpactor / phpactor

Mainly a PHP Language Server with more features than you can shake a stick at
MIT License
1.39k stars 128 forks source link

vim plugin not working with option `set autochdir` or when launching vim in a subfolder #1085

Open allan-simon opened 4 years ago

allan-simon commented 4 years ago

I'm using neovim and I have in my init.vim the line set autochdir , and it seems to not play nice with phpactor

is it possible for phpactor to try to find in the parent folders if there's a .git folder or composer file in order to choose the root of the project (like many other command line utilities do ) ?

dantleech commented 4 years ago

Phpactor doesn't support this directly but you can provide an anonymous function to detect the project root by g:PhpactorRootDirectoryStrategy (:help phpactor or https://phpactor.readthedocs.io/en/develop/vim-plugin/man.html).

Maybe @przepompownia or @elythyr can recommend a way to use this in conjunction with a root-detection strategy/plugin?

allan-simon commented 4 years ago

@dantleech thanks for the quick answer, as I'm not familiar with anonymous function in vimscript, do you have a quick example of a dirty workaround that would just return always the same root path ?

dantleech commented 4 years ago

Me neither in complete honesty, :help Funcref

I assume something like g:PhpactorRootDirectoryStrategy = function () { return getcwd() } for example.

allan-simon commented 4 years ago

ok thanks, I've seen the "expr-lambda" help but adding in my init.vim

let g:PhpactorRootDirectoryStrategy = {-> '/home/allan/myproject' }

does not seem to work

:Phpactorstatus still return the same working directory :(

dantleech commented 4 years ago

You'd need to restart VIM of course ?

allan-simon commented 4 years ago

haha, I did :) but still does not seem to works

przepompownia commented 4 years ago

@dantleech restarting is not needed. The function is executed each time we call to RPC. @allan-simon do you have the version that uses let g:PhpactorRootDirectoryStrategy? :Phpactorstatus or :checkhealth should print both the version and the working directory.

przepompownia commented 4 years ago

One detail: if you have set the buffer equivalent (b:PhpactorRootDirectoryStrategy) then it takes precedence over the global.

I noticed that we have this possibility undocumented.

Phpactor does not set this buffer variable itself.

allan-simon commented 4 years ago
Version: 111c3e6 (4 days ago) 0.16.1-1083
Filesystems: simple
Working directory: /vagrant/src

I've tripled-checked , I have only g:PhpactorRootDirectoryStrategy defined

Here's my init.vim

set mouse=a
" workaround https://github.com/neovim/neovim/issues/6041
set guicursor=
"
set tabstop=4
set softtabstop=4
set shiftwidth=4
set smarttab

set expandtab
set smartindent

set wildmode=longest,full

set autochdir

set showcmd

colorscheme torte

"the status bar is always displayed
set laststatus=2
if has("statusline")
    set statusline=%<%f%h%m%r%=%l,%c\ %P
elseif has("cmdline_info")
    set ruler " display cursor position
endif

" Specify a directory for plugins
" - For Neovim: ~/.local/share/nvim/plugged
" - Avoid using standard Vim directory names like 'plugin'
call plug#begin('~/.local/share/nvim/plugged')

" for autocompletion

Plug 'Shougo/deoplete.nvim', { 'do': ':UpdateRemotePlugins' }
" for fuzzy completion
Plug 'Shougo/denite.nvim'
Plug 'Shougo/echodoc.vim'
" to display in red extra whitespaces
Plug 'ntpeters/vim-better-whitespace'

Plug 'benekastah/neomake'

Plug 'evidens/vim-twig'
Plug 'phpactor/phpactor', {'for': 'php', 'branch': 'master', 'do': 'composer install --no-dev -o'}
Plug 'kristijanhusak/deoplete-phpactor'

" Initialize plugin system
call plug#end()

" for language server

" used by deoplete
call deoplete#custom#option('ignore_sources', {'php': ['omni']})

let g:PhpactorRootDirectoryStrategy = {-> '/vagrant' }
przepompownia commented 4 years ago

One low-level test (maybe to include in checkhealth in another form):

:echo printf('the type should be %s, is %s', type(function('type')), type(g:PhpactorRootDirectoryStrategy))
allan-simon commented 4 years ago

I got the type should be 2, is 2

przepompownia commented 4 years ago

OK, I reproduced this behaviour on Vim (as opposed to nvim). It seems that calling to RPC behaves with a correctly selected working directory, but we have a bug in status reporting. For test, please display the result of the function directly used for RPC call:

echo phpactor#getRootDirectory()
przepompownia commented 4 years ago

@dantleech is possible that the directories: this given by --working-dir and this resolved by '%project_root%' can differ?

przepompownia commented 4 years ago

But why would it depend on the caller...

I echoed (on Vim) the final command used to RPC call and it have the directory chosen by the strategy.

allan-simon commented 4 years ago
echo phpactor#getRootDirectory()

->

/vagrant

i.e it displays (correctly?) the value defined there

let g:PhpactorRootDirectoryStrategy = {-> '/vagrant' }

(I've tried changing the value to be 100% sure)

dantleech commented 4 years ago

is possible that the directories: this given by --working-dir and this resolved by '%project_root%' can differ?

yes when using the language server (language client has authority), no otherwise.

przepompownia commented 4 years ago

phpactor#Status() executes phpactor status without --working-dir. The fallback presentation method

phpactor#rpc("status", {'type': 'formatted'})

works correctly but usually (no :terminal support) is not reached.

dantleech commented 4 years ago

ah, yeah, i think we should revert that fix

camilledejoye commented 3 years ago

That was fixed since, @allan-simon can you confirm ? Otherwise we can close.

allan-simon commented 3 years ago

I will double check that .

allan-simon commented 3 years ago
Version: 3324205 (12 days ago) Do-not-overwrite-stub_paths-config-if-key-exists-1122
Filesystems: simple     
Working directory: /vagrant/src/Command

Config files (missing is not bad):

  ✘ /home/vagrant/.config/phpactor/phpactor.json
  ✘ /home/vagrant/.config/phpactor/phpactor.yml
  ✘ /vagrant/src/Command/.phpactor.json           
  ✘ /vagrant/src/Command/.phpactor.yml             

Diagnostics:                                         

  ✔ XDebug is disabled. XDebug has a negative effect on performance.
  ✘ Composer not found - some functionality will not be available (e.g. class creation) and class location will fallback to scanning the filesystem if index not enabled - this can be slow. Make sure you've run `
composer install` in your project!
  ✘ Git not detected. Some operations which would have been better scoped to your project repository will now include vendor paths.

[Process exited 0]                

(when I run PhpactorStatus , with or without putting let g:PhpactorRootDirectoryStrategy = {-> '/vagrant' } at the end of my init.vim )

However phpactor seems to work with Coc.vim as i have autocompletion working

camilledejoye commented 3 years ago

OK, I just realized that I forgot to pass the working directory when calling the status command >< Will try to fix that soon, but it should also only mean that the Working directory information is invalid in the result of this command.

However phpactor seems to work with Coc.vim as i have autocompletion working The LSP client is responsible for provided the project root, CoC in your case, so it should indeed works without issue in this case.

camilledejoye commented 3 years ago

https://github.com/phpactor/phpactor/pull/1127

I tested locally by starting vim from a sub-directory, the Working directory has the right value. Then I realized we didn't used it when looking for possible config files at the root of the project, so I fixed that too.

But I don't know what else to test, you said that the status was not working properly (and you were right) but I don't think it should prevent phpactor to work with RPC.

I hope I did it right this time :smile:

allan-simon commented 3 years ago

ok make sense ^^ , thanks for your hardwork