Introduce Replace [and Find] in find protocol

Previously there was a single replace action and the options would indicate if it was a “replace all”, this mimics the “find” (which can be a “find all”) but since single match replacement is somewhat different than a “replace all”, as the former may need to know the captures from a previous find action, it makes sense to factor out these things as different actions.
This commit is contained in:
Allan Odgaard
2013-03-07 14:12:08 +01:00
parent 599d903eb4
commit 02f32977f2
6 changed files with 33 additions and 25 deletions

View File

@@ -238,9 +238,10 @@ NSString* const FFFindWasTriggeredByEnter = @"FFFindWasTriggeredByEnter";
{
case FindActionFindNext:
case FindActionFindPrevious:
case FindActionFindAll: _findOperation = onlySelection ? kFindOperationFindInSelection : kFindOperationFind; break;
case FindActionCountMatches: _findOperation = onlySelection ? kFindOperationCountInSelection : kFindOperationCount; break;
case FindActionReplaceAll: _findOperation = onlySelection ? kFindOperationReplaceInSelection : kFindOperationReplace; break;
case FindActionFindAll: _findOperation = onlySelection ? kFindOperationFindInSelection : kFindOperationFind; break;
case FindActionCountMatches: _findOperation = onlySelection ? kFindOperationCountInSelection : kFindOperationCount; break;
case FindActionReplaceAll: _findOperation = onlySelection ? kFindOperationReplaceAllInSelection : kFindOperationReplaceAll; break;
case FindActionReplaceAndFind: _findOperation = kFindOperationReplaceAndFind; break;
}
self.closeWindowOnSuccess = action == FindActionFindNext && [[NSApp currentEvent] type] == NSKeyDown && to_s([NSApp currentEvent]) == utf8::to_s(NSCarriageReturnCharacter);

View File

@@ -7,7 +7,9 @@ enum find_operation_t {
kFindOperationFind,
kFindOperationFindInSelection,
kFindOperationReplace,
kFindOperationReplaceInSelection,
kFindOperationReplaceAndFind,
kFindOperationReplaceAll,
kFindOperationReplaceAllInSelection,
};
@protocol OakFindServerProtocol

View File

