Skip to content

Commit

Permalink
Include methods in UnitLevelKeywordIndentation
Browse files Browse the repository at this point in the history
  • Loading branch information
fourls authored and Cirras committed Oct 20, 2023
1 parent f8d58a1 commit 2f5b745
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Exclude dpr and dpk files in `UnusedImport`.
- Exclude dpr and dpk files in `ImportSpecificity`.
- Exclude uses clauses of dpr and dpk files in `LineTooLong`.
- Include methods in `UnitLevelKeywordIndentation`.
- Always enforce the `Attribute` suffix in `AttributeName`.
- Improve name resolution for declarations within types.
- Improve type resolution for array accesses into variants.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import org.sonar.plugins.communitydelphi.api.ast.InitializationSectionNode;
import org.sonar.plugins.communitydelphi.api.ast.InterfaceSectionNode;
import org.sonar.plugins.communitydelphi.api.ast.LibraryDeclarationNode;
import org.sonar.plugins.communitydelphi.api.ast.MethodDeclarationNode;
import org.sonar.plugins.communitydelphi.api.ast.MethodImplementationNode;
import org.sonar.plugins.communitydelphi.api.ast.PackageDeclarationNode;
import org.sonar.plugins.communitydelphi.api.ast.ProgramDeclarationNode;
import org.sonar.plugins.communitydelphi.api.ast.TypeSectionNode;
Expand All @@ -44,13 +46,13 @@
@DeprecatedRuleKey(ruleKey = "UnitLevelKeywordIndentationRule", repositoryKey = "delph")
@Rule(key = "UnitLevelKeywordIndentation")
public class UnitLevelKeywordIndentationCheck extends DelphiCheck {
private static final String MESSAGE = "Unindent this unit-level keyword.";
private static final String MESSAGE = "Unindent this top-level element.";

private static void checkNodeIndentation(DelphiNode node, DelphiCheckContext context) {
if (!IndentationUtils.getLineIndentation(node).equals("")) {
if (!IndentationUtils.getLineIndentation(node).isEmpty()) {
context
.newIssue()
.onFilePosition(FilePosition.from(node.getToken()))
.onFilePosition(FilePosition.from(node.getFirstToken()))
.withMessage(MESSAGE)
.report();
}
Expand Down Expand Up @@ -154,6 +156,30 @@ public DelphiCheckContext visit(
return super.visit(compoundStatementNode, context);
}

@Override
public DelphiCheckContext visit(
MethodDeclarationNode methodDeclarationNode, DelphiCheckContext context) {
if (methodDeclarationNode.getParent() instanceof InterfaceSectionNode) {
checkNodeIndentation(methodDeclarationNode, context);
}
return super.visit(methodDeclarationNode, context);
}

@Override
public DelphiCheckContext visit(
MethodImplementationNode methodImplementationNode, DelphiCheckContext context) {
checkNodeIndentation(methodImplementationNode, context);

DelphiNode block = methodImplementationNode.getBlock();

if (block != null) {
checkNodeIndentation(block, context);
checkNodeIndentation(getEnd(block), context);
}

return super.visit(methodImplementationNode, context);
}

@Override
public DelphiCheckContext visit(
InitializationSectionNode initializationSectionNode, DelphiCheckContext context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void testIndentedUnitShouldAddIssues() {
CheckVerifier.newVerifier()
.withCheck(new UnitLevelKeywordIndentationCheck())
.onFile(DelphiTestFileBuilder.fromResource(INDENTED_PAS))
.verifyIssueOnLine(1, 3, 5, 9, 13, 15, 19, 23, 25, 27);
.verifyIssueOnLine(1, 3, 5, 9, 13, 15, 17, 21, 28, 29, 31, 33, 34, 36, 38, 40, 42);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,30 @@ interface
TMyObject = class(TObject)
end;

procedure MyProc;

implementation

uses
StrUtils,
Math;

type
TMyObject = class(TObject)
TMyOtherObject = class(TObject)
private
class procedure MyClassProc;
end;

class procedure TMyOtherObject.MyClassProc;
begin
Writeln('Hello world');
end;

procedure MyProc;
begin
Writeln('Hello world');
end;

initialization

finalization
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,31 @@
TMyObject = class(TObject)
end;

procedure MyProc;

implementation

uses
StrUtils,
Math;

type
TMyObject = class(TObject)
TMyOtherObject = class(TObject)
private
class procedure MyClassProc;
end;


class procedure TMyOtherObject.MyClassProc;
begin
Writeln('Hello world');
end;

procedure MyProc;
begin
Writeln('Hello world');
end;

initialization

finalization
Expand Down

0 comments on commit 2f5b745

Please sign in to comment.