Skip to content

Instantly share code, notes, and snippets.

@SmileyChris
Last active November 19, 2019 18:56
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SmileyChris/4340807 to your computer and use it in GitHub Desktop.
Save SmileyChris/4340807 to your computer and use it in GitHub Desktop.
Better wrapping of docstring text with Sublime Text 2. Run: patch ~/.config/sublime-text-2/Packages/Default/paragraph.py < paragraph.diff

To install in Sublime Text 3, you'll need to extract the py file from the default package before patching it.

Here are short instructions to do it in Linux. First change to the directory that you have sublime text, unzip the file, and patch it:

cd /opt/sublime_text
unzip Packages/Default.sublime-package paragraph.py -d ~/.config/sublime-text-3/Packages/default
curl https://gist.githubusercontent.com/SmileyChris/4340807/raw/paragraph.diff | patch ~/.config/sublime-text-3/Packages/default/paragraph.py

Or if you've downloaded the diff, you can just patch it from that rather than `curl`ing:

patch ~/.config/sublime-text-3/Packages/default/paragraph.py < paragraph.diff
--- paragraph.py.orig 2011-03-29 05:55:24.000000000 +1300
+++ paragraph.py 2012-06-20 11:22:19.573602412 +1200
@@ -20,11 +20,21 @@
return view.full_line(sr.end())
-separating_line_pattern = re.compile("^[\\t ]*\\n?$")
+separating_line_pattern = re.compile(r"^[\t ]*(\"{3}|'{3})?\n?$")
+
def is_paragraph_separating_line(view, sr):
return separating_line_pattern.match(view.substr(sr)) != None
+def docstring_definition(view, sr, definition=None):
+ if definition:
+ pattern = r'({0})\s*(#.*)?$'.format(re.escape(definition))
+ else:
+ pattern = r"^\s*(\"{3}|'{3})"
+ match = re.search(pattern, view.substr(view.line(sr.begin())))
+ if match:
+ return match.group(1)
+
def has_prefix(view, line, prefix):
if not prefix:
return True
@@ -53,9 +63,14 @@
required_prefix = view.substr(sublime.Region(sr.begin(), comment_region.end()))
break
+ docstring = None
+
first = sr.begin()
prev = sr
while True:
+ docstring = docstring_definition(view, prev)
+ if docstring:
+ break
prev = previous_line(view, prev)
if (prev == None or is_paragraph_separating_line(view, prev) or
not has_prefix(view, prev, required_prefix)):
@@ -66,6 +81,8 @@
last = sr.end()
next = sr
while True:
+ if docstring and docstring_definition(view, next, docstring):
+ break
next = next_line(view, next)
if (next == None or is_paragraph_separating_line(view, next) or
not has_prefix(view, next, required_prefix)):
@@ -125,6 +142,11 @@
prefix = self.view.substr(sublime.Region(lines[0].begin(),
lines[0].begin() + initial_prefix_match.end()))
+ # Strip any leading docstring from the prefix.
+ prefix = re.sub(r"^(\s*)(\"{3}|'{3})\s*", r"\1", prefix)
+ if not prefix:
+ return None
+
for line in lines[1:]:
if self.view.substr(sublime.Region(line.begin(),
line.begin() + len(prefix))) != prefix:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment