Skip to content

Commit 4e8e7ce

Browse files
committed
fix bug where tab on first line inserted newline
1 parent 6b5ec0b commit 4e8e7ce

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

QCodeEditor.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,14 @@ def isStart(self):
252252
return True
253253
return False
254254

255+
def replaceBlockAtCursor(self, newText):
256+
cursor=self.textCursor()
257+
cursor.select(QTextCursor.BlockUnderCursor)
258+
if cursor.selectionStart() != 0:
259+
newText = "\n" + newText
260+
cursor.removeSelectedText()
261+
cursor.insertText(newText)
262+
255263
def keyPressEvent(self, event):
256264
if event.key() == Qt.Key_Backtab and self.textCursor().hasSelection():
257265
startCursor = self.textCursor()
@@ -301,9 +309,7 @@ def keyPressEvent(self, event):
301309
self.completionState = 0
302310
cursor = self.textCursor()
303311
cursor.beginEditBlock()
304-
cursor.select(QTextCursor.BlockUnderCursor)
305-
cursor.removeSelectedText()
306-
cursor.insertText("\n" + self.origText)
312+
self.replaceBlockAtCursor(self.origText)
307313
cursor.endEditBlock()
308314
self.origText == None
309315
return
@@ -318,22 +324,16 @@ def keyPressEvent(self, event):
318324
if self.completionState == 0:
319325
self.origText = self.textCursor().block().text()
320326
if self.completionState > 0:
321-
cursor.select(QTextCursor.BlockUnderCursor)
322-
cursor.removeSelectedText()
323-
cursor.insertText("\n" + self.origText)
327+
self.replaceBlockAtCursor(self.origText)
324328
newText = self.completer.complete(self.origText, self.completionState)
325329
if newText:
326330
if newText.find("(") > 0:
327331
newText = newText[0:newText.find("(") + 1]
328332
self.completionState += 1
329-
cursor.select(QTextCursor.BlockUnderCursor)
330-
cursor.removeSelectedText()
331-
cursor.insertText("\n" + newText)
333+
self.replaceBlockAtCursor(newText)
332334
else:
333335
self.completionState = 0
334-
cursor.select(QTextCursor.BlockUnderCursor)
335-
cursor.removeSelectedText()
336-
cursor.insertText("\n" + self.origText)
336+
self.replaceBlockAtCursor(self.origText)
337337
self.origText == None
338338
cursor.endEditBlock()
339339
self.completing = False

0 commit comments

Comments
 (0)