|
7 | 7 | from cola import cmds
|
8 | 8 | from cola import qtutils
|
9 | 9 | from cola.cmds import run
|
10 |
| -from cola.widgets import defs |
11 | 10 | from cola.widgets.text import DiffTextEdit
|
12 | 11 |
|
13 | 12 |
|
14 | 13 | class DiffEditor(DiffTextEdit):
|
| 14 | + |
15 | 15 | def __init__(self, parent):
|
16 | 16 | DiffTextEdit.__init__(self, parent)
|
17 | 17 | self.model = model = cola.model()
|
@@ -143,13 +143,40 @@ def setPlainText(self, text):
|
143 | 143 | highlight = (self.mode != self.model.mode_none and
|
144 | 144 | self.mode != self.model.mode_untracked)
|
145 | 145 | self.highlighter.set_enabled(highlight)
|
| 146 | + |
146 | 147 | scrollbar = self.verticalScrollBar()
|
147 | 148 | if scrollbar:
|
148 | 149 | scrollvalue = scrollbar.value()
|
149 |
| - if text is not None: |
150 |
| - DiffTextEdit.setPlainText(self, text) |
151 |
| - if scrollbar: |
152 |
| - scrollbar.setValue(scrollvalue) |
| 150 | + else: |
| 151 | + scrollvalue = None |
| 152 | + |
| 153 | + if text is None: |
| 154 | + return |
| 155 | + |
| 156 | + offset, selection = self.offset_and_selection() |
| 157 | + old_text = unicode(self.toPlainText()) |
| 158 | + |
| 159 | + DiffTextEdit.setPlainText(self, text) |
| 160 | + |
| 161 | + # If the old selection exists in the new text then |
| 162 | + # re-select it. |
| 163 | + if selection and selection in text: |
| 164 | + idx = text.index(selection) |
| 165 | + cursor = self.textCursor() |
| 166 | + cursor.setPosition(idx) |
| 167 | + cursor.setPosition(idx + len(selection), |
| 168 | + QtGui.QTextCursor.KeepAnchor) |
| 169 | + self.setTextCursor(cursor) |
| 170 | + |
| 171 | + # Otherwise, if the text is identical and there |
| 172 | + # is no selection then restore the cursor position. |
| 173 | + elif text == old_text: |
| 174 | + cursor = self.textCursor() |
| 175 | + cursor.setPosition(offset) |
| 176 | + self.setTextCursor(cursor) |
| 177 | + |
| 178 | + if scrollbar and scrollvalue is not None: |
| 179 | + scrollbar.setValue(scrollvalue) |
153 | 180 |
|
154 | 181 | def offset_and_selection(self):
|
155 | 182 | cursor = self.textCursor()
|
|
0 commit comments