use of org.graalvm.tools.lsp.server.utils.TextDocumentSurrogate in project graal by oracle.
the class HighlightRequestHandler method highlightWithEnteredContext.
public List<? extends DocumentHighlight> highlightWithEnteredContext(URI uri, int line, int character) {
TextDocumentSurrogate surrogate = surrogateMap.get(uri);
InstrumentableNode nodeAtCaret = findNodeAtCaret(surrogate, line, character);
if (nodeAtCaret != null) {
if (nodeAtCaret.hasTag(StandardTags.ReadVariableTag.class) || nodeAtCaret.hasTag(StandardTags.WriteVariableTag.class)) {
return findOtherReadOrWrites(surrogate, nodeAtCaret, line, character);
}
}
return Collections.emptyList();
}
use of org.graalvm.tools.lsp.server.utils.TextDocumentSurrogate in project graal by oracle.
the class SignatureHelpRequestHandler method signatureHelpWithEnteredContext.
public SignatureHelp signatureHelpWithEnteredContext(URI uri, int line, int originalCharacter) throws DiagnosticsNotification {
TextDocumentSurrogate surrogate = surrogateMap.get(uri);
if (isSignatureHelpTriggerCharOfLanguage(surrogate, line, originalCharacter)) {
InstrumentableNode nodeAtCaret = findNodeAtCaret(surrogate, line, originalCharacter, StandardTags.CallTag.class);
if (nodeAtCaret != null) {
SourceSection signatureSection = ((Node) nodeAtCaret).getSourceSection();
SourceSectionFilter.Builder builder = SourceCodeEvaluator.createSourceSectionFilter(surrogate.getUri(), signatureSection);
SourceSectionFilter eventFilter = builder.tagIs(StandardTags.CallTag.class).build();
SourceSectionFilter inputFilter = SourceSectionFilter.ANY;
EvaluationResult evalResult = sourceCodeEvaluator.runToSectionAndEval(surrogate, signatureSection, eventFilter, inputFilter);
// TODO: Are we asking for the signature on the correct object?
if (evalResult.isEvaluationDone() && !evalResult.isError()) {
Object result = evalResult.getResult();
if (INTEROP.accepts(result) && INTEROP.isExecutable(result)) {
try {
Object signature = LSP_INTEROP.getSignature(result);
LanguageInfo langInfo = surrogate.getLanguageInfo();
String label = INTEROP.asString(INTEROP.toDisplayString(env.getLanguageView(langInfo, signature)));
SignatureInformation info = SignatureInformation.create(label, null);
if (signature instanceof TruffleObject) {
if (INTEROP.isMemberReadable(signature, PROP_DOCUMENTATION)) {
Object doc = INTEROP.readMember(signature, PROP_DOCUMENTATION);
Object documentation = completionHandler.getDocumentation(doc, langInfo);
if (documentation != null) {
info.setDocumentation(documentation);
}
}
if (INTEROP.isMemberReadable(signature, PROP_PARAMETERS)) {
Object paramsObject = INTEROP.readMember(signature, PROP_PARAMETERS);
if (paramsObject instanceof TruffleObject && INTEROP.hasArrayElements(paramsObject)) {
long size = INTEROP.getArraySize(paramsObject);
List<ParameterInformation> paramInfos = new ArrayList<>((int) size);
for (long i = 0; i < size; i++) {
if (!INTEROP.isArrayElementReadable(paramsObject, i)) {
continue;
}
Object param = INTEROP.readArrayElement(paramsObject, i);
if (param instanceof TruffleObject) {
ParameterInformation paramInfo = getParameterInformation(param, label, langInfo);
if (paramInfo != null) {
paramInfos.add(paramInfo);
}
}
}
info.setParameters(paramInfos);
}
}
}
Object nodeObject = nodeAtCaret.getNodeObject();
Integer numberOfArguments = InteropUtils.getNumberOfArguments(nodeObject, logger);
// parameter
return SignatureHelp.create(Arrays.asList(info), 0, numberOfArguments != null ? numberOfArguments - 1 : 0);
} catch (UnsupportedMessageException e) {
logger.log(Level.FINEST, "GET_SIGNATURE message not supported for TruffleObject: {0}", result);
} catch (InteropException e) {
e.printStackTrace(err);
}
}
}
}
}
return SignatureHelp.create(Collections.emptyList(), null, null);
}
use of org.graalvm.tools.lsp.server.utils.TextDocumentSurrogate in project graal by oracle.
the class TruffleAdapter method processChangesAndParseWithContextEntered.
protected TextDocumentSurrogate processChangesAndParseWithContextEntered(List<? extends TextDocumentContentChangeEvent> list, URI uri) throws DiagnosticsNotification {
TextDocumentSurrogate surrogate = surrogateMap.get(uri);
if (surrogate == null) {
throw new IllegalStateException("No internal mapping for uri=" + uri.toString() + " found.");
}
if (list.isEmpty()) {
return surrogate;
}
surrogate.getChangeEventsSinceLastSuccessfulParsing().addAll(list);
surrogate.setLastChange(list.get(list.size() - 1));
surrogate.setEditorText(SourceUtils.applyTextDocumentChanges(list, surrogate.getSource(), surrogate, logger));
sourceCodeEvaluator.parse(surrogate);
return surrogate;
}
use of org.graalvm.tools.lsp.server.utils.TextDocumentSurrogate 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);
}
}
}
use of org.graalvm.tools.lsp.server.utils.TextDocumentSurrogate in project graal by oracle.
the class CoverageRequestHandler method clearRelatedCoverageData.
/**
* Clears all coverage data from previous runs, which was collected by running the runScriptUri
* resource. This avoids clearing coverage data collected by other script runs.
*
* Also clears all coverage data for the runScriptUri resource itself.
*
* @param runScriptUri URI of the script to kick-off the coverage analysis
*/
private void clearRelatedCoverageData(final URI runScriptUri) {
TextDocumentSurrogate surrogateOfRunScript = surrogateMap.get(runScriptUri);
assert surrogateOfRunScript != null;
surrogateOfRunScript.clearCoverage();
surrogateMap.getSurrogates().stream().forEach(surrogate -> surrogate.clearCoverage(runScriptUri));
}
Aggregations