Home Download Buy Blog Forum Support

command to highlight words from a set in a visible region

command to highlight words from a set in a visible region

Postby psychomachine on Sat Aug 03, 2013 10:05 am

Hi.

I am having a hard time figuring out highlighting regions and all that. I have read the API documentation and looked at many plugins, but I still don't understand something very simple.

Let's say I have a set containing certain strings.

Code: Select all
stopWords = ['word1', 'word2', 'word3']


I would like to run a sublime command that will highlight all the words that are in this set.

Now the context in which I am working looks like this:

Code: Select all
class HighlightStopWordsCommand(sublime_plugin.TextCommand):
    def __init__(self, view):
        self.view = view
        stopWords = ['word1', 'word2', 'word3']
        self.view.add_regions("inset", ???, "comment")


but I don't know how to finish it. How can I highlight all the stopWords on running this command?

Also -- how can I make a distinction between checking the entire file and only the part of the file that the user currently sees (so that the command will be run again when the user scrolls to a different portion of the text)?

I understand both of these things are simple for those who know them... hence your tips will be greatly appreciated.

All best,
Tench
Last edited by psychomachine on Sun Aug 04, 2013 10:18 am, edited 1 time in total.
psychomachine
 
Posts: 15
Joined: Thu Jan 17, 2013 8:45 pm

Re: command to highlight words from a set

Postby skuroda on Sat Aug 03, 2013 6:04 pm

The ??? should be filled with a list of regions. Regions can be gathered in a couple different ways. There are a number of methods that return regions (view#line, view#word, etc). If you have the end points, you can manually create a region by calling "sublime.Region(start, end)"

For example, in the console enter the following (ensure that the view has some characters in it). You could also use a small plugin, but it's pretty short and isn't really necessary to demonstrate the usage of add_regions.

Code: Select all
r = sublime.Region(0, view.size())
view.add_regions("test", [r], "comment")


The entire contents of the view will be highlighted. Of course, you may use something live view#find to get the regions.
skuroda
 
Posts: 689
Joined: Fri Oct 05, 2012 8:11 am

Re: command to highlight words from a set in a visible region

Postby psychomachine on Sun Aug 04, 2013 10:17 am

This is great, thanks so much! I think I am finally beginning to understand what regions are.

A region is a set of character offsets which can then be converted to a string, which can then be split into individual words etc.

Code: Select all
r = sublime.Region(0, view.size())
text = self.view.substr(r)
words = text.split()


Now, to get only the visible region, one can do:

Code: Select all
vis = self.view.visible_region()


and proceed from there.

But I have two pieces of the puzzle missing.

1. How can I get individual words (see above) AND their individual offset points (i.e. have my cake and eat it) limited only to the visible region? I need to know individual words because I need to process them, but I need to keep an association between these individual words and the corresponding regions (offset points) so that I can edit them, replace them etc

2. How I can keep executing my command as new regions become visible, i.e. come into view. I was hoping for an event listener to that effect, but I am not finding one.

Any advice?

Many thanks in advance.

All best,
Tench
psychomachine
 
Posts: 15
Joined: Thu Jan 17, 2013 8:45 pm

Re: command to highlight words from a set in a visible region

Postby adzenith on Mon Aug 05, 2013 2:22 pm

1. You'll have to manually check to see if they're in the visible region. I believe regions support the "in" operator.
2. There is no such callback - you'll have to poll to see if the view changes.
adzenith
 
Posts: 1217
Joined: Mon Oct 19, 2009 9:12 pm

Re: command to highlight words from a set in a visible region

Postby sapphirehamster on Mon Aug 05, 2013 8:02 pm

Why do you want to set up regions for only the visible portion instead of the entire file? It seems like that will substantially complicate things with no apparent benefit (to me).
sapphirehamster
 
Posts: 91
Joined: Sun Jul 01, 2012 11:19 pm

Re: command to highlight words from a set in a visible region

Postby psychomachine on Tue Aug 06, 2013 12:33 am

thanks for you messages guys.

i have to work with visible regions only because my files are very big and i need to do a lot of stuff with words programmatically (check spelling in different languages, lemmatize, check against different dictionaries, save individual words in different files etc...) so there is no way i can work with the whole file.

i kind of got it to work but it's not pretty. it would be much, much better if find_all would give me the option to search only within a given scope. but i have learned a lot about sublime and python while working on this, it's been actually a lot of fun.

by the way, does anybody have an example of a polling function in a sublime plugin? i am having a hard time pulling one together. i need to check periodically if the values of visible_region have changed (i.e. if there was any and how much scrolling done) and then execute a thread.

if you have something or can point to a link, i'd greatly appreciate it.
psychomachine
 
Posts: 15
Joined: Thu Jan 17, 2013 8:45 pm

Re: command to highlight words from a set in a visible region

Postby adzenith on Tue Aug 06, 2013 2:02 pm

You could probably just use sublime.set_timeout, and have the function just reschedule itself every time it runs.
adzenith
 
Posts: 1217
Joined: Mon Oct 19, 2009 9:12 pm

Re: command to highlight words from a set in a visible region

Postby psychomachine on Tue Aug 06, 2013 5:15 pm

thanks a lot for your help!
psychomachine
 
Posts: 15
Joined: Thu Jan 17, 2013 8:45 pm


Return to Plugin Development

Who is online

Users browsing this forum: No registered users and 7 guests