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

Code folding is buggy #1424

Merged
merged 1 commit into from
Apr 29, 2020
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 @@ -82,7 +82,7 @@ private void computeFoldingRanges(List<FoldingRange> foldingRanges, ITypeRoot un
scanner.resetTo(shift, shift + range.getLength());

int start = shift;
int token = scanner.getNextToken();
int token = 0;
Stack<Integer> regionStarts = new Stack<>();
while (token != ITerminalSymbols.TokenNameEOF) {
start = scanner.getCurrentTokenStartPosition();
Expand All @@ -109,19 +109,29 @@ private void computeFoldingRanges(List<FoldingRange> foldingRanges, ITypeRoot un
default:
break;
}
token = scanner.getNextToken();
token = getNextToken(scanner);
}

computeTypeRootRanges(foldingRanges, unit, scanner);
} catch (CoreException |

InvalidInputException e) {
} catch (CoreException e) {
JavaLanguageServerPlugin.logException("Problem with folding range for " + unit.getPath().toPortableString(), e);
monitor.setCanceled(true);
}
}

private void computeTypeRootRanges(List<FoldingRange> foldingRanges, ITypeRoot unit, IScanner scanner) throws CoreException, InvalidInputException {
private int getNextToken(IScanner scanner) {
int token = 0;
while (token == 0) {
try {
token = scanner.getNextToken();
} catch (InvalidInputException e) {
// ignore
// JavaLanguageServerPlugin.logException("Problem with folding range", e);
}
}
return token;
}

private void computeTypeRootRanges(List<FoldingRange> foldingRanges, ITypeRoot unit, IScanner scanner) throws CoreException {
if (unit.hasChildren()) {
for (IJavaElement child : unit.getChildren()) {
if (child instanceof IImportContainer) {
Expand All @@ -136,7 +146,7 @@ private void computeTypeRootRanges(List<FoldingRange> foldingRanges, ITypeRoot u
}
}

private void computeTypeRanges(List<FoldingRange> foldingRanges, IType unit, IScanner scanner) throws CoreException, InvalidInputException {
private void computeTypeRanges(List<FoldingRange> foldingRanges, IType unit, IScanner scanner) throws CoreException {
ISourceRange typeRange = unit.getSourceRange();
foldingRanges.add(new FoldingRange(scanner.getLineNumber(unit.getNameRange().getOffset()) - 1, scanner.getLineNumber(typeRange.getOffset() + typeRange.getLength()) - 1));
IJavaElement[] children = unit.getChildren();
Expand All @@ -149,15 +159,15 @@ private void computeTypeRanges(List<FoldingRange> foldingRanges, IType unit, ISc
}
}

private void computeMethodRanges(List<FoldingRange> foldingRanges, IMethod method, IScanner scanner) throws CoreException, InvalidInputException {
private void computeMethodRanges(List<FoldingRange> foldingRanges, IMethod method, IScanner scanner) throws CoreException {
ISourceRange sourceRange = method.getSourceRange();
final int shift = sourceRange.getOffset();
scanner.resetTo(shift, shift + sourceRange.getLength());

foldingRanges.add(new FoldingRange(scanner.getLineNumber(method.getNameRange().getOffset()) - 1, scanner.getLineNumber(shift + sourceRange.getLength()) - 1));

int start = shift;
int token = scanner.getNextToken();
int token = 0;
Stack<Integer> leftParens = null;
int prevCaseLine = -1;
Map<Integer, Integer> candidates = new HashMap<>();
Expand Down Expand Up @@ -208,7 +218,7 @@ private void computeMethodRanges(List<FoldingRange> foldingRanges, IMethod metho
default:
break;
}
token = scanner.getNextToken();
token = getNextToken(scanner);
}

for (Map.Entry<Integer, Integer> entry : candidates.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.sample;

/**
* InvalidInputRange
*/
public class InvalidInputRange {

void foo() {
char c = '';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ public void testErrorTypes() throws Exception {
assertHasFoldingRange(5, 7, null, foldingRanges);
}

@Test
public void testInvalidInput() throws Exception {
String className = "org.sample.InvalidInputRange";
List<FoldingRange> foldingRanges = getFoldingRanges(className);
assertTrue(foldingRanges.size() == 3);
assertHasFoldingRange(2, 4, "comment", foldingRanges);
assertHasFoldingRange(5, 10, null, foldingRanges);
assertHasFoldingRange(7, 9, null, foldingRanges);
}

@Test
public void testRegionFoldingRanges() throws Exception {
String className = "org.sample.RegionFoldingRange";
Expand Down