Skip to content

Commit

Permalink
Use topics to inform users of deprecated exemplars/methods
Browse files Browse the repository at this point in the history
  • Loading branch information
StevenLooman committed Mar 21, 2024
1 parent e1d050b commit 9a08d9a
Show file tree
Hide file tree
Showing 51 changed files with 819 additions and 135 deletions.
6 changes: 6 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
- Add `do_not_translate` to SwModuleDefinitionGrammar and SwProductDefinitionGrammar.
- Add `required_by` to SwModuleDefinitionGrammar.
- Add FormattingFixer to `magik-lint --apply-fixes`.
- Store exemplar and method topics at ExemplarDefinition and MethodDefinitions.
- Parse exemplar and method topics.
- Add DeprecatedTypeUsageTypedCheck check to mark deprecated exemplars.
- Add DeprecatedMethodUsageTypedCheck check to mark deprecated methods.
- Semantic tokens now mark deprecated types and methods.
- Completion provider now marks deprecated types and methods.

0.9.1 (2024-03-13)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import nl.ramsolutions.sw.magik.parser.MagikCommentExtractor;
import org.eclipse.lsp4j.CompletionItem;
import org.eclipse.lsp4j.CompletionItemKind;
import org.eclipse.lsp4j.CompletionItemTag;
import org.eclipse.lsp4j.CompletionOptions;
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4j.ServerCapabilities;
Expand All @@ -47,6 +48,7 @@ public class CompletionProvider {

private static final Logger LOGGER = LoggerFactory.getLogger(CompletionProvider.class);
private static final Set<Character> REMOVAL_STOP_CHARS = new HashSet<>();
private static final String TOPIC_DEPRECATED = "deprecated";

static {
REMOVAL_STOP_CHARS.add(' ');
Expand Down Expand Up @@ -232,6 +234,9 @@ private List<CompletionItem> provideGlobalCompletion(
item.setDetail(type.getFullName());
item.setDocumentation(type.getDoc());
item.setKind(CompletionItemKind.Class);
if (type.getTopics().contains(TOPIC_DEPRECATED)) {
item.setTags(List.of(CompletionItemTag.Deprecated));
}
return item;
})
.forEach(items::add);
Expand Down Expand Up @@ -292,6 +297,9 @@ private List<CompletionItem> provideMethodInvocationCompletion(
item.setDetail(method.getOwner().getFullName());
item.setDocumentation(method.getDoc());
item.setKind(CompletionItemKind.Method);
if (method.getTopics().contains(TOPIC_DEPRECATED)) {
item.setTags(List.of(CompletionItemTag.Deprecated));
}
return item;
})
.toList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,10 @@ private void buildMethodSignatureDoc(
final String moduleName = Objects.requireNonNullElse(method.getModuleName(), "");
builder.append("Module: ").append(moduleName).append(SECTION_END);

// Method topics.
final String topics = method.getTopics().stream().collect(Collectors.joining(", "));
builder.append("Topics: ").append(topics).append(SECTION_END);

// Method doc.
final String methodDoc = method.getDoc();
if (methodDoc != null) {
Expand All @@ -407,6 +411,10 @@ private void buildTypeSignatureDoc(final AbstractType type, final StringBuilder
final String moduleName = Objects.requireNonNullElse(type.getModuleName(), "");
builder.append("Module: ").append(moduleName).append(SECTION_END);

// Topics.
final String topics = type.getTopics().stream().collect(Collectors.joining(", "));
builder.append("Topics: ").append(topics).append(SECTION_END);

// Type doc.
final String typeDoc = type.getDoc();
if (typeDoc != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ public Integer getTokenType() {
public enum Modifier {
DOCUMENTATION(0x01),
READONLY(0x02),
VARIABLE_GLOBAL(0x04);
VARIABLE_GLOBAL(0x04),
DEPRECATED(0x08);

private final int value;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,18 @@
import java.util.stream.Collectors;
import nl.ramsolutions.sw.magik.MagikTypedFile;
import nl.ramsolutions.sw.magik.analysis.AstWalker;
import nl.ramsolutions.sw.magik.analysis.helpers.MethodInvocationNodeHelper;
import nl.ramsolutions.sw.magik.analysis.helpers.PackageNodeHelper;
import nl.ramsolutions.sw.magik.analysis.scope.GlobalScope;
import nl.ramsolutions.sw.magik.analysis.scope.Scope;
import nl.ramsolutions.sw.magik.analysis.scope.ScopeEntry;
import nl.ramsolutions.sw.magik.analysis.typing.ITypeKeeper;
import nl.ramsolutions.sw.magik.analysis.typing.reasoner.LocalTypeReasonerState;
import nl.ramsolutions.sw.magik.analysis.typing.types.AbstractType;
import nl.ramsolutions.sw.magik.analysis.typing.types.ExpressionResult;
import nl.ramsolutions.sw.magik.analysis.typing.types.MagikType;
import nl.ramsolutions.sw.magik.analysis.typing.types.TypeString;
import nl.ramsolutions.sw.magik.analysis.typing.types.UndefinedType;
import nl.ramsolutions.sw.magik.api.MagikGrammar;
import nl.ramsolutions.sw.magik.api.MagikKeyword;
import nl.ramsolutions.sw.magik.api.MagikOperator;
Expand All @@ -32,6 +37,7 @@
public class SemanticTokenWalker extends AstWalker {

private static final String DEFAULT_PACKAGE = "user";
private static final String TOPIC_DEPRECATED = "deprecated";

private static final List<String> MAGIK_MODIFIER_VALUES =
List.of(
Expand Down Expand Up @@ -278,7 +284,20 @@ protected void walkPreMethodInvocation(final AstNode node) {
return;
}

this.addSemanticToken(identifierNode, SemanticToken.Type.METHOD);
// Test for deprecation.
final MethodInvocationNodeHelper helper = new MethodInvocationNodeHelper(node);
final AstNode receiverNode = helper.getReceiverNode();
final LocalTypeReasonerState typeReasonerState = this.magikFile.getTypeReasonerState();
final ExpressionResult result = typeReasonerState.getNodeType(receiverNode);
final AbstractType type = result.get(0, UndefinedType.INSTANCE);
final String methodName = helper.getMethodName();
final Set<SemanticToken.Modifier> modifiers =
type.getMethods(methodName).stream()
.anyMatch(method -> method.getTopics().contains(TOPIC_DEPRECATED))
? Set.of(SemanticToken.Modifier.DEPRECATED)
: Collections.emptySet();

this.addSemanticToken(identifierNode, SemanticToken.Type.METHOD, modifiers);
}

@Override
Expand Down Expand Up @@ -353,9 +372,15 @@ private void walkPostIdentifierAtom(final AstNode node) {

case GLOBAL, DYNAMIC:
final TypeString typeString = TypeString.ofIdentifier(identifier, this.currentPakkage);
if (this.isKnownType(typeString)) {
this.addSemanticToken(
node, SemanticToken.Type.CLASS, Set.of(SemanticToken.Modifier.VARIABLE_GLOBAL));
final ITypeKeeper typeKeeper = this.magikFile.getTypeKeeper();
final AbstractType type = typeKeeper.getType(typeString);
if (type instanceof MagikType) {
final Set<SemanticToken.Modifier> modifiers =
type.getTopics().contains(TOPIC_DEPRECATED)
? Set.of(
SemanticToken.Modifier.VARIABLE_GLOBAL, SemanticToken.Modifier.DEPRECATED)
: Set.of(SemanticToken.Modifier.VARIABLE_GLOBAL);
this.addSemanticToken(node, SemanticToken.Type.CLASS, modifiers);
} else {
this.addSemanticToken(
node, SemanticToken.Type.VARIABLE, Set.of(SemanticToken.Modifier.VARIABLE_GLOBAL));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ void testMethodCompletionBare() {
Collections.emptySet(),
Collections.emptyList(),
null,
Collections.emptySet(),
ExpressionResultString.UNDEFINED,
ExpressionResultString.EMPTY));
final Position position = new Position(1, 6); // On '.'.
Expand Down Expand Up @@ -95,7 +96,8 @@ void testMethodCompletionSelf() {
ExemplarDefinition.Sort.SLOTTED,
aRef,
Collections.emptyList(),
Collections.emptyList()));
Collections.emptyList(),
Collections.emptySet()));
definitionKeeper.add(
new MethodDefinition(
null,
Expand All @@ -107,6 +109,7 @@ void testMethodCompletionSelf() {
Collections.emptySet(),
Collections.emptyList(),
null,
Collections.emptySet(),
ExpressionResultString.UNDEFINED,
ExpressionResultString.EMPTY));
final Position position = new Position(1, 10); // On '.'.
Expand Down Expand Up @@ -134,6 +137,7 @@ void testMethodCompletionExisting() {
Collections.emptySet(),
Collections.emptyList(),
null,
Collections.emptySet(),
ExpressionResultString.UNDEFINED,
ExpressionResultString.EMPTY));
final Position position = new Position(1, 8); // On 'i'.
Expand Down Expand Up @@ -196,7 +200,8 @@ void testGlobalCompletionSlot() {
ExemplarDefinition.Sort.SLOTTED,
aRef,
List.of(new SlotDefinition(null, code, code, null, code, aRef)),
Collections.emptyList()));
Collections.emptyList(),
Collections.emptySet()));

final Position position = new Position(1, 2); // On ''.
final List<CompletionItem> completions = this.getCompletions(code, definitionKeeper, position);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ void testProvideDefinitionsFromGlobal() {
ExemplarDefinition.Sort.SLOTTED,
ropeRef,
Collections.emptyList(),
Collections.emptyList()));
Collections.emptyList(),
Collections.emptySet()));

