Add a special option type LineAndFlag, use it for FlagLines highlighter
This commit is contained in:
parent
36dc6c23a0
commit
1982144b04
|
@ -19,6 +19,7 @@
|
|||
#include "client_manager.hh"
|
||||
#include "parameters_parser.hh"
|
||||
#include "utf8_iterator.hh"
|
||||
#include "option_types.hh"
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
|
@ -495,6 +496,8 @@ void declare_option(const CommandParameters& params, Context& context)
|
|||
opt = &GlobalOptions::instance().declare_option<std::vector<int>>(params[1], {});
|
||||
else if (params[0] == "str-list")
|
||||
opt = &GlobalOptions::instance().declare_option<std::vector<String>>(params[1], {});
|
||||
else if (params[0] == "line-flag-list")
|
||||
opt = &GlobalOptions::instance().declare_option<std::vector<LineAndFlag>>(params[1], {});
|
||||
else
|
||||
throw runtime_error("unknown type " + params[0]);
|
||||
|
||||
|
|
|
@ -8,6 +8,8 @@
|
|||
#include "utf8.hh"
|
||||
#include "utf8_iterator.hh"
|
||||
|
||||
#include "option_types.hh"
|
||||
|
||||
#include <sstream>
|
||||
#include <locale>
|
||||
|
||||
|
@ -316,30 +318,35 @@ void expand_unprintable(DisplayBuffer& display_buffer)
|
|||
class FlagLines
|
||||
{
|
||||
public:
|
||||
FlagLines(String flag, String lines_opt_name, const OptionManager& options)
|
||||
: m_flag(std::move(flag)), m_lines_opt_name(std::move(lines_opt_name)),
|
||||
FlagLines(Color bg, String option_name, const OptionManager& options)
|
||||
: m_bg(bg), m_option_name(std::move(option_name)),
|
||||
m_options(options)
|
||||
{
|
||||
// trigger an exception if option is not of right type.
|
||||
m_options[m_lines_opt_name].get<std::vector<int>>();
|
||||
m_options[m_option_name].get<std::vector<LineAndFlag>>();
|
||||
}
|
||||
|
||||
void operator()(DisplayBuffer& display_buffer)
|
||||
{
|
||||
auto& lines = m_options[m_lines_opt_name].get<std::vector<int>>();
|
||||
const String empty{' ', m_flag.char_length()};
|
||||
auto& lines = m_options[m_option_name].get<std::vector<LineAndFlag>>();
|
||||
|
||||
CharCount width = 0;
|
||||
for (auto& l : lines)
|
||||
width = std::max(width, l.flag.char_length());
|
||||
const String empty{' ', width};
|
||||
for (auto& line : display_buffer.lines())
|
||||
{
|
||||
const bool flagged = contains(lines, (int)line.buffer_line() + 1);
|
||||
DisplayAtom atom{AtomContent(flagged ? m_flag : empty)};
|
||||
atom.colors = { Color::Blue, Color::Cyan };
|
||||
int line_num = (int)line.buffer_line() + 1;
|
||||
auto it = find_if(lines, [&](const LineAndFlag& l) { return l.line == line_num; });
|
||||
DisplayAtom atom{AtomContent(it != lines.end() ? it->flag : empty)};
|
||||
atom.colors = { it != lines.end() ? it->color : Color::Default , m_bg };
|
||||
line.insert(line.begin(), std::move(atom));
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
String m_flag;
|
||||
String m_lines_opt_name;
|
||||
Color m_bg;
|
||||
String m_option_name;
|
||||
const OptionManager& m_options;
|
||||
};
|
||||
|
||||
|
@ -348,7 +355,7 @@ HighlighterAndId flag_lines_factory(const HighlighterParameters& params, const W
|
|||
if (params.size() != 2)
|
||||
throw runtime_error("wrong parameter count");
|
||||
|
||||
return {"hlflags_" + params[1], FlagLines{params[0], params[1], window.options()}};
|
||||
return {"hlflags_" + params[1], FlagLines{str_to_color(params[0]), params[1], window.options()}};
|
||||
}
|
||||
|
||||
template<void (*highlighter_func)(DisplayBuffer&)>
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
#include "option_manager.hh"
|
||||
#include "assert.hh"
|
||||
|
||||
#include "option_types.hh"
|
||||
|
||||
#include <sstream>
|
||||
|
||||
namespace Kakoune
|
||||
|
@ -144,6 +146,9 @@ template void Option::set<std::vector<String>>(const std::vector<String>&);
|
|||
template const Regex& Option::get<Regex>() const;
|
||||
template void Option::set<Regex>(const Regex&);
|
||||
|
||||
template const std::vector<LineAndFlag>& Option::get<std::vector<LineAndFlag>>() const;
|
||||
template void Option::set<std::vector<LineAndFlag>>(const std::vector<LineAndFlag>&);
|
||||
|
||||
OptionManager::OptionManager(OptionManager& parent)
|
||||
: m_parent(&parent)
|
||||
{
|
||||
|
@ -273,5 +278,6 @@ Option& GlobalOptions::declare_option(const String& name, const T& value)
|
|||
}
|
||||
|
||||
template Option& GlobalOptions::declare_option<>(const String&, const std::vector<int>&);
|
||||
template Option& GlobalOptions::declare_option<>(const String&, const std::vector<LineAndFlag>&);
|
||||
|
||||
}
|
||||
|
|
26
src/option_types.cc
Normal file
26
src/option_types.cc
Normal file
|
@ -0,0 +1,26 @@
|
|||
#include "option_types.hh"
|
||||
|
||||
#include "exception.hh"
|
||||
|
||||
namespace Kakoune
|
||||
{
|
||||
|
||||
String option_to_string(const LineAndFlag& opt)
|
||||
{
|
||||
return int_to_str((int)opt.line) + ":" + color_to_str(opt.color) + ":" + opt.flag;
|
||||
}
|
||||
|
||||
void option_from_string(const String& str, LineAndFlag& opt)
|
||||
{
|
||||
static Regex re{R"((\d+):(\w+):(.+))"};
|
||||
|
||||
boost::match_results<String::iterator> res;
|
||||
if (not boost::regex_match(str.begin(), str.end(), res, re))
|
||||
throw runtime_error("wrong syntax, expected <line>:<color>:<flag>");
|
||||
|
||||
opt.line = str_to_int(String{res[1].first, res[1].second});
|
||||
opt.color = str_to_color(String{res[2].first, res[2].second});
|
||||
opt.flag = String{res[3].first, res[3].second};
|
||||
}
|
||||
|
||||
}
|
29
src/option_types.hh
Normal file
29
src/option_types.hh
Normal file
|
@ -0,0 +1,29 @@
|
|||
#ifndef option_types_hh_INCLUDED
|
||||
#define option_types_hh_INCLUDED
|
||||
|
||||
#include "units.hh"
|
||||
#include "string.hh"
|
||||
#include "color.hh"
|
||||
|
||||
namespace Kakoune
|
||||
{
|
||||
|
||||
struct LineAndFlag
|
||||
{
|
||||
LineCount line;
|
||||
Color color;
|
||||
String flag;
|
||||
|
||||
bool operator==(const LineAndFlag& other) const
|
||||
{ return line == other.line and color == other.color and flag == other.flag; }
|
||||
|
||||
bool operator!=(const LineAndFlag& other) const
|
||||
{ return not (*this == other); }
|
||||
};
|
||||
|
||||
String option_to_string(const LineAndFlag& opt);
|
||||
void option_from_string(const String& str, LineAndFlag& opt);
|
||||
|
||||
}
|
||||
|
||||
#endif // option_types_hh_INCLUDED
|
Loading…
Reference in New Issue
Block a user