use of org.eclipse.lsp4j.FoldingRange in project xtext-core by eclipse.
the class FoldingRangeService method toFoldingRange.
protected FoldingRange toFoldingRange(Document document, org.eclipse.xtext.ide.editor.folding.FoldingRange range) {
int offset = range.getOffset();
int length = range.getLength();
int endOffset = offset + length;
Position start = document.getPosition(offset);
Position end = document.getPosition(endOffset);
FoldingRange result = new FoldingRange(start.getLine(), end.getLine());
if (range.getKind() != null) {
result.setKind(range.getKind().toString());
}
return result;
}
use of org.eclipse.lsp4j.FoldingRange in project eclipse.jdt.ls by eclipse.
the class FoldingRangeHandler method computeTypeRootRanges.
private void computeTypeRootRanges(List<FoldingRange> foldingRanges, ITypeRoot unit, IScanner scanner) throws CoreException {
if (unit.hasChildren()) {
for (IJavaElement child : unit.getChildren()) {
if (child instanceof IImportContainer) {
ISourceRange importRange = ((IImportContainer) child).getSourceRange();
FoldingRange importFoldingRange = new FoldingRange(scanner.getLineNumber(importRange.getOffset()) - 1, scanner.getLineNumber(importRange.getOffset() + importRange.getLength()) - 1);
importFoldingRange.setKind(FoldingRangeKind.Imports);
foldingRanges.add(importFoldingRange);
} else if (child instanceof IType) {
computeTypeRanges(foldingRanges, (IType) child, scanner);
}
}
}
}
use of org.eclipse.lsp4j.FoldingRange in project eclipse.jdt.ls by eclipse.
the class FoldingRangeHandler method computeMethodRanges.
private void computeMethodRanges(List<FoldingRange> foldingRanges, IMember member, IScanner scanner) throws CoreException {
ISourceRange sourceRange = member.getSourceRange();
final int shift = sourceRange.getOffset();
scanner.resetTo(shift, shift + sourceRange.getLength());
ISourceRange nameRange = member.getNameRange();
int nameStart = nameRange != null ? nameRange.getOffset() : sourceRange.getOffset();
foldingRanges.add(new FoldingRange(scanner.getLineNumber(nameStart) - 1, scanner.getLineNumber(shift + sourceRange.getLength()) - 1));
int start = shift;
int token = 0;
Stack<Integer> leftParens = null;
int prevCaseLine = -1;
Map<Integer, Integer> candidates = new HashMap<>();
while (token != ITerminalSymbols.TokenNameEOF) {
start = scanner.getCurrentTokenStartPosition();
switch(token) {
case ITerminalSymbols.TokenNameLBRACE:
if (leftParens == null) {
// Start of method body
leftParens = new Stack<>();
} else {
int startLine = scanner.getLineNumber(start) - 1;
// Start & end overlap, adjust the previous one for visibility:
if (candidates.containsKey(startLine)) {
int originalStartLine = candidates.remove(startLine);
if (originalStartLine < startLine - 1) {
candidates.put(startLine - 1, originalStartLine);
}
}
leftParens.push(startLine);
}
break;
case ITerminalSymbols.TokenNameRBRACE:
int endPos = scanner.getCurrentTokenEndPosition();
if (leftParens != null && leftParens.size() > 0) {
int endLine = scanner.getLineNumber(endPos) - 1;
int startLine = leftParens.pop();
if (startLine < endLine) {
candidates.put(endLine, startLine);
}
// Assume the last switch case:
if (startLine < prevCaseLine) {
if (endLine - 1 > prevCaseLine) {
candidates.put(endLine - 1, prevCaseLine);
}
prevCaseLine = -1;
}
}
break;
case ITerminalSymbols.TokenNamecase:
case ITerminalSymbols.TokenNamedefault:
int currentLine = scanner.getLineNumber(start) - 1;
if (prevCaseLine != -1 && currentLine - 1 >= prevCaseLine) {
candidates.put(scanner.getLineNumber(start) - 2, prevCaseLine);
}
prevCaseLine = currentLine;
break;
default:
break;
}
token = getNextToken(scanner);
}
for (Map.Entry<Integer, Integer> entry : candidates.entrySet()) {
foldingRanges.add(new FoldingRange(entry.getValue(), entry.getKey()));
}
}
use of org.eclipse.lsp4j.FoldingRange in project eclipse.jdt.ls by eclipse.
the class FoldingRangeHandler method computeTypeRanges.
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();
for (IJavaElement c : children) {
if (c instanceof IMethod || c instanceof IInitializer) {
computeMethodRanges(foldingRanges, (IMember) c, scanner);
} else if (c instanceof IType) {
computeTypeRanges(foldingRanges, (IType) c, scanner);
}
}
}
use of org.eclipse.lsp4j.FoldingRange in project xtext-core by eclipse.
the class AbstractLanguageServerTest method testFolding.
/**
* @since 2.26
*/
protected void testFolding(final Procedure1<? super FoldingConfiguration> configurator) {
try {
@Extension final FoldingConfiguration configuration = new FoldingConfiguration();
configuration.setFilePath(("MyModel." + this.fileExtension));
configurator.apply(configuration);
final FileInfo fileInfo = this.initializeContext(configuration);
FoldingRangeRequestParams _foldingRangeRequestParams = new FoldingRangeRequestParams();
final Procedure1<FoldingRangeRequestParams> _function = (FoldingRangeRequestParams it) -> {
String _uri = fileInfo.getUri();
TextDocumentIdentifier _textDocumentIdentifier = new TextDocumentIdentifier(_uri);
it.setTextDocument(_textDocumentIdentifier);
};
FoldingRangeRequestParams _doubleArrow = ObjectExtensions.<FoldingRangeRequestParams>operator_doubleArrow(_foldingRangeRequestParams, _function);
final List<FoldingRange> foldings = this.languageServer.foldingRange(_doubleArrow).get();
this.assertEqualsStricter(configuration.getExpectedFoldings(), this.toExpectation(foldings));
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
}
}
Aggregations