Code for expanding tabs in 1.1.5

I have a small tweak to the 1.1.5 source that allows source to be generated with spaces instead of tabs. It would be nice to see this make it into the next release.

I require this feature because my entire code base uses spaces instead of tabs. My editors expand tabs as a function of programming language, not programming project. If my Yii-generated code has tabs, I have no choice but to mix tabs and spaces in those files, because I’m not reconfiguring my editor every time I switch projects. Besides, whether or not to use tabs should be an in-house decision rather than a programming framework decision.

The fix is rather simple and enables a new Gii option called "tabExpansion." Its value is the number of spaces with which to replace each tab. A value of zero indicates that tabs are to be used. 0 is the default value.

First, add the following lines to GiiModule:




	/**

	 * @var integer Number of spaces by which to expand tabs; 0 to use tabs.

	 * Defaults to 0, using tabs.

	 */

	public $tabExpansion=0;



Next, at the end of the CCodeModel "render" method, replace the line "return ob_get_clean();" with the following lines:




		$code = ob_get_clean();

		$module = Yii::app()->controller->module;

		if($module->tabExpansion)

		{

		    $spaces = str_repeat(' ', $module->tabExpansion);

		    $code = str_replace("\t", $spaces, $code);

		}

		return $code;



That’s it! Works like a charm. It even enables Gii to preview the selected tab expansion.

Looks like we’ll need a fix for yiic too. I’ll get to that later.

Tabs just don’t seem like a good idea for open source, when people all over the world have the potential to contribute. Tab treatment is not uniform across editors. Space treatment is, at least for monospace fonts. In languages where whitespace is interchangeable (unlike tab-loving Python), any use of tabs also requires standardizing on the tab size, because sometimes spaces sneak in there, such as when aligning related lines of code. Might as well fix that by using spaces from the start. If 4 is too many, 2 is fine. At least that can easily be controlled from file to file, unlike tabs, which have to be configured in the editor and apply to all files.

I noticed that tabs also snuck into the generated HTML, making for horrendous long-spacing in browser source view. greps are annoying too.

Sorry, I didn’t notice the feature requests forum.

Oddly, as I keep bumping into Yii files containing tabs, I find myself typing and cursoring around much less, and liking it. I just don’t know how to deal with the fact that (1) I’ve got so much other PHP that uses spaces and not tabs, and (2) I dread loading up code from people who prefer other tab sizes.