-
Notifications
You must be signed in to change notification settings - Fork 713
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Addresses issue #1402 - Possible to search and replace method/scenari…
…o names with new name. (#1403) * Addresses issue #1402 - Possible to search and replace method/scenario names with new name. * Changed UI based on review comments. * Review comment suggestion. Co-authored-by: Fried Hoeben <[email protected]> * Review comment suggestion. Co-authored-by: Fried Hoeben <[email protected]> * Review comment suggestion. Co-authored-by: Fried Hoeben <[email protected]> * Review comment suggestion. Co-authored-by: Fried Hoeben <[email protected]> Co-authored-by: Fried Hoeben <[email protected]>
- Loading branch information
Showing
9 changed files
with
796 additions
and
12 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package fitnesse.util; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
|
||
public class WikiPageLineProcessingUtil { | ||
|
||
public static boolean isColumnSpecialWikiKeyWord(String stringPassed) { | ||
stringPassed = stringPassed.trim(); | ||
return stringPassed.isEmpty() || stringPassed.startsWith("$") || stringPassed.equals("ensure") || stringPassed.equals("reject") || stringPassed.equals("check") || stringPassed.equals("check not") || stringPassed.equals("show") || stringPassed.equals("note"); | ||
} | ||
|
||
public static boolean doesLineNeedExtraLastColumn(String stringPassed) { | ||
String firstColumn = Arrays.asList(stringPassed.split("\\|")).get(1).trim(); | ||
return firstColumn.equals("check") || firstColumn.equals("check not"); | ||
} | ||
|
||
public static String getLastColumn(String stringPassed) { | ||
if (isValidLine(stringPassed)) { | ||
List<String> allColumns = Arrays.asList(stringPassed.split("\\|")); | ||
return allColumns.get(allColumns.size() - 1); | ||
} | ||
return ""; | ||
} | ||
|
||
private static boolean isValidLine(String textPassed) { | ||
return (textPassed.startsWith("|") && textPassed.trim().endsWith("|")); | ||
} | ||
|
||
public static String getMethodNameFromLine(String textPassed) { | ||
String methodNameToReturn = ""; | ||
if (isValidLine(textPassed)) { | ||
List<String> methodSplit = new ArrayList<>(); | ||
methodSplit.addAll(Arrays.asList(textPassed.split("\\|"))); | ||
methodSplit.remove(0);//First one is always empty. We can discard it. | ||
String firstColumn = methodSplit.get(0); | ||
int counter = !isColumnSpecialWikiKeyWord(firstColumn) ? 0 : 1; | ||
boolean lineHasExtraColumn = doesLineNeedExtraLastColumn(textPassed); | ||
for (int i = counter; i < methodSplit.size(); i += 2) { | ||
//Ensure last column is not included in the method name. | ||
if (lineHasExtraColumn && methodSplit.size() == (i + 1)) { | ||
} else { | ||
List<String> currentCellWords = Arrays.asList(methodSplit.get(i).trim().split(" ")); | ||
for (String currentCellWord : currentCellWords) { | ||
if (!currentCellWord.trim().isEmpty()) { | ||
methodNameToReturn += org.apache.commons.lang3.StringUtils.capitalize(currentCellWord.trim()); | ||
} | ||
} | ||
} | ||
} | ||
methodNameToReturn = StringUtils.uncapitalize(methodNameToReturn); | ||
} | ||
return methodNameToReturn; | ||
} | ||
|
||
public static LinkedHashMap<Integer, String> getRowColumnsExcludingKeywordInFirstColumnIfPresent(String currentLine) { | ||
LinkedHashMap<Integer, String> allColumnsOfCurrentLine = new LinkedHashMap<>(); | ||
String remainingText = currentLine; | ||
int currentIndexOfPipe = 1; | ||
boolean isFirstColumnSpecialKey = true; | ||
while (remainingText.contains("|")) { | ||
int nextIndexOfPipe = currentLine.indexOf("|", currentIndexOfPipe); | ||
String currentColumnText = currentLine.substring(currentIndexOfPipe, nextIndexOfPipe); | ||
if (isFirstColumnSpecialKey && !isColumnSpecialWikiKeyWord(currentColumnText)) { | ||
isFirstColumnSpecialKey = false; | ||
allColumnsOfCurrentLine.put(currentIndexOfPipe, currentColumnText); | ||
} else if (!isFirstColumnSpecialKey) { | ||
allColumnsOfCurrentLine.put(currentIndexOfPipe, currentColumnText); | ||
} | ||
currentIndexOfPipe = nextIndexOfPipe + 1; | ||
remainingText = currentLine.substring(nextIndexOfPipe + 1); | ||
} | ||
if (doesLineNeedExtraLastColumn(currentLine)) | ||
allColumnsOfCurrentLine.remove(allColumnsOfCurrentLine.size() - 1); | ||
return allColumnsOfCurrentLine; | ||
} | ||
|
||
} |
80 changes: 80 additions & 0 deletions
80
src/fitnesse/wiki/refactoring/MethodReplacingSearchObserver.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,80 @@ | ||
package fitnesse.wiki.refactoring; | ||
|
||
import fitnesse.components.TraversalListener; | ||
import fitnesse.wiki.PageData; | ||
import fitnesse.wiki.WikiPage; | ||
|
||
import java.util.Iterator; | ||
import java.util.LinkedHashMap; | ||
|
||
import static fitnesse.util.WikiPageLineProcessingUtil.*; | ||
|
||
public class MethodReplacingSearchObserver implements TraversalListener<WikiPage> { | ||
|
||
private String searchMethodString; | ||
private String replacingMethodString; | ||
|
||
public MethodReplacingSearchObserver(String searchString, String replacement) { | ||
this.searchMethodString = searchString; | ||
this.replacingMethodString = replacement; | ||
} | ||
|
||
@Override | ||
public void process(WikiPage page) { | ||
PageData pageData = page.getData(); | ||
|
||
String content = pageData.getContent(); | ||
String[] lines = content.split(System.lineSeparator()); | ||
String newPageContent = ""; | ||
boolean isModified = false; | ||
String targetMethod = getMethodNameFromLine(searchMethodString); | ||
for (String eachLineOfFile : lines) { | ||
if (!eachLineOfFile.startsWith("|") || !getMethodNameFromLine(eachLineOfFile).equals(targetMethod)) { | ||
newPageContent += eachLineOfFile + System.lineSeparator(); | ||
} else { | ||
isModified = true; | ||
String modifiedLine = ""; | ||
LinkedHashMap<Integer, String> toBeReplacedText = getRowColumnsExcludingKeywordInFirstColumnIfPresent(eachLineOfFile); | ||
Iterator<Integer> toBeReplacedKeys = toBeReplacedText.keySet().iterator(); | ||
LinkedHashMap<Integer, String> toReplaceText = getRowColumnsExcludingKeywordInFirstColumnIfPresent(replacingMethodString); | ||
Iterator<Integer> toReplaceKeys = toReplaceText.keySet().iterator(); | ||
for (int i = 0; toBeReplacedKeys.hasNext() || toReplaceKeys.hasNext(); i++) { | ||
int toBeReplacedIndex = toBeReplacedKeys.hasNext() ? toBeReplacedKeys.next() : -1; | ||
int toReplaceIndex = toReplaceKeys.hasNext() ? toReplaceKeys.next() : -1; | ||
modifiedLine = modifiedLine.isEmpty() ? eachLineOfFile.substring(0, toBeReplacedIndex - 1) : modifiedLine; | ||
if (toBeReplacedIndex > 0 && toReplaceIndex > 0) { | ||
if (i % 2 == 1) { | ||
modifiedLine += "|" + eachLineOfFile.substring(toBeReplacedIndex, toBeReplacedIndex + toBeReplacedText.get(toBeReplacedIndex).length()); | ||
} else { | ||
modifiedLine += "|" + toReplaceText.get(toReplaceIndex); | ||
} | ||
} | ||
//Below case is when there are more columns in replacing text than in original text. | ||
else if (toBeReplacedIndex == -1) { | ||
//All remaining columns of replacing text should be appended. | ||
modifiedLine += "|" + toReplaceText.get(toReplaceIndex); | ||
while (toReplaceKeys.hasNext()) { | ||
modifiedLine += "|" + toReplaceText.get(toReplaceKeys.next()); | ||
} | ||
break; | ||
} | ||
//below case is when there are more columns in original text which need to be removed. | ||
else if (toReplaceIndex == -1) { | ||
modifiedLine += "|"; | ||
break; | ||
} | ||
} | ||
if (doesLineNeedExtraLastColumn(eachLineOfFile)) { | ||
modifiedLine += getLastColumn(eachLineOfFile); | ||
} | ||
modifiedLine = (!modifiedLine.isEmpty() && !modifiedLine.endsWith("|")) ? modifiedLine + "|" : modifiedLine; | ||
newPageContent += modifiedLine + System.lineSeparator(); | ||
} | ||
} | ||
if (isModified) { | ||
pageData.setContent(newPageContent); | ||
} | ||
page.commit(pageData); | ||
} | ||
|
||
} |
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,34 @@ | ||
package fitnesse.wiki.search; | ||
|
||
import fitnesse.components.TraversalListener; | ||
import fitnesse.wiki.WikiPage; | ||
|
||
import static fitnesse.util.WikiPageLineProcessingUtil.getMethodNameFromLine; | ||
|
||
public class MethodWikiPageFinder extends WikiPageFinder { | ||
|
||
private String methodToFind; | ||
|
||
public MethodWikiPageFinder(String methodToFind, TraversalListener<? super WikiPage> observer) { | ||
super(observer); | ||
this.methodToFind = methodToFind; | ||
} | ||
|
||
@Override | ||
protected boolean pageMatches(WikiPage page) { | ||
String pageContent = page.getData().getContent(); | ||
String targetMethod = getMethodNameFromLine(this.methodToFind); | ||
String[] contentLines = pageContent.split(System.lineSeparator()); | ||
|
||
//Both the inputs should follow the correct format | ||
if (!methodToFind.startsWith("|") || !methodToFind.endsWith("|")) | ||
return false; | ||
|
||
for (String eachLine : contentLines) { | ||
if (getMethodNameFromLine(eachLine).equals(targetMethod)) | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
} |
Oops, something went wrong.