Skip to content

Commit

Permalink
Keeping Sonar happy
Browse files Browse the repository at this point in the history
  • Loading branch information
StevenLooman committed Jul 9, 2023
1 parent 257b566 commit fa0a170
Show file tree
Hide file tree
Showing 13 changed files with 34 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,12 @@ public static boolean rangeOverlaps(final Range range1, final Range range2) {
// 3. wantedRange in testedRange --> true
// 4. wantedRange in testedRange and after testedRange --> true
// 5. wantedRange starts after testedRange end --> false
// Case 1/5.
final PositionComparator comparator = new PositionComparator();
if (comparator.compare(range1.getEnd(), range2.getStart()) < 0
|| comparator.compare(range1.getStart(), range2.getEnd()) > 0) {
return false;
}

// Case 3/4.
return true;
// Testing case 1/5 is enough.
final PositionComparator comparator = new PositionComparator();
return !(
comparator.compare(range1.getEnd(), range2.getStart()) < 0
|| comparator.compare(range1.getStart(), range2.getEnd()) > 0);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import nl.ramsolutions.sw.magik.analysis.Range;
import nl.ramsolutions.sw.magik.analysis.definitions.MethodDefinition;
import nl.ramsolutions.sw.magik.analysis.typing.LocalTypeReasoner;
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.ExpressionResultString;
import nl.ramsolutions.sw.magik.analysis.typing.types.TypeString;
Expand Down Expand Up @@ -47,15 +48,15 @@ public List<Either<Command, CodeAction>> provideCodeActions(
final org.eclipse.lsp4j.Range range,
final CodeActionContext context) {
return magikFile.getDefinitions().stream()
.filter(definition -> definition instanceof MethodDefinition)
.map(definition -> (MethodDefinition) definition)
.filter(methodDefinition -> methodDefinition.isActualMethodDefinition())
.filter(MethodDefinition.class::isInstance)
.map(MethodDefinition.class::cast)
.filter(MethodDefinition::isActualMethodDefinition)
.filter(methodDefinition -> Lsp4jUtils.rangeOverlaps(
range,
Lsp4jConversion.rangeToLsp4j(
nl.ramsolutions.sw.magik.analysis.Range.fromTree(methodDefinition.getNode()))))
.flatMap(methodDefinition -> this.extractReturnTypeCodeActions(magikFile, methodDefinition).stream())
.map(codeAction -> Either.<Command, CodeAction>forRight(codeAction))
.map(Either::<Command, CodeAction>forRight)
.collect(Collectors.toList());
}

Expand All @@ -66,7 +67,7 @@ private List<CodeAction> extractReturnTypeCodeActions(
final AstNode methodDefinitionNode = methodDefinition.getNode();
final ExpressionResult methodResult = reasoner.getNodeType(methodDefinitionNode);
final ExpressionResultString methodResultString = methodResult.stream()
.map(type -> type.getTypeString())
.map(AbstractType::getTypeString)
.collect(ExpressionResultString.COLLECTOR);

final TypeDocParser typeDocParser = new TypeDocParser(methodDefinition.getNode());
Expand All @@ -84,16 +85,15 @@ private List<CodeAction> extractReturnTypeCodeActions(
final Map.Entry<AstNode, TypeString> typeDocEntry = entry.getValue();
if (methodTypeString != null && typeDocEntry == null) {
// Code action: Add type-doc line.
final int insertLine = -1;
return this.createAddReturnCodeAction(magikFile, methodDefinition, insertLine, methodTypeString);
return this.createAddReturnCodeAction(magikFile, methodDefinition, methodTypeString);
}

final TypeString typeDocTypeString = typeDocEntry.getValue();
final AstNode typeDocNode = typeDocEntry.getKey();
final AstNode typeValueNode = typeDocNode.getFirstChild(TypeDocGrammar.TYPE_VALUE);
if (methodTypeString == null && typeDocEntry != null) {
// Code action: Remove type-doc line.
return this.createRemoveReturnCodeAction(magikFile, methodDefinition, typeValueNode);
return this.createRemoveReturnCodeAction(magikFile, typeValueNode);
} else if (methodTypeString != null // && typeDocTypeString != null
&& methodTypeString != TypeString.UNDEFINED
&& !methodTypeString.equals(typeDocTypeString)) {
Expand All @@ -120,7 +120,6 @@ private CodeAction createUpdateReturnCodeAction(

private CodeAction createRemoveReturnCodeAction(
final MagikTypedFile magikFile,
final MethodDefinition methodDefinition,
final AstNode typeValueNode) {
final AstNode typeDocReturnNode = typeValueNode.getParent();
final Range treeRange = Range.fromTree(typeDocReturnNode);
Expand All @@ -136,13 +135,12 @@ private CodeAction createRemoveReturnCodeAction(
private CodeAction createAddReturnCodeAction(
final MagikTypedFile magikFile,
final MethodDefinition methodDefinition,
final int line,
final TypeString methodTypeString) {
final int lastMethodDocLine = this.getLastMethodDocLine(methodDefinition);
final Range range = new Range(
new Position(lastMethodDocLine + 1, 0),
new Position(lastMethodDocLine + 1, 0));
final String textEdit = String.format("\t## @return {%s} Description\n", methodTypeString.getFullString());
final String textEdit = String.format("\t## @return {%s} Description%n", methodTypeString.getFullString());
final String description = String.format("Add @return type %s", methodTypeString.getFullString());
return Lsp4jUtils.createCodeAction(magikFile, range, textEdit, description);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ public List<Either<Command, CodeAction>> provideCodeActions(
final org.eclipse.lsp4j.Range range,
final CodeActionContext context) {
return magikFile.getDefinitions().stream()
.filter(definition -> definition instanceof MethodDefinition)
.map(definition -> (MethodDefinition) definition)
.filter(methodDefinition -> methodDefinition.isActualMethodDefinition())
.filter(MethodDefinition.class::isInstance)
.map(MethodDefinition.class::cast)
.filter(MethodDefinition::isActualMethodDefinition)
.filter(methodDefinition -> Lsp4jUtils.rangeOverlaps(
range,
Lsp4jConversion.rangeToLsp4j(
Range.fromTree(methodDefinition.getNode()))))
.flatMap(methodDefinition -> this.extractParameterCodeActions(magikFile, methodDefinition).stream())
.map(codeAction -> Either.<Command, CodeAction>forRight(codeAction))
.map(Either::<Command, CodeAction>forRight)
.collect(Collectors.toList());
}

Expand Down Expand Up @@ -87,7 +87,7 @@ private Collection<CodeAction> createAddParameterCodeAction(
new Position(lastTypeDocParameterLine + 1, 0),
new Position(lastTypeDocParameterLine + 1, 0));
final String typeDocLine =
String.format("\t## @param {_undefined} %s Description\n", paramDef.getName());
String.format("\t## @param {_undefined} %s Description%n", paramDef.getName());
final String description = String.format("Add type-doc for parameter %s", paramDef.getName());
return Lsp4jUtils.createCodeAction(magikFile, range, typeDocLine, description);
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,11 @@ private List<InlayHint> getMethodInvocationInlayHints(
final List<AstNode> argumentNodes = argumentsNode.getDescendants(MagikGrammar.ARGUMENT);
for (int i = 0; i < argumentNodes.size(); ++i) {
final AstNode argumentNode = argumentNodes.get(i);
if (!this.isSimpleAtomArgument(argumentNode)) {
if (!this.isSimpleAtomArgument(argumentNode)
|| i >= parameters.size()) {
continue;
}

if (i >= parameters.size()) {
break;
}

final Parameter parameter = parameters.get(i);
final InlayHint inlayHint = this.getArgumentInlayHint(argumentNode, parameter);
inlayHints.add(inlayHint);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public List<TypeHierarchyItem> prepareTypeHierarchy(final MagikTypedFile magikFi
Lsp4jConversion.positionFromLsp4j(position),
MagikGrammar.IDENTIFIER);
if (tokenNode == null) {
return null;
return null; // NOSONAR: LSP requires null.
}

// Ensure it is on a class identifier, or a variable.
Expand Down Expand Up @@ -101,7 +101,7 @@ public List<TypeHierarchyItem> prepareTypeHierarchy(final MagikTypedFile magikFi
}
}

return null;
return null; // NOSONAR: LSP requires null.
}

/**
Expand All @@ -114,7 +114,7 @@ public List<TypeHierarchyItem> typeHierarchySubtypes(final TypeHierarchyItem ite
final TypeString typeString = TypeString.ofIdentifier(itemName, "sw");
final AbstractType type = this.typeKeeper.getType(typeString);
if (type == UndefinedType.INSTANCE) {
return null;
return null; // NOSONAR: LSP requires null.
}

// Find children.
Expand All @@ -136,7 +136,7 @@ public List<TypeHierarchyItem> typeHierarchySupertypes(final TypeHierarchyItem i
final TypeString typeString = TypeString.ofIdentifier(itemName, "sw");
final AbstractType type = this.typeKeeper.getType(typeString);
if (type == UndefinedType.INSTANCE) {
return null;
return null; // NOSONAR: LSP requires null.
}

final Comparator<TypeHierarchyItem> byName = Comparator.comparing(TypeHierarchyItem::getName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ void testRemoveReturn() {
final List<TextEdit> changes = textDocumentEdit.getEdits();
assertThat(uri).isEqualTo("tests://unittest");
assertThat(changes).hasSize(1);
assertThat(changes.get(0).getNewText()).isEqualTo("");
assertThat(changes.get(0).getNewText()).isEmpty();
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
/**
* Tests for ParameterTypeProvider.
*/
public class ParameterTypeProviderTest {
class ParameterTypeProviderTest {

private List<CodeAction> getCodeActions(
final String code,
Expand Down Expand Up @@ -75,7 +75,7 @@ void testRemoveParameter() {
final List<TextEdit> changes = textDocumentEdit.getEdits();
assertThat(uri).isEqualTo("tests://unittest");
assertThat(changes).hasSize(1);
assertThat(changes.get(0).getNewText()).isEqualTo("");
assertThat(changes.get(0).getNewText()).isEmpty();
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
/**
* Tests for {@link InlayHintProvider}.
*/
public class InlayHintProviderTest {
class InlayHintProviderTest {

private static final URI TEST_URI = URI.create("tests://unittest");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
/**
* Tests for TypeHierarchyProvider.
*/
public class TypeHierarchyProviderTest {
class TypeHierarchyProviderTest {

private static final URI TEST_URI = URI.create("tests://unittest");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private void readFileProperties(final Path path) {
try (InputStream inputStream = new FileInputStream(file)) {
this.properties.load(inputStream);
} catch (FileNotFoundException exception) {
LOGGER.warn("Configuration not found at: " + path);
LOGGER.warn("Configuration not found at: {}", path);
} catch (IOException exception) {
LOGGER.error(exception.getMessage(), exception);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public void addParent(final TypeString parentTypeString) {
@Override
public Collection<AbstractType> getParents() {
return this.parents.stream()
.map(parentRef -> this.typeKeeper.getType(parentRef))
.map(this.typeKeeper::getType)
.collect(Collectors.toList());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
/**
* Type to represent self, to be evaluated later when the real type is known.
*/
public final class SelfType extends AbstractType {
public final class SelfType extends AbstractType { // NOSONAR: Singleton.

/**
* Instance of {@code _self}/{@code _clone} to be used in all cases.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
/**
* Special type used when the {@link TypeReasoner} cannot determine type.
*/
public final class UndefinedType extends AbstractType {
public final class UndefinedType extends AbstractType { // NOSONAR: Singleton.

/**
* Instance of {@code UndefinedType} to be used in all cases.
Expand Down

0 comments on commit fa0a170

Please sign in to comment.