vscode-as3mxml icon indicating copy to clipboard operation
vscode-as3mxml copied to clipboard

Code folding for groups of imports

Open joshtynjala opened this issue 6 years ago • 1 comments

Need to wait on something like FoldingRangeProvider to be exposed in the language server protocol.

https://code.visualstudio.com/updates/v1_22#_folding-provider-api https://code.visualstudio.com/updates/v1_23#_folding-provider-api

joshtynjala avatar May 09 '18 15:05 joshtynjala

Here's an implementation of folding ranges for imports. However, it seems that creating custom folding ranges disables VSCode's built-in folding detection for blocks surrounded by {} and multi-line comments like /* */. We don't want to lose those, so we either need to wait until VSCode allows both, or we need to implement it again manually (which isn't ideal).

@Override
public CompletableFuture<List<FoldingRange>> foldingRange(FoldingRangeRequestParams params)
{
    return CompletableFutures.computeAsync(compilerWorkspace.getExecutorService(), cancelToken ->
    {
        cancelToken.checkCanceled();

        //don't start the build until all other builds are done
        compilerWorkspace.startIdleState();
        compilerWorkspace.endIdleState(IWorkspace.NIL_COMPILATIONUNITS_TO_UPDATE);
        cancelToken.checkCanceled();

        compilerWorkspace.startBuilding();
        try
        {
            TextDocumentIdentifier textDocument = params.getTextDocument();
            RoyaleProject project = textDocumentIdentifierToProject(textDocument);
            if(project == null)
            {
                cancelToken.checkCanceled();
                return Collections.emptyList();
            }
            Path path = LanguageServerCompilerUtils.getPathFromLanguageServerURI(textDocument.getUri());
            if(path == null)
            {
                cancelToken.checkCanceled();
                return Collections.emptyList();
            }

            IASNode ast = getAST(path);
            if(ast == null)
            {
                cancelToken.checkCanceled();
                return Collections.emptyList();
            }

            List<FoldingRange> result = new ArrayList<>();
            findImportFoldingRanges(ast, result);

            cancelToken.checkCanceled();
            return result;
        }
        finally
        {
            compilerWorkspace.doneBuilding();
        }
    });
}

private void findImportFoldingRanges(IASNode node, List<FoldingRange> result)
{
    FoldingRange range = null;
    for(int i = 0; i < node.getChildCount(); i++)
    {
        IASNode child = node.getChild(i);
        if (child instanceof IImportNode)
        {
            if (range == null)
            {
                range = new FoldingRange(child.getLine(), child.getEndLine());
                range.setKind(FoldingRangeKind.Imports);
            }
            else
            {
                range.setEndLine(child.getEndLine());
            }
            continue;
        }
        else if(range != null)
        {
            result.add(range);
            range = null;
        }
        findImportFoldingRanges(child, result);
    }
} 

joshtynjala avatar Nov 13 '18 19:11 joshtynjala