From 37c4d0b1753c4e562a8c761adb816c27db861494 Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Tue, 24 Sep 2019 08:30:28 +0200 Subject: [PATCH] diagnostics: isolate caret_set_column * src/location.c (caret_info): Add width and skip members. (caret_set_column): New. Use it. --- src/location.c | 53 ++++++++++++++++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 17 deletions(-) diff --git a/src/location.c b/src/location.c index f0e2ab04..46cb10ea 100644 --- a/src/location.c +++ b/src/location.c @@ -224,6 +224,13 @@ static struct size_t offset; /* Length of the current line. */ int line_len; + /* Given the initial column to display, the offset (number of + characters to skip at the beginning of the line). */ + int skip; + + /* Available width to quote the source file. Eight chars are + consumed by the left-margin (with line number). */ + int width; } caret_info; void caret_init (void) @@ -352,6 +359,31 @@ caret_set_line (int line) return true; } +/* Compute CARET_INFO.WIDTH and CARET_INFO.SKIP based on the fact that + the first column to display in the current line is COL. */ +static bool +caret_set_column (int col) +{ + /* Available width. Eight chars are consumed by the left-margin + (with line number). */ + caret_info.width = screen_width - 8; + caret_info.skip = 0; + if (caret_info.width < caret_info.line_len) + { + /* We cannot quote the whole line. Make sure we can see the + beginning of the location. */ + caret_info.skip = caret_info.width < col ? col - 10 : 0; + } + /* If we skip the initial part, we insert "..." before. */ + if (caret_info.skip) + caret_info.width -= 3; + /* If the end of line does not fit, we also need to truncate the + end, and leave "..." there. */ + if (caret_info.width < caret_info.line_len - caret_info.skip) + caret_info.width -= 3; + return true; +} + void location_caret (location loc, const char *style, FILE *out) { @@ -361,24 +393,11 @@ location_caret (location loc, const char *style, FILE *out) return; if (!caret_set_line (loc.start.line)) return; + if (!caret_set_column (loc.start.column)) + return; - /* Available width. Eight chars are consumed by the left-margin - (with line number). */ - int width = screen_width - 8; - int skip = 0; - if (width < caret_info.line_len) - { - /* We cannot quote the whole line. Make sure we can see the - beginning of the location. */ - skip = width < loc.start.column ? loc.start.column - 10 : 0; - } - /* If we skip the initial part, we insert "..." before. */ - if (skip) - width -= 3; - /* If the end of line does not fit, we also need to truncate the - end, and leave "..." there. */ - if (width < caret_info.line_len - skip) - width -= 3; + const int width = caret_info.width; + const int skip = caret_info.skip; /* Read the actual line. Don't update the offset, so that we keep a pointer to the start of the line. */