-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add code action to create DEFINE PROTOTYPE (#427)
- Loading branch information
Showing
12 changed files
with
459 additions
and
14 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
libs/natlint/src/main/java/org/amshove/natlint/analyzers/DefinePrototypeAnalyzer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package org.amshove.natlint.analyzers; | ||
|
||
import org.amshove.natlint.api.AbstractAnalyzer; | ||
import org.amshove.natlint.api.DiagnosticDescription; | ||
import org.amshove.natlint.api.IAnalyzeContext; | ||
import org.amshove.natlint.api.ILinterContext; | ||
import org.amshove.natparse.DiagnosticSeverity; | ||
import org.amshove.natparse.NodeUtil; | ||
import org.amshove.natparse.ReadOnlyList; | ||
import org.amshove.natparse.natural.IDefinePrototypeNode; | ||
import org.amshove.natparse.natural.ISyntaxNode; | ||
|
||
public class DefinePrototypeAnalyzer extends AbstractAnalyzer | ||
{ | ||
public static final DiagnosticDescription PROTOTYPE_DEFINED_MORE_THAN_ONCE = DiagnosticDescription.create( | ||
"NL022", | ||
"Prototype for function %s is defined more than once", | ||
DiagnosticSeverity.WARNING | ||
); | ||
|
||
@Override | ||
public ReadOnlyList<DiagnosticDescription> getDiagnosticDescriptions() | ||
{ | ||
return ReadOnlyList.of(PROTOTYPE_DEFINED_MORE_THAN_ONCE); | ||
} | ||
|
||
@Override | ||
public void initialize(ILinterContext context) | ||
{ | ||
context.registerNodeAnalyzer(IDefinePrototypeNode.class, this::analyzeDefinePrototype); | ||
} | ||
|
||
private void analyzeDefinePrototype(ISyntaxNode node, IAnalyzeContext context) | ||
{ | ||
var prototype = ((IDefinePrototypeNode) node); | ||
var prototypesInModule = NodeUtil.findNodesOfType(context.getModule().syntaxTree(), IDefinePrototypeNode.class); | ||
|
||
for (var otherPrototype : prototypesInModule) | ||
{ | ||
if (otherPrototype == prototype) | ||
{ | ||
continue; | ||
} | ||
|
||
if (otherPrototype.nameToken().symbolName().equals(prototype.nameToken().symbolName())) | ||
{ | ||
context.report( | ||
PROTOTYPE_DEFINED_MORE_THAN_ONCE.createFormattedDiagnostic( | ||
otherPrototype.nameToken(), | ||
otherPrototype.nameToken().symbolName() | ||
) | ||
); | ||
} | ||
} | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
libs/natlint/src/test/java/org/amshove/natlint/analyzers/DefinePrototypeAnalyzerShould.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package org.amshove.natlint.analyzers; | ||
|
||
import org.amshove.natlint.linter.AbstractAnalyzerTest; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class DefinePrototypeAnalyzerShould extends AbstractAnalyzerTest | ||
{ | ||
|
||
@Test | ||
void beRaisedWhenMoreThanOnePrototypeForTheSameFunctionIsDefined() | ||
{ | ||
testDiagnostics( | ||
""" | ||
DEFINE DATA LOCAL | ||
END-DEFINE | ||
DEFINE PROTOTYPE FUNC RETURNS (L) | ||
END-PROTOTYPE | ||
DEFINE PROTOTYPE FUNC RETURNS (L) | ||
END-PROTOTYPE | ||
END | ||
""", | ||
expectDiagnostic(3, DefinePrototypeAnalyzer.PROTOTYPE_DEFINED_MORE_THAN_ONCE), | ||
expectDiagnostic(6, DefinePrototypeAnalyzer.PROTOTYPE_DEFINED_MORE_THAN_ONCE) | ||
); | ||
} | ||
|
||
@Test | ||
void notBeRaisedWhenUniquePrototypesAreDefined() | ||
{ | ||
testDiagnostics( | ||
""" | ||
DEFINE DATA LOCAL | ||
END-DEFINE | ||
DEFINE PROTOTYPE FUNC RETURNS (L) | ||
END-PROTOTYPE | ||
DEFINE PROTOTYPE FUNC2 RETURNS (L) | ||
END-PROTOTYPE | ||
END | ||
""", | ||
expectNoDiagnosticOfType(DefinePrototypeAnalyzer.PROTOTYPE_DEFINED_MORE_THAN_ONCE) | ||
); | ||
} | ||
|
||
@Test | ||
void notBeRaisedWhenNoPrototypeIsDefined() | ||
{ | ||
testDiagnostics( | ||
""" | ||
DEFINE DATA LOCAL | ||
END-DEFINE | ||
END | ||
""", | ||
expectNoDiagnosticOfType(DefinePrototypeAnalyzer.PROTOTYPE_DEFINED_MORE_THAN_ONCE) | ||
); | ||
} | ||
|
||
protected DefinePrototypeAnalyzerShould() | ||
{ | ||
super(new DefinePrototypeAnalyzer()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
libs/natls/src/main/java/org/amshove/natls/refactorings/DefinePrototypeAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package org.amshove.natls.refactorings; | ||
|
||
import org.amshove.natls.WorkspaceEditBuilder; | ||
import org.amshove.natls.codeactions.ICodeActionProvider; | ||
import org.amshove.natls.codeactions.RefactoringContext; | ||
import org.amshove.natls.quickfixes.CodeActionBuilder; | ||
import org.amshove.natparse.natural.IFunction; | ||
import org.amshove.natparse.natural.IFunctionCallNode; | ||
import org.amshove.natparse.natural.project.NaturalFileType; | ||
import org.eclipse.lsp4j.CodeAction; | ||
import org.eclipse.lsp4j.CodeActionKind; | ||
|
||
import java.util.List; | ||
|
||
public class DefinePrototypeAction implements ICodeActionProvider | ||
{ | ||
@Override | ||
public boolean isApplicable(RefactoringContext context) | ||
{ | ||
return context.file().getType() == NaturalFileType.FUNCTION && context.nodeAtStartPosition() instanceof IFunctionCallNode; | ||
} | ||
|
||
@Override | ||
public List<CodeAction> createCodeAction(RefactoringContext context) | ||
{ | ||
var functionCall = (IFunctionCallNode) context.nodeAtStartPosition(); | ||
if (functionCall.reference() == null) | ||
{ | ||
return List.of(); | ||
} | ||
|
||
var function = (IFunction) context.service().findNaturalFile(functionCall.reference().file().getPath()).module(); | ||
|
||
return List.of( | ||
new CodeActionBuilder("Define Prototype", CodeActionKind.Refactor) | ||
.appliesWorkspaceEdit( | ||
new WorkspaceEditBuilder() | ||
.addPrototype(context.file(), function) | ||
) | ||
.build() | ||
); | ||
} | ||
} |
Oops, something went wrong.