[gdb/tui] Fix tui compact-source a bit more

Message ID 20230510132118.13761-1-tdevries@suse.de
State Committed
Headers
Series [gdb/tui] Fix tui compact-source a bit more |

Commit Message

Tom de Vries May 10, 2023, 1:21 p.m. UTC
  Andrew pointed out that the behaviour as tested in gdb.tui/compact-source.exp
is incorrect:
...
0 +-compact-source.c--------------------------------------------------------+
1 |___3_{                                                                   |
2 |___4_  return 0;                                                         |
3 |___5_}                                                                   |
4 |___6_                                                                    |
5 |___7_                                                                    |
6 |___8_                                                                    |
7 |___9_                                                                    |
8 +-------------------------------------------------------------------------+
...

The last line number in the source file is 5, and there are 7 lines to display
source lines, so if we'd scroll all the way down, the first line number in the
source window would be 5, and the last one would be 11.

To represent 11 we'd need 2 digits, so we expect to see ___04_ here instead of
___4_, even though all line numbers currently in the src window (3-9) can be
represented with only 1 digit.

Fix this in tui_source_window::set_contents, by updating the computation of
max_line_nr:
...
-      int max_line_nr = std::max (lines_in_file, last_line_nr_in_window);
+      int max_line_nr = lines_in_file + nlines - 1;
...

Tested on x86_64-linux.

Co-Authored-By: Andrew Burgess <aburgess@redhat.com>
---
 gdb/testsuite/gdb.tui/compact-source.exp | 43 +++++++++++++++---------
 gdb/tui/tui-source.c                     |  3 +-
 2 files changed, 29 insertions(+), 17 deletions(-)


base-commit: 8b7b3b2bf4357781439e5434c4a5942ea29e983d
  

Comments

Tom Tromey May 10, 2023, 3:28 p.m. UTC | #1
>>>>> "Tom" == Tom de Vries via Gdb-patches <gdb-patches@sourceware.org> writes:

Tom> To represent 11 we'd need 2 digits, so we expect to see ___04_ here instead of
Tom> ___4_, even though all line numbers currently in the src window (3-9) can be
Tom> represented with only 1 digit.

Tom> Fix this in tui_source_window::set_contents, by updating the computation of
Tom> max_line_nr:
Tom> ...
Tom> -      int max_line_nr = std::max (lines_in_file, last_line_nr_in_window);
Tom> +      int max_line_nr = lines_in_file + nlines - 1;
Tom> ...

Makes sense to me.

Approved-By: Tom Tromey <tom@tromey.com>

Tom
  
Simon Marchi May 11, 2023, 2:55 p.m. UTC | #2
On 5/10/23 09:21, Tom de Vries via Gdb-patches wrote:
> Andrew pointed out that the behaviour as tested in gdb.tui/compact-source.exp
> is incorrect:
> ...
> 0 +-compact-source.c--------------------------------------------------------+
> 1 |___3_{                                                                   |
> 2 |___4_  return 0;                                                         |
> 3 |___5_}                                                                   |
> 4 |___6_                                                                    |
> 5 |___7_                                                                    |
> 6 |___8_                                                                    |
> 7 |___9_                                                                    |
> 8 +-------------------------------------------------------------------------+
> ...
> 
> The last line number in the source file is 5, and there are 7 lines to display
> source lines, so if we'd scroll all the way down, the first line number in the
> source window would be 5, and the last one would be 11.
> 
> To represent 11 we'd need 2 digits, so we expect to see ___04_ here instead of
> ___4_, even though all line numbers currently in the src window (3-9) can be
> represented with only 1 digit.

Just wondering: it makes sense to let the user scroll down all the way,
until the point where line 5 is the first line in the window.  However,
do we need to print line numbers for lines that are after the end of the
source file?  In other words, could we leave lines 6-11 blank, and
therefore we could keep using just one digit?

Simon
  
Tom de Vries May 11, 2023, 5:45 p.m. UTC | #3
On 5/11/23 16:55, Simon Marchi wrote:
> On 5/10/23 09:21, Tom de Vries via Gdb-patches wrote:
>> Andrew pointed out that the behaviour as tested in gdb.tui/compact-source.exp
>> is incorrect:
>> ...
>> 0 +-compact-source.c--------------------------------------------------------+
>> 1 |___3_{                                                                   |
>> 2 |___4_  return 0;                                                         |
>> 3 |___5_}                                                                   |
>> 4 |___6_                                                                    |
>> 5 |___7_                                                                    |
>> 6 |___8_                                                                    |
>> 7 |___9_                                                                    |
>> 8 +-------------------------------------------------------------------------+
>> ...
>>
>> The last line number in the source file is 5, and there are 7 lines to display
>> source lines, so if we'd scroll all the way down, the first line number in the
>> source window would be 5, and the last one would be 11.
>>
>> To represent 11 we'd need 2 digits, so we expect to see ___04_ here instead of
>> ___4_, even though all line numbers currently in the src window (3-9) can be
>> represented with only 1 digit.
> 
> Just wondering: it makes sense to let the user scroll down all the way,
> until the point where line 5 is the first line in the window.  However,
> do we need to print line numbers for lines that are after the end of the
> source file?  In other words, could we leave lines 6-11 blank, and
> therefore we could keep using just one digit?

