|
| Some useful scripts for PhpED | |
Joined: 08 Mar 2006 |
Posts: 63 |
|
|
|
Posted: Wed Apr 05, 2006 12:14 pm |
|
|
|
|
|
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
|
| Align Equals | |
Joined: 08 Mar 2006 |
Posts: 63 |
|
|
|
Posted: Wed Apr 05, 2006 12:15 pm |
|
|
|
|
|
Command line: @php5@ -n -f c:\MySource\PhpED\AlignEquals.php
<?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
|
|
| DoxyComment | |
Joined: 08 Mar 2006 |
Posts: 63 |
|
|
|
Posted: Wed Apr 05, 2006 12:16 pm |
|
|
|
|
|
Command line: @php5@ -n -f c:\MySource\PhpED\DoxyComment.php
<?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
|
|
| Wrap Lines | |
Joined: 08 Mar 2006 |
Posts: 63 |
|
|
|
Posted: Wed Apr 05, 2006 12:17 pm |
|
|
|
|
|
Command line: @php5@ -n -f c:\MySource\PhpED\WrapLines.php 78
... or whatever you want as right margin.
<?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]);
?> |
|
|
|
| | |
Joined: 18 Feb 2010 |
Posts: 4 |
Location: Switzerland |
|
|
Posted: Thu Feb 18, 2010 4:26 am |
|
|
|
|
|
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
|
|
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
|
|
|
| |