Transform boost/std regex_error to Kakoune::regex_error at Regex construction
Fixes #318
This commit is contained in:
parent
7d9ec52bf2
commit
17e3be48a5
|
@ -253,8 +253,6 @@ public:
|
||||||
if (params.size() < 2)
|
if (params.size() < 2)
|
||||||
throw runtime_error("wrong parameter count");
|
throw runtime_error("wrong parameter count");
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
FacesSpec faces;
|
FacesSpec faces;
|
||||||
for (auto it = params.begin() + 1; it != params.end(); ++it)
|
for (auto it = params.begin() + 1; it != params.end(); ++it)
|
||||||
{
|
{
|
||||||
|
@ -274,11 +272,6 @@ public:
|
||||||
return {id, make_unique<RegexHighlighter>(std::move(ex),
|
return {id, make_unique<RegexHighlighter>(std::move(ex),
|
||||||
std::move(faces))};
|
std::move(faces))};
|
||||||
}
|
}
|
||||||
catch (RegexError& err)
|
|
||||||
{
|
|
||||||
throw runtime_error(StringView{"regex error: "} + err.what());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// stores the range for each highlighted capture of each match
|
// stores the range for each highlighted capture of each match
|
||||||
|
@ -465,7 +458,7 @@ HighlighterAndId create_search_highlighter(HighlighterParameters params)
|
||||||
{
|
{
|
||||||
return s.empty() ? Regex{} : Regex{s.begin(), s.end()};
|
return s.empty() ? Regex{} : Regex{s.begin(), s.end()};
|
||||||
}
|
}
|
||||||
catch (RegexError& err)
|
catch (regex_error& err)
|
||||||
{
|
{
|
||||||
return Regex{};
|
return Regex{};
|
||||||
}
|
}
|
||||||
|
@ -1169,8 +1162,6 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
static HighlighterAndId create(HighlighterParameters params)
|
static HighlighterAndId create(HighlighterParameters params)
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
{
|
||||||
static const ParameterDesc param_desc{
|
static const ParameterDesc param_desc{
|
||||||
{ { "default", { true, "" } } },
|
{ { "default", { true, "" } } },
|
||||||
|
@ -1199,11 +1190,6 @@ public:
|
||||||
auto default_group = parser.get_switch("default").value_or(StringView{}).str();
|
auto default_group = parser.get_switch("default").value_or(StringView{}).str();
|
||||||
return {parser[0], make_unique<RegionsHighlighter>(std::move(regions), default_group)};
|
return {parser[0], make_unique<RegionsHighlighter>(std::move(regions), default_group)};
|
||||||
}
|
}
|
||||||
catch (RegexError& err)
|
|
||||||
{
|
|
||||||
throw runtime_error(StringView{"regex error: "} + err.what());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const NamedRegionDescList m_regions;
|
const NamedRegionDescList m_regions;
|
||||||
|
|
|
@ -596,27 +596,13 @@ void regex_prompt(Context& context, const String prompt, T func)
|
||||||
: Regex{str.begin(), str.end()};
|
: Regex{str.begin(), str.end()};
|
||||||
func(std::move(regex), event, context);
|
func(std::move(regex), event, context);
|
||||||
}
|
}
|
||||||
catch (RegexError& err)
|
catch (regex_error& err)
|
||||||
{
|
{
|
||||||
if (event == PromptEvent::Validate)
|
if (event == PromptEvent::Validate)
|
||||||
throw runtime_error(format("regex error: {}", err.what()));
|
throw;
|
||||||
else
|
else
|
||||||
context.input_handler().set_prompt_face(get_face("Error"));
|
context.input_handler().set_prompt_face(get_face("Error"));
|
||||||
}
|
}
|
||||||
catch (std::runtime_error& err)
|
|
||||||
{
|
|
||||||
if (event == PromptEvent::Validate)
|
|
||||||
throw runtime_error(format("regex error: {}", err.what()));
|
|
||||||
else
|
|
||||||
{
|
|
||||||
context.input_handler().set_prompt_face(get_face("Error"));
|
|
||||||
if (context.has_ui())
|
|
||||||
{
|
|
||||||
Face face = get_face("Information");
|
|
||||||
context.ui().info_show("regex error", err.what(), CharCoord{}, face, InfoStyle::Prompt);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (runtime_error&)
|
catch (runtime_error&)
|
||||||
{
|
{
|
||||||
context.selections_write_only() = selections;
|
context.selections_write_only() = selections;
|
||||||
|
@ -647,19 +633,12 @@ void search_next(Context& context, NormalParams params)
|
||||||
{
|
{
|
||||||
StringView str = context.main_sel_register_value("/");
|
StringView str = context.main_sel_register_value("/");
|
||||||
if (not str.empty())
|
if (not str.empty())
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
{
|
||||||
Regex ex{str.begin(), str.end()};
|
Regex ex{str.begin(), str.end()};
|
||||||
do {
|
do {
|
||||||
select_next_match<direction, mode>(context.buffer(), context.selections(), ex);
|
select_next_match<direction, mode>(context.buffer(), context.selections(), ex);
|
||||||
} while (--params.count > 0);
|
} while (--params.count > 0);
|
||||||
}
|
}
|
||||||
catch (RegexError& err)
|
|
||||||
{
|
|
||||||
throw runtime_error(format("regex error: ", err.what()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
throw runtime_error("no search pattern");
|
throw runtime_error("no search pattern");
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,15 +12,8 @@ String option_to_string(const Regex& re)
|
||||||
}
|
}
|
||||||
|
|
||||||
void option_from_string(StringView str, Regex& re)
|
void option_from_string(StringView str, Regex& re)
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
{
|
||||||
re = Regex{str.begin(), str.end()};
|
re = Regex{str.begin(), str.end()};
|
||||||
}
|
}
|
||||||
catch (RegexError& err)
|
|
||||||
{
|
|
||||||
throw runtime_error(format("unable to create regex: {}", err.what()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
22
src/regex.hh
22
src/regex.hh
|
@ -2,6 +2,7 @@
|
||||||
#define regex_hh_INCLUDED
|
#define regex_hh_INCLUDED
|
||||||
|
|
||||||
#include "string.hh"
|
#include "string.hh"
|
||||||
|
#include "exception.hh"
|
||||||
|
|
||||||
#ifdef KAK_USE_STDREGEX
|
#ifdef KAK_USE_STDREGEX
|
||||||
#include <regex>
|
#include <regex>
|
||||||
|
@ -12,18 +13,27 @@
|
||||||
namespace Kakoune
|
namespace Kakoune
|
||||||
{
|
{
|
||||||
|
|
||||||
|
struct regex_error : runtime_error
|
||||||
|
{
|
||||||
|
regex_error(StringView desc)
|
||||||
|
: runtime_error{format("regex error: '{}'", desc)}
|
||||||
|
{}
|
||||||
|
};
|
||||||
|
|
||||||
#ifdef KAK_USE_STDREGEX
|
#ifdef KAK_USE_STDREGEX
|
||||||
// Regex that keeps track of its string representation
|
// Regex that keeps track of its string representation
|
||||||
struct Regex : std::regex
|
struct Regex : std::regex
|
||||||
{
|
{
|
||||||
Regex() = default;
|
Regex() = default;
|
||||||
|
|
||||||
explicit Regex(StringView re, flag_type flags = ECMAScript)
|
explicit Regex(StringView re, flag_type flags = ECMAScript) try
|
||||||
: std::regex(re.begin(), re.end(), flags), m_str(re) {}
|
: std::regex(re.begin(), re.end(), flags), m_str(re) {}
|
||||||
|
catch (std::runtime_error& err) { throw regex_error(err.what()); }
|
||||||
|
|
||||||
template<typename Iterator>
|
template<typename Iterator>
|
||||||
Regex(Iterator begin, Iterator end, flag_type flags = ECMAScript)
|
Regex(Iterator begin, Iterator end, flag_type flags = ECMAScript) try
|
||||||
: std::regex(begin, end, flags), m_str(begin, end) {}
|
: std::regex(begin, end, flags), m_str(begin, end) {}
|
||||||
|
catch (std::runtime_error& err) { throw regex_error(err.what()); }
|
||||||
|
|
||||||
bool empty() const { return m_str.empty(); }
|
bool empty() const { return m_str.empty(); }
|
||||||
bool operator==(const Regex& other) const { return m_str == other.m_str; }
|
bool operator==(const Regex& other) const { return m_str == other.m_str; }
|
||||||
|
@ -40,12 +50,14 @@ struct Regex : boost::regex
|
||||||
{
|
{
|
||||||
Regex() = default;
|
Regex() = default;
|
||||||
|
|
||||||
explicit Regex(StringView re, flag_type flags = ECMAScript)
|
explicit Regex(StringView re, flag_type flags = ECMAScript) try
|
||||||
: boost::regex(re.begin(), re.end(), flags) {}
|
: boost::regex(re.begin(), re.end(), flags) {}
|
||||||
|
catch (std::runtime_error& err) { throw regex_error(err.what()); }
|
||||||
|
|
||||||
template<typename Iterator>
|
template<typename Iterator>
|
||||||
Regex(Iterator begin, Iterator end, flag_type flags = ECMAScript)
|
Regex(Iterator begin, Iterator end, flag_type flags = ECMAScript) try
|
||||||
: boost::regex(begin, end, flags) {}
|
: boost::regex(begin, end, flags) {}
|
||||||
|
catch (std::runtime_error& err) { throw regex_error(err.what()); }
|
||||||
|
|
||||||
String str() const { auto s = boost::regex::str(); return {s.begin(), s.end()}; }
|
String str() const { auto s = boost::regex::str(); return {s.begin(), s.end()}; }
|
||||||
};
|
};
|
||||||
|
@ -58,8 +70,6 @@ using RegexIterator = regex_ns::regex_iterator<Iterator>;
|
||||||
template<typename Iterator>
|
template<typename Iterator>
|
||||||
using MatchResults = regex_ns::match_results<Iterator>;
|
using MatchResults = regex_ns::match_results<Iterator>;
|
||||||
|
|
||||||
using RegexError = regex_ns::regex_error;
|
|
||||||
|
|
||||||
String option_to_string(const Regex& re);
|
String option_to_string(const Regex& re);
|
||||||
void option_from_string(StringView str, Regex& re);
|
void option_from_string(StringView str, Regex& re);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user