Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code folding for groups of imports #214

Open
joshtynjala opened this issue May 9, 2018 · 1 comment
Open

Code folding for groups of imports #214

joshtynjala opened this issue May 9, 2018 · 1 comment

Comments

@joshtynjala
Copy link
Member

joshtynjala commented May 9, 2018

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
Copy link
Member Author

joshtynjala commented Nov 13, 2018

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);
    }
} 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant