PHP Docblock generator plug-in for Coda

Share/Save

Panic's Coda is my current favorite editor. It's not perfect yet, but they just keep making it better. Coda 1.6 and later support a great plug-in architecture that allows users to tweak the editor's functionality to match their own needs and coding style.

Plug-ins have access to the text of the current document and can perform modifications on it as desired. For example, you could write a plug-in to insert the current date and time into your document, change the case of selected text, run code through a custom validator, or even wrap code with a special tag.

These plug-ins are pretty rad, let me tell you.

One of the things I missed most when I started using Coda was the ability to magically generate docblocks. You see, I'm absolutely horrid at writing documentation. With semi-automatic docblocks, writing documentation has a ton less friction so I find myself doing it more.

The new plug-in architecture lends itself well to things like that, so I decided to write a docblock generator. You can download my PHP docblock plug-in here:

PHP Docblock generator plug-in for Coda

Give it a spin, let me know where you'd like to see improvements. And be sure to check out the rest of the awesome plug-ins available over at Panic.

Updates

PHP Docblock Generator 0.2.4

Version 0.2.4 has one really important bugfix:

  • Sometimes Coda does a bad job of providing the environment variables promised by the plugin API... As far as I can tell, this is a problem only in Snow Leopard, and only with the bundled PHP 5.3.0 build. This version includes a fix for Coda's bug. :)

Two (slightly lamer) bugfixes:

  • Fixed display of static keyword on class member variables.
  • Re-enabled docblocks for functions that don't use the 1TBS.

And two brand new features:

  • Added docblock generation for interfaces.
  • Enabled documentation for one line functions and abstract functions.

PHP Docblock Generator 0.2.3

Version 0.2.3 fixes a couple of text selection and trigger quirks:

  • Fixed a bug where docblocks would be inserted mid-line if the cursor wasn't at the start (now they just aren't generated).
  • (Finally) fixed quirks triggering the plugin introduced by the Coda 1.6.1 plugin API changes.

PHP Docblock Generator 0.2.2

Version 0.2.2 fixes a few bugs in function and class declaration parsing:

  • Fixed a display bug in functions with a default array() value.
  • Fixed a bug that prevented documenting any function parameters which followed a default array() value.
  • Fixed a bug that prevented displaying @abstract, @final and @static in some docblocks.

PHP Docblock Generator 0.2.1

Version 0.2.1 adds more valid php file extensions:

  • PHP Docblock generator now works with php, phtml, php3, php4, php5, ph3, ph4, ph5, phps, module, inc and install extensions.
  • This one goes out to all you Drupal developers :)

PHP Docblock Generator 0.2

Version 0.2 has several needed improvements:

Bug fixes
  • Fixed a bug that prevented documenting functions like '&foo'.
  • Fixed a bug with functions declared 'public final' instead of 'final public'.
  • Fixed a bug that prevented documenting abstract and final classes.
Improvements
  • Improved flexibility of function declaration parsing regex.
  • Improved @access guessing for functions and member variables.
  • Improved member var docblock formatting and insertion point.
Features
  • Added member variable type guessing based on assigned values.
  • Added fuction/method param type guessing based on default values.

Example Docblocks

<?php
 
/**
 * alphabet interface.
 */
interface alphabet {
 
    /**
     * getA function.
     * 
     * @access public
     * @return void
     */
    public function getA();
 
    /**
     * getB function.
     * 
     * @access public
     * @return void
     */
    public function getB();
 
}
 
 
/**
 * Abstract Alpha class.
 * 
 * @abstract
 */
abstract class Alpha {
 
    /**
     * foo function.
     * 
     * @access public
     * @abstract
     * @static
     * @param mixed $bar
     * @param mixed $baz
     * @param mixed $qux
     * @return void
     */
    abstract static function foo($bar, $baz, $qux);
 
    /**
     * bar function.
     * 
     * @access private
     * @abstract
     * @return void
     */
    abstract private function bar();
}
 
/**
 * Beta class.
 * 
 * @extends Alpha
 * @implements alphabet
 */
class Beta extends Alpha implements alphabet {
 
    /**
     * a
     * 
     * (default value: true)
     * 
     * @var bool
     * @access public
     * @static
     */
    static var $a = true;
 
    /**
     * b
     * 
     * (default value: array())
     * 
     * @var array
     * @access protected
     */
    protected $b = array();
 
    /**
     * c
     * 
     * @var mixed
     * @access private
     */
    private $c;
 
