Refactor show_whitespaces a bit

This commit is contained in:
Maxime Coste 2017-02-09 23:52:38 +00:00
parent 6163134f30
commit 9ba1665e58

View File

@ -682,7 +682,8 @@ void expand_tabulations(const Context& context, HighlightFlags flags, DisplayBuf
}
void show_whitespaces(const Context& context, HighlightFlags flags, DisplayBuffer& display_buffer, BufferRange,
const String& character_tab, const String& character_tabpad, const String& character_spc, const String& character_lf, const String& character_nbsp)
StringView tab, StringView tabpad,
StringView spc, StringView lf, StringView nbsp)
{
const int tabstop = context.options()["tabstop"].get<int>();
auto whitespaceface = get_face("Whitespace");
@ -711,14 +712,14 @@ void show_whitespaces(const Context& context, HighlightFlags flags, DisplayBuffe
{
int column = (int)get_column(buffer, tabstop, it.coord());
int count = tabstop - (column % tabstop);
atom_it->replace(character_tab + String(character_tabpad[(CharCount)0], CharCount{count-1}));
atom_it->replace(tab + String(tabpad[(CharCount)0], CharCount{count-1}));
}
else if (cp == ' ')
atom_it->replace(character_spc);
atom_it->replace(spc.str());
else if (cp == '\n')
atom_it->replace(character_lf);
atom_it->replace(lf.str());
else if (cp == 0xA0)
atom_it->replace(character_nbsp);
atom_it->replace(nbsp.str());
atom_it->face = whitespaceface;
break;
}
@ -739,32 +740,19 @@ HighlighterAndId show_whitespaces_factory(HighlighterParameters params)
};
ParametersParser parser(params, param_desc);
StringView character_tab = parser.get_switch("tab").value_or("");
if (character_tab.char_length() != 1)
throw runtime_error("Tabulation character length is limited to 1");
StringView character_tabpad = parser.get_switch("tabpad").value_or(" ");
if (character_tabpad.char_length() != 1)
throw runtime_error("Tabulation padding character length is limited to 1");
StringView character_spc = parser.get_switch("spc").value_or("·");
if (character_spc.char_length() > 1)
throw runtime_error("Space character length is limited to 1");
StringView character_lf = parser.get_switch("lf").value_or("¬");
if (character_lf.char_length() > 1)
throw runtime_error("Line feed character length is limited to 1");
StringView character_nbsp = parser.get_switch("nbsp").value_or("");
if (character_nbsp.char_length() > 1)
throw runtime_error("Non breakable space character length is limited to 1");
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 parmeter", param)};
return value.str();
};
using namespace std::placeholders;
auto func = std::bind(show_whitespaces, _1, _2, _3, _4,
character_tab.str(), character_tabpad.str(),
character_spc.str(),
character_lf.str(),
character_nbsp.str());
get_param("tab", ""), get_param("tabpad", " "),
get_param("spc", "·"),
get_param("lf", "¬"),
get_param("nbsp", ""));
return {"show_whitespaces", make_simple_highlighter(std::move(func))};
}