From 7ef314ff949f85c88cd941d0dca5ab0c88ff8dfe Mon Sep 17 00:00:00 2001 From: James Westman Date: Sun, 25 Dec 2022 16:02:55 -0600 Subject: [PATCH] Fix diagnostic location reporting Text positions at the beginning of a line were being shown on the previous line. --- blueprintcompiler/utils.py | 5 ++--- tests/sample_errors/no_import_version.err | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/blueprintcompiler/utils.py b/blueprintcompiler/utils.py index 1c69fd9..5e939c7 100644 --- a/blueprintcompiler/utils.py +++ b/blueprintcompiler/utils.py @@ -74,9 +74,8 @@ def did_you_mean(word: str, options: T.List[str]) -> T.Optional[str]: def idx_to_pos(idx: int, text: str) -> T.Tuple[int, int]: if idx == 0 or len(text) == 0: return (0, 0) - sp = text[:idx].splitlines(keepends=True) - line_num = len(sp) - col_num = len(sp[-1]) + line_num = text.count("\n", 0, idx) + 1 + col_num = idx - text.rfind("\n", 0, idx) - 1 return (line_num - 1, col_num) diff --git a/tests/sample_errors/no_import_version.err b/tests/sample_errors/no_import_version.err index 4ee792f..db830e0 100644 --- a/tests/sample_errors/no_import_version.err +++ b/tests/sample_errors/no_import_version.err @@ -1 +1 @@ -1,10,0,Expected a version number for GTK +1,11,0,Expected a version number for GTK