Source Code Formatting Examples

Monday, September 7, 2009

When blogging, source code formatting is a huge PITA. I’m working on an Eclipse plugin to take away some of that pain. So bear with me as I play with some formatting. There is absolutely nothing useful at all in this post. This is merely a post that I can use to continually test tweaks.

Here’s some XML:

<target name="package" depends="clean,version" description="--> packages the appropriate files into the deployment-ready zip file. use this for testing; otherwise, use publish">
	<echo message="building ${zipfile}. basedir is  ${basedir}" />
	<zip destfile="${zipfile}" casesensitive="false">
		<zipfileset dir="${basedir}" 
			includes="/*.*,ant/,buildprops/,cfeclipse/,doc/,framework/,generator/,images/,PluginDemoTests/,resources/,runner/,samples/,tests/,lib/ant-contrib**" 
			excludes="MXUnitInstallTest.cfc,**/testresults/,**/junithtml/,.project,.deployment,**/unames.properties,tests/tmp/,ftp.listing,**/*.db,**/copysnippets.properties,**/copydictionary**,**/*.bak" 
			prefix="mxunit" casesensitive="false" />
	</zip>
</target>

And here’s some Java:

private static String getCommonWhitespacePrefix(String[] lines){
	//get rid of any completely blank lines, as they will throw off the getCommonPrefix function
	List<String> newLines = new ArrayList<String>();
	for (int i = 0; i < lines.length; i++) {
		if(lines[i].trim().length()>0){
			newLines.add(lines[i]);
		}
	}
	return StringUtils.getCommonPrefix(newLines.toArray(new String[newLines.size()]));
}

And some lovely ColdFusion

<cffunction name="_MixinAll" access="public">
	<cfargument name="objReceiver" required="true" hint="the object to receive the functions">
	<cfargument name="objGiver" required="true" hint="the object whose functions will be mixed in">
	<cfargument name="includedMethods" required="false" default="" hint="pass a list of methods; otherwise, all are included">
	<cfset var md = getMetadata(objGiver)>
	<cfset var a_functions = md.functions>
	<cfset var fn = 1>

	<cfset arguments.objReceiver._Mixin = _Mixin>
	<cfset arguments.objGiver._getComponentVariable = _getComponentVariable>

	<cfloop from="1" to="#ArrayLen(a_functions)#" index="fn">
		<cfif (arguments.includedMethods eq "" OR listFindNoCase(arguments.includedMethods,a_functions[fn].name))>
			<cfset arguments.objReceiver._Mixin(a_functions[fn].name,	arguments.objGiver._getComponentVariable(a_functions[fn].name))>
		</cfif>
	</cfloop>
</cffunction>

And some javascript, just to prove it looks OK

try
{
	if (window.XMLHttpRequest)
		req = new XMLHttpRequest();
	else if (window.ActiveXObject)
	{
		while (!req && Spry.Utils.msProgIDs.length)
		{
			try { req = new ActiveXObject(Spry.Utils.msProgIDs[0]); } catch (e) { req = null; }
			if (!req)
				Spry.Utils.msProgIDs.splice(0, 1);
		}
	}
}

How'd all that look?

5 comments:

John Allen said...

pretty good. osx 10.5 safari 4.0.3

Marc Esher said...

thanks John

Robert Burns said...

One thing jumps out, on line 14 of the cfml. Before arguments.objGiver there is what appears to be a tab or several spaces. Don't know if you intended that or not.

Marc Esher said...

Bob, do you know if there's a setting in Eclipse for showing tabs vs. spaces in the editor that you can easily toggle? this was a tab that masqueraded as a space.

thanks!

Robert Burns said...

May be the 'Display tab width' option in both the General/Editors/Text Editors and CFEclise/Editor preferences. By default I believe it is set to 4 spaces as how wide a tab should be. If you had that set to 1 a tab could look like a single space. There is also a check box on both of those preference screens for 'Insert spaces for tabs'.