Home Download Buy Blog Forum Support

googleIt - a quick way to look up code reference

googleIt - a quick way to look up code reference

Postby ehamiter on Thu Jun 09, 2011 6:08 pm

Inspired by EJ12N's PowerUser getOnlineHelp class

googleIt.py is a Google looker-upper. It takes the scope of the file and appends it to a Google search of either the word under the cursor or the selected text.

Image

googleIt.py
Code: Select all
import sublime
import sublime_plugin
import webbrowser


class googleItCommand(sublime_plugin.TextCommand):

    """
    This will search a word or a selection coupled with the file's
    scope. Default binding recommendation: "ctrl + alt + forward_slash"
    """

    def run(self, edit):
        if len(self.view.file_name()) > 0:
            word = self.view.substr(self.view.word(self.view.sel()[0].begin()))
            scope = self.view.scope_name(self.view.sel()[0].begin()).strip()
            getlang = scope.split('.')
            language = getlang[-1]
            sublime.status_message('googleIt invoked-- ' + 'Scope: ' + scope + \
                ' Word: ' + word + ' Language: ' + language)
            for region in self.view.sel():
                phrase = self.view.substr(region)
                search = 'http://google.com/search?q='
                # Feeling lucky? Use 'http://google.com/search?btnI=1&q=' instead
                if not region.empty():
                    webbrowser.open_new_tab(search + phrase + " " + language)
                else:
                    webbrowser.open_new_tab(search + word + " " + language)
        else:
            pass

    def is_enabled(self):
        return self.view.file_name() and len(self.view.file_name()) > 0


User key bindings
Code: Select all
{ "keys": ["ctrl+alt+forward_slash"], "command": "google_it" }
Last edited by ehamiter on Fri Jun 10, 2011 8:14 pm, edited 2 times in total.
ehamiter
 
Posts: 67
Joined: Wed Apr 27, 2011 2:42 am

Re: googleIt - a quick way to look up code reference

Postby dom111 on Thu Jun 09, 2011 6:52 pm

Nice work!

I made something similar myself, based on TextMate's look up function plugin, that checks the current scope and if it finds a match will send you to a custom page regarding that language, otherwise will Google the word/selection. I've just purloined your language lookup to use, I hope you don't mind, but I thought I'd share this with you, who knows, maybe you'll find it useful too!

You can get it here: http://dom111.co.uk/files/sublime/plugins/help_for_word.py

EDIT: I see after reading through the PowerUser script, it already did this, never mind!
dom111
 
Posts: 12
Joined: Wed Jun 08, 2011 9:13 am
Location: UK

Re: googleIt - a quick way to look up code reference

Postby iamntz on Thu Jun 09, 2011 6:55 pm

awesome, thanks!

Check out the key mapping though, you should use underscores ;)
Code: Select all
{ "keys": ["ctrl+alt+shift+f"], "command": "google_it" }
iamntz
 
Posts: 898
Joined: Fri Apr 29, 2011 8:52 am
Location: Romania

Re: googleIt - a quick way to look up code reference

Postby ehamiter on Fri Jun 10, 2011 2:20 am

iamntz wrote:awesome, thanks!

Check out the key mapping though, you should use underscores ;)
Code: Select all
{ "keys": ["ctrl+alt+shift+f"], "command": "google_it" }

Good catch, edited.
ehamiter
 
Posts: 67
Joined: Wed Apr 27, 2011 2:42 am

Re: googleIt - a quick way to look up code reference

Postby ehamiter on Fri Jun 10, 2011 2:30 am

dom111 wrote:I've just purloined your language lookup to use, I hope you don't mind, but I thought I'd share this with you, who knows, maybe you'll find it useful too!

You can get it here: http://dom111.co.uk/files/sublime/plugins/help_for_word.py

EDIT: I see after reading through the PowerUser script, it already did this, never mind!

Purloin away! I'm a novice to Python, and I had no idea you could chain together strip and split and then slice. Nifty, thanks for sharing!
ehamiter
 
Posts: 67
Joined: Wed Apr 27, 2011 2:42 am


Return to Plugin Announcements

Who is online

Users browsing this forum: Yahoo [Bot] and 6 guests