Skip to content

Commit

Permalink
Enhance comparing arbitrary selections
Browse files Browse the repository at this point in the history
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.)
  • Loading branch information
kaste committed Mar 3, 2024
1 parent c0ac92c commit ad8df2a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Context.sublime-menu
Original file line number Diff line number Diff line change
Expand Up @@ -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": "-" }
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
28 changes: 19 additions & 9 deletions sbs_compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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' )
Expand Down

0 comments on commit ad8df2a

Please sign in to comment.