From e6a4596142cbeaa3a519803b32207504ebda9b56 Mon Sep 17 00:00:00 2001 From: Aleh Kashnikau Date: Fri, 14 Aug 2015 03:41:02 +0300 Subject: [PATCH] added plugins support and updated README.md --- README.md | 59 ++++++++++++------- autoload/padawan.vim | 135 +++++++++++++++++++++++++++++++++++++------ padawan.php | 2 +- 3 files changed, 156 insertions(+), 40 deletions(-) diff --git a/README.md b/README.md index b7aba6b..9592499 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 ------------------- @@ -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 ------------- diff --git a/autoload/padawan.vim b/autoload/padawan.vim index 12f85fc..cd21330 100644 --- a/autoload/padawan.vim +++ b/autoload/padawan.vim @@ -6,9 +6,10 @@ if exists('did_padawan_autoload') finish endif let did_padawan_autoload = 1 +let padawanPath = expand(':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(':p:h:h') . '/padawan.php' @@ -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 @@ -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: @@ -66,16 +68,7 @@ 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: @@ -83,9 +76,22 @@ class PadawanClient: 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, @@ -93,19 +99,86 @@ class PadawanClient: ) 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) @@ -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 += '=' @@ -162,6 +236,9 @@ class PadawanClient: return curPath + def PadawanPHPPath(self): + return padawanPath + '/padawan.php/' + client = PadawanClient() EOF @@ -239,8 +316,7 @@ endfunction function! padawan#RestartServer() python << EOF -client.StopServer() -client.StartServer() +client.RestartServer() EOF endfunction @@ -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("") +command! -nargs=1 -bar PadawanRemovePlugin call padawan#RemovePlugin("") diff --git a/padawan.php b/padawan.php index fc6b720..b29061e 160000 --- a/padawan.php +++ b/padawan.php @@ -1 +1 @@ -Subproject commit fc6b720ac00d7a95f3dc9acfe5b63759e31b9c02 +Subproject commit b29061e161e751989ffaed88dee1cc5f44e40aa0