NuSphere Forums Forum Index
NuSphere Forums
Reply to topic
Some useful scripts for PhpED


Joined: 08 Mar 2006
Posts: 63
Reply with quote
PhpED doesn't have real scripting support, but you can do quite a lot by calling an external program and letting it work on the selected text in the current window. Here are a few scripts written i PHP5 that I find useful. If anybody else has written any good scripts, perhaps you could also post them to this thread.

To install any of these scripts, save the code in a .php file somewhere. Then under Settings / Tools / Integration create a new menu with a suitable name. In the Command parameters, set Execute with to Shell, Command line to what's indicated in the posts below. Then check Show this command in Tools menu, Work with editor, Take input from, and Selected text.


Last edited by svenax on Thu Apr 06, 2006 12:24 pm; edited 1 time in total
View user's profileFind all posts by svenaxSend private message
Align Equals


Joined: 08 Mar 2006
Posts: 63
Reply with quote
Command line: @php5@ -n -f c:\MySource\PhpED\AlignEquals.php
Code:
<?php

/**
 * Align the rightmost equal signs in the selected text. Useful for formatting
 * assignment lists and/or array assignments.
 */
class AlignEquals
{
    private $text;

    public function run()
    {
        // Needed because file doesn't work with php://stdin
        $this->text = explode("\n", rtrim(file_get_contents('php://stdin')));

        $this->align($this->find_maxpos());

        echo utf8_decode(implode("\n", $this->text));
    }

    private function align($pos)
    {
        for ($i = 0; $i < count($this->text); $i++) {
            $line = $this->text[$i];
            $curpos = strrpos($line, '=');
            $this->text[$i] = str_pad(substr($line, 0, $curpos-1), $pos) . '=' . substr($line, $curpos+1);
        }
    }

    private function find_maxpos()
    {
        $pos = 0;
        foreach ($this->text as $line) {
            $curpos = strrpos($line, '=');
            if ($curpos > $pos) $pos = $curpos;
        }

        return $pos;
    }
}

$ae = new AlignEquals;
$ae->run();

?>


Last edited by svenax on Thu Apr 06, 2006 12:18 pm; edited 1 time in total
View user's profileFind all posts by svenaxSend private message
DoxyComment


Joined: 08 Mar 2006
Posts: 63
Reply with quote
Command line: @php5@ -n -f c:\MySource\PhpED\DoxyComment.php
Code:
<?php

/**
 * Generates a JavaDoc-style comment for the selected class, function, or
 * variable.
 */
class DoxyComment
{
    private $text;
    private $indent;

    public function run()
    {
        $this->text = rtrim(file_get_contents('php://stdin'));

        $this->find_indent();

        echo utf8_decode($this->create_comment());
    }

    private function find_indent()
    {
        $text = ltrim($this->text);
        $this->indent = strlen($this->text) - strlen($text);
    }

    private function create_comment()
    {
        $pad = str_pad('', $this->indent);

        $abstract = $static = false;
        $access = '';
        $params   = array();

        $new_text  = "$pad/**\n";
        $new_text .= "$pad * Comment\n";

        if (preg_match('/(abstract )?(public |protected |private )?(static )?function \w+\((.*)*?\)/', $this->text, $matches)) {
            // Functions
            $abstract = !empty($matches[1]);
            $static = !empty($matches[3]);
            $access = $matches[2];
            $params = preg_split('/,\s*/', $matches[4], -1, PREG_SPLIT_NO_EMPTY);
        } elseif (preg_match('/(public |protected |private )?(static )?\$\w+/', $this->text, $matches)) {
            // Variables
            $static = !empty($matches[2]);
            $access = $matches[1];
        } elseif (preg_match('/(abstract )?class \w+/', $this->text, $matches)) {
            // Classes
            $abstract = empty($matches[1]);
        }

        if (count($params) > 0) {
            foreach ($params as $param) {
                $pos = strpos($param, '=');
                if ($pos) $param = trim(substr($param, 0, $pos-1));
                $new_text .= "$pad * @param $param [type] Description\n";
            }
        }
        if ($abstract) $new_text .= "$pad * @abstract\n";
        if ($static) $new_text .= "$pad * @static\n";
        if (!empty($access)) $new_text .= "$pad * @$access\n";

        $new_text .= "$pad */\n";

        return $new_text . $this->text;
    }

}

$dc = new DoxyComment;
$dc->run();

