Search in sources :

Example 1 with FoldingRange

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;
}
Also used : Position(org.eclipse.lsp4j.Position) FoldingRange(org.eclipse.lsp4j.FoldingRange)

Example 2 with FoldingRange

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);
            }
        }
    }
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) IImportContainer(org.eclipse.jdt.core.IImportContainer) FoldingRange(org.eclipse.lsp4j.FoldingRange) ISourceRange(org.eclipse.jdt.core.ISourceRange) IType(org.eclipse.jdt.core.IType)

Example 3 with FoldingRange

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()));
    }
}
Also used : HashMap(java.util.HashMap) FoldingRange(org.eclipse.lsp4j.FoldingRange) HashMap(java.util.HashMap) Map(java.util.Map) ISourceRange(org.eclipse.jdt.core.ISourceRange)

Example 4 with FoldingRange

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);
        }
    }
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) IInitializer(org.eclipse.jdt.core.IInitializer) IMethod(org.eclipse.jdt.core.IMethod) FoldingRange(org.eclipse.lsp4j.FoldingRange) ISourceRange(org.eclipse.jdt.core.ISourceRange) IType(org.eclipse.jdt.core.IType)

Example 5 with FoldingRange

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);
    }
}
Also used : Extension(org.eclipse.xtext.xbase.lib.Extension) FoldingRangeRequestParams(org.eclipse.lsp4j.FoldingRangeRequestParams) VersionedTextDocumentIdentifier(org.eclipse.lsp4j.VersionedTextDocumentIdentifier) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) FoldingRange(org.eclipse.lsp4j.FoldingRange)

Aggregations

FoldingRange (org.eclipse.lsp4j.FoldingRange)7 ISourceRange (org.eclipse.jdt.core.ISourceRange)4 IJavaElement (org.eclipse.jdt.core.IJavaElement)2 IType (org.eclipse.jdt.core.IType)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Stack (java.util.Stack)1 CoreException (org.eclipse.core.runtime.CoreException)1 IImportContainer (org.eclipse.jdt.core.IImportContainer)1 IInitializer (org.eclipse.jdt.core.IInitializer)1 IMethod (org.eclipse.jdt.core.IMethod)1 ITypeRoot (org.eclipse.jdt.core.ITypeRoot)1 IScanner (org.eclipse.jdt.core.compiler.IScanner)1 PreferenceManager (org.eclipse.jdt.ls.core.internal.preferences.PreferenceManager)1 FoldingRangeRequestParams (org.eclipse.lsp4j.FoldingRangeRequestParams)1 Position (org.eclipse.lsp4j.Position)1 TextDocumentIdentifier (org.eclipse.lsp4j.TextDocumentIdentifier)1 VersionedTextDocumentIdentifier (org.eclipse.lsp4j.VersionedTextDocumentIdentifier)1 Extension (org.eclipse.xtext.xbase.lib.Extension)1