[RFC,8.3,1/3] Make TUI react to "set style enabled"

Message ID 20190308210433.32683-2-tromey@adacore.com
State New
Headers show
Series
  • Some style fixes
Related show

Commit Message

Tom Tromey March 8, 2019, 9:04 p.m.
When the user toggles "set style enabled", the TUI should react by
redrawing the source window, if necessary.  This patch implements this
behavior.

No test because the TUI is generally not tested.

I was not sure of a clean way to force an update of the window's
contents; see the new tui_redisplay_source for the workaround I used.

gdb/ChangeLog
2019-03-08  Tom Tromey  <tromey@adacore.com>

	* cli/cli-style.c (set_style_enabled): Notify new observable.
	* tui/tui-hooks.c (tui_redisplay_source): New function.
	(tui_attach_detach_observers): Attach or detach
	tui_redisplay_source.
	* observable.h (source_styling_changed): New observable.
	* observable.c: Define source_styling_changed observable.
---
 gdb/ChangeLog       |  9 +++++++++
 gdb/cli/cli-style.c |  2 ++
 gdb/observable.c    |  1 +
 gdb/observable.h    |  4 ++++
 gdb/tui/tui-hooks.c | 14 ++++++++++++++
 5 files changed, 30 insertions(+)

-- 
2.20.1

Comments

Pedro Alves March 13, 2019, 7:28 p.m. | #1
On 03/08/2019 09:04 PM, Tom Tromey wrote:
> When the user toggles "set style enabled", the TUI should react by

> redrawing the source window, if necessary.  This patch implements this

> behavior.

> 

> No test because the TUI is generally not tested.

> 

> I was not sure of a clean way to force an update of the window's

> contents; see the new tui_redisplay_source for the workaround I used.


I went looking, and thought that it could be:

  tui_show_source_content (tui_win_list[SRC_WIN]);

That's what tui_refresh_all_win, coming from Ctrl-L, does.

But that won't work I think because we'd just refresh the current
source code cache, which includes the previous/now-stale
highlighting mode.

Maybe something like this?

From 5b7c86c6666ddc16b206fe04a492e99a33570a7f Mon Sep 17 00:00:00 2001
From: Pedro Alves <palves@redhat.com>

Date: Wed, 13 Mar 2019 18:14:14 +0000
Subject: [PATCH] refactor

---
 gdb/tui/tui-hooks.c     |  2 +-
 gdb/tui/tui-winsource.c | 44 +++++++++++++++++++++++++-------------------
 gdb/tui/tui-winsource.h |  1 +
 3 files changed, 27 insertions(+), 20 deletions(-)

diff --git a/gdb/tui/tui-hooks.c b/gdb/tui/tui-hooks.c
index 162d6ef525..4a1d79e0ad 100644
--- a/gdb/tui/tui-hooks.c
+++ b/gdb/tui/tui-hooks.c
@@ -212,7 +212,7 @@ tui_redisplay_source ()
   if (tui_is_window_visible (SRC_WIN))
     {
       /* Force redisplay.  */
-      tui_horizontal_source_scroll (tui_win_list[SRC_WIN], LEFT_SCROLL, 0);
+      tui_refill_source_window (tui_win_list[SRC_WIN]);
     }
 }
 
diff --git a/gdb/tui/tui-winsource.c b/gdb/tui/tui-winsource.c
index 9336f7b1f7..ab7756c1c4 100644
--- a/gdb/tui/tui-winsource.c
+++ b/gdb/tui/tui-winsource.c
@@ -306,8 +306,32 @@ tui_show_source_content (struct tui_win_info *win_info)
   win_info->generic.content_in_use = TRUE;
 }
 
+/* Refill the source window's source cache and update it.  If WIN_INFO
+   is a disassembly window, then just update it.  */
+
+void
+tui_refill_source_window (struct tui_win_info *win_info)
+{
+  symtab *s = nullptr;
+
+  if (win_info->generic.type == SRC_WIN)
+    {
+      symtab_and_line cursal = get_current_source_symtab_and_line ();
+      s = (cursal.symtab == NULL
+	   ? find_pc_line_symtab (get_frame_pc (get_selected_frame (NULL)))
+	   : cursal.symtab);
+    }
+
+  tui_update_source_window_as_is (win_info,
+				  win_info->detail.source_info.gdbarch,
+				  s,
+				  win_info->generic.content[0]
+				    ->which_element.source.line_or_addr,
+				  FALSE);
+}
 
 /* Scroll the source forward or backward horizontally.  */
