Skip to content

Commit

Permalink
editor: Optimize LineNumberArea painting for large documents
Browse files Browse the repository at this point in the history
block.isVisible() is always True, check the rectangle to find out
if it is really in the viewport.

Break the loop if the block is below the viewport.

Move some common variables outside the loop.

Refs #360.
  • Loading branch information
mitya57 committed Jul 13, 2018
1 parent e4dfefa commit 3fe1c5f
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions ReText/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,21 +457,25 @@ def paintEvent(self, event):
return QWidget.paintEvent(self, event)
painter = QPainter(self)
painter.fillRect(event.rect(), colorValues['lineNumberArea'])
painter.setPen(colorValues['lineNumberAreaText'])
cursor = QTextCursor(self.editor.document())
cursor.movePosition(QTextCursor.Start)
atEnd = False
fontHeight = self.fontMetrics().height()
height = self.editor.height()
if globalSettings.relativeLineNumbers:
relativeTo = self.editor.textCursor().blockNumber()
else:
relativeTo = -1
while not atEnd:
rect = self.editor.cursorRect(cursor)
block = cursor.block()
if block.isVisible():
if rect.top() >= height:
break
elif rect.bottom() > 0:
number = str(cursor.blockNumber() - relativeTo).replace('-', '−')
painter.setPen(colorValues['lineNumberAreaText'])
painter.drawText(0, rect.top(), self.width() - 2,
self.fontMetrics().height(), Qt.AlignRight, number)
fontHeight, Qt.AlignRight, number)
cursor.movePosition(QTextCursor.EndOfBlock)
atEnd = cursor.atEnd()
if not atEnd:
Expand Down

0 comments on commit 3fe1c5f

Please sign in to comment.