-
-
Save wojtha/5125812 to your computer and use it in GitHub Desktop.
Command for Sublime Text 2 - Adds YARD documentation to method. Supports current line or selection.
This file contains hidden or 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
# Add YARD documentation to method. | |
# | |
# Supports current line or selection. | |
# | |
# Useful references: | |
# http://www.sublimetext.com/docs/2/api_reference.html | |
# http://net.tutsplus.com/tutorials/python-tutorials/how-to-create-a-sublime-text-2-plugin/ | |
# http://www.pythonregex.com/ | |
# | |
# Key binding: | |
# { "keys": ["ctrl+shift+y"], "command": "add_yard_docs"}, | |
import sublime | |
import sublime_plugin | |
import re | |
class AddYardDocsCommand(sublime_plugin.TextCommand): | |
def run(self, edit): | |
for region in self.view.sel(): | |
if region.empty(): | |
line = self.view.line(region) | |
line_contents = self.view.substr(line) | |
yard = self.parse_line(line_contents, edit) | |
if yard: | |
self.view.insert(edit, line.begin(), yard) | |
else: | |
for line in reversed(self.view.lines(region)): | |
line_contents = self.view.substr(line) | |
yard = self.parse_line(line_contents, edit) | |
if yard: | |
self.view.insert(edit, line.begin(), yard) | |
def parse_line(self, line, edit): | |
regex = re.compile("(?P<intend>\s+)def (?P<method>[\w_.=!?<>]+)\(?(?P<params>[^)]+)\)?", re.UNICODE) | |
match = regex.search(line) | |
if match: | |
intend = match.group(1) | |
params = [p.strip() for p in match.group(3).split(',')] | |
lines = ["%s# @param [] %s" % (intend, param) for param in params] | |
lines.insert(0, intend + "# TODO: Write description\n" + intend + "#") | |
lines.append(intend + "# @return ") | |
return "\n".join(lines) + "\n" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment