Search in sources :

Example 1 with SourceFix

use of org.graalvm.tools.lsp.server.utils.SourceUtils.SourceFix in project graal by oracle.

the class CompletionRequestHandler method completionWithEnteredContext.

public CompletionList completionWithEnteredContext(final URI uri, int line, int column, CompletionContext completionContext) throws DiagnosticsNotification {
    logger.log(Level.FINER, "Start finding completions for {0}:{1}:{2}", new Object[] { uri, line, column });
    TextDocumentSurrogate surrogate = surrogateMap.get(uri);
    if (surrogate == null) {
        logger.info("Completion requested in an unknown document: " + uri);
        return emptyList;
    }
    Source source = surrogate.getSource();
    if (!SourceUtils.isLineValid(line, source) || !SourceUtils.isColumnValid(line, column, source)) {
        logger.fine("line or column is out of range, line=" + line + ", column=" + column);
        return emptyList;
    }
    List<String> completionTriggerCharacters = languageCompletionTriggerCharacters.getTriggerCharacters(surrogate.getLanguageId());
    CompletionKind completionKind = getCompletionKind(source, SourceUtils.zeroBasedLineToOneBasedLine(line, source), column, completionTriggerCharacters, completionContext);
    if (surrogate.isSourceCodeReadyForCodeCompletion()) {
        return createCompletions(surrogate, line, column, completionKind);
    } else {
        // Try fixing the source code, parse again, then create the completions
        SourceFix sourceFix = SourceUtils.removeLastTextInsertion(surrogate, column, logger);
        if (sourceFix == null) {
            logger.fine("Unable to fix unparsable source code. No completion possible.");
            return emptyList;
        }
        TextDocumentSurrogate fixedSurrogate = surrogate.copy();
        // TODO(ds) Should we reset coverage data etc? Or adjust the SourceLocations?
        fixedSurrogate.setEditorText(sourceFix.text);
        SourceWrapper sourceWrapper = fixedSurrogate.prepareParsing();
        CallTarget callTarget = null;
        try {
            callTarget = env.parse(sourceWrapper.getSource());
        } catch (Exception e) {
            err.println("Parsing a fixed source caused an exception: " + e.getClass().getSimpleName() + " > " + e.getLocalizedMessage());
            return emptyList;
        } finally {
            fixedSurrogate.notifyParsingDone(callTarget);
        }
        // We need to replace the original surrogate with the fixed one so that when a run
        // script wants to import this fixed source, it will find the fixed surrogate via the
        // custom file system callback
        surrogateMap.put(uri, fixedSurrogate);
        try {
            return createCompletions(fixedSurrogate, line, sourceFix.characterIdx, getCompletionKind(sourceFix.removedCharacters, completionTriggerCharacters));
        } finally {
            surrogateMap.put(uri, surrogate);
        }
    }
}
Also used : SourceWrapper(org.graalvm.tools.lsp.server.utils.SourceWrapper) CallTarget(com.oracle.truffle.api.CallTarget) TextDocumentSurrogate(org.graalvm.tools.lsp.server.utils.TextDocumentSurrogate) SourceFix(org.graalvm.tools.lsp.server.utils.SourceUtils.SourceFix) Source(com.oracle.truffle.api.source.Source) InteropException(com.oracle.truffle.api.interop.InteropException) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException)

Aggregations

CallTarget (com.oracle.truffle.api.CallTarget)1 InteropException (com.oracle.truffle.api.interop.InteropException)1 UnsupportedMessageException (com.oracle.truffle.api.interop.UnsupportedMessageException)1 Source (com.oracle.truffle.api.source.Source)1 SourceFix (org.graalvm.tools.lsp.server.utils.SourceUtils.SourceFix)1 SourceWrapper (org.graalvm.tools.lsp.server.utils.SourceWrapper)1 TextDocumentSurrogate (org.graalvm.tools.lsp.server.utils.TextDocumentSurrogate)1