Refactor whitespace highlighter into a struct
This commit is contained in:
parent
a32d7069c6
commit
b06ba627b2
|
@ -954,9 +954,39 @@ struct TabulationHighlighter : Highlighter
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
void show_whitespaces(HighlightContext context, DisplayBuffer& display_buffer, BufferRange,
|
struct ShowWhitespacesHighlighter : Highlighter
|
||||||
StringView tab, StringView tabpad,
|
{
|
||||||
StringView spc, StringView lf, StringView nbsp)
|
ShowWhitespacesHighlighter(String tab, String tabpad, String spc, String lf, String nbsp)
|
||||||
|
: Highlighter{HighlightPass::Move}, m_tab{std::move(tab)}, m_tabpad{std::move(tabpad)},
|
||||||
|
m_spc{std::move(spc)}, m_lf{std::move(lf)}, m_nbsp{std::move(nbsp)}
|
||||||
|
{}
|
||||||
|
|
||||||
|
static std::unique_ptr<Highlighter> create(HighlighterParameters params, Highlighter*)
|
||||||
|
{
|
||||||
|
static const ParameterDesc param_desc{
|
||||||
|
{ { "tab", { true, "" } },
|
||||||
|
{ "tabpad", { true, "" } },
|
||||||
|
{ "spc", { true, "" } },
|
||||||
|
{ "lf", { true, "" } },
|
||||||
|
{ "nbsp", { true, "" } } },
|
||||||
|
ParameterDesc::Flags::None, 0, 0
|
||||||
|
};
|
||||||
|
ParametersParser parser(params, param_desc);
|
||||||
|
|
||||||
|
auto get_param = [&](StringView param, StringView fallback) {
|
||||||
|
StringView value = parser.get_switch(param).value_or(fallback);
|
||||||
|
if (value.char_length() != 1)
|
||||||
|
throw runtime_error{format("-{} expects a single character parameter", param)};
|
||||||
|
return value.str();
|
||||||
|
};
|
||||||
|
|
||||||
|
return std::make_unique<ShowWhitespacesHighlighter>(
|
||||||
|
get_param("tab", "→"), get_param("tabpad", " "), get_param("spc", "·"),
|
||||||
|
get_param("lf", "¬"), get_param("nbsp", "⍽"));
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
void do_highlight(HighlightContext context, DisplayBuffer& display_buffer, BufferRange)
|
||||||
{
|
{
|
||||||
const int tabstop = context.context.options()["tabstop"].get<int>();
|
const int tabstop = context.context.options()["tabstop"].get<int>();
|
||||||
auto whitespaceface = context.context.faces()["Whitespace"];
|
auto whitespaceface = context.context.faces()["Whitespace"];
|
||||||
|
@ -987,14 +1017,14 @@ void show_whitespaces(HighlightContext context, DisplayBuffer& display_buffer, B
|
||||||
const ColumnCount column = get_column(buffer, tabstop, coord);
|
const ColumnCount column = get_column(buffer, tabstop, coord);
|
||||||
const ColumnCount count = tabstop - (column % tabstop) -
|
const ColumnCount count = tabstop - (column % tabstop) -
|
||||||
std::max(win_column - column, 0_col);
|
std::max(win_column - column, 0_col);
|
||||||
atom_it->replace(tab + String(tabpad[(CharCount)0], count - tab.column_length()));
|
atom_it->replace(m_tab + String(m_tabpad[(CharCount)0], count - m_tab.column_length()));
|
||||||
}
|
}
|
||||||
else if (cp == ' ')
|
else if (cp == ' ')
|
||||||
atom_it->replace(spc.str());
|
atom_it->replace(m_spc);
|
||||||
else if (cp == '\n')
|
else if (cp == '\n')
|
||||||
atom_it->replace(lf.str());
|
atom_it->replace(m_lf);
|
||||||
else if (cp == 0xA0)
|
else if (cp == 0xA0)
|
||||||
atom_it->replace(nbsp.str());
|
atom_it->replace(m_nbsp);
|
||||||
atom_it->face = merge_faces(atom_it->face, whitespaceface);
|
atom_it->face = merge_faces(atom_it->face, whitespaceface);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -1003,34 +1033,8 @@ void show_whitespaces(HighlightContext context, DisplayBuffer& display_buffer, B
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<Highlighter> show_whitespaces_factory(HighlighterParameters params, Highlighter*)
|
const String m_tab, m_tabpad, m_spc, m_lf, m_nbsp;
|
||||||
{
|
|
||||||
static const ParameterDesc param_desc{
|
|
||||||
{ { "tab", { true, "" } },
|
|
||||||
{ "tabpad", { true, "" } },
|
|
||||||
{ "spc", { true, "" } },
|
|
||||||
{ "lf", { true, "" } },
|
|
||||||
{ "nbsp", { true, "" } } },
|
|
||||||
ParameterDesc::Flags::None, 0, 0
|
|
||||||
};
|
};
|
||||||
ParametersParser parser(params, param_desc);
|
|
||||||
|
|
||||||
auto get_param = [&](StringView param, StringView fallback) {
|
|
||||||
StringView value = parser.get_switch(param).value_or(fallback);
|
|
||||||
if (value.char_length() != 1)
|
|
||||||
throw runtime_error{format("-{} expects a single character parameter", param)};
|
|
||||||
return value.str();
|
|
||||||
};
|
|
||||||
|
|
||||||
using namespace std::placeholders;
|
|
||||||
auto func = std::bind(show_whitespaces, _1, _2, _3,
|
|
||||||
get_param("tab", "→"), get_param("tabpad", " "),
|
|
||||||
get_param("spc", "·"),
|
|
||||||
get_param("lf", "¬"),
|
|
||||||
get_param("nbsp", "⍽"));
|
|
||||||
|
|
||||||
return make_highlighter(std::move(func), HighlightPass::Move);
|
|
||||||
}
|
|
||||||
|
|
||||||
struct LineNumbersHighlighter : Highlighter
|
struct LineNumbersHighlighter : Highlighter
|
||||||
{
|
{
|
||||||
|
@ -2143,7 +2147,7 @@ void register_highlighters()
|
||||||
"Apply the MatchingChar face to the char matching the one under the cursor" } });
|
"Apply the MatchingChar face to the char matching the one under the cursor" } });
|
||||||
registry.insert({
|
registry.insert({
|
||||||
"show-whitespaces",
|
"show-whitespaces",
|
||||||
{ show_whitespaces_factory,
|
{ ShowWhitespacesHighlighter::create,
|
||||||
"Display whitespaces using symbols \n"
|
"Display whitespaces using symbols \n"
|
||||||
"Parameters: -tab <separator> -tabpad <separator> -lf <separator> -spc <separator> -nbsp <separator>\n" } });
|
"Parameters: -tab <separator> -tabpad <separator> -lf <separator> -spc <separator> -nbsp <separator>\n" } });
|
||||||
registry.insert({
|
registry.insert({
|
||||||
|
|
Loading…
Reference in New Issue
Block a user