Extract RegexSelector methods as plain selectors
This commit is contained in:
parent
2677fa7961
commit
b67c36358d
|
@ -1,7 +1,6 @@
|
|||
#include "window.hh"
|
||||
#include "buffer.hh"
|
||||
#include "file.hh"
|
||||
#include "regex_selector.hh"
|
||||
#include "command_manager.hh"
|
||||
#include "buffer_manager.hh"
|
||||
#include "register_manager.hh"
|
||||
|
@ -474,7 +473,7 @@ void do_search(Window& window)
|
|||
else
|
||||
RegisterManager::instance()['/'] = ex;
|
||||
|
||||
window.select(RegexSelector(ex));
|
||||
window.select(std::bind(select_next_match, _1, ex));
|
||||
}
|
||||
catch (prompt_aborted&) {}
|
||||
}
|
||||
|
@ -483,7 +482,7 @@ void do_search_next(Window& window)
|
|||
{
|
||||
std::string& ex = RegisterManager::instance()['/'];
|
||||
if (not ex.empty())
|
||||
window.select(RegexSelector(ex));
|
||||
window.select(std::bind(select_next_match, _1, ex));
|
||||
else
|
||||
print_status("no search pattern");
|
||||
}
|
||||
|
@ -522,8 +521,8 @@ void do_select_regex(Window& window, int count)
|
|||
{
|
||||
try
|
||||
{
|
||||
RegexSelector selector(prompt("select: "));
|
||||
window.multi_select(selector);
|
||||
std::string ex = prompt("select: ");
|
||||
window.multi_select(std::bind(select_all_matches, _1, ex));
|
||||
}
|
||||
catch (prompt_aborted&) {}
|
||||
}
|
||||
|
|
|
@ -1,71 +0,0 @@
|
|||
#include "regex_selector.hh"
|
||||
#include "exception.hh"
|
||||
|
||||
namespace Kakoune
|
||||
{
|
||||
|
||||
RegexSelector::RegexSelector(const std::string& exp)
|
||||
: m_regex(exp) {}
|
||||
|
||||
Selection RegexSelector::operator()(const BufferIterator& cursor) const
|
||||
{
|
||||
BufferIterator begin = cursor;
|
||||
BufferIterator end = cursor;
|
||||
Selection::CaptureList captures;
|
||||
|
||||
try
|
||||
{
|
||||
boost::match_results<BufferIterator> matches;
|
||||
|
||||
if (boost::regex_search(cursor, cursor.buffer().end(), matches,
|
||||
m_regex))
|
||||
{
|
||||
begin = matches[0].first;
|
||||
end = matches[0].second;
|
||||
std::copy(matches.begin(), matches.end(),
|
||||
std::back_inserter(captures));
|
||||
}
|
||||
else if (boost::regex_search(cursor.buffer().begin(), cursor, matches,
|
||||
m_regex))
|
||||
{
|
||||
begin = matches[0].first;
|
||||
end = matches[0].second;
|
||||
std::copy(matches.begin(), matches.end(),
|
||||
std::back_inserter(captures));
|
||||
}
|
||||
}
|
||||
catch (boost::regex_error& err)
|
||||
{
|
||||
throw runtime_error("regex error");
|
||||
}
|
||||
|
||||
|
||||
if (begin == end)
|
||||
++end;
|
||||
|
||||
return Selection(begin, end - 1, std::move(captures));
|
||||
}
|
||||
|
||||
SelectionList RegexSelector::operator()(const Selection& selection) const
|
||||
{
|
||||
boost::regex_iterator<BufferIterator> re_it(selection.begin(),
|
||||
selection.end(),
|
||||
m_regex);
|
||||
boost::regex_iterator<BufferIterator> re_end;
|
||||
|
||||
SelectionList result;
|
||||
for (; re_it != re_end; ++re_it)
|
||||
{
|
||||
BufferIterator begin = (*re_it)[0].first;
|
||||
BufferIterator end = (*re_it)[0].second;
|
||||
|
||||
if (begin == end)
|
||||
++end;
|
||||
|
||||
Selection::CaptureList captures(re_it->begin(), re_it->end());
|
||||
result.push_back(Selection(begin, end-1, std::move(captures)));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,26 +0,0 @@
|
|||
#ifndef regex_selector_hh_INCLUDED
|
||||
#define regex_selector_hh_INCLUDED
|
||||
|
||||
#include "buffer.hh"
|
||||
#include "window.hh"
|
||||
|
||||
#include <boost/regex.hpp>
|
||||
|
||||
namespace Kakoune
|
||||
{
|
||||
|
||||
class RegexSelector
|
||||
{
|
||||
public:
|
||||
RegexSelector(const std::string& exp);
|
||||
|
||||
Selection operator()(const BufferIterator& cursor) const;
|
||||
SelectionList operator()(const Selection& selection) const;
|
||||
|
||||
private:
|
||||
boost::regex m_regex;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // regex_selector_hh_INCLUDED
|
|
@ -1,5 +1,6 @@
|
|||
#include "selectors.hh"
|
||||
|
||||
#include <boost/regex.hpp>
|
||||
#include <algorithm>
|
||||
|
||||
namespace Kakoune
|
||||
|
@ -297,4 +298,70 @@ SelectionList select_whole_lines(const Selection& selection)
|
|||
return result;
|
||||
}
|
||||
|
||||
Selection select_next_match(const BufferIterator& cursor,
|
||||
const std::string& regex)
|
||||
{
|
||||
boost::regex ex(regex);
|
||||
|
||||
BufferIterator begin = cursor;
|
||||
BufferIterator end = cursor;
|
||||
Selection::CaptureList captures;
|
||||
|
||||
try
|
||||
{
|
||||
boost::match_results<BufferIterator> matches;
|
||||
|
||||
if (boost::regex_search(cursor, cursor.buffer().end(), matches,
|
||||
ex))
|
||||
{
|
||||
begin = matches[0].first;
|
||||
end = matches[0].second;
|
||||
std::copy(matches.begin(), matches.end(),
|
||||
std::back_inserter(captures));
|
||||
}
|
||||
else if (boost::regex_search(cursor.buffer().begin(), cursor, matches,
|
||||
ex))
|
||||
{
|
||||
begin = matches[0].first;
|
||||
end = matches[0].second;
|
||||
std::copy(matches.begin(), matches.end(),
|
||||
std::back_inserter(captures));
|
||||
}
|
||||
}
|
||||
catch (boost::regex_error& err)
|
||||
{
|
||||
throw runtime_error("regex error");
|
||||
}
|
||||
|
||||
|
||||
if (begin == end)
|
||||
++end;
|
||||
|
||||
return Selection(begin, end - 1, std::move(captures));
|
||||
}
|
||||
|
||||
SelectionList select_all_matches(const Selection& selection,
|
||||
const std::string& regex)
|
||||
{
|
||||
boost::regex ex(regex);
|
||||
boost::regex_iterator<BufferIterator> re_it(selection.begin(),
|
||||
selection.end(),
|
||||
ex);
|
||||
boost::regex_iterator<BufferIterator> re_end;
|
||||
|
||||
SelectionList result;
|
||||
for (; re_it != re_end; ++re_it)
|
||||
{
|
||||
BufferIterator begin = (*re_it)[0].first;
|
||||
BufferIterator end = (*re_it)[0].second;
|
||||
|
||||
if (begin == end)
|
||||
++end;
|
||||
|
||||
Selection::CaptureList captures(re_it->begin(), re_it->end());
|
||||
result.push_back(Selection(begin, end-1, std::move(captures)));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -23,6 +23,12 @@ Selection select_to_eol_reverse(const BufferIterator& cursor);
|
|||
|
||||
SelectionList select_whole_lines(const Selection& selection);
|
||||
|
||||
Selection select_next_match(const BufferIterator& cursor,
|
||||
const std::string& regex);
|
||||
|
||||
SelectionList select_all_matches(const Selection& selection,
|
||||
const std::string& regex);
|
||||
|
||||
}
|
||||
|
||||
#endif // selectors_hh_INCLUDED
|
||||
|
|
Loading…
Reference in New Issue
Block a user