Search in sources :

Example 6 with FoldingRange

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);
    }
}
Also used : ArrayList(java.util.ArrayList) ITypeRoot(org.eclipse.jdt.core.ITypeRoot) FoldingRange(org.eclipse.lsp4j.FoldingRange) PreferenceManager(org.eclipse.jdt.ls.core.internal.preferences.PreferenceManager)

Example 7 with FoldingRange

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);
    }
}
Also used : IScanner(org.eclipse.jdt.core.compiler.IScanner) CoreException(org.eclipse.core.runtime.CoreException) FoldingRange(org.eclipse.lsp4j.FoldingRange) ISourceRange(org.eclipse.jdt.core.ISourceRange) Stack(java.util.Stack)

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