Skip to content

Commit

Permalink
added plugins support and updated README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
mkusher committed Aug 14, 2015
1 parent bba4550 commit e6a4596
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 40 deletions.
59 changes: 39 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ Padawan.vim
===========

Padawan.vim is a vim plugin for [padawan.php server
](https://github.com/mkusher/padawan.php).
](https://github.com/mkusher/padawan.php) - smart php intelligent code
completion server for composer projects.

This plugin includes:
- Omnifunc
Expand Down Expand Up @@ -55,32 +56,16 @@ How to use
- Run index generation command in your php composer
project folder using this vim command:
```vim
:call padawan#GenerateIndex()
:PadawanGenerateIndex
```
- Start padawan's server with:
```vim
:call padawan#StartServer()
:PadawanStartServer
```
- Enjoy smart completion

It can take a while. You should generate index manually for each of your
project only one time. After it start server with
And that's all!

Configuring
-----------

You may want to change composer to the one installed in your system.
You can do it using:
```vim
let g:padawan#composer_command = 'php /usr/bin/composer.phar'
```
Another option you may want to change is http request timeout.
You can do it using
```vim
let g:padawan#timeout = 0.1
```
It will set timeout to 100 ms.
project only one time.

Autocomplete engines
-------------------
Expand All @@ -103,6 +88,40 @@ let g:neocomplete#force_omni_input_patterns.php =
\ '\h\w*\|[^- \t]->\w*'
```

Plugins(Extensions)
-------------------

You can extend padawan.php by installing different plugins.
See [Plugins List](https://github.com/mkusher/padawan.php/wiki/Plugins-list)
for more info.

### Installing
To install plugin run `:PadawanAddPlugin PLUGIN_NAME`, for example:
```vim
:PadawanAddPlugin mkusher/padawan-symfony
```

### Removing
To remove plugin run `:PadawanRemovePlugin PLUGIN_NAME`, for example:
```vim
:PadawanRemovePlugin mkusher/padawan-symfony
```

Configuring
-----------

You may want to change composer to the one installed in your system.
You can do it using:
```vim
let g:padawan#composer_command = 'php /usr/bin/composer.phar'
```
Another option you may want to change is http request timeout.
You can do it using
```vim
let g:padawan#timeout = 0.1
```
It will set timeout to 100 ms.

Vim functions
-------------

Expand Down
135 changes: 116 additions & 19 deletions autoload/padawan.vim
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ if exists('did_padawan_autoload')
finish
endif
let did_padawan_autoload = 1
let padawanPath = expand('<sfile>:p:h:h')

if !exists('g:padawan#composer_command')
let g:padawan#composer_command = 'composer'
let g:padawan#composer_command = padawanPath . '/composer.phar'
endif
if !exists('g:padawan#server_path')
let g:padawan#server_path = expand('<sfile>:p:h:h') . '/padawan.php'
Expand All @@ -20,7 +21,7 @@ if !exists('g:padawan#enabled')
let g:padawan#enabled = 1
endif
if !exists('g:padawan#timeout')
let g:padawan#timeout = 0.1
let g:padawan#timeout = 0.15
endif

python << EOF
Expand All @@ -40,6 +41,7 @@ server_addr = vim.eval('g:padawan#server_addr')
server_path = vim.eval('g:padawan#server_path')
composer = vim.eval('g:padawan#composer_command')
timeout = float(vim.eval('g:padawan#timeout'))
padawanPath = vim.eval('padawanPath')


class PadawanClient:
Expand All @@ -66,46 +68,117 @@ class PadawanClient:

def DoRequest(self, command, params, data=''):
try:
addr = server_addr + "/"+command+"?" + urllib.urlencode(params)
response = urllib2.urlopen(
addr,
urllib.quote_plus(data),
timeout
)
completions = json.load(response)
if "error" in completions:
raise ValueError(completions["error"])
return completions
return self.SendRequest(command, params, data)
except urllib2.URLError:
vim.command('echo "Padawan.php is not running"')
except Exception as e:
vim.command('echom "Error occured {0}"'.format(e))

return False

def SendRequest(self, command, params, data=''):
addr = server_addr + "/"+command+"?" + urllib.urlencode(params)
response = urllib2.urlopen(
addr,
urllib.quote_plus(data),
timeout
)
completions = json.load(response)
if "error" in completions:
raise ValueError(completions["error"])
return completions


def StartServer(self):
command = '{0}/bin/server.php > {0}/../logs/server.log'.format(server_path)
self.server_process = subprocess.Popen(
subprocess.Popen(
command,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT
)

def StopServer(self):
self.server_process.kill()
try:
return self.SendRequest('kill', {})
except Exception:
return False

def RestartServer(self):
self.StopServer()
self.StartServer()

def AddPlugin(self, plugin):
curPath = self.GetProjectRoot(filepath)
self.ComposerDumpAutoload(curPath)
composerCommand = composer + ' require '
generatorCommand = server_path + '/bin/cli'

command = 'cd {0} && {1} {3} && {2} plugin add {3}'.format(
self.PadawanPHPPath(),
composerCommand,
generatorCommand,
plugin
)

stream = subprocess.Popen(
'cd ' + curPath + ' && ' + generatorCommand + ' plugin add ' + plugin,
command,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT
)

while True:
retcode = stream.poll()
if retcode is not None:
break

line = stream.stdout.readline()
vim.command("echo '%s'" % line.replace("'", "''"))
time.sleep(0.005)

if not retcode:
self.RestartServer()
vim.command("echom 'Plugin installed'")
else:
vim.command("echom 'Plugin installation failed'")

def RemovePlugin(self, plugin):
composerCommand = composer + ' remove'
generatorCommand = server_path + '/bin/cli'

command = 'cd {0} && {1} {2}'.format(
self.PadawanPHPPath(),
composerCommand,
plugin
)

stream = subprocess.Popen(
command,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT
)

while True:
retcode = stream.poll()
if retcode is not None:
break

line = stream.stdout.readline()
vim.command("echo '%s'" % line)
time.sleep(0.005)

subprocess.Popen(
'cd {0} && {1}'.format(
self.PadawanPHPPath(),
generatorCommand + ' plugin remove ' + plugin
),
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT
).wait()
self.RestartServer()
vim.command("echom 'Plugin removed'")

def Generate(self, filepath):
curPath = self.GetProjectRoot(filepath)
self.ComposerDumpAutoload(curPath)
Expand Down Expand Up @@ -138,6 +211,7 @@ class PadawanClient:
vim.command("redraw | echo 'Progress "+barsStr+' '+str(progress)+"%'")
time.sleep(0.005)
time.sleep(0.005)
self.RestartServer()
barsStr = ''
for i in range(20):
barsStr += '='
Expand All @@ -162,6 +236,9 @@ class PadawanClient:

return curPath

def PadawanPHPPath(self):
return padawanPath + '/padawan.php/'

client = PadawanClient()
EOF

Expand Down Expand Up @@ -239,8 +316,7 @@ endfunction

function! padawan#RestartServer()
python << EOF
client.StopServer()
client.StartServer()
client.RestartServer()
EOF
endfunction

Expand All @@ -257,3 +333,24 @@ filepath = vim.eval("expand('%:p')")
client.Generate(filepath)
endpython
endfunction

function! padawan#AddPlugin(pluginName)
python << endpython
pluginName = vim.eval("a:pluginName")
client.AddPlugin(pluginName)
endpython
endfunction

function! padawan#RemovePlugin(pluginName)
python << endpython
pluginName = vim.eval("a:pluginName")
client.RemovePlugin(pluginName)
endpython
endfunction

command! -nargs=0 -bar PadawanStartServer call padawan#StartServer()
command! -nargs=0 -bar PadawanStopServer call padawan#StopServer()
command! -nargs=0 -bar PadawanRestartServer call padawan#RestartServer()
command! -nargs=0 -bar PadawanGenerateIndex call padawan#GenerateIndex()
command! -nargs=1 -bar PadawanAddPlugin call padawan#AddPlugin("<args>")
command! -nargs=1 -bar PadawanRemovePlugin call padawan#RemovePlugin("<args>")
2 changes: 1 addition & 1 deletion padawan.php

0 comments on commit e6a4596

Please sign in to comment.