Sublime Forum

HelpIt Plugin - Based off GoogleIt

#1

I’ve made a modified version of the GoogleIt plugin, allowing you to specify different search options for different language scopes (for example search PHP.net for PHP file). The original GoogleIt plugin is here: googleIt - a quick way to look up code reference

[code]
#Plugin inspired and modified from googleIt - a quick way to look up code reference

import sublime
import sublime_plugin
import webbrowser
class helpItCommand(sublime_plugin.TextCommand):

"""
This will search a word in a language's documenation or google it with it's scope otherwise
"""

def run(self, edit):
    if len(self.view.file_name()) > 0:
        settings = sublime.load_settings('HelpIt.sublime-settings');
        item = None
        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]
        if language == 'basic':
            language = getlang-2]
        
        if language == 'html': #HTML shows up A LOT for internal CSS, PHP and JS
            if 'php' in getlang:
                language = 'php'
            elif 'js' in getlang:
                language = 'js'
            elif 'css' in getlang:
                language = 'css'

        #Map languages if needed. For example: Map .less files to .css searches
        if settings.has(language):
            item = settings.get(language)
            
        if item != None and 'map' in item:
            language = item'map']

        sublime.status_message('helpIt invoked-- ' + 'Scope: ' + scope + \
            ' Word: ' + word + ' Language: ' + language)
        for region in self.view.sel():
            phrase = self.view.substr(region)
            search = 'http://google.com/search?q='
            
            custom = False

            if item != None:
                if type(item) == unicode:
                    search = item
                    custom = True
                elif 'url' in item:
                    search = item'url']   
                    custom = True

            if not region.empty():
                webbrowser.open_new_tab(search + phrase + "" if custom else " " + language)
            else:
                webbrowser.open_new_tab(search + word + "" if custom else " " + language)
    else:
        pass

def is_enabled(self):
    return self.view.file_name() and len(self.view.file_name()) > 0[/code]

And the HelpIt.sublime-settings file looks like this at the moment:

{ "php" : { "url" : "http://php.net/" }, "less" : { "map" : "css" } }

The map property allows you to make a language use the url of another or use it’s scope name in the googleIt search (I have lesscss.tmLanguage package so I’ve set it to use css in the google search). Hope this is helpful to someone (this is my first attempt at a plugin and any real python, so please excuse any stupid mistakes). Oh my key binding is Alt+F1 (from Notepad++):

{ "keys": "alt+f1"], "command": "help_it" }

Edit: Forgot to mention. If you just supply a string for a language in the options file rather than an object, it’ll use that string as the search url. A good one for HTML that I like is "“http://www.google.com/search?btnI=1&q=site:w3schools.com/html5+tag+” to search any tag within the html5 spec.

0 Likes

#2

Improved the code a bit so the urls specified now use the %s string formatting thing (I don’t know it’s official name). As well as having sub-rules in each language to search for library specific entities, and also remove extra information (I’ve used it for jQuery look ups)

[code]#Plugin inspired and modified from googleIt - a quick way to look up code reference

import sublime
import sublime_plugin
import webbrowser
import re
class helpItCommand(sublime_plugin.TextCommand):

"""
This will search a word in a language's documenation or google it with it's scope otherwise
"""

def run(self, edit):
    if len(self.view.file_name()) > 0:
        settings = sublime.load_settings('HelpIt.sublime-settings');
        item = None
        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]
        if language == 'basic':
            language = getlang-2]
        
        if language == 'html': #HTML shows up A LOT for internal CSS, PHP and JS
            if 'php' in getlang:
                language = 'php'
            elif 'js' in getlang:
                language = 'js'
            elif 'css' in getlang:
                language = 'css'

        #Map languages if needed. For example: Map .less files to .css searches
        if settings.has(language):
            item = settings.get(language)
            if 'map' in item:
                language = item'map']

        sublime.status_message('helpIt invoked-- ' + 'Scope: ' + scope + ' Word: ' + word + ' Language: ' + language)
        for region in self.view.sel():
            phrase = self.view.substr(region)
            search = 'http://google.com/search?q=%s'                
            custom = False 

            #Define our search term
            if not region.empty():
                term = phrase
            else:
                term = word

            if item != None:
                if 'sub' in item: #check for sub searches based on our term
                    subs = item'sub']
                    for sub in subs:
                        if 'contains' in sub and 'url' in sub: #Make sure we have everything
                            if term.count(sub'contains']):
                                if 'remove' in sub:
                                    term = re.sub(sub'remove'], '', term)
                                search = sub'url']
                                custom = True
                                break

                if not custom:
                    if type(item) == unicode:
                        search = item
                        custom = True
                    elif 'url' in item:
                        search = item'url']   
                        custom = True

            if not custom:
                term += " " + language

           try:
                search = search % (term)
            except TypeError:
                print "No replacements"

            #Search our terms
            webbrowser.open_new_tab(search)
    else:
        pass

def is_enabled(self):
    return self.view.file_name() and len(self.view.file_name()) > 0[/code]

The config file I’ve tested with:

{ "php" : { "url" : "http://php.net/%s" }, "less" : { "map" : "css" }, "js" : { "sub" : { "contains" : "$", "url" : "http://api.jquery.com/%s", "remove" : "\\$.*\\." }] }, "html" : "http://www.google.com/search?btnI=1&q=site:w3schools.com/html5+tag+%s", "plain": "http://answers.com/%s" }

The remove property is just a regular expression. This one basically removes everything up the last function ($(element).click().children() will return children(). Because this only searches for a $, if you name any jQuery variables like so $myElement it’ll still do the search.

0 Likes