use of com.google.gerrit.entities.FixReplacement in project gerrit by GerritCodeReview.
the class FixCalculatorVariousTest method severalChangesInNonConsecutiveLines.
@Test
public void severalChangesInNonConsecutiveLines() throws Exception {
FixReplacement replace = new FixReplacement("path", new Range(1, 1, 1, 3), "ABC");
FixReplacement insert = new FixReplacement("path", new Range(3, 5, 3, 5), "DEFG");
FixReplacement delete = new FixReplacement("path", new Range(5, 9, 6, 0), "");
FixResult result = FixCalculator.calculateFix(multilineContent, ImmutableList.of(replace, insert, delete));
assertThat(result).text().isEqualTo("FABCst line\nSecond line\nThirdDEFG line\nFourth line\nFifth lin");
assertThat(result).edits().hasSize(3);
assertThat(result).edits().element(0).isReplace(0, 1, 0, 1);
assertThat(result).edits().element(0).internalEdits().onlyElement().isReplace(1, 2, 1, 3);
assertThat(result).edits().element(1).isReplace(2, 1, 2, 1);
assertThat(result).edits().element(1).internalEdits().onlyElement().isInsert(5, 5, 4);
assertThat(result).edits().element(2).isReplace(4, 1, 4, 1);
assertThat(result).edits().element(2).internalEdits().onlyElement().isDelete(9, 2, 9);
}
use of com.google.gerrit.entities.FixReplacement in project gerrit by GerritCodeReview.
the class FixCalculatorVariousTest method changesMayTouch.
@Test
public void changesMayTouch() throws Exception {
FixReplacement firstReplace = new FixReplacement("path", new Range(1, 6, 2, 7), "modified ");
FixReplacement consecutiveReplace = new FixReplacement("path", new Range(2, 7, 3, 5), "content");
FixResult result = FixCalculator.calculateFix(multilineContent, ImmutableList.of(firstReplace, consecutiveReplace));
assertThat(result).text().isEqualTo("First modified content line\nFourth line\nFifth line\n");
assertThat(result).edits().hasSize(1);
Edit edit = result.edits.get(0);
assertThat(edit).isReplace(0, 3, 0, 1);
// The current code creates two inline edits even though only one would be necessary. It
// shouldn't make a visual difference to the user and hence we can ignore this.
assertThat(edit).internalEdits().hasSize(2);
assertThat(edit).internalEdits().element(0).isReplace(6, 12, 6, 9);
assertThat(edit).internalEdits().element(1).isReplace(18, 10, 15, 7);
}
use of com.google.gerrit.entities.FixReplacement in project gerrit by GerritCodeReview.
the class FixReplacementInterpreterTest method treeModificationsTargetCorrectFiles.
@Test
public void treeModificationsTargetCorrectFiles() throws Exception {
FixReplacement fixReplacement = new FixReplacement(filePath1, new Range(1, 6, 3, 2), "Modified content");
FixReplacement fixReplacement2 = new FixReplacement(filePath1, new Range(3, 5, 3, 5), "Second modification");
mockFileContent(filePath1, "First line\nSecond line\nThird line\n");
FixReplacement fixReplacement3 = new FixReplacement(filePath2, new Range(2, 0, 3, 0), "Another modified content");
mockFileContent(filePath2, "1st line\n2nd line\n3rd line\n");
CommitModification commitModification = toCommitModification(fixReplacement, fixReplacement3, fixReplacement2);
List<TreeModification> sortedTreeModifications = getSortedCopy(commitModification.treeModifications());
assertThatList(sortedTreeModifications).element(0).asChangeFileContentModification().filePaths().containsExactly(filePath1);
assertThatList(sortedTreeModifications).element(0).asChangeFileContentModification().newContent().startsWith("First");
assertThatList(sortedTreeModifications).element(1).asChangeFileContentModification().filePaths().containsExactly(filePath2);
assertThatList(sortedTreeModifications).element(1).asChangeFileContentModification().newContent().startsWith("1st");
}
use of com.google.gerrit.entities.FixReplacement in project gerrit by GerritCodeReview.
the class FixCalculator method calculateFix.
/**
* Returns a result of applying fixes to an original content and list of applied edits.
*
* @param originalText is a text to which fixes must be applied
* @param fixReplacements is a list of fixes to be applied
* @return {@link FixResult}
* @throws ResourceConflictException if the fixReplacements contains invalid data (for example, if
* an item points to an invalid range or if some ranges are intersected).
*/
public static FixResult calculateFix(Text originalText, List<FixReplacement> fixReplacements) throws ResourceConflictException {
List<FixReplacement> sortedReplacements = new ArrayList<>(fixReplacements);
sortedReplacements.sort(ASC_RANGE_FIX_REPLACEMENT_COMPARATOR);
if (!sortedReplacements.isEmpty() && sortedReplacements.get(0).range.startLine <= 0) {
throw new ResourceConflictException(String.format("Cannot calculate fix replacement for range %s", toString(sortedReplacements.get(0).range)));
}
ContentBuilder builder = new ContentBuilder(originalText);
for (FixReplacement fixReplacement : sortedReplacements) {
try {
builder.addReplacement(fixReplacement);
} catch (IndexOutOfBoundsException e) {
throw new ResourceConflictException(String.format("Cannot calculate fix replacement for range %s", toString(fixReplacement.range)), e);
}
}
return builder.build();
}
use of com.google.gerrit.entities.FixReplacement in project gerrit by GerritCodeReview.
the class FixReplacementInterpreter method toCommitModification.
/**
* Transforms the given {@code FixReplacement}s into {@code TreeModification}s.
*
* @param repository the affected Git repository
* @param projectState the affected project
* @param patchSetCommitId the patch set which should be modified
* @param fixReplacements the replacements which should be applied
* @return a list of {@code TreeModification}s representing the given replacements
* @throws ResourceNotFoundException if a file to which one of the replacements refers doesn't
* exist
* @throws ResourceConflictException if the replacements can't be transformed into {@code
* TreeModification}s
*/
public CommitModification toCommitModification(Repository repository, ProjectState projectState, ObjectId patchSetCommitId, List<FixReplacement> fixReplacements) throws BadRequestException, ResourceNotFoundException, IOException, ResourceConflictException {
requireNonNull(fixReplacements, "Fix replacements must not be null");
Map<String, List<FixReplacement>> fixReplacementsPerFilePath = fixReplacements.stream().collect(groupingBy(fixReplacement -> fixReplacement.path));
CommitModification.Builder modificationBuilder = CommitModification.builder();
for (Map.Entry<String, List<FixReplacement>> entry : fixReplacementsPerFilePath.entrySet()) {
if (Objects.equals(entry.getKey(), Patch.COMMIT_MSG)) {
String newCommitMessage = getNewCommitMessage(repository, patchSetCommitId, entry.getValue());
modificationBuilder.newCommitMessage(newCommitMessage);
} else {
TreeModification treeModification = toTreeModification(repository, projectState, patchSetCommitId, entry.getKey(), entry.getValue());
modificationBuilder.addTreeModification(treeModification);
}
}
return modificationBuilder.build();
}
Aggregations