1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-29 11:55:40 +02:00

Improve source hover comment heuristic

This commit is contained in:
Anton Leherbauer 2007-04-10 15:41:24 +00:00
parent ce48d44b56
commit 2922ae70fd

View file

@ -299,13 +299,22 @@ public class CSourceHover extends AbstractCEditorTextHover implements ITextHover
} else { } else {
final int nameLine= doc.getLineOfOffset(nameOffset); final int nameLine= doc.getLineOfOffset(nameOffset);
sourceStart= doc.getLineOffset(nameLine); sourceStart= doc.getLineOffset(nameLine);
int commentBound= scanner.scanBackward(sourceStart, CHeuristicScanner.UNBOUND, new char[] { '{', '}', ';', ':' }); int commentBound= scanner.scanBackward(sourceStart, CHeuristicScanner.UNBOUND, new char[] { '{', '}', ';' });
if (commentBound == CHeuristicScanner.NOT_FOUND) { if (commentBound == CHeuristicScanner.NOT_FOUND) {
commentBound= -1; // unbound commentBound= -1; // unbound
} }
int commentStart= searchCommentBackward(doc, sourceStart, commentBound); int commentStart= searchCommentBackward(doc, sourceStart, commentBound);
if (commentStart >= 0) { if (commentStart >= 0) {
sourceStart= commentStart; sourceStart= commentStart;
} else {
int nextNonWS= scanner.findNonWhitespaceForward(commentBound+1, sourceStart);
if (nextNonWS != CHeuristicScanner.NOT_FOUND) {
int nextNonWSLine= doc.getLineOfOffset(nextNonWS);
int lineOffset= doc.getLineOffset(nextNonWSLine);
if (doc.get(lineOffset, nextNonWS - lineOffset).trim().length() == 0) {
sourceStart= doc.getLineOffset(nextNonWSLine);
}
}
} }
} }
// expand forward to the end of the definition/declaration // expand forward to the end of the definition/declaration
@ -319,10 +328,12 @@ public class CSourceHover extends AbstractCEditorTextHover implements ITextHover
boolean searchBrace= false; boolean searchBrace= false;
boolean searchSemi= false; boolean searchSemi= false;
boolean searchComma= false; boolean searchComma= false;
if (binding instanceof ICompositeType || binding instanceof IEnumeration || binding instanceof IFunction) { if (binding instanceof ICompositeType || binding instanceof IEnumeration) {
searchBrace= true; searchBrace= true;
} else if (binding instanceof ICPPTemplateDefinition) { } else if (binding instanceof ICPPTemplateDefinition) {
searchBrace= true; searchBrace= true;
} else if (binding instanceof IFunction && name.isDefinition()) {
searchBrace= true;
} else if (binding instanceof IParameter || binding instanceof IEnumerator || binding instanceof ICPPTemplateParameter) { } else if (binding instanceof IParameter || binding instanceof IEnumerator || binding instanceof ICPPTemplateParameter) {
searchComma= true; searchComma= true;
} else if (binding instanceof IVariable || binding instanceof ITypedef) { } else if (binding instanceof IVariable || binding instanceof ITypedef) {
@ -548,7 +559,7 @@ public class CSourceHover extends AbstractCEditorTextHover implements ITextHover
private static int searchCommentBackward(IDocument doc, int start, int bound) throws BadLocationException { private static int searchCommentBackward(IDocument doc, int start, int bound) throws BadLocationException {
int firstLine= doc.getLineOfOffset(start); int firstLine= doc.getLineOfOffset(start);
if (firstLine == 0) { if (firstLine == 0) {
return -1; return 0;
} }
ITypedRegion partition= TextUtilities.getPartition(doc, ICPartitions.C_PARTITIONING, start, true); ITypedRegion partition= TextUtilities.getPartition(doc, ICPartitions.C_PARTITIONING, start, true);
int currentOffset= Math.max(doc.getLineOffset(firstLine - 1), partition.getOffset() - 1); int currentOffset= Math.max(doc.getLineOffset(firstLine - 1), partition.getOffset() - 1);
@ -557,25 +568,28 @@ public class CSourceHover extends AbstractCEditorTextHover implements ITextHover
partition= TextUtilities.getPartition(doc, ICPartitions.C_PARTITIONING, currentOffset, true); partition= TextUtilities.getPartition(doc, ICPartitions.C_PARTITIONING, currentOffset, true);
currentOffset= partition.getOffset() - 1; currentOffset= partition.getOffset() - 1;
if (ICPartitions.C_MULTI_LINE_COMMENT.equals(partition.getType())) { if (ICPartitions.C_MULTI_LINE_COMMENT.equals(partition.getType())) {
int previousCommentOffset= commentOffset; final int partitionOffset= partition.getOffset();
commentOffset = partition.getOffset(); final int startLine= doc.getLineOfOffset(partitionOffset);
final int startLine= doc.getLineOfOffset(commentOffset);
final int lineOffset= doc.getLineOffset(startLine); final int lineOffset= doc.getLineOffset(startLine);
if (commentOffset == lineOffset || if (partitionOffset == lineOffset ||
doc.get(lineOffset, commentOffset - lineOffset).trim().length() == 0) { doc.get(lineOffset, partitionOffset - lineOffset).trim().length() == 0) {
return lineOffset; return lineOffset;
} }
return previousCommentOffset; return commentOffset;
} else if (ICPartitions.C_SINGLE_LINE_COMMENT.equals(partition.getType())) { } else if (ICPartitions.C_SINGLE_LINE_COMMENT.equals(partition.getType())) {
commentOffset = partition.getOffset(); final int partitionOffset= partition.getOffset();
final int startLine= doc.getLineOfOffset(commentOffset); final int startLine= doc.getLineOfOffset(partitionOffset);
final int lineOffset= doc.getLineOffset(startLine); final int lineOffset= doc.getLineOffset(startLine);
if (commentOffset == lineOffset || if (partitionOffset == lineOffset ||
doc.get(lineOffset, commentOffset - lineOffset).trim().length() == 0) { doc.get(lineOffset, partitionOffset - lineOffset).trim().length() == 0) {
commentOffset= lineOffset;
continue; continue;
} }
return commentOffset; return commentOffset;
} else if (IDocument.DEFAULT_CONTENT_TYPE.equals(partition.getType())) { } else if (IDocument.DEFAULT_CONTENT_TYPE.equals(partition.getType())) {
if (doc.get(partition.getOffset(), partition.getLength()).trim().length() == 0) {
continue;
}
if (commentOffset >= 0) { if (commentOffset >= 0) {
break; break;
} }