git-gui: use path name instead of list index to track last clicked file
diff mbox series

Message ID 20190903144536.8935-1-me@yadavpratyush.com
State New
Headers show
Series
  • git-gui: use path name instead of list index to track last clicked file
Related show

Commit Message

Pratyush Yadav Sept. 3, 2019, 2:45 p.m. UTC
Birger,

You would probably want to squash this patch with yours when you send a
re-roll. Of course, I'd like some comments and tests on the patch before
considering it "done". Just letting you know that I'd like to have this
change in your original patch/commit, not as a separate commit. I put it
in a separate patch for now for easier readability.

Also, FYI, pass '--scissors' to git-am when applying this to not get the
above text in the commit message.

-- >8 --
For using the hotkeys CTRL/CMD+1/2/3/4, we save the index of the last
clicked file. This index may change if some external command changes the
repo state. So use the path name of the file instead.

Signed-off-by: Pratyush Yadav <me@yadavpratyush.com>
---
 git-gui.sh | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)

--
2.21.0

Patch
diff mbox series

diff --git a/git-gui.sh b/git-gui.sh
index ce620f1..9be1b6a 100755
--- a/git-gui.sh
+++ b/git-gui.sh
@@ -2527,7 +2527,7 @@  proc toggle_or_diff {mode w args} {
 	$ui_index tag remove in_sel 0.0 end
 	$ui_workdir tag remove in_sel 0.0 end

-	set file_lists_last_clicked($w) $lno
+	set file_lists_last_clicked($w) $path

 	# Determine the state of the file
 	if {[info exists file_states($path)]} {
@@ -2648,17 +2648,15 @@  proc select_path_in {widget} {

 	set _list_length [llength $file_lists($widget)]
 	if {$_list_length > 0} {
-
-		set _index $file_lists_last_clicked($widget)
-		if {$_index eq {}} {
-			set _index 1
-		} elseif {$_index > $_list_length} {
-			set _index $_list_length
+		set path $file_lists_last_clicked($widget)
+		set index [lsearch -sorted -exact $file_lists($widget) $path]
+		if {$index < 0} {
+			set index 0
+			set path [lindex $file_lists($widget) $index]
 		}

 		focus $widget
-		set last_clicked [list $widget $_index]
-		set path [lindex $file_lists($widget) [expr $_index - 1]]
+		set last_clicked [list $widget [expr $index + 1]]
 		array unset selected_paths
 		set selected_paths($path) 1
 		show_diff $path $widget