Skip to content

Commit 6da2534

Browse files
committed
[text] add indent guides for blank lines
1 parent df51de7 commit 6da2534

4 files changed

Lines changed: 52 additions & 19 deletions

File tree

src/base/string_util.cc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,24 @@ is_blank(const std::string& str)
329329
str.begin(), str.end(), [](const auto ch) { return isspace(ch); });
330330
}
331331

332+
size_t
333+
compute_indent_size(const std::string& str)
334+
{
335+
size_t retval = 0;
336+
for (const auto& ch : str) {
337+
if (ch == ' ') {
338+
retval += 1;
339+
} else if (ch == '\t') {
340+
do {
341+
retval += 1;
342+
} while (retval % 8);
343+
} else {
344+
break;
345+
}
346+
}
347+
return retval;
348+
}
349+
332350
std::string
333351
scrub_ws(const char* in, ssize_t len)
334352
{

src/base/string_util.hh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,8 @@ utf8_string_length(const std::string& str)
209209

210210
bool is_blank(const std::string& str);
211211

212+
size_t compute_indent_size(const std::string& str);
213+
212214
size_t abbreviate_str(char* str, size_t len, size_t max_len);
213215

214216
size_t last_word_str(char* str, size_t len, size_t max_len);

src/plain_text_source.cc

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "plain_text_source.hh"
3131

3232
#include "base/intern_string.hh"
33+
#include "base/string_util.hh"
3334
#include "base/itertools.hh"
3435
#include "config.h"
3536
#include "document.sections.hh"
@@ -175,16 +176,18 @@ plain_text_source::text_value_for_line(textview_curses& tc,
175176
text_sub_source::line_flags_t flags)
176177
{
177178
value_out = this->tds_lines[row].tl_value.get_string();
178-
this->tds_line_indent_size = 0;
179-
for (const auto& ch : value_out) {
180-
if (ch == ' ') {
181-
this->tds_line_indent_size += 1;
182-
} else if (ch == '\t') {
183-
do {
184-
this->tds_line_indent_size += 1;
185-
} while (this->tds_line_indent_size % 8);
186-
} else {
187-
break;
179+
this->tds_line_indent_size = compute_indent_size(value_out);
180+
if (this->tds_line_indent_size == 0 && value_out.empty()) {
181+
for (auto next = row + 1;
182+
next < (int) this->tds_lines.size();
183+
++next)
184+
{
185+
const auto& next_str
186+
= this->tds_lines[next].tl_value.get_string();
187+
this->tds_line_indent_size = compute_indent_size(next_str) + 1;
188+
if (!next_str.empty()) {
189+
break;
190+
}
188191
}
189192
}
190193

src/textfile_sub_source.cc

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#include "base/itertools.hh"
4545
#include "base/map_util.hh"
4646
#include "base/math_util.hh"
47+
#include "base/string_util.hh"
4748
#include "bound_tags.hh"
4849
#include "config.h"
4950
#include "data_scanner.hh"
@@ -186,15 +187,24 @@ textfile_sub_source::text_value_for_line(textview_curses& tc,
186187
if (meta.m_valid_utf && meta.m_has_ansi) {
187188
scrub_ansi_string(value_out, &this->tss_plain_line_attrs);
188189
}
189-
for (const auto& ch : value_out) {
190-
if (ch == ' ') {
191-
this->tss_line_indent_size += 1;
192-
} else if (ch == '\t') {
193-
do {
194-
this->tss_line_indent_size += 1;
195-
} while (this->tss_line_indent_size % 8);
196-
} else {
197-
break;
190+
this->tss_line_indent_size = compute_indent_size(value_out);
191+
if (this->tss_line_indent_size == 0 && value_out.empty()) {
192+
for (auto next = line + 1;
193+
next < (ssize_t) lfo->lfo_filter_state.tfs_index.size();
194+
++next)
195+
{
196+
auto next_ll
197+
= lf->begin() + lfo->lfo_filter_state.tfs_index[next];
198+
auto next_result = lf->read_line(next_ll, read_opts);
199+
if (next_result.isOk()) {
200+
auto next_sbr = next_result.unwrap();
201+
auto next_str = to_string(next_sbr);
202+
this->tss_line_indent_size
203+
= compute_indent_size(next_str) + 1;
204+
if (!next_str.empty()) {
205+
break;
206+
}
207+
}
198208
}
199209
}
200210
if (lf->has_line_metadata() && this->tas_display_time_offset) {

0 commit comments

Comments
 (0)