VIM Insert PHPdoc automatically

前端 未结 3 1166
有刺的猬
有刺的猬 2021-01-31 10:53

Is there a way to insert PHPDoc in VIM using a command or key combination?

For example, I have a class:

class MyClass
{

  public function __construct()          


        
相关标签:
3条回答
  • 2021-01-31 11:34

    This can be done pretty effectively with the lightweight phpDocumentor plugin.

    Edit Here's a modified version with more recent development.

    Edit Here's version 2 of the phpDocumentor plugin. It is more recent than the above two links.

    Install the plugin into your $VIMFILES/plugin directory and add this to your .vimrc:

    " PHP documenter script bound to Control-P
    autocmd FileType php inoremap <C-p> <ESC>:call PhpDocSingle()<CR>i
    autocmd FileType php nnoremap <C-p> :call PhpDocSingle()<CR>
    autocmd FileType php vnoremap <C-p> :call PhpDocRange()<CR> 
    

    The above binds phpDocumentor to Ctrlp in insert, normal, and visual modes. Place your cursor on a class, function, or variable definition, press Ctrlp, and the plugin will attempt to form a doc block based on the definition.

    Example function doc block:

    /**
     * testDocBlock 
     * 
     * @param mixed $param1 
     * @param mixed $param2 
     * @static
     * @access public
     * @return boolean
     */
    public static function testDocBlock($param1, $param2) {
      // Pressed Ctl-p while cursor was on the function definition line above...
    }
    

    Example class doc block

    Copyright, version, author, etc are included by default in a class doc block. You can modify the plugin to include your own default values for these:

    /**
     * TestClass  
     * 
     * @package 
     * @version $id$
     * @copyright 
     * @author Michael <me@exmaple.com> 
     * @license 
     */
    class TestClass {
    
    }
    

    Full abstract class example:

    <?php
    /**
     * TestClass 
     * 
     * @abstract
     * @package 
     * @version $id$
     * @copyright 
     * @author Michael <email@example.com>
     * @license 
     */
    abstract class TestClass {
      /**
       * publicProp  
       * 
       * @var string
       * @access public
       */
      public $publicProp;
      /**
       * privateProp  
       * 
       * @var string
       * @access private
       */
      private $privateProp;
    
      /**
       * testDocBlock  
       * 
       * @param string $param1 
       * @param string $param2 
       * @static
       * @access public
       * @return boolean
       */
      public static function testDocBlock($param1, $param2) {
        // code here...
      }
    }
    ?>
    
    0 讨论(0)
  • 2021-01-31 11:46

    Assume you place your template at ~/templates/phpdoc.php. With the below abbreviation, if you type ,p, you will read the contents of your phpdoc.php into the current file, ( at or below the current line - one of those ).

    map ,p :r ~/templates/phpdoc.php<cr>
    

    Then just add the line to your .vimrc file, replacing the file-path and ,p to your liking.

    0 讨论(0)
  • 2021-01-31 11:47

    I can't speak for PHP specifically but you have a couple of options. You can use abbreviations (maybe not good for the specific example) or you can look for a plugin. I can suggest https://github.com/garbas/vim-snipmate ( I use it and it works fine).

    0 讨论(0)
提交回复
热议问题