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 9ea6ca8
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions ReText/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ def documentIndentLess(document, cursor, globalSettings=globalSettings):
class ReTextEdit(QTextEdit):
resized = pyqtSignal(QRect)
scrollLimitReached = pyqtSignal(QWheelEvent)
returnBlockPattern = re.compile("^[\\s]*([*>-]|\\d+\\.) ")
orderedListPattern = re.compile("^([\\s]*)(\\d+)\\. $")

def __init__(self, parent):
QTextEdit.__init__(self)
Expand Down Expand Up @@ -242,15 +244,23 @@ 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()
match = self.returnBlockPattern.search(text)
if match is not None:
matchedText = match.group(0)
if len(matchedText) == length:
cursor.removeSelectedText()
matchedText = ''
else:
matchOL = self.orderedListPattern.match(matchedText)
if matchOL is not None:
matchedPrefix = matchOL.group(1)
matchedNumber = int(matchOL.group(2))
matchedText = matchedPrefix + str(matchedNumber + 1) + ". "
else:
matchedText = ''
# Reset the cursor
cursor = self.textCursor()
cursor.insertText(('\n' + text[:pos]) if pos < length else '\n')
cursor.insertText('\n' + matchedText)
self.ensureCursorVisible()

def lineNumberAreaWidth(self):
Expand Down

0 comments on commit 9ea6ca8

Please sign in to comment.