[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 4/8] diagnostics: isolate caret_set_column
From: |
Akim Demaille |
Subject: |
[PATCH 4/8] diagnostics: isolate caret_set_column |
Date: |
Wed, 2 Oct 2019 08:22:15 +0200 |
* 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 bd211ea0..304d84ac 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,33 +359,45 @@ caret_set_line (int line)
return true;
}
-void
-location_caret (location loc, const char *style, FILE *out)
+/* 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)
{
- if (loc.start.column == -1 || loc.start.line == -1)
- return;
- if (!caret_set_file (loc.start.file))
- return;
- if (!caret_set_line (loc.start.line))
- 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)
+ 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. */
- skip = width < loc.start.column ? loc.start.column - 10 : 0;
+ caret_info.skip = caret_info.width < col ? col - 10 : 0;
}
/* If we skip the initial part, we insert "..." before. */
- if (skip)
- width -= 3;
+ 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 (width < caret_info.line_len - skip)
- width -= 3;
+ 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)
+{
+ if (loc.start.column == -1 || loc.start.line == -1)
+ return;
+ if (!caret_set_file (loc.start.file))
+ return;
+ if (!caret_set_line (loc.start.line))
+ return;
+ if (!caret_set_column (loc.start.column))
+ return;
+
+ 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. */
--
2.23.0
- [PATCH 0/8] RFC: diagnostics: add suggestions, Akim Demaille, 2019/10/02
- [PATCH 1/8] TODO: update, Akim Demaille, 2019/10/02
- [PATCH 2/8] tests: use tput to get the number of columns, Akim Demaille, 2019/10/02
- [PATCH 3/8] diagnostics: isolate caret_set_file, Akim Demaille, 2019/10/02
- [PATCH 4/8] diagnostics: isolate caret_set_column,
Akim Demaille <=
- [PATCH 5/8] diagnostics: display suggested update after the caret-info, Akim Demaille, 2019/10/02
- [PATCH 7/8] style: isolate complain_symbol_undeclared, Akim Demaille, 2019/10/02
- [PATCH 6/8] style: simplify the handling of symbol and semantic_type tables, Akim Demaille, 2019/10/02
- [PATCH 8/8] diagnostics: suggest fixes for undeclared symbols, Akim Demaille, 2019/10/02