Skip to content

Commit

Permalink
Support also C statements (not only C++)
Browse files Browse the repository at this point in the history
  • Loading branch information
chookapp committed Jan 14, 2012
1 parent 033f116 commit bea717d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,22 @@
package com.chookapp.org.bracketeer.cdt;

import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTCompositeTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTCompoundStatement;
import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTExpression;
import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
import org.eclipse.cdt.core.dom.ast.IASTForStatement;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition;
import org.eclipse.cdt.core.dom.ast.IASTIfStatement;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTParameterDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTStatement;
import org.eclipse.cdt.core.dom.ast.IASTSwitchStatement;
import org.eclipse.cdt.core.dom.ast.IASTWhileStatement;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionDefinition;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTIfStatement;

import com.chookapp.org.bracketeer.common.Hint;
Expand Down Expand Up @@ -55,8 +56,8 @@ public int visit(IASTStatement statement)
{
try
{
if( statement instanceof ICPPASTIfStatement )
visitIf((ICPPASTIfStatement) statement);
if( statement instanceof IASTIfStatement )
visitIf((IASTIfStatement) statement);

if( statement instanceof IASTSwitchStatement )
visitSwitch((IASTSwitchStatement) statement);
Expand All @@ -74,15 +75,23 @@ public int visit(IASTStatement statement)
return shouldContinue();
}

private void visitIf(ICPPASTIfStatement statement)
private void visitIf(IASTIfStatement statement)
{
/* TODO: specific params: don't show the if hint if there's an "else if" after it (by checking if the elseClause is an instance of ifstatment) */

String hint = "";
if( statement.getConditionExpression() != null )
{
hint = statement.getConditionExpression().getRawSignature();
else if( statement.getConditionDeclaration() != null )
hint = statement.getConditionDeclaration().getRawSignature();
}
else
{
if( (statement instanceof ICPPASTIfStatement) &&
((ICPPASTIfStatement)statement).getConditionDeclaration() != null )
{
hint = ((ICPPASTIfStatement)statement).getConditionDeclaration().getRawSignature();
}
}

IASTStatement thenClause = statement.getThenClause();
IASTStatement elseClause = statement.getElseClause();
Expand All @@ -98,7 +107,7 @@ else if( statement.getConditionDeclaration() != null )
}

// if the else looks like this "} else {", then show the hint on the "{"
if( !showIfHint && !(elseClause instanceof ICPPASTIfStatement) )
if( !showIfHint && !(elseClause instanceof IASTIfStatement) )
{
endLoc = elseClause.getFileLocation().getNodeOffset();
showIfHint = true;
Expand All @@ -117,7 +126,7 @@ else if( statement.getConditionDeclaration() != null )
_container.add(new Hint("if", startLoc, endLoc, "if("+hint+")")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
}

if( elseClause != null && !(elseClause instanceof ICPPASTIfStatement) &&
if( elseClause != null && !(elseClause instanceof IASTIfStatement) &&
(elseClause instanceof IASTCompoundStatement))
{
IASTFileLocation location = elseClause.getFileLocation();
Expand Down Expand Up @@ -178,8 +187,8 @@ public int visit(IASTDeclaration declaration)
{
try
{
if( declaration instanceof ICPPASTFunctionDefinition )
visitFunc((ICPPASTFunctionDefinition) declaration);
if( declaration instanceof IASTFunctionDefinition )
visitFunc((IASTFunctionDefinition) declaration);

if( declaration instanceof IASTSimpleDeclaration)
visitType((IASTSimpleDeclaration) declaration);
Expand All @@ -191,7 +200,7 @@ public int visit(IASTDeclaration declaration)
return shouldContinue();
}

private void visitFunc(ICPPASTFunctionDefinition declaration)
private void visitFunc(IASTFunctionDefinition declaration)
{
IASTStatement body = declaration.getBody();
if(!( body instanceof IASTCompoundStatement) )
Expand Down Expand Up @@ -232,9 +241,9 @@ private void visitType(IASTSimpleDeclaration declaration)
/* TODO: specific params: include type('class' / 'struct') */

IASTDeclSpecifier spec = declaration.getDeclSpecifier();
if( spec instanceof ICPPASTCompositeTypeSpecifier )
if( spec instanceof IASTCompositeTypeSpecifier )
{
String hint = ((ICPPASTCompositeTypeSpecifier)spec).getName().getRawSignature();
String hint = ((IASTCompositeTypeSpecifier)spec).getName().getRawSignature();
if( hint.isEmpty() )
return;

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@ Build 1.1.0 (next release)
* Option of using a rectangle outline when highlighting brackets.
* Option to ignore pairs which are close to eachother.
* Added annotation to "missing pair".
* Fixed [issue 4](https://github.com/chookapp/Bracketeer/issues/4) - Support also C statements (not only C++).

0 comments on commit bea717d

Please sign in to comment.