Skip to content

Instantly share code, notes, and snippets.

@linkarys
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save linkarys/9701541 to your computer and use it in GitHub Desktop.
Save linkarys/9701541 to your computer and use it in GitHub Desktop.
sublime plugin notes
### Insert text to new file
### ----------------------------------------------------
### ====================================================
import sublime
import sys
view = sublime.active_window().new_file()
def insert_text(view):
if sys.version > '3':
view.run_command('append', {'characters': content })
else:
edit = view.begin_edit()
view.insert(edit, 0, content)
view.end_edit(edit)
def write(view, str):
view.run_command(
'insert_snippet', {
'contents': str
}
)
### Set Syntax
### ----------------------------------------------------
### ====================================================
import os
def set_syntax(language, view):
if language == 'C':
new_syntax = os.path.join('C++', "{0}.tmLanguage".format(language))
else:
new_syntax = os.path.join(language, "{0}.tmLanguage".format(language))
new_syntax_path = os.path.join('Packages', new_syntax)
if sublime.platform() == 'windows':
new_syntax_path = new_syntax_path.replace('\\', '/')
try:
sublime.load_resource(new_syntax_path)
view.set_syntax_file(new_syntax_path)
except:
print ('Syntax file for ' + new_syntax + ' does not exist at ' + new_syntax_path)
### Get Syntax
### ----------------------------------------------------
### ====================================================
def get_scope_name(view):
scope = view.scope_name(view.sel()[0].end())
res = re.search('\\bsource\\.([a-z+\-]+)', scope)
return res.group(1) if res else None
def get_syntax(view):
return view.settings().get('syntax')
### Get Word
### ----------------------------------------------------
### ====================================================
def get_word(view):
return view.substr(view.word( view.sel()[0] ))
### HTML Parser
### ----------------------------------------------------
### ====================================================
class MyHTMLParser(HTMLParser):
def handle_starttag(self, tag, attrs):
print("Start tag:", tag)
for attr in attrs:
print(" attr:", attr)
def handle_endtag(self, tag):
print("End tag :", tag)
def handle_data(self, data):
print("Data :", data)
def handle_comment(self, data):
print("Comment :", data)
def handle_entityref(self, name):
pass
def handle_charref(self, name):
if name.startswith('x'):
c = chr(int(name[1:], 16))
else:
c = chr(int(name))
print("Num ent :", c)
def handle_decl(self, data):
print("Decl :", data)
def get_result(self):
return self.data
### Log Commands
### ----------------------------------------------------
### ====================================================
sublime.log_commands(True)
### Parse Commands
### ----------------------------------------------------
### ====================================================
class fn:
# match pattern like google lan:en key:python other:ot
full_arg = re.compile(r'^[\w]+([^\S\n]+\w+[^\S\n]*:[^\S\n]*\w+)+[^\S\n]*$')
# match pattern like google lan:en key:python other:ot
short_arg = re.compile(r'^[\w]+([^\S\n]+\w+)+[^\S\n]*$')
# match pattern like php
single_word = re.compile(r'^[^\S\n][\w]+[^\S\n]*$')
@staticmethod
def get_url(input):
settings = sublime.load_settings("QuickDocsLauncher.sublime-settings")
s_patterns = settings.get('search_patterns')
arg_pair = re.compile(r'^(\w+):([\w\d.]+)$')
words = input.split()
syntax = words.pop(0)
def repl_fun(key, val):
def repl(matchobj):
if matchobj.group(1) == key:
return str(val)
return matchobj.group(0)
return repl
def fillin_defaul(matchobj):
return matchobj.group(2)
try:
s_pattern = s_patterns[syntax]['pattern']
reg_repl = re.compile(r'\$\{\s*([\w\d+]+)\s*:([^\}]+)\}')
keyword = []
for word in words:
match = arg_pair.match(word)
if match:
key = match.group(1)
val = match.group(2)
print (val)
repl = repl_fun(key, val)
s_pattern = reg_repl.sub(repl, s_pattern)
else:
keyword.append(word)
s_pattern = reg_repl.sub(fillin_defaul, s_pattern)
return s_pattern + ' '.join(keyword)
except:
return settings.get('default', 'https://www.google.com/search?q=') + input
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment