1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-17 21:25:58 +02:00

Improve folding position keys

This commit is contained in:
Anton Leherbauer 2008-01-25 10:10:12 +00:00
parent ccabb31c65
commit 067fd3eb17

View file

@ -55,10 +55,13 @@ import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTBreakStatement; import org.eclipse.cdt.core.dom.ast.IASTBreakStatement;
import org.eclipse.cdt.core.dom.ast.IASTCaseStatement; import org.eclipse.cdt.core.dom.ast.IASTCaseStatement;
import org.eclipse.cdt.core.dom.ast.IASTCompoundStatement; import org.eclipse.cdt.core.dom.ast.IASTCompoundStatement;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTDefaultStatement; import org.eclipse.cdt.core.dom.ast.IASTDefaultStatement;
import org.eclipse.cdt.core.dom.ast.IASTDoStatement; import org.eclipse.cdt.core.dom.ast.IASTDoStatement;
import org.eclipse.cdt.core.dom.ast.IASTFileLocation; import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
import org.eclipse.cdt.core.dom.ast.IASTForStatement; 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.IASTIfStatement;
import org.eclipse.cdt.core.dom.ast.IASTNodeLocation; import org.eclipse.cdt.core.dom.ast.IASTNodeLocation;
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorElifStatement; import org.eclipse.cdt.core.dom.ast.IASTPreprocessorElifStatement;
@ -1102,6 +1105,8 @@ public class DefaultCFoldingStructureProvider implements ICFoldingStructureProvi
* It tells us whether or not to include the last line of the region * It tells us whether or not to include the last line of the region
*/ */
public boolean inclusive; public boolean inclusive;
public String function;
public int level;
} }
/** /**
@ -1115,9 +1120,12 @@ public class DefaultCFoldingStructureProvider implements ICFoldingStructureProvi
ast.accept(new ASTVisitor() { ast.accept(new ASTVisitor() {
{ {
shouldVisitStatements = true; shouldVisitStatements = true;
shouldVisitDeclarations = true;
} }
int fLevel= 0;
String fFunction= ""; //$NON-NLS-1$
public int visit(IASTStatement statement) { public int visit(IASTStatement statement) {
++fLevel;
// if it's not part of the displayed - file, we don't need it // if it's not part of the displayed - file, we don't need it
if (!statement.isPartOfTranslationUnitFile()) if (!statement.isPartOfTranslationUnitFile())
return PROCESS_SKIP;// we neither need its descendants return PROCESS_SKIP;// we neither need its descendants
@ -1131,6 +1139,8 @@ public class DefaultCFoldingStructureProvider implements ICFoldingStructureProvi
int ifOffset= fl.getNodeOffset(); int ifOffset= fl.getNodeOffset();
IASTStatement tmp; IASTStatement tmp;
mr = new ModifiableRegionExtra(); mr = new ModifiableRegionExtra();
mr.function= fFunction;
mr.level= fLevel;
tmp = ifstmt.getThenClause(); tmp = ifstmt.getThenClause();
if (tmp==null) return PROCESS_CONTINUE; if (tmp==null) return PROCESS_CONTINUE;
fl = tmp.getFileLocation(); fl = tmp.getFileLocation();
@ -1145,6 +1155,8 @@ public class DefaultCFoldingStructureProvider implements ICFoldingStructureProvi
} }
iral.push(mr); iral.push(mr);
mr = new ModifiableRegionExtra(); mr = new ModifiableRegionExtra();
mr.function= fFunction;
mr.level= fLevel;
fl = tmp.getFileLocation(); fl = tmp.getFileLocation();
mr.setLength(fl.getNodeLength()); mr.setLength(fl.getNodeLength());
mr.setOffset(fl.getNodeOffset()); mr.setOffset(fl.getNodeOffset());
@ -1153,6 +1165,8 @@ public class DefaultCFoldingStructureProvider implements ICFoldingStructureProvi
return PROCESS_CONTINUE; return PROCESS_CONTINUE;
} }
mr = new ModifiableRegionExtra(); mr = new ModifiableRegionExtra();
mr.function= fFunction;
mr.level= fLevel;
mr.inclusive = true; mr.inclusive = true;
if (statement instanceof IASTDoStatement) if (statement instanceof IASTDoStatement)
mr.inclusive = false; mr.inclusive = false;
@ -1174,6 +1188,8 @@ public class DefaultCFoldingStructureProvider implements ICFoldingStructureProvi
} }
IASTFileLocation tmpfl; IASTFileLocation tmpfl;
tmpmr = new ModifiableRegionExtra(); tmpmr = new ModifiableRegionExtra();
tmpmr.function= fFunction;
tmpmr.level= fLevel+1;
tmpmr.inclusive = true; tmpmr.inclusive = true;
if (tmpstmt instanceof IASTCaseStatement) { if (tmpstmt instanceof IASTCaseStatement) {
IASTCaseStatement casestmt = (IASTCaseStatement) tmpstmt; IASTCaseStatement casestmt = (IASTCaseStatement) tmpstmt;
@ -1206,13 +1222,32 @@ public class DefaultCFoldingStructureProvider implements ICFoldingStructureProvi
return PROCESS_ABORT; return PROCESS_ABORT;
} }
} }
public int leave(IASTStatement statement) {
--fLevel;
return PROCESS_CONTINUE;
}
public int visit(IASTDeclaration declaration) {
if (!declaration.isPartOfTranslationUnitFile())
return PROCESS_SKIP;// we neither need its descendants
if (declaration instanceof IASTFunctionDefinition) {
final IASTFunctionDeclarator declarator = ((IASTFunctionDefinition)declaration).getDeclarator();
if (declarator != null) {
fFunction= new String(declarator.getName().toCharArray());
}
}
return PROCESS_CONTINUE;
}
public int leave(IASTDeclaration declaration) {
fFunction= ""; //$NON-NLS-1$
return PROCESS_CONTINUE;
}
}); });
while (!iral.empty()) { while (!iral.empty()) {
ModifiableRegionExtra mr = (ModifiableRegionExtra) iral.pop(); ModifiableRegionExtra mr = (ModifiableRegionExtra) iral.pop();
IRegion aligned = alignRegion(mr, ctx,mr.inclusive); IRegion aligned = alignRegion(mr, ctx,mr.inclusive);
if (aligned != null) { if (aligned != null) {
Position alignedPos= new Position(aligned.getOffset(), aligned.getLength()); Position alignedPos= new Position(aligned.getOffset(), aligned.getLength());
ctx.addProjectionRange(new CProjectionAnnotation(false, computeKey(mr, ctx), false), alignedPos); ctx.addProjectionRange(new CProjectionAnnotation(false, mr.function + mr.level + computeKey(mr, ctx), false), alignedPos);
} }
} }
} }