use of org.graalvm.tools.lsp.server.utils.NearestNode in project graal by oracle.
the class CompletionRequestHandler method fillCompletionsWithGlobalsAndLocals.
private void fillCompletionsWithGlobalsAndLocals(int line, TextDocumentSurrogate surrogate, int column, List<CompletionItem> completions) {
Node nearestNode = findNearestNode(surrogate.getSourceWrapper(), line, column);
if (nearestNode == null) {
// Cannot locate a valid node near the caret position, therefore provide globals only
fillCompletionsWithGlobals(surrogate, completions);
return;
}
if (!surrogate.hasCoverageData()) {
// No coverage data, so we simply display locals without specific frame information and
// globals
fillCompletionsWithLocals(surrogate, nearestNode, completions, null);
fillCompletionsWithGlobals(surrogate, completions);
return;
}
// We have coverage data, so we try to derive locals from coverage data
List<CoverageData> coverages = surrogate.getCoverageData(nearestNode.getSourceSection());
if (coverages == null || coverages.isEmpty()) {
coverages = SourceCodeEvaluator.findCoverageDataBeforeNode(surrogate, nearestNode);
}
if (coverages != null && !coverages.isEmpty()) {
CoverageData coverageData = coverages.get(coverages.size() - 1);
MaterializedFrame frame = coverageData.getFrame();
fillCompletionsWithLocals(surrogate, nearestNode, completions, frame);
// Call again, regardless if it was called with a frame argument before,
// because duplicates will be filter, but it will add missing local
// variables which were dropped (because of null values) in the call above
fillCompletionsWithLocals(surrogate, nearestNode, completions, null);
fillCompletionsWithGlobals(surrogate, completions);
} else {
// No coverage data found for the designated source section, so use the default look-up
// as fallback
fillCompletionsWithGlobals(surrogate, completions);
fillCompletionsWithLocals(surrogate, nearestNode, completions, null);
}
}
use of org.graalvm.tools.lsp.server.utils.NearestNode in project graal by oracle.
the class CompletionRequestHandler method fillCompletionsWithObjectProperties.
private void fillCompletionsWithObjectProperties(TextDocumentSurrogate surrogate, int line, int column, List<CompletionItem> completions) throws DiagnosticsNotification {
SourceWrapper sourceWrapper = surrogate.getSourceWrapper();
Source source = sourceWrapper.getSource();
NearestNode nearestNodeHolder = NearestSectionsFinder.findExprNodeBeforePos(source, line, column, env);
Node nearestNode = nearestNodeHolder.getNode();
if (nearestNode != null) {
Future<EvaluationResult> future = contextAwareExecutor.executeWithNestedContext(() -> sourceCodeEvaluator.tryDifferentEvalStrategies(surrogate, nearestNode), true);
EvaluationResult evalResult = getFutureResultOrHandleExceptions(future);
if (evalResult != null && evalResult.isEvaluationDone()) {
if (!evalResult.isError()) {
fillCompletionsFromTruffleObject(completions, surrogate.getLanguageInfo(), evalResult.getResult());
} else {
Object result = evalResult.getResult();
if (result != null && INTEROP.isException(result)) {
SourceSection sourceLocation;
String exceptionMessage;
try {
sourceLocation = INTEROP.hasSourceLocation(result) ? INTEROP.getSourceLocation(result) : null;
exceptionMessage = INTEROP.hasExceptionMessage(result) ? INTEROP.asString(INTEROP.getExceptionMessage(result)) : null;
} catch (UnsupportedMessageException um) {
throw CompilerDirectives.shouldNotReachHere(um);
}
throw DiagnosticsNotification.create(surrogate.getUri(), Diagnostic.create(SourceUtils.sourceSectionToRange(sourceLocation), "An error occurred during execution: " + exceptionMessage, DiagnosticSeverity.Warning, null, "Graal", null));
} else {
((Exception) evalResult.getResult()).printStackTrace(err);
}
}
} else {
throw DiagnosticsNotification.create(surrogate.getUri(), Diagnostic.create(SourceUtils.sourceSectionToRange(nearestNode.getSourceSection()), "No type information available for this source section.", DiagnosticSeverity.Information, null, "Graal", null));
}
} else {
logger.fine("No object property completion possible. Caret is not directly at the end of a source section. Line: " + line + ", column: " + column);
}
}
Aggregations