Showing posts with label AStyle. Show all posts
Showing posts with label AStyle. Show all posts

Mar 23, 2011

Xcode + AStyle. Part 2. (for Xcode 4.x)

Anar here...

Long time ago I've described a recipe to use astyle from within Xcode 3.x.

Now as you may know, Xcode 4 doesn't provide the "user script" menu. I knew about it since Xcode 4 developer preview versions, but kept hoping the menu item would be back.
This didn't happen :( We have now a stable release of Xcode 4 and there is no sign of user's script in it.

What I think is that Apple either couldn't implement a support for "user script" or they want us to use automator services.
So, now I try to use automator to accomplish the same as I did in XCode 3's user script menu.


#1
Run Automator. Start a new service workflow.

#2
Select, that you service will receives selected "text" and check that it will "Replace selected text"

#3
Drag the "Run Shell Scrip" to your workflow and add commands as in the following screenshot:

Save your service and now when you want to re-indent your code, just select the whole source file content and select your service from the service menu. Works OK, but there are some drawbacks, see below (at the very end of the post).

Of course you must write the path to your own astyle option file, in my case I use "/Users/anar/Documents/workspace/Support/astylerc", as you can see on the screenshot, which sets the following:
#Indent a C or C++ file.
mode=c
# ANSI style formatting/indenting
style=ansi
# Do not retain a backup of the original file
suffix=none
# Indent 'class' and 'struct' blocks so that the blocks 'public:', 'protected:' and 'private:' are indented.
indent-classes
# Indent 'switch' blocks so that the 'case X:' statements are indented in the switch block.
indent-switches
# Indent 'case X:' blocks from the 'case X:' headers.
indent-cases
# Add extra indentation to namespace blocks.
indent-namespaces
# Indent multi-line preprocessor definitions ending with a backslash.
indent-preprocessor
# Converts tabs into single spaces.
convert-tabs
# Set the minimal indent that is added when a header is built of multiple-lines.
min-conditional-indent=0
# Indent a maximum of # spaces in a continuous statement
max-instatement-indent=80
# Insert space padding around operators.
pad=oper
# Insert space padding around parenthesis on the inside only. 
pad=paren-in
# Remove extra space padding around parenthesis on the inside and outside.
unpad=paren


Drawbacks

The following is the only, what I found so far:

  • Unfortunately I don't see any way to keep the potision of the cursor after the service is called.
  • One should select the whole file content and execute the service on it. Which is inconvenient in some cases. I didn't find a way to get a file name of the current file, which I could then provide to the service.

Sep 20, 2010

Xcode + AStyle

Anar here...

You know, I think there are many people, who find the auto-formatting of source code in Xcode is really terrible :( I mean, it is very weak (in terms of configuration) for such a descent development environment, which XCode actually is.

Since a very long time I use AStyle (Artistic Style) to format my code. It actually does remarkable job and it is an extremely tunable tool. You can adjust it almost for all, if not for all, indentations schemas...

I have already posted a long time ago a recipe of how to setup astyle for eclipse. Now, it is time to use it in XCode.

I use a user script menu to add my own script, which will than trigger the astyle command to format a source code for me.

Use the User Script menu to add a new script. You should add your script to a section "Text", the type of the script should - a shell script.
Next is the properties of the script:
Input: Entire Document
Directory: Selection
Output: Replace Document Contents
Errors: Display in Alert

And the content of the script:

#!/bin/bash

# I pass selected file instead of a selection, because astyle 1.24
# fails to parse struct with access modifiers. It just hangs instead.
# I also use a tmp file in order to avoid annoying Xcode message, that
# source file was changed outside of Xcode.

# Important: there must be always a new line at the end of the source file.
# Next time I provide a fix for that.


TMP=`mktemp -t xcode.XXXXXXXXXX` || exit 1

cat << 'EOFEOFEOF' > "$TMP"
%%%{PBXAllText}%%%EOFEOFEOF

astyle --options=<***PATH_TO_YOUR_ASTYLE_RC_FILE***> $TMP &>/dev/null

echo -n "%%%{PBXSelection}%%%"
cat $TMP
echo -n "%%%{PBXSelection}%%%"

rm $TMP

Restriction: Be advised, there must be always a new line at the end of the source file!

BTW, don't forget to change that <***PATH_TO_YOUR_ASTYLE_RC_FILE***> to a real path to your astyle resource file ;)

The script above is actually a second version of my script. The first one was much simpler:

#!/bin/sh

echo -n "%%%{PBXSelection}%%%"
/usr/bin/astyle --options=<***PATH_TO_YOUR_ASTYLE_RC_FILE***> <&0 echo -n "%%%{PBXSelection}%%%"
But this version didn't work for me with astyle 1.24. This is why in the second version of the script:
I pass selected file instead of a selection, because astyle 1.24 fails to parse struct with access modifiers. It just hangs instead. I also use a tmp file in order to avoid annoying Xcode message, that source file was changed outside of Xcode.
Finally, just assign some shortcuts (I prefer Options + Command + L) to you script and voilĂ ...

Nov 10, 2007

Useful Eclipse plug-ins for C++ Developers

With this post I would like to share my preferences of plug-ins for Eclipse.
Most of the time I do a C++ development (mostly under Linux). Since I came from Windows world and I was using the best development environment ever made - MS Visual Studio :) I wanted to find something similar in the Linux world. I tried emacs, KDevelop and Eclipse. I wont compare all of them, probably I will do that some time later, I will just conclude that I choose Eclipse and CDT.
Well, currently I am suing the following useful plug-ins:
Also I don't use the original code formating mechanism of Eclipse CDT, rather I use AStyle as an external tool, which is much more powerful. Here I have an instraction for you how to use AStyle in Eclipse:
  • Select the dropdown button for "External Tools", then click on "Externals Tools..."
  • Select "Program" in the Configurations window then click the New button
  • For the "Name" call it "AStyle", for the "Location" put the full path and filename for AStyle (i.e. /usr/bin/astyle)
  • For "Arguments", put the AStyle arguments to get the code formatted to your style. I like ANSI style and 3 spaces for indentation, hence, the arguments would be "--style=ansi -s3" (no quotes) or use --options=####
  • Now put your cursor at the end of the "Arguments" and click the "Variables" button and then select the resource_loc" and click OK
  • Your arguments should look like "--style=ansi -s3 ${resource_loc}" (no quotes)
  • Now click the "Refresh" tab, and click the check-box "Refresh resources upon completion" and then click the "The Selected Resource" radio button.
  • Click the "Apply" button then the "Close" button.