Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions rascal-lsp/src/main/checkerframework/lsp4j.astub
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,25 @@ public abstract class ResourceOperation {
public @Nullable String getAnnotationId() {}
public void setAnnotationId(final @Nullable String annotationId) { }
}


package org.eclipse.lsp4j;

import java.util.List;
import org.checkerframework.checker.nullness.qual.*;

public class Diagnostic {
public void setRelatedInformation(@Nullable final List<DiagnosticRelatedInformation> relatedInformation) { }
}


package org.eclipse.lsp4j;

import org.eclipse.lsp4j.*;
import java.util.List;
import org.checkerframework.checker.nullness.qual.*;

public class CallHierarchyItem {
public void setDetail(@Nullable final String detail) { }
public void setData(@Nullable final Object data) { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import org.rascalmpl.vscode.lsp.RascalLSPMonitor;
import org.rascalmpl.vscode.lsp.parametric.LanguageRegistry.LanguageParameter;
import org.rascalmpl.vscode.lsp.parametric.model.RascalADTs.LanguageContributions;
import org.rascalmpl.vscode.lsp.rascal.conversion.KeywordParameter;
import org.rascalmpl.vscode.lsp.util.EvaluatorUtil;
import org.rascalmpl.vscode.lsp.util.EvaluatorUtil.LSPContext;
import org.rascalmpl.vscode.lsp.util.concurrent.InterruptibleFuture;
Expand Down Expand Up @@ -266,8 +267,7 @@ private static boolean isTrue(@Nullable IConstructor constructor, String paramet
if (constructor == null) {
return false;
}
var val = constructor.asWithKeywordParameters().getParameter(parameter);
return !(val instanceof IBool) || ((IBool)val).getValue();
return KeywordParameter.get(parameter, constructor.asWithKeywordParameters(), true);
}

private static ISet loadContributions(Evaluator eval, LanguageParameter lang) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@
import org.rascalmpl.vscode.lsp.rascal.conversion.DocumentChanges;
import org.rascalmpl.vscode.lsp.rascal.conversion.DocumentSymbols;
import org.rascalmpl.vscode.lsp.rascal.conversion.FoldingRanges;
import org.rascalmpl.vscode.lsp.rascal.conversion.KeywordParameter;
import org.rascalmpl.vscode.lsp.rascal.conversion.SelectionRanges;
import org.rascalmpl.vscode.lsp.rascal.conversion.SemanticTokenizer;
import org.rascalmpl.vscode.lsp.uri.FallbackResolver;
Expand All @@ -159,7 +160,6 @@
import org.rascalmpl.vscode.lsp.util.locations.Locations;
import org.rascalmpl.vscode.lsp.util.locations.impl.TreeSearch;

import io.usethesource.vallang.IBool;
import io.usethesource.vallang.IConstructor;
import io.usethesource.vallang.IList;
import io.usethesource.vallang.ISet;
Expand Down Expand Up @@ -637,16 +637,16 @@ private InlayHint rowToInlayHint(IValue v) {
var label = ((IString) t.get("label")).getValue();
var kind = (IConstructor) t.get("kind");
var tKW = t.asWithKeywordParameters();
var toolTip = (IString)tKW.getParameter("toolTip");
var atEnd = tKW.hasParameter("atEnd") && ((IBool)tKW.getParameter("atEnd")).getValue();
var toolTip = KeywordParameter.get("toolTip", tKW, (String) null);
var atEnd = KeywordParameter.get("atEnd", tKW, false);

// translate to lsp
var result = new InlayHint(Locations.toPosition(loc, columns, atEnd), Either.forLeft(label.trim()));
result.setKind(kind.getName().equals("type") ? InlayHintKind.Type : InlayHintKind.Parameter);
result.setPaddingLeft(label.startsWith(" "));
result.setPaddingRight(label.endsWith(" "));
if (toolTip != null && toolTip.length() > 0) {
result.setTooltip(toolTip.getValue());
result.setTooltip(toolTip);
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import java.util.concurrent.Executor;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.checkerframework.checker.initialization.qual.UnderInitialization;
Expand All @@ -54,6 +53,7 @@
import org.rascalmpl.vscode.lsp.parametric.model.ParametricSummary.SummaryLookup;
import org.rascalmpl.vscode.lsp.parametric.model.RascalADTs.SummaryFields;
import org.rascalmpl.vscode.lsp.rascal.conversion.Diagnostics;
import org.rascalmpl.vscode.lsp.rascal.conversion.KeywordParameter;
import org.rascalmpl.vscode.lsp.util.Lazy;
import org.rascalmpl.vscode.lsp.util.Versioned;
import org.rascalmpl.vscode.lsp.util.concurrent.InterruptibleFuture;
Expand All @@ -69,7 +69,6 @@
import io.usethesource.vallang.IString;
import io.usethesource.vallang.ITuple;
import io.usethesource.vallang.IValue;
import io.usethesource.vallang.IWithKeywordParameters;

/**
* The purpose of this interface is to provide a general abstraction for
Expand Down Expand Up @@ -252,15 +251,8 @@ public void invalidate() {
}

private InterruptibleFuture<Lazy<List<Diagnostic>>> extractMessages(@UnderInitialization MessagesOnlyScheduledSummary this, InterruptibleFuture<IConstructor> summary) {
return summary.thenApply(s -> Lazy.defer(() -> {
var sum = s.asWithKeywordParameters();
if (sum.hasParameter("messages")) {
return ((ISet)sum.getParameter("messages")).stream()
.map(d -> Diagnostics.translateDiagnostic((IConstructor)(((ITuple)d).get(1)), columns))
.collect(Collectors.toList());
}
return Collections.emptyList();
}));
return summary.thenApply(s -> Lazy.defer(() ->
new ArrayList<>(KeywordParameter.get("messages", s.asWithKeywordParameters(), Collections.emptySet(), d -> Diagnostics.translateDiagnostic((IConstructor)(((ITuple)d).get(1)), columns)))));
}
}

Expand Down Expand Up @@ -339,14 +331,7 @@ private <T> InterruptibleFuture<Lazy<IRangeMap<List<T>>>> mapCalculation(@UnderI
.thenApply(IConstructor::asWithKeywordParameters)
.thenApply(s ->
Lazy.defer(() ->
translateRelation(logName, getKWFieldSet(s, kwField), valueMapper)));
}

private IRelation<ISet> getKWFieldSet(@UnderInitialization FullScheduledSummary this, IWithKeywordParameters<? extends IConstructor> data, String name) {
if (data.hasParameter(name)) {
return ((ISet) data.getParameter(name)).asRelation();
}
return IRascalValueFactory.getInstance().set().asRelation();
translateRelation(logName, KeywordParameter.get(kwField, s, IRascalValueFactory.getInstance().set()).asRelation(), valueMapper)));
}

private <T> IRangeMap<List<T>> translateRelation(@UnderInitialization FullScheduledSummary this, String logName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@
package org.rascalmpl.vscode.lsp.rascal.conversion;

import com.google.gson.JsonPrimitive;
import java.util.Collections;
import java.util.HashMap;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.eclipse.lsp4j.CallHierarchyItem;
import org.rascalmpl.util.locations.ColumnMaps;
import org.rascalmpl.values.IRascalValueFactory;
Expand Down Expand Up @@ -102,15 +104,9 @@ public CallHierarchyItem toLSP(IConstructor cons, ColumnMaps columns) {

var ci = new CallHierarchyItem(name, kind, Locations.toUri(def.top()).toString(), definitionRange, selectionRange);
var kws = cons.asWithKeywordParameters();
if (kws.hasParameter(CallHierarchyFields.TAGS)) {
ci.setTags(DocumentSymbols.symbolTagsToLSP((ISet) kws.getParameter(CallHierarchyFields.TAGS)));
}
if (kws.hasParameter(CallHierarchyFields.DETAIL)) {
ci.setDetail(((IString) kws.getParameter(CallHierarchyFields.DETAIL)).getValue());
}
if (kws.hasParameter(CallHierarchyFields.DATA)) {
ci.setData(serializeData((IConstructor) kws.getParameter(CallHierarchyFields.DATA)));
}
ci.setTags(KeywordParameter.get(ISet.class, CallHierarchyFields.TAGS, kws, Collections.emptyList(), DocumentSymbols::symbolTagToLSP, Collectors.toList()));
ci.setDetail(KeywordParameter.get(CallHierarchyFields.DETAIL, kws, (String) null));
ci.setData(KeywordParameter.get(CallHierarchyFields.DATA, kws, null, this::serializeData));

return ci;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
import org.rascalmpl.vscode.lsp.parametric.model.RascalADTs.CompletionFields;
import org.rascalmpl.vscode.lsp.util.locations.Locations;

import io.usethesource.vallang.IBool;
import io.usethesource.vallang.IConstructor;
import io.usethesource.vallang.IInteger;
import io.usethesource.vallang.IList;
Expand Down Expand Up @@ -86,22 +85,24 @@ public List<CompletionItem> toLSP(final IBaseTextDocumentService docService, ILi

var edit = (IConstructor) c.get(CompletionFields.EDIT);
ci.setTextEdit(Either.forRight(editToLSP(edit, editLine, offsets)));
ci.setInsertTextFormat(isSnippet(edit) ? InsertTextFormat.Snippet : InsertTextFormat.PlainText);
ci.setInsertTextFormat(KeywordParameter.get(CompletionFields.SNIPPET, edit.asWithKeywordParameters(), false)
? InsertTextFormat.Snippet
: InsertTextFormat.PlainText);
ci.setInsertTextMode(InsertTextMode.AsIs);
ci.setLabel(((IString) c.get(CompletionFields.LABEL)).getValue());
ci.setKind(itemKindToLSP((IConstructor) c.get(CompletionFields.KIND)));
var label = new CompletionItemLabelDetails();
label.setDetail(getKwParamString(kws, CompletionFields.LABEL_DETAIL, ""));
label.setDescription(getKwParamString(kws, CompletionFields.LABEL_DESCRIPTION, ""));
label.setDetail(KeywordParameter.get(CompletionFields.LABEL_DETAIL, kws, ""));
label.setDescription(KeywordParameter.get(CompletionFields.LABEL_DESCRIPTION, kws, ""));
ci.setLabelDetails(label);
ci.setDetail(getKwParamString(kws, CompletionFields.DETAIL, ""));
ci.setDocumentation(new MarkupContent(MarkupKind.MARKDOWN, getKwParamString(kws, CompletionFields.DOCUMENTATION, "")));
ci.setSortText(getKwParamString(kws, CompletionFields.SORT_TEXT, ""));
ci.setFilterText(getKwParamString(kws, CompletionFields.FILTER_TEXT, ""));
ci.setTags(getKwParamBool(kws, CompletionFields.DEPRECATED, false) ? List.of(CompletionItemTag.Deprecated) : Collections.emptyList());
ci.setPreselect(getKwParamBool(kws, CompletionFields.PRESELECT, false));
ci.setCommitCharacters(getKwParamList(kws, CompletionFields.COMMIT_CHARACTERS, VF.list()).stream().map(ch -> ((IString) ch).getValue()).collect(Collectors.toList()));
ci.setAdditionalTextEdits(DocumentChanges.translateTextEdits(getKwParamList(kws, CompletionFields.ADDITIONAL_CHANGES, VF.list()), docService.getColumnMaps()));
ci.setDetail(KeywordParameter.get(CompletionFields.DETAIL, kws, ""));
ci.setDocumentation(new MarkupContent(MarkupKind.MARKDOWN, KeywordParameter.get(CompletionFields.DOCUMENTATION, kws, "")));
ci.setSortText(KeywordParameter.get(CompletionFields.SORT_TEXT, kws, ""));
ci.setFilterText(KeywordParameter.get(CompletionFields.FILTER_TEXT, kws, ""));
ci.setTags(KeywordParameter.get(CompletionFields.DEPRECATED, kws, false) ? List.of(CompletionItemTag.Deprecated) : Collections.emptyList());
ci.setPreselect(KeywordParameter.get(CompletionFields.PRESELECT, kws, false));
ci.setCommitCharacters(KeywordParameter.get(CompletionFields.COMMIT_CHARACTERS, kws, List.of(), ch -> ((IString) ch).getValue()));
ci.setAdditionalTextEdits(DocumentChanges.translateTextEdits(KeywordParameter.get(CompletionFields.ADDITIONAL_CHANGES, kws, VF.list()), docService.getColumnMaps()));
var command = getCommand(kws, dedicatedLanguageName, languageName);
if (command != null) {
ci.setCommand(command);
Expand Down Expand Up @@ -130,11 +131,6 @@ private InsertReplaceEdit editToLSP(IConstructor edit, int currentLine, LineColu
);
}

private boolean isSnippet(IConstructor edit) {
return edit.asWithKeywordParameters().hasParameter(CompletionFields.SNIPPET)
&& ((IBool) edit.asWithKeywordParameters().getParameter(CompletionFields.SNIPPET)).getValue();
}

private CompletionItemKind itemKindToLSP(IConstructor kind) {
var docSymKind = DocumentSymbols.symbolKindToLSP(kind);
try {
Expand All @@ -145,21 +141,6 @@ private CompletionItemKind itemKindToLSP(IConstructor kind) {
}
}

private String getKwParamString(IWithKeywordParameters<? extends IConstructor> c, String label, String defaultVal) {
var param = c.getParameter(label);
return param instanceof IString ? ((IString) param).getValue() : defaultVal;
}

private boolean getKwParamBool(IWithKeywordParameters<? extends IConstructor> c, String label, boolean defaultVal) {
var param = c.getParameter(label);
return param instanceof IBool ? ((IBool) param).getValue() : defaultVal;
}

private IList getKwParamList(IWithKeywordParameters<? extends IConstructor> c, String label, IList defaultVal) {
var param = c.getParameter(label);
return param instanceof IList ? (IList) param : defaultVal;
}

public IConstructor triggerKindToRascal(@Nullable CompletionContext context) {
if (context == null) {
return invoked;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,19 +213,12 @@ public static Diagnostic translateDiagnostic(IConstructor d, Range range, Column
result.setSeverity(translateSeverity(d));
result.setMessage(getMessageString(d));
result.setRange(range);


var dKW = d.asWithKeywordParameters();
if (dKW.hasParameter("causes")) {
result.setRelatedInformation(
((IList) dKW.getParameter("causes")).stream()
.map(IConstructor.class::cast)
.map(c -> new DiagnosticRelatedInformation(
result.setRelatedInformation(KeywordParameter.get("causes", d.asWithKeywordParameters(), (List<DiagnosticRelatedInformation>) null, v -> {
var c = (IConstructor) v;
return new DiagnosticRelatedInformation(
Locations.toLocation(getMessageLocation(c), otherFiles),
getMessageString(c)))
.collect(Collectors.toList())
);
}
getMessageString(c));
}));

storeFixCommands(d, result);
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
import org.rascalmpl.vscode.lsp.parametric.model.RascalADTs;
import org.rascalmpl.vscode.lsp.util.locations.Locations;

import io.usethesource.vallang.IBool;
import io.usethesource.vallang.IConstructor;
import io.usethesource.vallang.IList;
import io.usethesource.vallang.ISourceLocation;
Expand Down Expand Up @@ -121,14 +120,9 @@ private static boolean hasAnnotation(IWithKeywordParameters<? extends IConstruct

// Mirror defaults in `util::LanguageServer`
// Setting any of those, means setting the defaults for the remaing ones
var label = kws.hasParameter(RascalADTs.TextEditFields.LABEL)
? ((IString) kws.getParameter(RascalADTs.TextEditFields.LABEL)).getValue()
: "";
var description = kws.hasParameter(RascalADTs.TextEditFields.DESCRIPTION)
? ((IString) kws.getParameter(RascalADTs.TextEditFields.DESCRIPTION)).getValue()
: label;
var needsConfirmation = kws.hasParameter(RascalADTs.TextEditFields.NEEDS_CONFIRMATION)
&& ((IBool) kws.getParameter(RascalADTs.TextEditFields.NEEDS_CONFIRMATION)).getValue();
var label = KeywordParameter.get(RascalADTs.TextEditFields.LABEL, kws, "");
var description = KeywordParameter.get(RascalADTs.TextEditFields.DESCRIPTION, kws, label);
var needsConfirmation = KeywordParameter.get(RascalADTs.TextEditFields.NEEDS_CONFIRMATION, kws, false);
var key = String.format("%s_%s_%b", label, description, needsConfirmation);

changeAnnotations.computeIfAbsent(key, k -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@
*/
package org.rascalmpl.vscode.lsp.rascal.conversion;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.eclipse.lsp4j.DocumentSymbol;
Expand All @@ -45,6 +47,7 @@
import io.usethesource.vallang.ISet;
import io.usethesource.vallang.ISourceLocation;
import io.usethesource.vallang.IString;
import io.usethesource.vallang.IValue;
import io.usethesource.vallang.IWithKeywordParameters;
import io.usethesource.vallang.type.Type;
import io.usethesource.vallang.type.TypeFactory;
Expand Down Expand Up @@ -87,26 +90,16 @@ public static List<Either<SymbolInformation, DocumentSymbol>> toLSP(IList symbol
public static DocumentSymbol toLSP(IConstructor symbol, final LineColumnOffsetMap om) {
IWithKeywordParameters<?> kwp = symbol.asWithKeywordParameters();

List<DocumentSymbol> children = kwp.hasParameter("children") ?
((IList) kwp.getParameter("children"))
.stream()
.map(c -> toLSP((IConstructor) c, om))
.collect(Collectors.toList())
: Collections.emptyList();

List<DocumentSymbol> children = KeywordParameter.get("children", kwp, Collections.emptyList(), c -> toLSP((IConstructor) c, om));
SymbolKind kind = symbolKindToLSP((IConstructor) symbol.get("kind"));
String symbolName = ((IString) symbol.get("name")).getValue();
Range range = Locations.toRange((ISourceLocation) symbol.get("range"), om);
Range selection = kwp.hasParameter("selection")
? Locations.toRange(((ISourceLocation) kwp.getParameter("selection")), om)
: range;
String detail = kwp.hasParameter("detail") ? ((IString) kwp.getParameter("detail")).getValue() : null;
List<SymbolTag> tags = kwp.hasParameter("tags") ?
symbolTagsToLSP((ISet) kwp.getParameter("tags"))
: Collections.emptyList();
Range selection = KeywordParameter.get("selection", kwp, range, om);
String detail = KeywordParameter.get("detail", kwp, symbolName); // LSP default for detail is name
Set<SymbolTag> tags = KeywordParameter.get("tags", kwp, Collections.emptySet(), DocumentSymbols::symbolTagToLSP);

var lspSymbol = new DocumentSymbol(symbolName, kind, range, selection, detail, children);
lspSymbol.setTags(tags); // since 3.16
lspSymbol.setTags(new ArrayList<>(tags)); // since 3.16
return lspSymbol;
}

Expand All @@ -118,16 +111,9 @@ public static IConstructor symbolKindToRascal(SymbolKind kind) {
return VF.constructor(TF.constructor(store, symbolKindAdt, kind.name().toLowerCase()));
}

public static List<SymbolTag> symbolTagsToLSP(@Nullable ISet tags) {
if (tags == null) {
return Collections.emptyList();
}
return tags.stream()
.map(IConstructor.class::cast)
.map(IConstructor::getName)
.map(DocumentSymbols::capitalize)
.map(SymbolTag::valueOf)
.collect(Collectors.toList());
static SymbolTag symbolTagToLSP(IValue tag) {
var name = ((IConstructor) tag).getName();
return SymbolTag.valueOf(DocumentSymbols.capitalize(name));
}

public static ISet symbolTagsToRascal(@Nullable List<SymbolTag> tags) {
Expand Down
Loading
Loading