Quantcast
Viewing latest article 3
Browse Latest Browse All 6

Tidy assignments in Coda and TextWrangler

I have written a simple tool to tidy assignments. It basically takes a group of assignments and uses spaces to make the code more readable.

This tool will help code meet the indentation rules in the WordPress coding standards.

If you have a group of basic assignments like…

$variable1 = 0;
$long_variable2 = 123213123;
$short = 'test';
$s = 'test2';

This tool will space them so…

$variable1      = 0;
$long_variable2 = 123213123;
$short          = 'test';
$s              = 'test2';

It will also work associative arrays…

array(
'magpie' => 0,
'blog' => 1,
'random' => 2,
'too_long_string' => 3
);

… with spacing …

array(
'magpie'          => 0,
'blog'            => 1,
'random'          => 2,
'too_long_string' => 3
);

The tool is available as a Coda plugin and a TextWrangler UNIX filter.

For Coda

  • Download the Coda plugin
  • Extract the zip file and double-click the tidy-assignment.codaplugin file
  • Select code, right-click, goto Plug-ins->Tidy Assignment

Image may be NSFW.
Clik here to view.
Highlight and select Tidy Assignment plugin

To remove/uninstall the plugin;

  • Close Coda
  • Goto [your_home_folder]/Library/Application Support/Coda/Plug-ins/
  • Delete the file tidy-assignment.codaplugin

For TextWrangler

  • Open TextWrangler and open a new text file.
  • Copy and paste the code below into this file.
#!/usr/bin/php
<?php

if ( !isset( $_SERVER['argv'] ) && !isset( $_SERVER['argv'][1] ) )
	die();

$fc    = file_get_contents( $_SERVER['argv'][1] );
$lines = explode( "\n", $fc );
$clean = array();
$assoc = false;

$longest_line_index  = 0;
$longest_line_length = 0;

foreach ( $lines as $key => $line ) {
	//remove whitspace
	$line = preg_replace( '/\s\s+/', ' ', trim( $line ) );
	$parts = array();
	
	if ( mb_stripos( $line, '=>' ) !== FALSE ) {
		$parts = explode( "=>", $line );
		$assoc = true;
	}
	elseif ( mb_stripos( $line, '=' ) !== FALSE ) {
		$parts = explode( "=", $line );
	}

	if ( !empty( $parts ) && mb_strlen( $parts[0] ) > $longest_line_length ) {
		$longest_line_index = $key;
		$longest_line_length = mb_strlen( $parts[0] );
	}
	
	if ( mb_strlen( $line ) > 0 )
		$clean[$key] = $line;
}

$longest_line = $clean[$longest_line_index];
$operator = $assoc ? '=>' : '=' ;

//add spaces to line
if ( mb_stripos( $longest_line, $operator ) !== FALSE ) {
	$parts = explode( $operator, $longest_line );
	$longest_line = sprintf( "%s %s %s", trim( $parts[0] ), $operator, trim( $parts[1] ) );
}
else {
	$longest_line = sprintf( "%s", trim( $longest_line ) );
}

//now get the position of equals
$pos = mb_stripos( $longest_line, $operator );
if ( $pos === FALSE )
	$pos = mb_strlen( $longest_line );

foreach ( $clean as $key => &$line ) {
	if ( $key == $longest_line_index )
		$line = $longest_line;
	elseif ( mb_stripos( $line, $operator ) !== FALSE ) {
		$spaces = ' ';
		$parts = explode( $operator, $line );
		if ( !empty( $parts ) )
			while ( mb_stripos( $line, $operator ) < $pos ) {
				$line = sprintf( "%s%s%s %s", trim( $parts[0] ), $spaces, $operator, trim( $parts[1] ) );
				$spaces .= " ";
			}
	}
	else {
		$line = sprintf( "%s", trim( $line ) );
	}
}

echo implode( "\n", $clean );
  • Save the file, something like Tidy Assignment.php, in the Filters folder.
  • You can find the filters folder by going to the #! menu and go to the Unix Filters sub-menu, and select the Open Filters Folder, like below.
    Image may be NSFW.
    Clik here to view.
  • Now anytime you want to tidy assignments, just go to this menu and select the Tidy Assignment script.
  • Image may be NSFW.
    Clik here to view.
    Select tidy assignment script in TextWrangler

    First time I created a coda plugin and I am surprised how simple it is. I may experiment with this in the future if I can think of anything useful.


    Filed under: Code Tagged: assignments, coda, indentation, plugin, textwrangler, tidy code, Unix filter, WordPress coding standards Image may be NSFW.
    Clik here to view.
    Image may be NSFW.
    Clik here to view.

    Viewing latest article 3
    Browse Latest Browse All 6

    Trending Articles