Created
June 20, 2012 15:44
-
-
Save rodrigoamaral/2960571 to your computer and use it in GitHub Desktop.
Sublime Text plugin to insert docstrings for all function/method definitions in a Python file
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
import sublime, sublime_plugin | |
class AddPythonDocstringsCommand(sublime_plugin.TextCommand): | |
DEF_KEYWORD = 'def' | |
DEF_PATTERN = '%s\s+' % DEF_KEYWORD | |
def _find_function_region(self, start): | |
return self.view.find(self.DEF_PATTERN, start) | |
def insert_docstrings(self, edit): | |
start = 0 | |
region = self._find_function_region(start) | |
while region: | |
line = self.view.line(region) | |
line_str = self.view.substr(line) | |
indent = '\n\t%s' % line_str[0:line_str.find(self.DEF_KEYWORD)] | |
line_tokens = line_str.split() | |
if line_tokens[0] == self.DEF_KEYWORD: | |
function_name = line_tokens[1][0:line_tokens[1].find('(')].strip() | |
docstring = '%s""" TODO: write documentation for %s.%s"""' % (indent, function_name, indent * 2) | |
self.view.insert(edit, line.end(), docstring) | |
start = region.end() | |
region = self._find_function_region(start) | |
def run(self, edit): | |
self.insert_docstrings(edit) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment