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