?>


Last edited by svenax on Sat Apr 08, 2006 12:03 pm; edited 2 times in total
View user's profileFind all posts by svenaxSend private message
Wrap Lines


Joined: 08 Mar 2006
Posts: 63
Reply with quote
Command line: @php5@ -n -f c:\MySource\PhpED\WrapLines.php 78
... or whatever you want as right margin.
Code:
<?php

/**
 * Wordwrap the selected text to the given right margin. Keeps the existing
 * right indent and comment prefix. Removes all extra whitespace, so it is
 * only suitable for plain text, not code samples and such.
 */
class WrapLines
{
    private $text;
    private $right_margin, $left_margin;
    private $comment_start = '', $comment_mid = '', $comment_end = '';

    public function run($right_margin)
    {
        $this->text = file_get_contents('php://stdin');
        $this->right_margin = $right_margin;

        $this->find_left_margin();
        $this->find_comment_prefix();
        $this->clean_text();

        echo utf8_decode($this->rewrap_lines());
    }

    private function find_left_margin()
    {
        $text = ltrim($this->text, " \t");
        $this->left_margin = strlen($this->text) - strlen($text);
        $this->text = rtrim($text);
    }

    private function find_comment_prefix()
    {
        if (preg_match('!^(/\*+|\*+ |//+ )!', $this->text, $matches)) {
            $this->comment_start = $matches[1];
            if (strstr($this->comment_start, '/*')) {
                $this->comment_mid = ' * ';
                $this->comment_end = ' */';
            } else {
                $this->comment_mid = $this->comment_start;
            }
        }
    }

    private function clean_text()
    {
        $comments = array();
        if ($this->comment_start != '') $comments[] = $this->comment_start;
        if ($this->comment_mid != '')   $comments[] = $this->comment_mid;
        if ($this->comment_end != '')   $comments[] = $this->comment_end;
        $this->text = trim(preg_replace('/[\r\n ]+/', ' ', str_replace($comments, ' ', $this->text)));
    }

    private function rewrap_lines()
    {
        $wrap_len = $this->right_margin - $this->left_margin - strlen($this->comment_mid);
        $padding  = str_pad('', $this->left_margin);
        return $padding . $this->comment_start
             . (strstr($this->comment_start, '/*') ? "\n$padding" . $this->comment_mid : '')
             . wordwrap($this->text, $wrap_len, "\n$padding".$this->comment_mid)
             . (strstr($this->comment_start, '/*') ? "\n$padding" . $this->comment_end : '');
    }
}

$argv = $_SERVER['argv'];
$wl = new WrapLines;
$wl->run($argv[1]);

?>
View user's profileFind all posts by svenaxSend private message


Joined: 16 May 2008
Posts: 5
Reply with quote
Is there a way to get this working on the latest versions of PHPEd?

Can't find the Integration in the settings->tools window.
View user's profileFind all posts by cuteboiSend private message
Site Admin

Joined: 13 Jul 2003
Posts: 8334
Reply with quote
Quote:
Can't find the Integration in the settings->tools window.

Tools -> Settings -> Integration
Please note that only PRO version supports this feature.

_________________
The PHP IDE team
View user's profileFind all posts by dmitriSend private messageVisit poster's website


Joined: 03 Jun 2008
Posts: 76
Location: Bavaria, Germany
Reply with quote
donĀ“t forget to check 'Return results to editor' in Command parameters (at least for JavaDocStyle Comments)
View user's profileFind all posts by bitseekerSend private message


Joined: 28 Nov 2009
Posts: 2
Reply with quote
Can you give me a demo or a video clip about three scripts? I still don't understand how and when I'll use it.
Thanks a lot
View user's profileFind all posts by undefinedSend private message


Joined: 18 Feb 2010
Posts: 4
Location: Switzerland
Reply with quote
undefined wrote:
Can you give me a demo or a video clip about three scripts? I still don't understand how and when I'll use it.
Thanks a lot


Check this threads, describing how to call an external PHP script from the NuSphere PHP IDE:
http://forum.nusphere.com/create-get-variablename-and-set-variablename-functions-t6891.html

and mainly this one:
http://forum.nusphere.com/howto-use-a-php-script-for-my-editing-t753.html
View user's profileFind all posts by ILavignySend private message
Some useful scripts for PhpED
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
All times are GMT - 5 Hours  
Page 1 of 1  

  
  
 Reply to topic