Skip to content

Instantly share code, notes, and snippets.

@deqline
Created November 26, 2022 16:34
Show Gist options
  • Save deqline/0f632ce27f7af14a278ddc6cf33b9968 to your computer and use it in GitHub Desktop.
Save deqline/0f632ce27f7af14a278ddc6cf33b9968 to your computer and use it in GitHub Desktop.
Better sublime text 3 intellitip extension (replace py in packages). Gives function description and syntax on hover.
import sublime_plugin, sublime, json, webbrowser
import re, os
from time import time
settings = {}
class IntellitipCommand(sublime_plugin.EventListener):
cache = {}
region_row = []
lang = None
completions = []
def on_activated(self, view):
Pref.time = time()
sublime.set_timeout(lambda:self.run(view, 'activated'), 0)
def on_modified(self, view):
Pref.time = time()
def on_selection_modified(self, view):
now = time()
sublime.set_timeout(lambda:self.run(view, 'selection_modified'), 0)
Pref.time = now
def on_activated(self, view):
global lang, completions
lang = self.getLang(view)
print(lang)
if lang not in self.cache: #DEBUG disable cache: or 1 == 1
path_db = os.path.dirname(os.path.abspath(__file__))+"/db/%s.json" % lang
self.debug("Loaded intelliDocs db:", path_db)
if os.path.exists(path_db):
self.cache[lang] = json.load(open(path_db))
else:
self.cache[lang] = {}
completions = self.cache[lang]
def on_hover(self, view, point, hover_zone):
global completions
if(hover_zone == 1):
region = view.word(point)
word = view.substr(region)
found = completions.get(word)
if found:
menus = ['<style>%s</style>' % Pref.css]
menus.append("<i>%s</i><br>" % found["syntax"])
for descr in re.sub("(.{100,120}[\.]) ", "\\1||", found["descr"]).split("||"): #Spit long description lines
menus.append("<br>"+descr+"<br>")
if found["params"]:
menus.append("<h3>Parameters:</h3>")
for parameter in found["params"]:
menus.append("- <b>"+parameter["name"]+":</b> "+parameter["descr"]+"<br>")
if found["path"]:
menus.append("<br><small>Defined in %s</small><br>" % found["path"])
menus.append("Type: <small>%s</small>" % found["type"])
self.appendLinks(menus, found)
view.show_popup(''.join(menus), location=region.a, max_width=600, on_navigate=self.on_navigate)
else:
view.hide_popup()
def run(self, view, where):
global region_row, lang
view_settings = view.settings()
if view_settings.get('is_widget'):
return
for region in view.sel():
region_row, region_col = view.rowcol(region.begin())
if region_row != view_settings.get('intellitip_row', -1):
view_settings.set('intellitip_row', region_row)
else:
return
def appendLinks(self, menus, found):
for pattern, link in sorted(Pref.help_links.items()):
if re.match(pattern, found["path"]):
host = re.match(".*?//(.*?)/", link).group(1)
#menus.append('<br>Open docs: <a href="%s">Docs</a>' % found["name"])
break
return menus
def getLang(self, view):
scope = view.scope_name(view.sel()[0].b) #try to match against the current scope
for match, lang in Pref.docs.items():
if re.match(".*"+match, scope): return lang
return re.match(".*/(.*?).sublime-syntax", view.settings().get("syntax")).group(1) #no match in predefined docs, return from syntax filename
def getFunctionNames(self, view, completions):
global region_row
return [view.substr(view.word(view.sel()[0]))]
def on_navigate(self, link):
global lang
webbrowser.open_new_tab(Pref.help_links[lang.lower()] % link)
def debug(self, *text):
if Pref.debug:
print(*text)
def init_css():
css_file = 'Packages/' + Pref.css_file
try:
Pref.css = sublime.load_resource(css_file).replace('\r', '')
except:
Pref.css = None
settings.clear_on_change('reload')
settings.add_on_change('reload', 'init_css')
def plugin_loaded():
global Pref, settings
class Pref:
def load(self):
settings = sublime.load_settings("intellitip.sublime-settings")
Pref.wait_time = 0.12
Pref.time = time()
Pref.css_file = settings.get('css_file', "Intellitip/css/default.css")
Pref.help_links = settings.get('help_links', [])
Pref.docs = settings.get('docs', None)
Pref.debug = settings.get('debug', False)
Pref.css = None
if(Pref.docs != None):
print("Intellitip Loaded successfully!")
settings = sublime.load_settings("intellitip.sublime-settings")
Pref = Pref()
Pref.load()
init_css()
settings.add_on_change('reload', lambda:Pref.load())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment