Skip to content

Commit

Permalink
Handle return in ordered lists context
Browse files Browse the repository at this point in the history
This allows to continue an ordered list by pressing return, the same way
unordered lists and quote blocks are handled.

I changed the detection to use a regex that might ease other supports
  • Loading branch information
xgouchet committed Sep 21, 2017
1 parent dd51530 commit 4dc8677
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions ReText/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,15 +242,25 @@ def handleReturn(self, cursor):
cursor.movePosition(QTextCursor.StartOfBlock, QTextCursor.KeepAnchor)
text = cursor.selectedText()
length = len(text)
pos = 0
while pos < length and (text[pos] in (' ', '\t')
or text[pos:pos+2] in ('* ', '- ', '> ')):
pos += 1
if pos == length:
cursor.removeSelectedText()
# Reset the cursor
cursor = self.textCursor()
cursor.insertText(('\n' + text[:pos]) if pos < length else '\n')
match = re.search("^[\\s]*([*>-]|\\d+\\.) ", text)
if match is not None:
matchedText = match.group(0)
if len(matchedText) == length:
cursor.removeSelectedText()
matchedText = ''
else :
matchOL = re.match("^([\\s]*)(\\d+)\\. $", matchedText)
if matchOL is not None:
matchedPrefix = matchOL.group(1)
matchedNumber = int(matchOL.group(2))
matchedText = matchedPrefix + str(matchedNumber + 1) + ". "
# Reset the cursor
cursor = self.textCursor()
cursor.insertText('\n' + matchedText)
else :
# Reset the cursor
cursor = self.textCursor()
cursor.insertText('\n')
self.ensureCursorVisible()

def lineNumberAreaWidth(self):
Expand Down

0 comments on commit 4dc8677

Please sign in to comment.