From ad8df2ac3d9c1f26a09d436996853a6e97fe59dd Mon Sep 17 00:00:00 2001 From: herr kaste Date: Sun, 3 Mar 2024 22:21:43 +0100 Subject: [PATCH] Enhance comparing arbitrary selections 1. Implement `is_enabled` to to grey out the "Compare selection" menu entry in the context menu when it is not applicable. 2. Reset the stored selections when the user opens "Compare selection". 3. Make it easier to use: After the user makes one selection and chooses "Mark selection...", it enough to make a next selection and choose "Compare selection" directly. (I.e. it is not necessary anymore to store both(!) selections using the "Mark..." command.) --- Context.sublime-menu | 2 +- README.md | 2 +- sbs_compare.py | 28 +++++++++++++++++++--------- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/Context.sublime-menu b/Context.sublime-menu index d0b1052..59bc17c 100644 --- a/Context.sublime-menu +++ b/Context.sublime-menu @@ -2,7 +2,7 @@ { "caption": "-" }, { "caption": "Compare with...", "command": "sbs_compare" }, { "caption": "Mark selection for comparison", "command": "sbs_mark_sel" }, - { "caption": "Compare selection", "command": "sbs_compare", "args": { "compare_selections": true } }, + { "caption": "Compare selections", "command": "sbs_compare", "args": { "compare_selections": true } }, { "caption": "Previous modification", "command": "sbs_prev_diff" }, { "caption": "Next modification", "command": "sbs_next_diff" }, { "caption": "-" } diff --git a/README.md b/README.md index c463b67..77cbb80 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ Usage Options - Right click somewhere in the active view and select "Compare with..." - Right click on a tab and select "Compare with active tab" - Highlight text, right click -> "Mark selection for comparison" - - Mark a second selection, then right click -> "Compare selections" + Mark a second selection, then right click -> "Compare selections" - Create two selections by holding CTRL, then "Compare selections" - From the command line: [see README_COMMANDS.md](README_COMMANDS.md) - Jump to next: `alt+n`, jump to previous: `alt+p` diff --git a/sbs_compare.py b/sbs_compare.py index 14aee99..fca3778 100644 --- a/sbs_compare.py +++ b/sbs_compare.py @@ -335,7 +335,18 @@ def compare_views( self, view1, view2 ): sublime.message_dialog( intraDiff + str( len( highlightA ) ) + ' lines removed, ' + str( len( highlightB ) ) + ' lines added\n' + str( numDiffs ) + ' line differences total' ) - def run( self, edit, with_active = False, group = -1, index = -1, compare_selections = False ): + def is_enabled( self, with_active = False, group = -1, index = -1, compare_selections = False ): + if compare_selections: + if len(self.view.sel()) == 2: + return all(map(bool, self.view.sel())) + if all(map(bool, sbs_markedSelection)): + return True + if sbs_markedSelection[1] and len(self.view.sel()) == 1 and bool(self.view.sel()[0]): + return True + return False + return True + + def run( self, edit, with_active = False, group = -1, index = -1, compare_selections = False ): global sbs_markedSelection, sbs_files active_view = self.view @@ -518,18 +529,17 @@ def compare_from_views( view1, view2 ): compare_from_views( view1, view2 ) del sbs_files[:] elif compare_selections == True: - selA = sbs_markedSelection[0] - selB = sbs_markedSelection[1] - sel = active_view.sel() - selNum = 0 - for selection in sel: - selNum += 1 - - if selNum == 2: + if len(sel) == 2: selA = active_view.substr( sel[0] ) selB = active_view.substr( sel[1] ) + else: + selA = sbs_markedSelection[0] + selB = sbs_markedSelection[1] + if not selA: + selA, selB = selB, active_view.substr( sel[0] ) + sbs_markedSelection = [ '', '' ] syntax = active_view.settings().get( 'syntax' ) create_comparison( selA, selB, syntax, 'selection A', 'selection B' )