Created
September 4, 2012 18:50
-
-
Save Westacular/3624923 to your computer and use it in GitHub Desktop.
Sublime autocomplete plugin for Markdown reference links
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: UTF-8 -*- | |
import sublime, sublime_plugin | |
import re | |
# 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] + u'…' | |
# Provide completions that match just after typing an opening square bracket | |
class MarkdownReferenceCompletions(sublime_plugin.EventListener): | |
def on_query_completions(self, view, prefix, locations): | |
# Only trigger within Markdown reference-style link | |
if not view.match_selector(locations[0], | |
"text.html.markdown meta.link.reference constant.other.reference.link.markdown, " | |
"text.html.markdown meta.link.reference punctuation.definition.constant.end.markdown"): | |
return [] | |
pt = locations[0] - len(prefix) | |
ch = view.substr(sublime.Region(pt - 2, pt)) | |
# Make sure we're inside the reference name portion | |
if ch != '][': | |
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment