@@ -252,6 +252,14 @@ def isStart(self):
252
252
return True
253
253
return False
254
254
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
+
255
263
def keyPressEvent (self , event ):
256
264
if event .key () == Qt .Key_Backtab and self .textCursor ().hasSelection ():
257
265
startCursor = self .textCursor ()
@@ -301,9 +309,7 @@ def keyPressEvent(self, event):
301
309
self .completionState = 0
302
310
cursor = self .textCursor ()
303
311
cursor .beginEditBlock ()
304
- cursor .select (QTextCursor .BlockUnderCursor )
305
- cursor .removeSelectedText ()
306
- cursor .insertText ("\n " + self .origText )
312
+ self .replaceBlockAtCursor (self .origText )
307
313
cursor .endEditBlock ()
308
314
self .origText == None
309
315
return
@@ -318,22 +324,16 @@ def keyPressEvent(self, event):
318
324
if self .completionState == 0 :
319
325
self .origText = self .textCursor ().block ().text ()
320
326
if self .completionState > 0 :
321
- cursor .select (QTextCursor .BlockUnderCursor )
322
- cursor .removeSelectedText ()
323
- cursor .insertText ("\n " + self .origText )
327
+ self .replaceBlockAtCursor (self .origText )
324
328
newText = self .completer .complete (self .origText , self .completionState )
325
329
if newText :
326
330
if newText .find ("(" ) > 0 :
327
331
newText = newText [0 :newText .find ("(" ) + 1 ]
328
332
self .completionState += 1
329
- cursor .select (QTextCursor .BlockUnderCursor )
330
- cursor .removeSelectedText ()
331
- cursor .insertText ("\n " + newText )
333
+ self .replaceBlockAtCursor (newText )
332
334
else :
333
335
self .completionState = 0
334
- cursor .select (QTextCursor .BlockUnderCursor )
335
- cursor .removeSelectedText ()
336
- cursor .insertText ("\n " + self .origText )
336
+ self .replaceBlockAtCursor (self .origText )
337
337
self .origText == None
338
338
cursor .endEditBlock ()
339
339
self .completing = False
0 commit comments