[vala/staging] vala: Add multi-line support to Vala.Report



commit 32366df9e1cfc73a9d521675a017a7db25f03a7c
Author: Lorenz Wildberg <lorenz wild-fisch de>
Date:   Sun Sep 19 01:30:42 2021 +0100

    vala: Add multi-line support to Vala.Report
    
    To fix multi-line errors, the "^~~~"-underline appears now under all lines
    and columns between the begin and end of the source reference.
    
    Also a line number with a "|" at the left side is added for better finding
    the source of the error. This helps also one-line errors.
    
    Fixes https://gitlab.gnome.org/GNOME/vala/issues/764

 vala/valareport.vala | 59 +++++++++++++++++++++++++++-------------------------
 1 file changed, 31 insertions(+), 28 deletions(-)
---
diff --git a/vala/valareport.vala b/vala/valareport.vala
index 0aad89f15..3d3a95fc8 100644
--- a/vala/valareport.vala
+++ b/vala/valareport.vala
@@ -232,39 +232,42 @@ public class Vala.Report {
         * Pretty-print the actual line of offending code if possible.
         */
        private void report_source (SourceReference source) {
-               if (source.begin.line != source.end.line) {
-                       // FIXME Cannot report multi-line issues currently
-                       return;
-               }
-
-               string offending_line = source.file.get_source_line (source.begin.line);
-
-               if (offending_line != null) {
-                       stderr.printf ("%s\n", offending_line);
-                       int idx;
-
-                       /* We loop in this manner so that we don't fall over on differing
-                        * tab widths. This means we get the ^s in the right places.
-                        */
-                       for (idx = 1; idx < source.begin.column; ++idx) {
-                               if (offending_line[idx - 1] == '\t') {
-                                       stderr.printf ("\t");
-                               } else {
-                                       stderr.printf (" ");
-                               }
-                       }
-
+               for (int idx = source.begin.line; idx <= source.end.line; idx++) {
+                       string offending_line = source.file.get_source_line (idx);
+                       stderr.printf ("%5d | %s\n", idx, offending_line);
+                       stderr.printf ("      | ");
                        stderr.puts (caret_color_start);
-                       for (idx = source.begin.column; idx <= source.end.column; ++idx) {
-                               if (offending_line[idx - 1] == '\t') {
-                                       stderr.printf ("\t");
+                       for (int jdx = 0; jdx < offending_line.length; jdx++) {
+                               if (offending_line[jdx] == '\t') {
+                                       stderr.putc ('\t');
+                                       continue;
+                               }
+                               bool caret = false;
+                               unowned SourceLocation begin = source.begin;
+                               unowned SourceLocation end = source.end;
+                               if (begin.line == idx && end.line == idx) {
+                                       if (begin.column <= jdx + 1 <= end.column) {
+                                               caret = true;
+                                       }
+                               } else if (begin.line == idx && begin.column <= jdx + 1) {
+                                       caret = true;
+                               } else if (begin.line < idx < end.line) {
+                                       caret = true;
+                               } else if (end.line == idx && end.column >= jdx + 1) {
+                                       caret = true;
+                               }
+                               if (caret) {
+                                       if (begin.line == idx && begin.column == jdx + 1) {
+                                               stderr.putc ('^');
+                                       } else {
+                                               stderr.putc ('~');
+                                       }
                                } else {
-                                       stderr.printf ("^");
+                                       stderr.putc (' ');
                                }
                        }
                        stderr.puts (caret_color_end);
-
-                       stderr.printf ("\n");
+                       stderr.putc ('\n');
                }
        }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]