Sublime Forum

NavigateSelections

#1

A small but surprisingly useful (for me) plugin for navigating between selections:
It scroll the view up/down to show the next selections.

Very useful after a Find All to check if the selections is what you have expected.
Enjoy!

[code]import sublime, sublime_plugin

class NavigateSelectionsCommand(sublime_plugin.WindowCommand):
def run(self, forward=True):
sels = self.window.active_view().sel()
if len(sels) == 0:
return

    visible_region = self.window.active_view().visible_region()
    if forward:
        iterator = reversed(sels)
    else:
        iterator = sels
    next_sel = None
    for s in iterator:
        if visible_region.contains(s):
            break
        next_sel = s

    if next_sel is None:
        if forward:
            next_sel = sels[0]
        else:
            next_sel = sels-1]
    self.window.active_view().show(next_sel, True)[/code]

//Navigate Selections { "keys": "super+alt+down"], "command": "navigate_selections", "args": {"forward": true} }, { "keys": "super+alt+up"], "command": "navigate_selections", "args": {"forward": false} }

0 Likes

Focus follows cursor?
#2

this is useful.
thanks.

0 Likes

#3

A small fix and ST3 compatible:

[code]import sublime_plugin

class NavigateSelectionsCommand(sublime_plugin.WindowCommand):
“”“Scroll the view up/down to the next selections”""
def run(self, forward=True, wrap=True):
if not self.window.active_view():
return

    sels = self.window.active_view().sel()
    if len(sels) == 0:
        return

    visible_region = self.window.active_view().visible_region()
    if forward:
        iterator = reversed(sels)
    else:
        iterator = sels

    next_sel = None
    for s in iterator:
        if visible_region.intersects(s):
            break
        next_sel = s

    if next_sel is None and wrap:
        if forward:
            next_sel = sels[0]
        else:
            next_sel = sels[len(sels) - 1]

    if next_sel is not None:
        self.window.active_view().show(next_sel, True)[/code]
0 Likes

#4

Console told me that command: navigate_selections {“forward”: true} or command: navigate_selections {“forward”: false} when i pressed keys

but selection not go to next or previous

what am doing wrong?

/Users/z/Library/Application Support/Sublime Text/Packages/User/NavigateSelections.py

Plugin:

import sublime_plugin

class NavigateSelectionsCommand(sublime_plugin.WindowCommand):
# “”“Scroll the view up/down to the next selections“”“
    def run(self, forward=True, wrap=True):
        if not self.window.active_view():
            return
            
            sels = self.window.active_view().sel()
            if len(sels) == 0:
                return
            
            visible_region = self.window.active_view().visible_region()
            if forward:
                iterator = reversed(sels)
            else:
                iterator = sels
            
            next_sel = None
            for s in iterator:
                if visible_region.intersects(s):
                    break
                next_sel = s
            
            if next_sel is None and wrap:
                if forward:
                    next_sel = sels[0]
                else:
                    next_sel = sels[len(sels) - 1]
            
            if next_sel is not None:
                self.window.active_view().show(next_sel, True)

~/Library/Application Support/Sublime Text/Packages/User/Default (OSX).sublime-keymap
Hotkeys

{ “keys”: [“super+alt+down”], “command”: “navigate_selections”, “args”: {“forward”: true} },
{ “keys”: [“super+alt+up”], “command”: “navigate_selections”, “args”: {“forward”: false} },

0 Likes

#5

some kind of mistake in formatting this working:

thanks for plugin

class NavigateSelectionsCommand(sublime_plugin.WindowCommand):
    def run(self, forward=True):
        sels = self.window.active_view().sel()
        if len(sels) == 0:
            return

        visible_region = self.window.active_view().visible_region()
        if forward:
            iterator = reversed(sels)
        else:
            iterator = sels
        next_sel = None
        for s in iterator:
            if visible_region.intersects(s):
                break
            next_sel = s
    
        if next_sel is None and wrap:
            if forward:
                next_sel = sels[0]
            else:
                next_sel = sels[len(sels) - 1]
    
        if next_sel is not None:
            self.window.active_view().show(next_sel, True)
0 Likes

#6

I was getting an error. It seems you need to define the default value for wrap. I am not a Python programmer, though.

0 Likes

#7

thanks but now its working, error was in formating i was post working code

0 Likes