diff --git a/README.md b/README.md index 4510692..50c125a 100644 --- a/README.md +++ b/README.md @@ -21,13 +21,24 @@ It requires [neovim](https://github.com/neovim/neovim) as deoplete requires it. You need to install [padawan.php](https://github.com/mkusher/padawan.php) and index your project. The plugin requires padawan.php server running to work. -Go to project GitHub page for details. + +To install server within the plugin directory you can run +``` +`:call deoplete#sources#padawan#InstallServer()` +``` +It will install the padawan.php server in the plugin directory. +To update server run +``` +`:call deoplete#sources#padawan#UpdateServer()` +``` +Composer needs to be in your system path in order to install or update server. + Using [vim-plug](https://github.com/junegunn/vim-plug): ```vim Plug 'Shougo/deoplete.nvim' -Plug 'padawan-php/deoplete-padawan' +Plug 'padawan-php/deoplete-padawan', { 'do': 'composer install' } ``` Using [Vundle](https://github.com/VundleVim/Vundle.vim): @@ -48,6 +59,7 @@ options. |:----------------------------------------------|:--------------------------| | `g:deoplete#sources#padawan#server_addr` | `http://127.0.0.1:15155` | | `g:deoplete#sources#padawan#server_command` | `padawan-server` | +| `g:deoplete#sources#padawan#composer_command` | `composer` | | `g:deoplete#sources#padawan#log_file` | `/tmp/padawan-server.log` | | `g:deoplete#sources#padawan#server_autostart` | `1` | | `g:deoplete#sources#padawan#add_parentheses` | `0` | @@ -62,6 +74,12 @@ Address to padawan.php server. By default, it is `http://127.0.0.1:15155` If your padawan-server bin is not in $PATH then you can set up this to point it directly, ie: `/path/to/padawan/bin/padawan-server`. +- `g:deoplete#sources#padawan#composer_command` + +Composer is used to install and update the padawan.php server in the plugin +directory. If `composer` is not in your system path you can set full command +directly, ie: `php ~/bin/composer.phar`. + - `g:deoplete#sources#padawan#log_file` Padawan.php log file path, if empty log won't be stored anywhere. By default, it goes @@ -98,16 +116,32 @@ Will kill padawan-server. Will kill padawan-server and start it again. +- `call deoplete#sources#padawan#InstallServer()` + +Will install padawan.php server. + +- `call deoplete#sources#padawan#UpdateServer()` + +Will update padawan.php server. + +- `call deoplete#sources#padawan#Generate()` + +Will generate index file for current project. Command will run as neovim job, +if you would like to see the output you can pass 1 as an argument to this +function. + ### Custom commands If you would like to have simpler commands, you can add them to your -`vimrc` file. Snippet below shows how to add `StartPadawan`, `StopPadawan` and -`RestartPadawan` commands. +`vimrc` file. Snippet below shows how to add commands. ```vim -command! StartPadawan call deoplete#sources#padawan#StartServer() -command! StopPadawan call deoplete#sources#padawan#StopServer() -command! RestartPadawan call deoplete#sources#padawan#RestartServer() +command! PadawanStart call deoplete#sources#padawan#StartServer() +command! PadawanStop call deoplete#sources#padawan#StopServer() +command! PadawanRestart call deoplete#sources#padawan#RestartServer() +command! PadawanInstall call deoplete#sources#padawan#InstallServer() +command! PadawanUpdate call deoplete#sources#padawan#UpdatePadawan() +command! -bang PadawanGenerate call deoplete#sources#padawan#Generate(0) ``` ## Compatibility with other plugins diff --git a/autoload/deoplete/sources/padawan.vim b/autoload/deoplete/sources/padawan.vim index 9343657..7a5f921 100644 --- a/autoload/deoplete/sources/padawan.vim +++ b/autoload/deoplete/sources/padawan.vim @@ -4,6 +4,14 @@ if (get(g:, 'deoplete#sources#padawan#loaded', 0)) finish endif +let s:plugin_directory = expand(':p:h:h:h:h') +let s:server_command = s:plugin_directory . '/vendor/bin/padawan-server' +let s:padawan_command = s:plugin_directory . '/vendor/bin/padawan' +if !executable(s:server_command) || !executable(s:padawan_command) + let s:server_command = 'padawan-server' + let s:padawan_command = 'padawan' +endif + let g:deoplete#sources#padawan#loaded = 1 let lib_path = expand(':p:h:h:h:h') . '/rplugin/python3/deoplete/sources/deoplete_padawan' @@ -11,9 +19,13 @@ let lib_path = expand(':p:h:h:h:h') . '/rplugin/python3/deoplete/sources/ let g:deoplete#sources#padawan#server_addr = \ get(g:, 'deoplete#sources#padawan#server_addr', 'http://127.0.0.1:15155') let g:deoplete#sources#padawan#server_command = - \ get(g:, 'deoplete#sources#padawan#server_command', 'padawan-server') + \ get(g:, 'deoplete#sources#padawan#server_command', s:server_command) +let g:deoplete#sources#padawan#padawan_command = + \ get(g:, 'deoplete#sources#padawan#padawan_command', s:padawan_command) let g:deoplete#sources#padawan#log_file = \ get(g:, 'deoplete#sources#padawan#log_file', '/tmp/padawan-server.log') +let g:deoplete#sources#padawan#composer_command = + \ get(g:, 'deoplete#sources#padawan#composer_command', 'composer') let g:deoplete#sources#padawan#server_autostart = \ get(g:, 'deoplete#sources#padawan#server_autostart', 1) @@ -22,6 +34,7 @@ let g:deoplete#sources#padawan#add_parentheses = let g:deoplete#sources#padawan#auto_update = \ get(g:, 'deoplete#sources#padawan#auto_update', 0) + python3 << PYTHON import vim import sys @@ -31,14 +44,26 @@ lib_path = vim.eval('lib_path') sys.path.insert(0, os.path.join(lib_path)) import padawan_server +import padawan_helper server_addr = vim.eval('g:deoplete#sources#padawan#server_addr') server_command = vim.eval('g:deoplete#sources#padawan#server_command') log_file = vim.eval('g:deoplete#sources#padawan#log_file') _padawan_server = padawan_server.Server(server_addr, server_command, log_file) +_padawan_helper = padawan_helper.Helper() PYTHON +function! deoplete#sources#padawan#InstallServer() + let l:composer = g:deoplete#sources#padawan#composer_command + exec "!cd " . s:plugin_directory . " && " . l:composer . " install" +endfunction + +function! deoplete#sources#padawan#UpdateServer() + let l:composer = g:deoplete#sources#padawan#composer_command + exec "!cd " . s:plugin_directory . " && " . l:composer . " update" +endfunction + function! deoplete#sources#padawan#StartServer() " @todo - add some feedback with information if started python3 _padawan_server.start() @@ -53,3 +78,38 @@ function! deoplete#sources#padawan#RestartServer() " @todo - add some feedback with information if restarted python3 _padawan_server.restart() endfunction + +function! deoplete#sources#padawan#Generate(...) + if empty(get(b:, 'padawan_project_root', 0)) +python3 << PYTHON +file_name = vim.eval('expand("%:p")') +vim.command("let b:padawan_project_root = '{}'".format( + _padawan_helper.get_project_root(file_name)) +) +PYTHON + endif + if confirm("Are you sure you want to generate index in " + \. b:padawan_project_root . "?", "&Yes\n&No", 2) == 1 + let cmd = "cd " . b:padawan_project_root . " && " + \. g:deoplete#sources#padawan#padawan_command . " generate" + if get(a:, 1, 0) + exec "!" . l:cmd + else + call jobstart(l:cmd, { + \'on_exit': function('s:generate_exit'), + \'on_stdout': function('s:generate_stdout')}) + endif + endif +endfunction + +function! s:generate_stdout(id, out, ...) + for l:line in a:out + if l:line =~ 'Progress:' + echo l:line + endif + endfor +endfunction + +function! s:generate_exit(...) + echo "Padawan.php: Index generating has finished!" +endfunction diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..cc8344d --- /dev/null +++ b/composer.json @@ -0,0 +1,6 @@ +{ + "minimum-stability": "dev", + "require": { + "mkusher/padawan": "dev-master" + } +} diff --git a/rplugin/python3/deoplete/sources/deoplete_padawan.py b/rplugin/python3/deoplete/sources/deoplete_padawan.py index 897cb77..8d5fce1 100644 --- a/rplugin/python3/deoplete/sources/deoplete_padawan.py +++ b/rplugin/python3/deoplete/sources/deoplete_padawan.py @@ -16,6 +16,7 @@ sys.path.insert(1, path.dirname(__file__) + '/deoplete_padawan') import padawan_server # noqa +import padawan_helper # noqa class Source(Base): @@ -23,6 +24,7 @@ class Source(Base): def __init__(self, vim): Base.__init__(self, vim) + self.helper = padawan_helper.Helper() self.name = 'padawan' self.mark = '[padawan]' self.filetypes = ['php'] @@ -50,7 +52,7 @@ def on_init(self, context): def on_event(self, context): if (context['event'] == 'BufWritePost' and self.auto_update == 1): file_path = self.current.buffer.name - current_path = self.get_project_root(file_path) + current_path = self.helper.get_project_root(file_path) params = { 'path': current_path } @@ -89,7 +91,7 @@ def get_patterns_position(self, context, patterns): def gather_candidates(self, context): file_path = self.current.buffer.name - current_path = self.get_project_root(file_path) + current_path = self.helper.get_project_root(file_path) [line_num, _] = self.current.window.cursor column_num = self.get_padawan_column(context) @@ -168,15 +170,3 @@ def do_request(self, command, params, data=''): self.vim.command("echom 'Padawan.php error: {}'".format(error)) # any other error can bouble to deoplete return False - - def get_project_root(self, file_path): - current_path = path.dirname(file_path) - while current_path != '/' and not path.exists( - path.join(current_path, 'composer.json') - ): - current_path = path.dirname(current_path) - - if current_path == '/': - current_path = path.dirname(file_path) - - return current_path diff --git a/rplugin/python3/deoplete/sources/deoplete_padawan/padawan_helper.py b/rplugin/python3/deoplete/sources/deoplete_padawan/padawan_helper.py new file mode 100644 index 0000000..b5427c6 --- /dev/null +++ b/rplugin/python3/deoplete/sources/deoplete_padawan/padawan_helper.py @@ -0,0 +1,20 @@ +# ============================================================================= +# FILE: padawan_helper.py +# AUTHOR: Pawel Bogut +# ============================================================================= +from os import path + + +class Helper: + + def get_project_root(self, file_path): + current_path = path.dirname(file_path) + while current_path != '/' and not path.exists( + path.join(current_path, 'composer.json') + ): + current_path = path.dirname(current_path) + + if current_path == '/': + current_path = path.dirname(file_path) + + return current_path