use of org.graalvm.tools.lsp.server.utils.CoverageData in project graal by oracle.
the class SourceCodeEvaluator method findCoverageDataBeforeNode.
static List<CoverageData> findCoverageDataBeforeNode(TextDocumentSurrogate surrogate, Node targetNode) {
List<CoverageData> coveragesBeforeNode = new ArrayList<>();
targetNode.getRootNode().accept(new NodeVisitor() {
boolean found = false;
@Override
public boolean visit(Node node) {
if (found) {
return false;
}
if (node.equals(targetNode)) {
found = true;
return false;
}
SourceSection sourceSection = node.getSourceSection();
if (sourceSection != null && sourceSection.isAvailable()) {
List<CoverageData> coverageData = surrogate.getCoverageData(sourceSection);
if (coverageData != null) {
coveragesBeforeNode.addAll(coverageData);
}
}
return true;
}
});
return coveragesBeforeNode;
}
use of org.graalvm.tools.lsp.server.utils.CoverageData in project graal by oracle.
the class AbstractRequestHandler method getScope.
protected static Object getScope(TextDocumentSurrogate surrogate, InstrumentableNode node) {
List<CoverageData> coverageData = surrogate.getCoverageData(((Node) node).getSourceSection());
MaterializedFrame frame = null;
if (coverageData != null) {
CoverageData data = coverageData.stream().findFirst().orElse(null);
if (data != null) {
frame = data.getFrame();
}
}
NodeLibrary nodeLibrary = NodeLibrary.getUncached(node);
if (nodeLibrary.hasScope(node, frame)) {
try {
return nodeLibrary.getScope(node, frame, true);
} catch (UnsupportedMessageException e) {
throw CompilerDirectives.shouldNotReachHere(e);
}
} else {
return null;
}
}
use of org.graalvm.tools.lsp.server.utils.CoverageData in project graal by oracle.
the class HoverRequestHandler method evalHoverInfos.
private Hover evalHoverInfos(List<CoverageData> coverages, SourceSection hoverSection, LanguageInfo langInfo) {
String textAtHoverPosition = hoverSection.getCharacters().toString();
for (CoverageData coverageData : coverages) {
Hover frameSlotHover = tryFrameScope(coverageData.getFrame(), coverageData.getCoverageEventNode(), textAtHoverPosition, langInfo, hoverSection);
if (frameSlotHover != null) {
return frameSlotHover;
}
Hover coverageDataHover = tryCoverageDataEvaluation(hoverSection, langInfo, textAtHoverPosition, coverageData);
if (coverageDataHover != null) {
return coverageDataHover;
}
}
return Hover.create(Collections.emptyList());
}
use of org.graalvm.tools.lsp.server.utils.CoverageData in project graal by oracle.
the class HoverRequestHandler method hoverWithEnteredContext.
public Hover hoverWithEnteredContext(URI uri, int line, int column) {
TextDocumentSurrogate surrogate = surrogateMap.get(uri);
InstrumentableNode nodeAtCaret = findNodeAtCaret(surrogate, line, column);
if (nodeAtCaret != null) {
SourceSection hoverSection = ((Node) nodeAtCaret).getSourceSection();
logger.log(Level.FINER, "Hover: SourceSection({0})", hoverSection.getCharacters());
if (surrogate.hasCoverageData()) {
List<CoverageData> coverages = surrogate.getCoverageData(hoverSection);
if (coverages != null) {
return evalHoverInfos(coverages, hoverSection, surrogate.getLanguageInfo());
}
} else if (developerMode) {
String sourceText = hoverSection.getCharacters().toString();
MarkupContent content = MarkupContent.create(MarkupKind.PlainText, "Language: " + surrogate.getLanguageId() + ", Section: " + sourceText + "\n" + "Node class: " + nodeAtCaret.getClass().getSimpleName() + "\n" + "Tags: " + getTags(nodeAtCaret));
return Hover.create(content).setRange(SourceUtils.sourceSectionToRange(hoverSection));
}
}
return Hover.create(Collections.emptyList());
}
use of org.graalvm.tools.lsp.server.utils.CoverageData in project graal by oracle.
the class SourceCodeEvaluator method evalWithCoverageData.
private EvaluationResult evalWithCoverageData(TextDocumentSurrogate textDocumentSurrogate, Node nearestNode) {
if (!textDocumentSurrogate.hasCoverageData()) {
return EvaluationResult.createEvaluationSectionNotReached();
}
List<CoverageData> dataBeforeNode = findCoverageDataBeforeNode(textDocumentSurrogate, nearestNode);
if (dataBeforeNode == null || dataBeforeNode.isEmpty()) {
return EvaluationResult.createEvaluationSectionNotReached();
}
CoverageData coverageData = dataBeforeNode.get(dataBeforeNode.size() - 1);
if (((InstrumentableNode) nearestNode).hasTag(StandardTags.ReadVariableTag.class)) {
// Shortcut for variables
InteropUtils.VariableInfo[] variables = InteropUtils.getNodeObjectVariables((InstrumentableNode) nearestNode);
if (variables.length == 1) {
InteropUtils.VariableInfo var = variables[0];
NodeLibrary nodeLibrary = NodeLibrary.getUncached(nearestNode);
if (nodeLibrary.hasScope(nearestNode, coverageData.getFrame())) {
try {
Object scope = nodeLibrary.getScope(nearestNode, coverageData.getFrame(), true);
if (INTEROP.isMemberReadable(scope, var.getName())) {
logger.fine("Coverage-based variable look-up");
Object value = INTEROP.readMember(scope, var.getName());
return EvaluationResult.createResult(value);
}
} catch (UnknownIdentifierException | UnsupportedMessageException ex) {
throw CompilerDirectives.shouldNotReachHere(ex);
}
}
}
}
LanguageInfo info = nearestNode.getRootNode().getLanguageInfo();
String code = nearestNode.getSourceSection().getCharacters().toString();
Source inlineEvalSource = Source.newBuilder(info.getId(), code, "in-line eval (hover request)").cached(false).build();
ExecutableNode executableNode = env.parseInline(inlineEvalSource, nearestNode, coverageData.getFrame());
CoverageEventNode coverageEventNode = coverageData.getCoverageEventNode();
coverageEventNode.insertOrReplaceChild(executableNode);
try {
logger.fine("Trying coverage-based eval...");
Object result = executableNode.execute(coverageData.getFrame());
return EvaluationResult.createResult(result);
} catch (Exception e) {
e.printStackTrace();
} finally {
coverageEventNode.clearChild();
}
return EvaluationResult.createEvaluationSectionNotReached();
}
Aggregations