Skip to content

Commit

Permalink
WIP: Provide server with plugin (as an option) (#19)
Browse files Browse the repository at this point in the history
Add padawan.php server to the plugin.
  • Loading branch information
pbogut committed Apr 29, 2017
1 parent 2cd82d9 commit 3fdd595
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 22 deletions.
48 changes: 41 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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` |
Expand All @@ -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
Expand Down Expand Up @@ -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(<bang>0)
```

## Compatibility with other plugins
Expand Down
62 changes: 61 additions & 1 deletion autoload/deoplete/sources/padawan.vim
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,28 @@ if (get(g:, 'deoplete#sources#padawan#loaded', 0))
finish
endif

let s:plugin_directory = expand('<sfile>: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('<sfile>:p:h:h:h:h') . '/rplugin/python3/deoplete/sources/deoplete_padawan'

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)
Expand All @@ -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
Expand All @@ -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()
Expand All @@ -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
6 changes: 6 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"minimum-stability": "dev",
"require": {
"mkusher/padawan": "dev-master"
}
}
18 changes: 4 additions & 14 deletions rplugin/python3/deoplete/sources/deoplete_padawan.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
sys.path.insert(1, path.dirname(__file__) + '/deoplete_padawan')

import padawan_server # noqa
import padawan_helper # noqa


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']
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 3fdd595

Please sign in to comment.