1
1
mirror of https://github.com/mawww/kakoune.git synced 2024-12-25 20:41:49 +03:00

Add -width <max_width> support in the wrap highlighter

Will always wrap at the minimum between max_width and actual window
width.

Fixes #1424
This commit is contained in:
Maxime Coste 2017-06-08 07:05:44 +01:00
parent acc2dbf79c
commit eadf8930fb
3 changed files with 19 additions and 8 deletions

View File

@ -1251,7 +1251,8 @@ General highlighters are:
-hlcursor switch will highlight the cursor line with a separate face. With the -hlcursor switch will highlight the cursor line with a separate face. With the
-separator switch one can specify a string to separate the line numbers column with -separator switch one can specify a string to separate the line numbers column with
the rest of the buffer, default is `|`. the rest of the buffer, default is `|`.
* `wrap \<-word>`: Soft wrap buffer content to window width, wrap at word boundaries if `-word` is specified. * `wrap \<-word> \<-width <max_width>`: Soft wrap buffer content to the smallest of window width and
max_width. Wrap at word boundaries if `-word` is specified.
* `fill <face>`: fill using given face, mostly useful with <<regions-highlighters,Regions highlighters>> * `fill <face>`: fill using given face, mostly useful with <<regions-highlighters,Regions highlighters>>
* `ranges <option_name>`: use the data in the range-specs option of the given name to highlight the buffer. * `ranges <option_name>`: use the data in the range-specs option of the given name to highlight the buffer.
The string part of the is interpretted as a face to apply to the range. The string part of the is interpretted as a face to apply to the range.

View File

@ -90,6 +90,9 @@ General highlighters
*-word*::: *-word*:::
wrap at word boundaries instead of codepoint boundaries. wrap at word boundaries instead of codepoint boundaries.
*-width <max_width>*:::
wrap text at *max_width* if the window is wider.
*ranges* <option_name>:: *ranges* <option_name>::
use the data in the range-specs option of the given name to highlight use the data in the range-specs option of the given name to highlight
the buffer. The string part of the is interpretted as a face to apply the buffer. The string part of the is interpretted as a face to apply

View File

@ -704,12 +704,13 @@ HighlighterAndId create_column_highlighter(HighlighterParameters params)
struct WrapHighlighter : Highlighter struct WrapHighlighter : Highlighter
{ {
WrapHighlighter(bool word_wrap) : Highlighter{HighlightPass::Wrap}, m_word_wrap{word_wrap} {} WrapHighlighter(ColumnCount max_width, bool word_wrap)
: Highlighter{HighlightPass::Wrap}, m__max_width{max_width}, m_word_wrap{word_wrap} {}
void do_highlight(const Context& context, HighlightPass pass, void do_highlight(const Context& context, HighlightPass pass,
DisplayBuffer& display_buffer, BufferRange) override DisplayBuffer& display_buffer, BufferRange) override
{ {
const ColumnCount wrap_column = context.window().range().column; const ColumnCount wrap_column = std::min(m__max_width, context.window().range().column);
if (wrap_column <= 0) if (wrap_column <= 0)
return; return;
@ -759,7 +760,7 @@ struct WrapHighlighter : Highlighter
void do_compute_display_setup(const Context& context, HighlightPass, DisplaySetup& setup) override void do_compute_display_setup(const Context& context, HighlightPass, DisplaySetup& setup) override
{ {
const ColumnCount wrap_column = setup.window_range.column; const ColumnCount wrap_column = std::min(setup.window_range.column, m__max_width);
if (wrap_column <= 0) if (wrap_column <= 0)
return; return;
@ -862,15 +863,21 @@ struct WrapHighlighter : Highlighter
static HighlighterAndId create(HighlighterParameters params) static HighlighterAndId create(HighlighterParameters params)
{ {
static const ParameterDesc param_desc{ static const ParameterDesc param_desc{
{ { "word", { false, "" } } }, { { "word", { false, "" } },
{ "width", { true, "" } } },
ParameterDesc::Flags::None, 0, 0 ParameterDesc::Flags::None, 0, 0
}; };
ParametersParser parser(params, param_desc); ParametersParser parser(params, param_desc);
return {"wrap", make_unique<WrapHighlighter>((bool)parser.get_switch("word"))}; ColumnCount max_width{std::numeric_limits<int>::max()};
if (auto width = parser.get_switch("width"))
max_width = str_to_int(*width);
return {"wrap", make_unique<WrapHighlighter>(max_width, (bool)parser.get_switch("word"))};
} }
const bool m_word_wrap; const bool m_word_wrap;
const ColumnCount m__max_width;
}; };
void expand_tabulations(const Context& context, HighlightPass, DisplayBuffer& display_buffer, BufferRange) void expand_tabulations(const Context& context, HighlightPass, DisplayBuffer& display_buffer, BufferRange)
@ -2002,8 +2009,8 @@ void register_highlighters()
registry.insert({ registry.insert({
"wrap", "wrap",
{ WrapHighlighter::create, { WrapHighlighter::create,
"Parameters: [-word]\n" "Parameters: [-word] [-width <max_width>]\n"
"Wrap lines to given column,\n" "Wrap lines to window width, or max_width if given and window is wider,\n"
"wrap at word boundaries instead of codepoint boundaries if -word is given" } }); "wrap at word boundaries instead of codepoint boundaries if -word is given" } });
registry.insert({ registry.insert({
"ref", "ref",