+
 void
 tui_horizontal_source_scroll (struct tui_win_info *win_info,
 			      enum tui_scroll_direction direction,
@@ -315,20 +339,7 @@ tui_horizontal_source_scroll (struct tui_win_info *win_info,
 {
   if (win_info->generic.content != NULL)
     {
-      struct gdbarch *gdbarch = win_info->detail.source_info.gdbarch;
       int offset;
-      struct symtab *s = NULL;
-
-      if (win_info->generic.type == SRC_WIN)
-	{
-	  struct symtab_and_line cursal
-	    = get_current_source_symtab_and_line ();
-
-	  if (cursal.symtab == NULL)
-	    s = find_pc_line_symtab (get_frame_pc (get_selected_frame (NULL)));
-	  else
-	    s = cursal.symtab;
-	}
 
       if (direction == LEFT_SCROLL)
 	offset = win_info->detail.source_info.horizontal_offset
@@ -341,13 +352,8 @@ tui_horizontal_source_scroll (struct tui_win_info *win_info,
 	    offset = 0;
 	}
       win_info->detail.source_info.horizontal_offset = offset;
-      tui_update_source_window_as_is (win_info, gdbarch, s,
-				      win_info->generic.content[0]
-					->which_element.source.line_or_addr,
-				      FALSE);
+      tui_refill_source_window (win_info);
     }
-
-  return;
 }
 
 
diff --git a/gdb/tui/tui-winsource.h b/gdb/tui/tui-winsource.h
index db33a4f73e..920032b04e 100644
--- a/gdb/tui/tui-winsource.h
+++ b/gdb/tui/tui-winsource.h
@@ -56,6 +56,7 @@ extern void tui_show_source_content (struct tui_win_info *);
 extern void tui_horizontal_source_scroll (struct tui_win_info *,
 					  enum tui_scroll_direction, 
 					  int);
+extern void tui_refill_source_window (struct tui_win_info *);
 extern enum tui_status tui_set_exec_info_content (struct tui_win_info *);
 extern void tui_show_exec_info_content (struct tui_win_info *);
 extern void tui_erase_exec_info_content (struct tui_win_info *);
-- 
2.14.4
Tom Tromey March 14, 2019, 11:43 a.m. | #2
Pedro> Maybe something like this?
[...]

Thanks.  I've incorporated this into the patch.

Tom

Patch

diff --git a/gdb/cli/cli-style.c b/gdb/cli/cli-style.c
index 234cc410dda..729a024c47a 100644
--- a/gdb/cli/cli-style.c
+++ b/gdb/cli/cli-style.c
@@ -21,6 +21,7 @@ 
 #include "cli/cli-cmds.h"
 #include "cli/cli-style.h"
 #include "source-cache.h"
+#include "observable.h"
 
 /* True if styling is enabled.  */
 
@@ -216,6 +217,7 @@  static void
 set_style_enabled  (const char *args, int from_tty, struct cmd_list_element *c)
 {
   g_source_cache.clear ();
+  gdb::observers::source_styling_changed.notify ();
 }
 
 static void
diff --git a/gdb/observable.c b/gdb/observable.c
index 33112062424..c077b025932 100644
--- a/gdb/observable.c
+++ b/gdb/observable.c
@@ -74,6 +74,7 @@  DEFINE_OBSERVABLE (inferior_call_pre);
 DEFINE_OBSERVABLE (inferior_call_post);
 DEFINE_OBSERVABLE (register_changed);
 DEFINE_OBSERVABLE (user_selected_context_changed);
+DEFINE_OBSERVABLE (source_styling_changed);
 
 } /* namespace observers */
 } /* namespace gdb */
diff --git a/gdb/observable.h b/gdb/observable.h
index 999ecfbc353..edd1fffae0a 100644
--- a/gdb/observable.h
+++ b/gdb/observable.h
@@ -228,6 +228,10 @@  extern observable<struct frame_info *, int> register_changed;
    frame has changed.  */
 extern observable<user_selected_what> user_selected_context_changed;
 
+/* This is notified when the source styling setting has changed and
+   should be reconsulted.  */
+extern observable<> source_styling_changed;
+
 } /* namespace observers */
 
 } /* namespace gdb */
diff --git a/gdb/tui/tui-hooks.c b/gdb/tui/tui-hooks.c
index 98c6fd651fa..162d6ef525a 100644
--- a/gdb/tui/tui-hooks.c
+++ b/gdb/tui/tui-hooks.c
@@ -204,6 +204,18 @@  tui_normal_stop (struct bpstats *bs, int print_frame)
   tui_refresh_frame_and_register_information (/*registers_too_p=*/1);
 }
 
+/* Observer for source_cache_cleared.  */
+
+static void
+tui_redisplay_source ()
+{
+  if (tui_is_window_visible (SRC_WIN))
+    {
+      /* Force redisplay.  */
+      tui_horizontal_source_scroll (tui_win_list[SRC_WIN], LEFT_SCROLL, 0);
+    }
+}
+
 /* Token associated with observers registered while TUI hooks are
    installed.  */
 static const gdb::observers::token tui_observers_token {};
@@ -239,6 +251,8 @@  tui_attach_detach_observers (bool attach)
 		    tui_normal_stop, attach);
   attach_or_detach (gdb::observers::register_changed,
 		    tui_register_changed, attach);
+  attach_or_detach (gdb::observers::source_styling_changed,
+		    tui_redisplay_source, attach);
 }
 
 /* Install the TUI specific hooks.  */