This demonstrator patch implements that approach.

Thanks,
- Tom
  
Tom Tromey May 12, 2023, 6:40 p.m. UTC | #4
>>>>> "Tom" == Tom de Vries via Gdb-patches <gdb-patches@sourceware.org> writes:

>> Just wondering: it makes sense to let the user scroll down all the
>> way,
>> until the point where line 5 is the first line in the window.  However,
>> do we need to print line numbers for lines that are after the end of the
>> source file?  In other words, could we leave lines 6-11 blank, and
>> therefore we could keep using just one digit?

Tom> This demonstrator patch implements that approach.

FWIW I'd be fine with this.

Tom
  
Tom de Vries May 15, 2023, 4:03 a.m. UTC | #5
On 5/12/23 20:40, Tom Tromey wrote:
>>>>>> "Tom" == Tom de Vries via Gdb-patches <gdb-patches@sourceware.org> writes:
> 
>>> Just wondering: it makes sense to let the user scroll down all the
>>> way,
>>> until the point where line 5 is the first line in the window.  However,
>>> do we need to print line numbers for lines that are after the end of the
>>> source file?  In other words, could we leave lines 6-11 blank, and
>>> therefore we could keep using just one digit?
> 
> Tom> This demonstrator patch implements that approach.
> 
> FWIW I'd be fine with this.

Submitted the patch here ( 
https://sourceware.org/pipermail/gdb-patches/2023-May/199568.html ).

Thanks,
- Tom
  

Patch

diff --git a/gdb/testsuite/gdb.tui/compact-source.exp b/gdb/testsuite/gdb.tui/compact-source.exp
index 71e6b7b0b0a..f972d961d72 100644
--- a/gdb/testsuite/gdb.tui/compact-source.exp
+++ b/gdb/testsuite/gdb.tui/compact-source.exp
@@ -23,14 +23,15 @@  standard_testfile
 
 # Let's generate the source file.  We want a short file, with less than 10
 # lines, and the copyright notice by itself is already more that that.
-set src_txt \
-    [join \
-	 [list \
-	      "int" \
-	      "main (void)" \
-	      "{" \
-	      "  return 0;" \
-	      "}"] "\n"]
+set src_list \
+    [list \
+	 "int" \
+	 "main (void)" \
+	 "{" \
+	 "  return 0;" \
+	 "}"]
+set re_line_four [string_to_regexp [lindex $src_list 3]]
+set src_txt [join $src_list "\n"]
 set srcfile [standard_output_file $srcfile]
 set fd [open $srcfile w]
 puts $fd $src_txt
@@ -40,7 +41,7 @@  if {[build_executable "failed to prepare" ${testfile} ${srcfile}] == -1} {
     return -1
 }
 
-Term::clean_restart 17 80 $binfile
+Term::clean_restart 24 80 $binfile
 
 gdb_test_no_output "maint set tui-left-margin-verbose on"
 gdb_test_no_output "set tui compact-source on"
@@ -51,11 +52,23 @@  if {![Term::enter_tui]} {
 }
 
 set re_border "\\|"
-Term::check_contents "compact source format" \
-    "${re_border}___04_  return 0; *$re_border"
 
-with_test_prefix window-resize=1 {
-    Term::command "wh src -1"
-    Term::check_contents "compact source" \
-	"${re_border}___4_  return 0; *$re_border"
+foreach_with_prefix src_window_size {7 8} {
+    set src_window_lines [expr $src_window_size - 2]
+    set max_line_nr_in_source_file [llength $src_list]
+    set max_line_nr_in_source_window \
+	[expr $max_line_nr_in_source_file + $src_window_lines - 1]
+
+    Term::command "wh src $src_window_size"
+
+    if { $max_line_nr_in_source_window == 9 } {
+	set re_left_margin "___4_"
+    } elseif { $max_line_nr_in_source_window == 10 }  {
+	set re_left_margin "___04_"
+    } else {
+	error "unhandled max_line_nr_in_source_window"
+    }
+
+    Term::check_contents "compact source format" \
+	"$re_border$re_left_margin$re_line_four *$re_border"
 }
diff --git a/gdb/tui/tui-source.c b/gdb/tui/tui-source.c
index 1233e945cab..9d0376000de 100644
--- a/gdb/tui/tui-source.c
+++ b/gdb/tui/tui-source.c
@@ -80,8 +80,7 @@  tui_source_window::set_contents (struct gdbarch *arch,
       /* Solaris 11+gcc 5.5 has ambiguous overloads of log10, so we
 	 cast to double to get the right one.  */
       int lines_in_file = offsets->size ();
-      int last_line_nr_in_window = line_no + nlines - 1;
-      int max_line_nr = std::max (lines_in_file, last_line_nr_in_window);
+      int max_line_nr = lines_in_file + nlines - 1;
       int digits_needed = 1 + (int)log10 ((double) max_line_nr);
       int trailing_space = 1;
       m_digits = digits_needed + trailing_space;