@@ -1670,14 +1670,18 @@ static void update_menu_key_equivalents (NSMenu* menu, action_to_key_t const& ac
break;
case kFindOperationReplace:
case kFindOperationReplaceInSelection:
case kFindOperationReplaceAndFind:
break;
case kFindOperationReplaceAll:
case kFindOperationReplaceAllInSelection:
{
std::string const findStr = to_s(aFindServer.findString);
std::string const replaceStr = to_s(aFindServer.replaceString);
find::options_t options = aFindServer.findOptions;
[self recordSelector:(options & find::all_matches) ? (aFindServer.findOperation == kFindOperationReplace ? @selector(replaceAll:) : @selector(replaceAllInSelection:)) : @selector(replace:) withArgument:nil];
[self recordSelector:(options & find::all_matches) ? (aFindServer.findOperation == kFindOperationReplaceAll ? @selector(replaceAll:) : @selector(replaceAllInSelection:)) : @selector(replace:) withArgument:nil];
ng::ranges_t const res = editor->replace(findStr, replaceStr, options, aFindServer.findOperation == kFindOperationReplaceInSelection);
ng::ranges_t const res = editor->replace_all(findStr, replaceStr, options, aFindServer.findOperation == kFindOperationReplaceAllInSelection);
[aFindServer didReplace:res.size() occurrencesOf:aFindServer.findString with:aFindServer.replaceString];
}
break;
@@ -1807,10 +1811,11 @@ static void update_menu_key_equivalents (NSMenu* menu, action_to_key_t const& ac
- (IBAction)findAll:(id)sender { [self performFindOperation:[OakTextViewFindServer findServerWithTextView:self operation:kFindOperationFind options:[[OakPasteboard pasteboardWithName:NSFindPboard] current].findOptions | find::all_matches]]; }
- (IBAction)findAllInSelection:(id)sender { [self performFindOperation:[OakTextViewFindServer findServerWithTextView:self operation:kFindOperationFindInSelection options:[[OakPasteboard pasteboardWithName:NSFindPboard] current].findOptions | find::all_matches]]; }
- (IBAction)replace:(id)sender { [self performFindOperation:[OakTextViewFindServer findServerWithTextView:self operation:kFindOperationReplace options:[[OakPasteboard pasteboardWithName:NSFindPboard] current].findOptions]]; }
- (IBAction)replaceAll:(id)sender { [self performFindOperation:[OakTextViewFindServer findServerWithTextView:self operation:kFindOperationReplace options:[[OakPasteboard pasteboardWithName:NSFindPboard] current].findOptions | find::all_matches]]; }
- (IBAction)replaceAllInSelection:(id)sender { [self performFindOperation:[OakTextViewFindServer findServerWithTextView:self operation:kFindOperationReplaceInSelection options:[[OakPasteboard pasteboardWithName:NSFindPboard] current].findOptions | find::all_matches]]; }
- (IBAction)replaceAndFind:(id)sender { /* TODO replaceAndFind: */ }
- (IBAction)replace:(id)sender { [self performFindOperation:[OakTextViewFindServer findServerWithTextView:self operation:kFindOperationReplace options:[[OakPasteboard pasteboardWithName:NSFindPboard] current].findOptions]]; }
- (IBAction)replaceAndFind:(id)sender { [self performFindOperation:[OakTextViewFindServer findServerWithTextView:self operation:kFindOperationReplaceAndFind options:[[OakPasteboard pasteboardWithName:NSFindPboard] current].findOptions]]; }
- (IBAction)replaceAll:(id)sender { [self performFindOperation:[OakTextViewFindServer findServerWithTextView:self operation:kFindOperationReplaceAll options:[[OakPasteboard pasteboardWithName:NSFindPboard] current].findOptions | find::all_matches]]; }
- (IBAction)replaceAllInSelection:(id)sender { [self performFindOperation:[OakTextViewFindServer findServerWithTextView:self operation:kFindOperationReplaceAllInSelection options:[[OakPasteboard pasteboardWithName:NSFindPboard] current].findOptions | find::all_matches]]; }
- (void)findWithOptions:(NSDictionary*)someOptions
{

View File

@@ -178,18 +178,18 @@ namespace ng
std::string where;
bool searchOnlySelection = plist::get_key_path(args, "replaceAllScope", where) && where == "selection";
if(action == "replaceAndFind")
if(action == "replace")
{
perform(kReplace);
}
else if(action == "replaceAndFind")
{
perform(kReplace);
find(searchFor, options, searchOnlySelection);
}
else if(action == "replace")
{
replace(searchFor, replaceWith, options, searchOnlySelection);
}
else if(action == "replaceAll")
{
replace(searchFor, replaceWith, options|find::all_matches, searchOnlySelection);
replace_all(searchFor, replaceWith, options|find::all_matches, searchOnlySelection);
}
else // findNext, findPrevious, and findAll
{

View File

@@ -899,7 +899,6 @@ namespace ng
}
break;
case kReplace:
case kReplaceAll:
case kReplaceAllInSelection:
{
@@ -911,14 +910,18 @@ namespace ng
if(action == kReplaceAll || action == kReplaceAllInSelection)
options = options | find::all_matches;
replace(findEntry->content(), replaceEntry->content(), options, action == kReplaceAllInSelection);
replace_all(findEntry->content(), replaceEntry->content(), options, action == kReplaceAllInSelection);
}
}
break;
case kReplace:
case kReplaceAndFind:
{
perform(kReplace, layout, indentCorrections, scopeAttributes);
if(action == kReplace)
{
/* TODO Implement Replace (after find) action */
}
perform(kFindNext, layout, indentCorrections, scopeAttributes);
}
break;
@@ -1440,7 +1443,7 @@ namespace ng
_selections = res;
}
ranges_t editor_t::replace (std::string const& searchFor, std::string const& replaceWith, find::options_t options, bool searchOnlySelection)
ranges_t editor_t::replace_all (std::string const& searchFor, std::string const& replaceWith, find::options_t options, bool searchOnlySelection)
{
ranges_t res;
if(options & find::all_matches)
@@ -1452,9 +1455,6 @@ namespace ng
res = this->replace(replacements, true);
_selections = helper.get(false);
}
else
{
}
return res;
}

View File

@@ -165,7 +165,7 @@ namespace ng
void insert (std::string const& str, bool selectInsertion = false);
void insert_with_pairing (std::string const& str, bool indentCorrections = false, std::string const& scopeAttributes = NULL_STR);
void move_selection_to (ng::index_t const& index, bool selectInsertion = true);
ranges_t replace (std::string const& searchFor, std::string const& replaceWith, find::options_t options = find::none, bool searchOnlySelection = false);
ranges_t replace_all (std::string const& searchFor, std::string const& replaceWith, find::options_t options = find::none, bool searchOnlySelection = false);
void delete_tab_trigger (std::string const& str);
void macro_dispatch (plist::dictionary_t const& args, std::map<std::string, std::string> const& variables);