use of org.eclipse.lsp4j.FoldingRange in project eclipse.jdt.ls by eclipse.
the class FoldingRangeHandler method foldingRange.
public List<FoldingRange> foldingRange(FoldingRangeRequestParams params, IProgressMonitor monitor) {
List<FoldingRange> $ = new ArrayList<>();
ITypeRoot unit = null;
try {
PreferenceManager preferenceManager = JavaLanguageServerPlugin.getInstance().getPreferencesManager();
boolean returnCompilationUnit = preferenceManager == null ? false : preferenceManager.isClientSupportsClassFileContent() && (preferenceManager.getPreferences().isIncludeDecompiledSources());
unit = JDTUtils.resolveTypeRoot(params.getTextDocument().getUri(), returnCompilationUnit, monitor);
if (unit == null || (monitor != null && monitor.isCanceled())) {
return $;
}
computeFoldingRanges($, unit, monitor);
return $;
} finally {
JDTUtils.discardClassFileWorkingCopy(unit);
}
}
use of org.eclipse.lsp4j.FoldingRange in project eclipse.jdt.ls by eclipse.
the class FoldingRangeHandler method computeFoldingRanges.
private void computeFoldingRanges(List<FoldingRange> foldingRanges, ITypeRoot unit, IProgressMonitor monitor) {
try {
ISourceRange range = unit.getSourceRange();
if (!SourceRange.isAvailable(range)) {
return;
}
String contents = unit.getSource();
if (StringUtils.isBlank(contents)) {
return;
}
final int shift = range.getOffset();
IScanner scanner = getScanner();
scanner.setSource(contents.toCharArray());
scanner.resetTo(shift, shift + range.getLength());
int start = shift;
int token = 0;
Stack<Integer> regionStarts = new Stack<>();
while (token != ITerminalSymbols.TokenNameEOF) {
start = scanner.getCurrentTokenStartPosition();
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);
commentFoldingRange.setKind(FoldingRangeKind.Comment);
foldingRanges.add(commentFoldingRange);
break;
case ITerminalSymbols.TokenNameCOMMENT_LINE:
String currentSource = String.valueOf(scanner.getCurrentTokenSource());
if (REGION_START_PATTERN.matcher(currentSource).lookingAt()) {
regionStarts.push(start);
} else if (REGION_END_PATTERN.matcher(currentSource).lookingAt()) {
if (regionStarts.size() > 0) {
FoldingRange regionFolding = new FoldingRange(scanner.getLineNumber(regionStarts.pop()) - 1, scanner.getLineNumber(start) - 1);
regionFolding.setKind(FoldingRangeKind.Region);
foldingRanges.add(regionFolding);
}
}
break;
default:
break;
}
token = getNextToken(scanner);
}
computeTypeRootRanges(foldingRanges, unit, scanner);
} catch (CoreException e) {
JavaLanguageServerPlugin.logException("Problem with folding range for " + unit.getPath().toPortableString(), e);
monitor.setCanceled(true);
}
}
Aggregations