Sublime Forum

googleIt - a quick way to look up code reference

#1

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.

googleIt.py

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

{ "keys": "ctrl+alt+forward_slash"], "command": "google_it" }
0 Likes

HelpIt Plugin - Based off GoogleIt
HelpIt Plugin - Based off GoogleIt
#2

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!

0 Likes

#3

awesome, thanks!

Check out the key mapping though, you should use underscores :wink:

{ "keys": "ctrl+alt+shift+f"], "command": "google_it" }
0 Likes

#4

[quote=“iamntz”]awesome, thanks!

Check out the key mapping though, you should use underscores :wink:

{ "keys": "ctrl+alt+shift+f"], "command": "google_it" }

Good catch, edited.

0 Likes

#5

[quote=“dom111”]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![/quote]

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!

0 Likes