Dienstag, 18. Oktober 2011

Xtext Objective C Formatter/Beautifier

This post shows how I integrated uncrustify into Xtext. At the end of this post you will be able to package uncrustify with your language UI plugin and run uncrustify as part of a MWE2 workflow (the described approach was tested with Xtext 2.0 on Mac OS X 10.7).

First, you have to get uncrustify. Unpack it, run ./configure and then make. The binary is located in the src/ folder and is named uncrustify - what a surprise. Create a folder formatting/ in your language's UI plugin. Copy the binary into this folder. To tell uncrustify how to format the code we have to supply it with a config file. A config file for objective c can be found here. Download this file and put it into the formatting/ folder. Besides the binary and a config file we need a shell script that runs uncrustify. Create a file formatSource.sh in formatting/. The shell script looks like this:
#! /bin/sh

touch files.txt
find . -name "*.[hm]" > files.txt

while read line; do
./uncrustify_osx -l OC -c ./uncrustify_obj_c.cfg --no-backup $line
done < files.txt  
rm files.txt 
This script will look for *.h and *.m files recursively down from its location, run over them, and format them without creating a backup copy.

Now that we have the necessary files for running uncrustify ... oh well ... we must be able to run uncrustify from within java. For executing shell scripts from within a java process - welcome platform dependency - check out my ShellCommandExecutor. This class is also used for making the shell script formatSource.sh executable after copying it to a language project:
private void copyFormattingFiles(final IProject project){
 Bundle bundle = SystemDslActivator.getInstance().getBundle();
 IPath scriptPath = copyFile("formatting/formatSource.sh"     , "formatSource.sh",      project, bundle);
 IPath binaryPath = copyFile("formatting/uncrustify_osx",       "uncrustify_osx",       project, bundle);

 copyFile("formatting/uncrustify_obj_c.cfg", "uncrustify_obj_c.cfg", project, bundle);

 //make script and binary executable
 try {
  ShellCommandExecutor.execute("chmod", "+x", scriptPath.toString());
  ShellCommandExecutor.execute("chmod", "+x", binaryPath.toString());
 } catch (Exception e) {
  //TODO: write to error log
 }
}
The above method can be found in this class. The execution of formatSource.sh in a MWE2 workflow component looks like this:
public class ObjectiveCFormatter extends org.eclipse.emf.mwe.core.lib.AbstractWorkflowComponent2{

private static final String SCRIPT_PATH = "./formatSource.sh";

@Override
protected void invokeInternal(WorkflowContext ctx, ProgressMonitor monitor, Issues issues) {
try{
 CommandResult cr = ShellCommandExecutor.execute(SCRIPT_PATH, new String[]{});

 if (cr.success){
  System.out.println("Formatting complete!");
 } else{
  issues.addError(cr.output);
 }
}catch (Exception e){
 issues.addError(e.getMessage());
}
}
}
If you add this component after your objective c generator in your workflow all *.h and *.m files will be formatted as described by the uncrustify objective c config file.

regards,

steven

Dienstag, 19. Juli 2011

LaTex: Linebreak, NewLine in Caption

I you want to have a line break, a.k.a. new line, in your latex document's captions import the package "caption" like this:

\usepackage{caption}


In your caption's you can add the \newline command:


\begin{figure}

\centering

\includegraphics{}

\caption{Bla Bla Bla \newline Bla Bla Bla after line break}

\label{fig:aLabel}

\end{figure}


regards

Dienstag, 15. März 2011

Montag, 7. März 2011

CoreData Performance Measurement

world,

check out this evaluation on CoreData Performance.


s

Dienstag, 23. November 2010

Mittwoch, 30. Juni 2010

Git ignore already added files

dear world,

I wanted git to ignore files that I have already added to the repository. Unfortunately, if a file was added, changing .gitignore does not help. But there are two options:

  • git update-index --assume-unchanged [filename(s)]
  • git update-index --skip-worktree [path_to_ignore]
the first tells git to ignore changes to single files;
the second tells it to ignore all files below a given path .. a complete directory.

so far this works and I hope I did not mess something up.

regards,

s

Freitag, 5. März 2010

error C2275 illegal use of this type as an expression

You are probably programming c or c++ and banging your head against the wall because of error C2275 illegal use of this type as an expression.

check if you have declared a variable after the frist assignment in a function! remember: in c all variables have to be declared before any other statement.

stop banging!