final String code =
"" + "_method object.method\n" + " _return rope.new()\n" + "_endmethod\n";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ void testProvideHoverMethodDefinitionName() {
Collections.emptySet(),
Collections.emptyList(),
null,
Collections.emptySet(),
ExpressionResultString.UNDEFINED,
ExpressionResultString.EMPTY));

Expand Down Expand Up @@ -73,7 +74,8 @@ void testProvideHoverExemplarName() {
ExemplarDefinition.Sort.SLOTTED,
hoverMeTypeRef,
Collections.emptyList(),
Collections.emptyList()));
Collections.emptyList(),
Collections.emptySet()));

final String code = "" + "_method hover_me_type.method()\n" + "_endmethod";
final Position position = new Position(0, 10); // On 'hover_me_type'.
Expand Down Expand Up @@ -101,6 +103,7 @@ void testProvideHoverMethod() {
Collections.emptySet(),
Collections.emptyList(),
null,
Collections.emptySet(),
ExpressionResultString.UNDEFINED,
ExpressionResultString.EMPTY));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ void testProvideAbstractMethodImplementation() {
ExemplarDefinition.Sort.SLOTTED,
aRef,
Collections.emptyList(),
Collections.emptyList()));
Collections.emptyList(),
Collections.emptySet()));
definitionKeeper.add(
new MethodDefinition(
new Location(
Expand All @@ -52,6 +53,7 @@ void testProvideAbstractMethodImplementation() {
Set.of(MethodDefinition.Modifier.ABSTRACT),
Collections.emptyList(),
null,
Collections.emptySet(),
ExpressionResultString.UNDEFINED,
ExpressionResultString.EMPTY));
final TypeString bRef = TypeString.ofIdentifier("b", "user");
Expand All @@ -64,7 +66,8 @@ void testProvideAbstractMethodImplementation() {
ExemplarDefinition.Sort.SLOTTED,
bRef,
Collections.emptyList(),
List.of(aRef)));
List.of(aRef),
Collections.emptySet()));
definitionKeeper.add(
new MethodDefinition(
new Location(
Expand All @@ -78,6 +81,7 @@ void testProvideAbstractMethodImplementation() {
Collections.emptySet(), // Concrete.
Collections.emptyList(),
null,
Collections.emptySet(),
ExpressionResultString.UNDEFINED,
ExpressionResultString.EMPTY));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ void testProvideParameterHint() {
ParameterDefinition.Modifier.NONE,
TypeString.UNDEFINED)),
null,
Collections.emptySet(),
ExpressionResultString.UNDEFINED,
ExpressionResultString.EMPTY));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ void testProvideMethodReferenceFromMethodInvocation() {
Collections.emptySet(),
Collections.emptyList(),
null,
Collections.emptySet(),
ExpressionResultString.UNDEFINED,
ExpressionResultString.EMPTY,
Collections.emptySet(),
Expand Down Expand Up @@ -73,6 +74,7 @@ void testProvideMethodReferenceFromMethodDefintion() {
Collections.emptySet(),
Collections.emptyList(),
null,
Collections.emptySet(),
ExpressionResultString.UNDEFINED,
ExpressionResultString.EMPTY,
Collections.emptySet(),
Expand Down Expand Up @@ -100,6 +102,7 @@ void testProvideTypeReferenceFromAtom() {
Collections.emptySet(),
Collections.emptyList(),
null,
Collections.emptySet(),
ExpressionResultString.UNDEFINED,
ExpressionResultString.EMPTY,
Set.of(new GlobalUsage(TypeString.SW_INTEGER, EMPTY_LOCATION)),
Expand Down Expand Up @@ -127,6 +130,7 @@ void testProvideTypeReferenceFromMethodDefinition() {
Collections.emptySet(),
Collections.emptyList(),
null,
Collections.emptySet(),
ExpressionResultString.UNDEFINED,
ExpressionResultString.EMPTY,
Set.of(new GlobalUsage(TypeString.SW_INTEGER, EMPTY_LOCATION)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ void testPrepareTypeHierarchyMethodDefinitionExemplarName() {
ExemplarDefinition.Sort.SLOTTED,
exemplarRef,
Collections.emptyList(),
Collections.emptyList()));
Collections.emptyList(),
Collections.emptySet()));

final String code = "" + "_method exemplar.method\n" + "_endmethod\n";
final Position position = new Position(0, 10); // On 'exemplar'.
Expand All @@ -64,7 +65,8 @@ void testPrepareTypeHierarchyGlobal() {
ExemplarDefinition.Sort.SLOTTED,
ropeRef,
Collections.emptyList(),
Collections.emptyList()));
Collections.emptyList(),
Collections.emptySet()));

