-
Notifications
You must be signed in to change notification settings - Fork 710
Addresses issue #1402 - Possible to search and replace method/scenario names with new name. #1403
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4dc05a9
Addresses issue #1402 - Possible to search and replace method/scenari…
suratdas 45d18d5
Changed UI based on review comments.
suratdas 949da1d
Review comment suggestion.
suratdas 56d2ffd
Review comment suggestion.
suratdas 37ef1a7
Review comment suggestion.
suratdas c07dbdf
Review comment suggestion.
suratdas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.