Skip to content

Commit

Permalink
Prevent duplicate panes in context.layout (#1153)
Browse files Browse the repository at this point in the history
## Description

Make sure panes added in `context.layout` have a unique name

Fixes #1145
  • Loading branch information
hugsy authored Nov 11, 2024
1 parent 0f21eea commit 3bdba58
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
8 changes: 4 additions & 4 deletions gef.py
Original file line number Diff line number Diff line change
Expand Up @@ -10035,13 +10035,13 @@ def add_context_layout_mapping(self, current_pane_name: str, display_pane_functi

def add_context_pane(self, pane_name: str, display_pane_function: Callable, pane_title_function: Callable, condition: Callable | None) -> None:
"""Add a new context pane to ContextCommand."""
context = self.commands["context"]
assert isinstance(context, ContextCommand)

# assure users can toggle the new context
corrected_settings_name: str = pane_name.replace(" ", "_")
gef.config["context.layout"] += f" {corrected_settings_name}"
if corrected_settings_name in gef.config["context.layout"]:
warn(f"Duplicate name for `{pane_name}` (`{corrected_settings_name}`), skipping")
return

gef.config["context.layout"] += f" {corrected_settings_name}"
self.add_context_layout_mapping(corrected_settings_name, display_pane_function, pane_title_function, condition)

def load(self) -> None:
Expand Down
23 changes: 22 additions & 1 deletion tests/commands/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,25 @@ class ContextCommand(RemoteGefUnitTestGeneric):
cmd = "context"


# See https://github.com/hugsy/gef/projects/10
# TODO See https://github.com/hugsy/gef/projects/10

def test_duplicate_pane_name(self):
# Make sure we cannot have 2 context panes with an identical identifier
# See #1145 , #1153
gdb = self._gdb
gef = self._gef

new_pane_id = "new_pane1"
current_layout = gef.config["context.layout"]
assert not current_layout.endswith(new_pane_id)

res = gdb.execute(f"pi register_external_context_pane('{new_pane_id}', print, print, None)", to_string=True)
assert "Python Exception" not in res
new_layout = gef.config["context.layout"]
assert new_layout.endswith(new_pane_id)

res = gdb.execute(f"pi register_external_context_pane('{new_pane_id}', print, print, None)", to_string=True)
assert "Python Exception" not in res
new_layout2 = gef.config["context.layout"]
assert new_layout == new_layout2
assert f"Duplicate name for `{new_pane_id}`" in res

0 comments on commit 3bdba58

Please sign in to comment.