final String code = "" + "_method exemplar.method\n" + " rope.new()\n" + "_endmethod\n";
final Position position = new Position(1, 4); // On 'rope'.
Expand All @@ -87,7 +89,8 @@ void testGetSubtypes() {
ExemplarDefinition.Sort.SLOTTED,
exemplarRef,
Collections.emptyList(),
Collections.emptyList()));
Collections.emptyList(),
Collections.emptySet()));
final TypeString subExemplarRef = TypeString.ofIdentifier("sub_exemplar", "user");
definitionKeeper.add(
new ExemplarDefinition(
Expand All @@ -98,7 +101,8 @@ void testGetSubtypes() {
ExemplarDefinition.Sort.SLOTTED,
subExemplarRef,
Collections.emptyList(),
List.of(exemplarRef)));
List.of(exemplarRef),
Collections.emptySet()));

final TypeHierarchyItem item =
new TypeHierarchyItem(
Expand All @@ -125,7 +129,8 @@ void testGetSupertypes() {
ExemplarDefinition.Sort.SLOTTED,
exemplarRef,
Collections.emptyList(),
List.of(TypeString.ofIdentifier("slotted_format_mixin", "sw"))));
List.of(TypeString.ofIdentifier("slotted_format_mixin", "sw")),
Collections.emptySet()));

final TypeHierarchyItem item =
new TypeHierarchyItem(
Expand Down
Loading

0 comments on commit 9a08d9a

Please sign in to comment.