Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add folding range for multiple single-line comments #2797

Merged
merged 1 commit into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,22 @@ private void computeFoldingRanges(List<FoldingRange> foldingRanges, ITypeRoot un
scanner.resetTo(shift, shift + range.getLength());

int start = shift;
int startLine = -1;
int token = 0;
int classFileImportStart = -1;
int classFileImportEnd = -1;
int singleLineCommentStart = -1;
int singleLineCommentEnd = -1;
int prevTokenLine = -1;
Stack<Integer> regionStarts = new Stack<>();
while (token != ITerminalSymbols.TokenNameEOF) {
start = scanner.getCurrentTokenStartPosition();
startLine = scanner.getLineNumber(start);
switch (token) {
case ITerminalSymbols.TokenNameCOMMENT_JAVADOC:
case ITerminalSymbols.TokenNameCOMMENT_BLOCK:
int end = scanner.getCurrentTokenEndPosition();
FoldingRange commentFoldingRange = new FoldingRange(scanner.getLineNumber(start) - 1, scanner.getLineNumber(end) - 1);
FoldingRange commentFoldingRange = new FoldingRange(startLine - 1, scanner.getLineNumber(end) - 1);
commentFoldingRange.setKind(FoldingRangeKind.Comment);
foldingRanges.add(commentFoldingRange);
break;
Expand All @@ -116,6 +121,14 @@ private void computeFoldingRanges(List<FoldingRange> foldingRanges, ITypeRoot un
regionFolding.setKind(FoldingRangeKind.Region);
foldingRanges.add(regionFolding);
}
} else if (prevTokenLine == startLine) {
addCommentRangeIfSuitable(foldingRanges, singleLineCommentStart, singleLineCommentEnd);
} else if (startLine == singleLineCommentEnd + 1) {
singleLineCommentEnd = startLine;
} else {
addCommentRangeIfSuitable(foldingRanges, singleLineCommentStart, singleLineCommentEnd);
singleLineCommentStart = startLine;
singleLineCommentEnd = startLine;
}
break;
case ITerminalSymbols.TokenNameimport:
Expand All @@ -125,13 +138,15 @@ private void computeFoldingRanges(List<FoldingRange> foldingRanges, ITypeRoot un
default:
break;
}
prevTokenLine = startLine;
token = getNextToken(scanner);
}
if (unit.getElementType() == IJavaElement.CLASS_FILE && classFileImportStart != -1) {
FoldingRange importFoldingRange = new FoldingRange(scanner.getLineNumber(classFileImportStart) - 1, scanner.getLineNumber(classFileImportEnd) - 1);
importFoldingRange.setKind(FoldingRangeKind.Imports);
foldingRanges.add(importFoldingRange);
}
addCommentRangeIfSuitable(foldingRanges, singleLineCommentStart, singleLineCommentEnd);
computeTypeRootRanges(foldingRanges, unit, scanner);
} catch (CoreException e) {
JavaLanguageServerPlugin.logException("Problem with folding range for " + unit.getPath().toPortableString(), e);
Expand Down Expand Up @@ -248,4 +263,12 @@ private void computeMethodRanges(List<FoldingRange> foldingRanges, IMember membe
foldingRanges.add(new FoldingRange(entry.getValue(), entry.getKey()));
}
}

private void addCommentRangeIfSuitable(List<FoldingRange> foldingRanges, int start, int end) {
if (end > start) {
FoldingRange singleLineCommentFoldingRange = new FoldingRange(start - 1, end - 1);
singleLineCommentFoldingRange.setKind(FoldingRangeKind.Comment);
foldingRanges.add(singleLineCommentFoldingRange);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,11 @@ void bar() {
if (true) {
System.out.println("True");
}
}
}
// this
} // shouldn't
// fold
}

// multiple
// single-line
// comments
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,14 @@ public void testFoldingRanges() throws Exception {
public void testTypes() throws Exception {
String className = "org.sample.SimpleFoldingRange";
List<FoldingRange> foldingRanges = getFoldingRanges(className);
assertTrue(foldingRanges.size() == 7);
assertTrue(foldingRanges.size() == 8);
assertHasFoldingRange(2, 3, FoldingRangeKind.Imports, foldingRanges);
assertHasFoldingRange(5, 7, FoldingRangeKind.Comment, foldingRanges);
assertHasFoldingRange(8, 24, null, foldingRanges);
assertHasFoldingRange(8, 26, null, foldingRanges);
assertHasFoldingRange(10, 14, FoldingRangeKind.Comment, foldingRanges);
assertHasFoldingRange(19, 23, null, foldingRanges);
assertHasFoldingRange(19, 24, null, foldingRanges);
assertHasFoldingRange(20, 22, null, foldingRanges);
assertHasFoldingRange(28, 30, FoldingRangeKind.Comment, foldingRanges);
}

@Test
Expand Down