Skip to content

Commit 3fdd595

Browse files
authored
WIP: Provide server with plugin (as an option) (#19)
Add padawan.php server to the plugin.
1 parent 2cd82d9 commit 3fdd595

File tree

5 files changed

+132
-22
lines changed

5 files changed

+132
-22
lines changed

README.md

+41-7
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,24 @@ It requires [neovim](https://github.com/neovim/neovim) as deoplete requires it.
2121

2222
You need to install [padawan.php](https://github.com/mkusher/padawan.php) and
2323
index your project. The plugin requires padawan.php server running to work.
24-
Go to project GitHub page for details.
24+
25+
To install server within the plugin directory you can run
26+
```
27+
`:call deoplete#sources#padawan#InstallServer()`
28+
```
29+
It will install the padawan.php server in the plugin directory.
30+
To update server run
31+
```
32+
`:call deoplete#sources#padawan#UpdateServer()`
33+
```
34+
Composer needs to be in your system path in order to install or update server.
35+
2536

2637
Using [vim-plug](https://github.com/junegunn/vim-plug):
2738
```vim
2839
Plug 'Shougo/deoplete.nvim'
2940
30-
Plug 'padawan-php/deoplete-padawan'
41+
Plug 'padawan-php/deoplete-padawan', { 'do': 'composer install' }
3142
```
3243

3344
Using [Vundle](https://github.com/VundleVim/Vundle.vim):
@@ -48,6 +59,7 @@ options.
4859
|:----------------------------------------------|:--------------------------|
4960
| `g:deoplete#sources#padawan#server_addr` | `http://127.0.0.1:15155` |
5061
| `g:deoplete#sources#padawan#server_command` | `padawan-server` |
62+
| `g:deoplete#sources#padawan#composer_command` | `composer` |
5163
| `g:deoplete#sources#padawan#log_file` | `/tmp/padawan-server.log` |
5264
| `g:deoplete#sources#padawan#server_autostart` | `1` |
5365
| `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`
6274
If your padawan-server bin is not in $PATH then you can set up this
6375
to point it directly, ie: `/path/to/padawan/bin/padawan-server`.
6476

77+
- `g:deoplete#sources#padawan#composer_command`
78+
79+
Composer is used to install and update the padawan.php server in the plugin
80+
directory. If `composer` is not in your system path you can set full command
81+
directly, ie: `php ~/bin/composer.phar`.
82+
6583
- `g:deoplete#sources#padawan#log_file`
6684

6785
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.
98116

99117
Will kill padawan-server and start it again.
100118

119+
- `call deoplete#sources#padawan#InstallServer()`
120+
121+
Will install padawan.php server.
122+
123+
- `call deoplete#sources#padawan#UpdateServer()`
124+
125+
Will update padawan.php server.
126+
127+
- `call deoplete#sources#padawan#Generate()`
128+
129+
Will generate index file for current project. Command will run as neovim job,
130+
if you would like to see the output you can pass 1 as an argument to this
131+
function.
132+
101133
### Custom commands
102134

103135
If you would like to have simpler commands, you can add them to your
104-
`vimrc` file. Snippet below shows how to add `StartPadawan`, `StopPadawan` and
105-
`RestartPadawan` commands.
136+
`vimrc` file. Snippet below shows how to add commands.
106137

107138
```vim
108-
command! StartPadawan call deoplete#sources#padawan#StartServer()
109-
command! StopPadawan call deoplete#sources#padawan#StopServer()
110-
command! RestartPadawan call deoplete#sources#padawan#RestartServer()
139+
command! PadawanStart call deoplete#sources#padawan#StartServer()
140+
command! PadawanStop call deoplete#sources#padawan#StopServer()
141+
command! PadawanRestart call deoplete#sources#padawan#RestartServer()
142+
command! PadawanInstall call deoplete#sources#padawan#InstallServer()
143+
command! PadawanUpdate call deoplete#sources#padawan#UpdatePadawan()
144+
command! -bang PadawanGenerate call deoplete#sources#padawan#Generate(<bang>0)
111145
```
112146

113147
## Compatibility with other plugins

autoload/deoplete/sources/padawan.vim

+61-1
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,28 @@ if (get(g:, 'deoplete#sources#padawan#loaded', 0))
44
finish
55
endif
66

7+
let s:plugin_directory = expand('<sfile>:p:h:h:h:h')
8+
let s:server_command = s:plugin_directory . '/vendor/bin/padawan-server'
9+
let s:padawan_command = s:plugin_directory . '/vendor/bin/padawan'
10+
if !executable(s:server_command) || !executable(s:padawan_command)
11+
let s:server_command = 'padawan-server'
12+
let s:padawan_command = 'padawan'
13+
endif
14+
715
let g:deoplete#sources#padawan#loaded = 1
816

917
let lib_path = expand('<sfile>:p:h:h:h:h') . '/rplugin/python3/deoplete/sources/deoplete_padawan'
1018

1119
let g:deoplete#sources#padawan#server_addr =
1220
\ get(g:, 'deoplete#sources#padawan#server_addr', 'http://127.0.0.1:15155')
1321
let g:deoplete#sources#padawan#server_command =
14-
\ get(g:, 'deoplete#sources#padawan#server_command', 'padawan-server')
22+
\ get(g:, 'deoplete#sources#padawan#server_command', s:server_command)
23+
let g:deoplete#sources#padawan#padawan_command =
24+
\ get(g:, 'deoplete#sources#padawan#padawan_command', s:padawan_command)
1525
let g:deoplete#sources#padawan#log_file =
1626
\ get(g:, 'deoplete#sources#padawan#log_file', '/tmp/padawan-server.log')
27+
let g:deoplete#sources#padawan#composer_command =
28+
\ get(g:, 'deoplete#sources#padawan#composer_command', 'composer')
1729

1830
let g:deoplete#sources#padawan#server_autostart =
1931
\ get(g:, 'deoplete#sources#padawan#server_autostart', 1)
@@ -22,6 +34,7 @@ let g:deoplete#sources#padawan#add_parentheses =
2234
let g:deoplete#sources#padawan#auto_update =
2335
\ get(g:, 'deoplete#sources#padawan#auto_update', 0)
2436

37+
2538
python3 << PYTHON
2639
import vim
2740
import sys
@@ -31,14 +44,26 @@ lib_path = vim.eval('lib_path')
3144
sys.path.insert(0, os.path.join(lib_path))
3245

3346
import padawan_server
47+
import padawan_helper
3448

3549
server_addr = vim.eval('g:deoplete#sources#padawan#server_addr')
3650
server_command = vim.eval('g:deoplete#sources#padawan#server_command')
3751
log_file = vim.eval('g:deoplete#sources#padawan#log_file')
3852

3953
_padawan_server = padawan_server.Server(server_addr, server_command, log_file)
54+
_padawan_helper = padawan_helper.Helper()
4055
PYTHON
4156

57+
function! deoplete#sources#padawan#InstallServer()
58+
let l:composer = g:deoplete#sources#padawan#composer_command
59+
exec "!cd " . s:plugin_directory . " && " . l:composer . " install"
60+
endfunction
61+
62+
function! deoplete#sources#padawan#UpdateServer()
63+
let l:composer = g:deoplete#sources#padawan#composer_command
64+
exec "!cd " . s:plugin_directory . " && " . l:composer . " update"
65+
endfunction
66+
4267
function! deoplete#sources#padawan#StartServer()
4368
" @todo - add some feedback with information if started
4469
python3 _padawan_server.start()
@@ -53,3 +78,38 @@ function! deoplete#sources#padawan#RestartServer()
5378
" @todo - add some feedback with information if restarted
5479
python3 _padawan_server.restart()
5580
endfunction
81+
82+
function! deoplete#sources#padawan#Generate(...)
83+
if empty(get(b:, 'padawan_project_root', 0))
84+
python3 << PYTHON
85+
file_name = vim.eval('expand("%:p")')
86+
vim.command("let b:padawan_project_root = '{}'".format(
87+
_padawan_helper.get_project_root(file_name))
88+
)
89+
PYTHON
90+
endif
91+
if confirm("Are you sure you want to generate index in "
92+
\. b:padawan_project_root . "?", "&Yes\n&No", 2) == 1
93+
let cmd = "cd " . b:padawan_project_root . " && "
94+
\. g:deoplete#sources#padawan#padawan_command . " generate"
95+
if get(a:, 1, 0)
96+
exec "!" . l:cmd
97+
else
98+
call jobstart(l:cmd, {
99+
\'on_exit': function('s:generate_exit'),
100+
\'on_stdout': function('s:generate_stdout')})
101+
endif
102+
endif
103+
endfunction
104+
105+
function! s:generate_stdout(id, out, ...)
106+
for l:line in a:out
107+
if l:line =~ 'Progress:'
108+
echo l:line
109+
endif
110+
endfor
111+
endfunction
112+
113+
function! s:generate_exit(...)
114+
echo "Padawan.php: Index generating has finished!"
115+
endfunction

composer.json

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"minimum-stability": "dev",
3+
"require": {
4+
"mkusher/padawan": "dev-master"
5+
}
6+
}

rplugin/python3/deoplete/sources/deoplete_padawan.py

+4-14
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@
1616
sys.path.insert(1, path.dirname(__file__) + '/deoplete_padawan')
1717

1818
import padawan_server # noqa
19+
import padawan_helper # noqa
1920

2021

2122
class Source(Base):
2223

2324
def __init__(self, vim):
2425
Base.__init__(self, vim)
2526

27+
self.helper = padawan_helper.Helper()
2628
self.name = 'padawan'
2729
self.mark = '[padawan]'
2830
self.filetypes = ['php']
@@ -50,7 +52,7 @@ def on_init(self, context):
5052
def on_event(self, context):
5153
if (context['event'] == 'BufWritePost' and self.auto_update == 1):
5254
file_path = self.current.buffer.name
53-
current_path = self.get_project_root(file_path)
55+
current_path = self.helper.get_project_root(file_path)
5456
params = {
5557
'path': current_path
5658
}
@@ -89,7 +91,7 @@ def get_patterns_position(self, context, patterns):
8991

9092
def gather_candidates(self, context):
9193
file_path = self.current.buffer.name
92-
current_path = self.get_project_root(file_path)
94+
current_path = self.helper.get_project_root(file_path)
9395

9496
[line_num, _] = self.current.window.cursor
9597
column_num = self.get_padawan_column(context)
@@ -168,15 +170,3 @@ def do_request(self, command, params, data=''):
168170
self.vim.command("echom 'Padawan.php error: {}'".format(error))
169171
# any other error can bouble to deoplete
170172
return False
171-
172-
def get_project_root(self, file_path):
173-
current_path = path.dirname(file_path)
174-
while current_path != '/' and not path.exists(
175-
path.join(current_path, 'composer.json')
176-
):
177-
current_path = path.dirname(current_path)
178-
179-
if current_path == '/':
180-
current_path = path.dirname(file_path)
181-
182-
return current_path
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# =============================================================================
2+
# FILE: padawan_helper.py
3+
# AUTHOR: Pawel Bogut
4+
# =============================================================================
5+
from os import path
6+
7+
8+
class Helper:
9+
10+
def get_project_root(self, file_path):
11+
current_path = path.dirname(file_path)
12+
while current_path != '/' and not path.exists(
13+
path.join(current_path, 'composer.json')
14+
):
15+
current_path = path.dirname(current_path)
16+
17+
if current_path == '/':
18+
current_path = path.dirname(file_path)
19+
20+
return current_path

0 commit comments

Comments
 (0)