Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions autoload/phpactor.vim
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,25 @@ function! phpactor#GenerateAccessors()
call phpactor#rpc("generate_accessor", { "source": phpactor#_source(), "path": expand('%:p'), 'offset': phpactor#_offset() })
endfunction

let s:indexerJobId = v:null
function! phpactor#IndexStart()
let s:indexerJobId = phpactor#nvim#asyncCall("index", {"watch": v:true})

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Abstract the VIM/Neovim job control

if s:indexerJobId == v:null
return
endif

echo "Indexer starated with job ID " . s:indexerJobId
endfunction

function! phpactor#IndexStop()
if s:indexerJobId == v:null
echo "Indexer not running"
return
endif

call jobstop(s:indexerJobId)
echo "Indexer stopped"
endfunction
"""""""""""""""""""""""
" Utility functions
"""""""""""""""""""""""
Expand Down Expand Up @@ -434,16 +453,8 @@ function! phpactor#rpc(action, arguments)
let result = system(cmd, json_encode(request))

if (v:shell_error == 0)
try
let response = json_decode(result)
catch
throw "Could not parse response from Phpactor: " . v:exception
endtry

let actionName = response['action']
let parameters = response['parameters']

let response = phpactor#_rpc_dispatch(actionName, parameters)
let response = phpactor#rpc#handleRawResponse(result)

if !empty(response)
return response
Expand Down
49 changes: 49 additions & 0 deletions autoload/phpactor/nvim.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
let s:lock = v:null

func! phpactor#nvim#asyncCall(action, arguments)
" TODO: Include either the original "action" in the response or a request
" ID (probably better to include both)
if s:lock != v:null
echo "An asynchronous RPC '" . s:lock . "' action is already running"
return
endif

let s:lock = a:action
let callbacks = {
\ 'on_stdout': function('phpactor#nvim#asyncHandle'),
\ 'on_stderr': function('phpactor#nvim#asyncHandle'),
\ 'on_exit': function('phpactor#nvim#asyncHandle')
\ }
let job = jobstart([ g:phpactorPhpBin, g:phpactorbinpath, 'rpc', '--working-dir=' . g:phpactorInitialCwd ], callbacks)

let request = { "action": a:action, "parameters": a:arguments }

call chansend(job, json_encode(request))
call chanclose(job, 'stdin')

return job
endfunc

let s:stdout = []

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also still have no idea how to catch the response without using non-local scope (like the above).

What do you think about creating a RPC server between vim and one-time phpactor commands? PHPCD mentioned already somewhere had a such solution.

I did not tested behaviour of vim-specific ch_evalraw.

@dantleech dantleech Mar 16, 2020 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One idea of this PR was to allow messages to be sent from Phpactor during a command's execution (i.e. to send progress notifications from the Indexer).

I'm not sure there are benefits to having an 2-way RPC connection. I can imagine a use-case would be to cancel an operation - and in that case sending a SIGTERM from VIM should be enough?

@dantleech dantleech Mar 16, 2020 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But in anycase it's worth concentrating on the language server solution for now - we can support most of Phpactor's operations (even refactoring) over LSP and it if it works well, it could replace the Phpactor VIM client one day.

Although it's still worth refactoring to use the jobstart / VIM equivalent mechanism rather than system

let s:stderr = []

func! phpactor#nvim#asyncHandle(jobId, data, event)
if a:event == 'stdout'
call extend(s:stdout, a:data)
return
elseif a:event == 'stderr'
call extend(s:stderr, a:data)
return
endif

if a:data != 0
echo "Phpactor returned an error: " . join(s:stderr)
return
endif

let s:lock = v:null
call phpactor#rpc#handleRawResponse(join(s:stdout))

let s:stdout = []
let s:stderr = []
endfunc
16 changes: 16 additions & 0 deletions autoload/phpactor/rpc.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
func! phpactor#rpc#handleRawResponse(response)
if "" == a:response
return v:null
endif

try
let response = json_decode(a:response)
catch
throw "Could not parse response from Phpactor: " . v:exception
endtry

let actionName = response['action']
let parameters = response['parameters']

return phpactor#_rpc_dispatch(actionName, parameters)
endfunc
4 changes: 0 additions & 4 deletions ftplugin/php.vim
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,3 @@ endif
if g:phpactorOmniAutoClassImport == v:true
autocmd CompleteDone *.php call phpactor#_completeImportClass(v:completed_item)
endif


" vim: et ts=4 sw=4 fdm=marker

5 changes: 5 additions & 0 deletions ftplugin/php/commands.vim
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,8 @@ command! -nargs=0 PhpactorChangeVisibility call phpactor#ChangeVisibility()
""
" Generate accessors for the current class
command! -nargs=0 PhpactorGenerateAccessors call phpactor#GenerateAccessors()

""
" Generate accessors for the current class
command! -nargs=0 PhpactorIndexStart call phpactor#IndexStart()

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove spurious doc

command! -nargs=0 PhpactorIndexStop call phpactor#IndexStop()