Or you can try this version (for ST 3), which will auto invoke auto_complete
when you type the second []
.
import re
import sublime
import sublime_plugin
# Utility function for shortening text.
# From: http://www.leancrew.com/all-this/2012/08/more-markdown-reference-links-in-bbedit/
def shorten(str, n):
""" Truncate str to no more than n chars """
return str if len(str) <= n else str[: n - 1] + "…"
# Provide completions that match just after typing an opening square bracket
class MarkdownReferenceCompletions(sublime_plugin.EventListener):
working_scope = (
"text.html.markdown meta.link.reference"
" (constant.other.reference.link.markdown | punctuation.definition.constant.end.markdown)"
)
def on_post_text_command(self, view, command_name, args):
cursor = view.sel()[0].b
# auto invoke auto_complete
if view.match_selector(cursor, self.working_scope):
view.run_command("auto_complete")
def on_query_completions(self, view, prefix, locations):
cursor = locations[0]
# Only trigger within Markdown reference-style link
if not view.match_selector(cursor, self.working_scope):
return []
# Make sure we're inside the reference name portion
pt = cursor - len(prefix)
if view.substr(sublime.Region(pt - 2, pt)) != "][":
return []
reflinks = view.find_all(r"^\[([^^\]]+)\]:[ \t]+(https?://)?(.+)$")
refs = []
for r in reflinks:
m = re.match(r"^\[([^^\]]+)\]:[ \t]+(https?://)?(.+)$", view.substr(r))
if m:
refs.append(
(m.group(1) + "\t" + shorten(m.group(3), 30 - len(m.group(1))), m.group(1))
)
return (refs, sublime.INHIBIT_WORD_COMPLETIONS | sublime.INHIBIT_EXPLICIT_COMPLETIONS)