Visible region

Visible region

Postby gpfsmurf on Tue Mar 17, 2009 6:01 pm

Is there a way to get the currently visible region?

What I'm trying to do is to filter the current selection so that the selected text that is not currently visible to the user is discarded from the selection.
gpfsmurf
 
Posts: 181
Joined: Mon Jun 23, 2008 6:31 pm

Re: Visible region

Postby sublimator on Wed Mar 18, 2009 12:23 am

Interesting.... what for?
sublimator
 
Posts: 1123
Joined: Thu Mar 20, 2008 5:41 am

Re: Visible region

Postby gpfsmurf on Wed Mar 18, 2009 5:19 pm

I extensively use Alt-F3 for quick search & replace.

However, it can sometimes select some unwanted text somewhere far away in the same file. I can usually see it in the minimap so it's not error-inducing but it means I need to select the region I want and perform a normal search/replace.

I figured using Shift-Alt-F3 to perform a quick find on the visible part of the file might be handy, although I must admit I won't know for sure until I actually try it
gpfsmurf
 
Posts: 181
Joined: Mon Jun 23, 2008 6:31 pm

Re: Visible region

Postby sublimator on Wed Mar 18, 2009 10:44 pm

I like that idea

For that sort of situation I generally create a selection of the area I want to search, then use the find panel, make sure `search in selection` is set (generally it infers that you are wanting to do this) then type in the word and press enter to get all the selections.

Not as fast as the work flow you have in mind but you are sure to only get the selections you want.

With the Python API as it is I am pretty sure there is no way you can get the visible area as a Region.

For the moment, maybe you could make a plugin where you ctrl mouse click the extents as 2 empty selections, then the search as a 3rd full selection.

Search between bookmarks if there is only one selection? Set up search areas.
sublimator
 
Posts: 1123
Joined: Thu Mar 20, 2008 5:41 am

Re: Visible region

Postby gpfsmurf on Thu Mar 19, 2009 5:20 pm

Search between bookmarks if there is only one selection? Set up search areas.

Sounds good but I don't see anything about bookmarks in the API documentation.
In the same line of thinking, the search could be done over the current syntax scope (ctrl-shift-space) or the current bracket scope (ctrl-shift-m), but those scopes are not always useful. A 'current function scope' would be nicer.

For the moment, maybe you could make a plugin where you ctrl mouse click the extents as 2 empty selections, then the search as a 3rd full selection.

Sounds like a fair alternative... I'll try that.
The two outer selections must be empty and will delimit the search area.
The middle selection will be used as a search pattern; if it's an empty selection, the whole word will be used.
gpfsmurf
 
Posts: 181
Joined: Mon Jun 23, 2008 6:31 pm

Re: Visible region

Postby sublimator on Thu Mar 19, 2009 9:41 pm

Sounds good but I don't see anything about bookmarks in the API documentation.


You'd have to store the selection, `runCommand('prevBookmark')`, store the selection, `runCommand('nextBookmark')` and so on....

I think it could work...
sublimator
 
Posts: 1123
Joined: Thu Mar 20, 2008 5:41 am

Re: Visible region

Postby sublimator on Thu Mar 19, 2009 10:18 pm

Code: Select all
def find_all(view, search, start, end, flags=0):
    regions = [sublime.Region(start, start)]

    while True:
        regions.append(view.find(search, regions[-1].end(), flags))
        if not regions[-1] or regions[-1].begin() > end:
            break   

    return regions[1:-1]

class SearchBetweenBookmarks(sublimeplugin.TextCommand):
    def run(self, view, args):
        if len(view.sel()) == 3:
            start, search_sel, end = view.sel()
        else:
            search_sel = view.sel()[0]
           
            view.runCommand('prevBookmark')
            start = view.sel()[0]
           
            view.runCommand('nextBookmark')
            end = view.sel()[-1]
       
        search = view.substr(search_sel or view.word(search_sel))
        if search == view.substr(view.word(search_sel)):
            search = r'\b%s\b' % search       

        finds = find_all(view, search, start.begin(), end.end())

        view.sel().clear()
        for sel in [search_sel] + finds:
            view.sel().add(sel)

Last edited by sublimator on Thu Mar 19, 2009 11:10 pm, edited 2 times in total.
sublimator
 
Posts: 1123
Joined: Thu Mar 20, 2008 5:41 am

Re: Visible region

Postby sublimator on Thu Mar 19, 2009 10:45 pm

If 3 selections then use first and last as search extents, falling back to bookmarks.

Seems to work :)
sublimator
 
Posts: 1123
Joined: Thu Mar 20, 2008 5:41 am

Re: Visible region

Postby sublimator on Thu Mar 19, 2009 11:13 pm

Code: Select all
class BookmarkArea(sublimeplugin.TextCommand):
    def run(self, view, args):
        sels = view.sel()
        start = view.sel()[0].begin()
        end = view.sel()[-1].end()

        sels.clear()

        for pos in (start, end):
            view.sel().add(sublime.Region(pos, pos))
            view.runCommand('toggleBookmark')
            view.sel().clear()
           
        sels.add(sublime.Region(start, start))


A bookmark area function
sublimator
 
Posts: 1123
Joined: Thu Mar 20, 2008 5:41 am

Re: Visible region

Postby sublimator on Thu Mar 19, 2009 11:23 pm

Full (updated) code: http://pastie.org/421590

Will put it on the repo at some point..
sublimator
 
Posts: 1123
Joined: Thu Mar 20, 2008 5:41 am

Next

Return to Plugin Development

Who is online

Users browsing this forum: No registered users and 1 guest