Skip to content

Commit

Permalink
Fix blame for the closing bracket of try blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
tsantalis committed Jul 3, 2024
1 parent 0679aaa commit 3af848e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/main/java/org/codetracker/BlockTrackerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,6 @@ public History<Block> track() throws Exception {
if (startBlock == null) {
throw new CodeElementNotFoundException(filePath, changeHistory.getBlockType().getName(), changeHistory.getBlockStartLineNumber());
}
if (startBlock.getLocation().getEndLine() == changeHistory.getBlockEndLineNumber() && startBlock.getComposite() instanceof CompositeStatementObject) {
startBlock.setClosingCurlyBracket(true);
}
changeHistory.get().addNode(startBlock);

ArrayDeque<Block> blocks = new ArrayDeque<>();
Expand Down Expand Up @@ -340,6 +337,9 @@ public History.HistoryInfo<Block> blame() throws Exception {
if (startBlock == null) {
throw new CodeElementNotFoundException(filePath, changeHistory.getBlockType().getName(), changeHistory.getBlockStartLineNumber());
}
if (startBlock.getLocation().getEndLine() == changeHistory.getBlockEndLineNumber() && startBlock.getComposite() instanceof CompositeStatementObject) {
startBlock.setClosingCurlyBracket(true);
}
changeHistory.get().addNode(startBlock);

ArrayDeque<Block> blocks = new ArrayDeque<>();
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/org/codetracker/util/CodeElementLocatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,24 @@ public void testClosingBracketElseBlockLocator2() throws Exception {
}
}

@Test
public void testClosingBracketTryLocator() throws Exception {
GitService gitService = new GitServiceImpl();
final String filePath = "src/main/java/com/puppycrawl/tools/checkstyle/Checker.java";
final String commitId = "119fd4fb33bef9f5c66fc950396669af842c21a3";
final int lineNumber = 322;
try (Repository repository = gitService.cloneIfNotExists(FOLDER_TO_CLONE + "checkstyle\\checkstyle",
"https://github.com/checkstyle/checkstyle.git")){
CodeElementLocator locator = new CodeElementLocator(repository, commitId, filePath, lineNumber);
CodeElement codeElement = locator.locate();
assertNotNull(codeElement);
assertEquals(codeElement.getClass(), Block.class);
assertEquals(codeElement.getLocation().getCodeElementType(), CodeElementType.TRY_STATEMENT);
assertEquals(codeElement.getLocation().getStartLine(), 317);
assertTrue(((Block)codeElement).isClosingCurlyBracket());
}
}

@Test
public void testClosingBracketCatchLocator() throws Exception {
GitService gitService = new GitServiceImpl();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,21 @@ public void testClosingBracketElseBlockLocator2() throws Exception {
assertTrue(((Block)codeElement).isClosingCurlyBracket());
}

@Test
public void testClosingBracketTryLocator() throws Exception {
final String cloneURL = "https://github.com/checkstyle/checkstyle.git";
final String filePath = "src/main/java/com/puppycrawl/tools/checkstyle/Checker.java";
final String commitId = "119fd4fb33bef9f5c66fc950396669af842c21a3";
final int lineNumber = 322;
CodeElementLocatorWithLocalFiles locator = new CodeElementLocatorWithLocalFiles(cloneURL, commitId, filePath, lineNumber);
CodeElement codeElement = locator.locate();
assertNotNull(codeElement);
assertEquals(codeElement.getClass(), Block.class);
assertEquals(codeElement.getLocation().getCodeElementType(), CodeElementType.TRY_STATEMENT);
assertEquals(codeElement.getLocation().getStartLine(), 317);
assertTrue(((Block)codeElement).isClosingCurlyBracket());
}

@Test
public void testClosingBracketCatchLocator() throws Exception {
final String cloneURL = "https://github.com/checkstyle/checkstyle.git";
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/blame/blameTestWithLocalRepo.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ ae3a4f8a5 src/main/java/dat/MakeIntels.java (Pouryafard75 2024-06-24 21:47:09 -0
e5e209d52 src/main/java/dat/Make.java (Pouryafard75 2024-03-06 20:33:47 -0500 40) // if (case_count == 2) break;
e5e209d52 src/main/java/dat/Make.java (Pouryafard75 2024-03-06 20:33:47 -0500 41)
e5e209d52 src/main/java/dat/Make.java (Pouryafard75 2024-03-06 20:33:47 -0500 42) }
e5e209d52 src/main/java/dat/Make.java (Pouryafard75 2024-03-06 20:33:47 -0500 43) }
ba790bfa4 src/main/java/dat/Make.java (Pouryafard75 2024-03-06 20:33:48 -0500 43) }
ba790bfa4 src/main/java/dat/Make.java (Pouryafard75 2024-03-06 20:33:48 -0500 44) catch (Exception e) {
ba790bfa4 src/main/java/dat/Make.java (Pouryafard75 2024-03-06 20:33:48 -0500 45) logger.error("Error in DAT", e);
ba790bfa4 src/main/java/dat/Make.java (Pouryafard75 2024-03-06 20:33:48 -0500 46) logger.debug("Error in DAT", e);
Expand Down

0 comments on commit 3af848e

Please sign in to comment.