    /**
     * foo function.
     * 
     * @access public
     * @static
     * @param mixed $bar
     * @param int $baz. (default: 1)
     * @param mixed $qux. (default: null)
     * @return void
     */
    static function foo($bar, $baz = 1, $qux = null) { }
 
    /**
     * bar function.
     * 
     * @access private
     * @return void
     */
    private function bar();
 
    /**
     * _baz function.
     * 
     * @access private
     * @final
     * @param string $qux. (default: 'quux')
     * @return void
     */
    final function _baz($qux = 'quux') { }
 
    /**
     * qux function.
     * 
     * @access protected
     * @return void
     */
    protected function qux() { }
 
    /**
     * getA function.
     * 
     * @access public
     * @return void
     */
    public function getA() { }
 
    /**
     * getB function.
     * 
     * @access public
     * @return void
     */
    public function getB() { }
 
}
 
?>

Comments

Very useful, thanks!

Very useful indeed, thank you very much. The plugin, however, lacks support for final functions and does not work when asked to comment such a function. Example:

public final function Bar(){}

Best wishes,
Philipp

Thanks for pointing that out. It did support functions declared as final public, but not public final. I've improved the logic (in a couple more places, too), and it's far more flexible now.

See the rest of the improvements above, or download PHP Docblock Generator 0.2 and try it for yourself :)

Let me know if that works for you.

Hey there, I've been having some trouble.
Sometimes it acts funky. I can normally add a docblock anywhere, but sometimes, in certain classes/methods, it freezes coda and I have to force restart. It's basically un unsable for me because of this, although it is a great plugin.

I've never run into that problem with this plugin. Can you help me track it down?

What other plugins do you have installed? Can you send me a file/class/method that you're having trouble with? Can you reliably recreate the problem, or does it seem random?

Thanks.

Note: This issue has since been resolved. To my knowledge, the current version is completely stable. If you know of anything that makes PHP Docblock Generator freeze, please let me know :)

Great plugin! Thanks!

But I had to sniff through the code to see that you're checking file extensions. I'm using Drupal and the extensions there are... (module, install). Could you add those by default? Would be great.

$valid_ext = array('php', 'phtml', 'php3', 'php4', 'php5', 'ph3', 'ph4', 'ph5', 'module', 'inc', 'install');

Done. I even added phps for PHP source files.

It would be great if Coda told the plugin which syntax mode was currently active, but I haven't been able to figure that one out.

I don't get how this is suppose to work, nothing seems to happen.

Can you give an example on how to use ?

Sure.

Position the cursor in the white space at the start of the first line of a function, class variable, or class definition (or the empty line before) and hit control+shift+D. You can also select "Generate PHP Docblock" from the plugin menu.

It should automagically come up with a docblock for you and position your cursor where you need to start typing the description.

So type this:

class Foo extends Bar {

Then hit cmd+left to get to the start of the line (or up to get to the line above), then control+shift+D. It will add a comment that looks like this:

/**
 * Foo class.
 * 
 * @extends Bar
 */
class Foo extends Bar {

Aside: in previous versions of Coda you didn't have to position the cursor in the white space before or the above the line you want to document, but they changed the way the plugin API works slightly and that functionality broke

Great plugin, if I'll get it to work. Im using the exact commands from your example but it's not working for me:-(

Snow Leopard, Code 1.6.5

Any help is much appreciated...

Coda seems to have a plugin API bug with some Snow Leopard users... Try the new version (available above) and see if that fixes things for you.

Just started playing with Coda and this plugin is a huge time saver. Thanks for your dev!

very useful plugin, thanks for sharing

Hi,

I have been using this plugin for a while, but then I started getting an error

Warning: explode(): Empty delimiter in /Users/Joe/Library/Application Support/Coda/Plug-ins/PHP Docblock.codaplugin/Contents/Resources/28E7D08A-1719-4226-A247-D80156DF47D7/docblock.php on line 63

It seemed that none of the $_ENV[] variables were set, $_ENV was empty, not sure if this is something coda has changed recently, so I found all these variables were available through $_SERVER[] instead, so I did a global Find and Replace, updated the bundle and reinstalled th plugin, now is working well.

Thanks for pointing that out. I never ran into this problem on my Snow Leopard boxes, but it seems that other people have. I've applied your fix as a fallback in my latest version of the plugin (available above).

Please try it out, let me know if it works for you.

Thanks for the fix! Works great in Snow Leopard now and it was sorely missed when it didn't

So when are you going to post about your line commands plugin?

Soon :)

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li>
  • Lines and paragraphs break automatically.

More information about formatting options