From bea717daa38174046dc2b102b00a87779025d801 Mon Sep 17 00:00:00 2001 From: Gil Barash Date: Sat, 14 Jan 2012 14:28:41 +0200 Subject: [PATCH] Support also C statements (not only C++) --- .../cdt/ClosingBracketHintVisitor.java | 37 ++++++++++++------- README.md | 1 + 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/BracketeerCDT/src/com/chookapp/org/bracketeer/cdt/ClosingBracketHintVisitor.java b/BracketeerCDT/src/com/chookapp/org/bracketeer/cdt/ClosingBracketHintVisitor.java index f37bf4b..e4ed33b 100644 --- a/BracketeerCDT/src/com/chookapp/org/bracketeer/cdt/ClosingBracketHintVisitor.java +++ b/BracketeerCDT/src/com/chookapp/org/bracketeer/cdt/ClosingBracketHintVisitor.java @@ -11,6 +11,7 @@ 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; @@ -18,14 +19,14 @@ 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; @@ -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); @@ -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(); @@ -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; @@ -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(); @@ -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); @@ -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) ) @@ -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; diff --git a/README.md b/README.md index df71f3b..a5b003f 100644 --- a/README.md +++ b/README.md @@ -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++).