use of org.graalvm.tools.lsp.server.utils.TextDocumentSurrogate in project graal by oracle.
the class CoverageRequestHandler method getCoverageWithEnteredContext.
public Coverage getCoverageWithEnteredContext(URI uri) {
final TextDocumentSurrogate surrogate = surrogateMap.get(uri);
if (surrogate != null && surrogate.getSourceWrapper() != null && surrogate.getSourceWrapper().isParsingSuccessful()) {
final SourceSectionFilter filter = //
SourceSectionFilter.newBuilder().sourceIs(//
surrogate.getSourceWrapper().getSource()).tagIs(//
StatementTag.class).build();
final Set<SourceSection> duplicateFilter = new HashSet<>();
final List<Range> covered = new ArrayList<>();
final List<Range> uncovered = new ArrayList<>();
env.getInstrumenter().attachLoadSourceSectionListener(filter, new LoadSourceSectionListener() {
@Override
public void onLoad(LoadSourceSectionEvent event) {
SourceSection section = event.getSourceSection();
if (duplicateFilter.add(section)) {
if (surrogate.isLocationCovered(SourceSectionReference.from(section))) {
covered.add(SourceUtils.sourceSectionToRange(section));
} else {
uncovered.add(SourceUtils.sourceSectionToRange(section));
}
}
}
}, true).dispose();
return Coverage.create(covered, uncovered);
}
return null;
}
use of org.graalvm.tools.lsp.server.utils.TextDocumentSurrogate in project graal by oracle.
the class CoverageRequestHandler method runCoverageAnalysisWithEnteredContext.
public Boolean runCoverageAnalysisWithEnteredContext(final URI uri) throws DiagnosticsNotification {
final TextDocumentSurrogate surrogateOfOpenedFile = surrogateMap.get(uri);
if (surrogateOfOpenedFile == null) {
return Boolean.FALSE;
}
TextDocumentSurrogate surrogateOfTestFile = sourceCodeEvaluator.createSurrogateForTestFile(surrogateOfOpenedFile, null);
final URI runScriptUri = surrogateOfTestFile.getUri();
clearRelatedCoverageData(runScriptUri);
try {
final CallTarget callTarget = sourceCodeEvaluator.parse(surrogateOfTestFile);
LanguageInfo languageInfo = surrogateOfTestFile.getLanguageInfo();
SourcePredicate predicate = SourcePredicateBuilder.newBuilder().language(languageInfo).excludeInternal(env.getOptions()).build();
SourceSectionFilter eventFilter = SourceSectionFilter.newBuilder().sourceIs(predicate).build();
EventBinding<ExecutionEventNodeFactory> eventFactoryBinding = env.getInstrumenter().attachExecutionEventFactory(eventFilter, new ExecutionEventNodeFactory() {
private final long creatorThreadId = Thread.currentThread().getId();
@Override
public ExecutionEventNode create(final EventContext eventContext) {
final SourceSection section = eventContext.getInstrumentedSourceSection();
if (section != null && section.isAvailable()) {
final Node instrumentedNode = eventContext.getInstrumentedNode();
Function<URI, TextDocumentSurrogate> func = (sourceUri) -> {
return surrogateMap.getOrCreateSurrogate(sourceUri, () -> instrumentedNode.getRootNode().getLanguageInfo());
};
return new CoverageEventNode(section, instrumentedNode, runScriptUri, func, creatorThreadId);
} else {
return null;
}
}
});
try {
callTarget.call();
} finally {
eventFactoryBinding.dispose();
}
surrogateOfOpenedFile.setCoverageAnalysisDone(true);
return Boolean.TRUE;
} catch (DiagnosticsNotification e) {
throw e;
} catch (Exception e) {
InteropLibrary interopLib = InteropLibrary.getUncached();
if (interopLib.isException(e)) {
SourceSection sourceSection;
try {
sourceSection = interopLib.hasSourceLocation(e) ? interopLib.getSourceLocation(e) : null;
} catch (UnsupportedMessageException um) {
throw CompilerDirectives.shouldNotReachHere(um);
}
URI uriOfErronousSource = sourceSection != null ? sourceSection.getSource().getURI() : null;
if (uriOfErronousSource == null) {
uriOfErronousSource = uri;
}
throw DiagnosticsNotification.create(uriOfErronousSource, Diagnostic.create(SourceUtils.getRangeFrom(e, interopLib), e.getMessage(), DiagnosticSeverity.Error, null, "Coverage analysis", null));
}
throw e;
}
}
use of org.graalvm.tools.lsp.server.utils.TextDocumentSurrogate 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.TextDocumentSurrogate in project graal by oracle.
the class SourceCodeEvaluator method runToSectionAndEval.
public EvaluationResult runToSectionAndEval(final TextDocumentSurrogate surrogate, final SourceSection sourceSection, SourceSectionFilter eventFilter, SourceSectionFilter inputFilter) throws DiagnosticsNotification {
Set<URI> coverageUris = surrogate.getCoverageUris(sourceSection);
URI runScriptUriFallback = coverageUris == null ? null : coverageUris.stream().findFirst().orElseGet(() -> null);
TextDocumentSurrogate surrogateOfTestFile = createSurrogateForTestFile(surrogate, runScriptUriFallback);
final CallTarget callTarget = parse(surrogateOfTestFile);
final boolean isInputFilterDefined = inputFilter != null;
EventBinding<ExecutionEventNodeFactory> binding = env.getInstrumenter().attachExecutionEventFactory(eventFilter, inputFilter, new ExecutionEventNodeFactory() {
StringBuilder indent = new StringBuilder("");
@Override
public ExecutionEventNode create(EventContext context) {
return new ExecutionEventNode() {
private String sourceSectionFormat(SourceSection section) {
return "SourceSection(" + section.getCharacters().toString().replaceAll("\n", Matcher.quoteReplacement("\\n")) + ")";
}
@Override
public void onReturnValue(VirtualFrame frame, Object result) {
if (logger.isLoggable(Level.FINEST)) {
logOnReturnValue(result);
}
if (!isInputFilterDefined) {
CompilerDirectives.transferToInterpreter();
throw new EvaluationResultException(result);
}
}
@TruffleBoundary
private void logOnReturnValue(Object result) {
if (indent.length() > 1) {
indent.setLength(indent.length() - 2);
}
logger.log(Level.FINEST, "{0}onReturnValue {1} {2} {3} {4}", new Object[] { indent, context.getInstrumentedNode().getClass().getSimpleName(), sourceSectionFormat(context.getInstrumentedSourceSection()), result });
}
@Override
public void onReturnExceptional(VirtualFrame frame, Throwable exception) {
if (logger.isLoggable(Level.FINEST)) {
logOnReturnExceptional();
}
}
@TruffleBoundary
private void logOnReturnExceptional() {
indent.setLength(indent.length() - 2);
logger.log(Level.FINEST, "{0}onReturnExceptional {1}", new Object[] { indent, sourceSectionFormat(context.getInstrumentedSourceSection()) });
}
@Override
public void onEnter(VirtualFrame frame) {
if (logger.isLoggable(Level.FINEST)) {
logOnEnter();
}
}
@TruffleBoundary
private void logOnEnter() {
logger.log(Level.FINEST, "{0}onEnter {1} {2}", new Object[] { indent, context.getInstrumentedNode().getClass().getSimpleName(), sourceSectionFormat(context.getInstrumentedSourceSection()) });
indent.append(" ");
}
@Override
public void onInputValue(VirtualFrame frame, EventContext inputContext, int inputIndex, Object inputValue) {
if (logger.isLoggable(Level.FINEST)) {
logOnInputValue(inputContext, inputIndex, inputValue);
}
CompilerDirectives.transferToInterpreter();
throw new EvaluationResultException(inputValue);
}
@TruffleBoundary
private void logOnInputValue(EventContext inputContext, int inputIndex, Object inputValue) {
indent.setLength(indent.length() - 2);
Object view = env.getLanguageView(inputContext.getInstrumentedNode().getRootNode().getLanguageInfo(), inputValue);
String metaObject;
try {
metaObject = INTEROP.hasMetaObject(view) ? INTEROP.asString(INTEROP.toDisplayString(INTEROP.getMetaObject(view))) : null;
} catch (UnsupportedMessageException e) {
CompilerDirectives.transferToInterpreter();
throw new AssertionError(e);
}
logger.log(Level.FINEST, "{0}onInputValue idx:{1} {2} {3} {4} {5} {6}", new Object[] { indent, inputIndex, inputContext.getInstrumentedNode().getClass().getSimpleName(), sourceSectionFormat(context.getInstrumentedSourceSection()), sourceSectionFormat(inputContext.getInstrumentedSourceSection()), inputValue, metaObject });
indent.append(" ");
}
};
}
});
try {
callTarget.call();
} catch (EvaluationResultException e) {
return e.isError() ? EvaluationResult.createError(e.getResult()) : EvaluationResult.createResult(e.getResult());
} catch (RuntimeException e) {
if (INTEROP.isException(e)) {
try {
if (INTEROP.getExceptionType(e) == ExceptionType.EXIT) {
return EvaluationResult.createEvaluationSectionNotReached();
} else {
return EvaluationResult.createError(e);
}
} catch (UnsupportedMessageException ume) {
throw CompilerDirectives.shouldNotReachHere(ume);
}
}
} finally {
binding.dispose();
}
return EvaluationResult.createEvaluationSectionNotReached();
}
use of org.graalvm.tools.lsp.server.utils.TextDocumentSurrogate in project graal by oracle.
the class SourceCodeEvaluator method createSurrogateForTestFile.
public TextDocumentSurrogate createSurrogateForTestFile(TextDocumentSurrogate surrogateOfOpenedFile, URI runScriptUriFallback) {
URI runScriptUri = runScriptUriFallback != null ? runScriptUriFallback : surrogateOfOpenedFile.getUri();
final String langIdOfTestFile = findLanguageOfTestFile(runScriptUri, surrogateOfOpenedFile.getLanguageId());
LanguageInfo languageInfo = env.getLanguages().get(langIdOfTestFile);
assert languageInfo != null;
TextDocumentSurrogate surrogateOfTestFile = surrogateMap.getOrCreateSurrogate(runScriptUri, languageInfo);
return surrogateOfTestFile;
}
Aggregations