@@ -174,33 +174,25 @@ void GolangFmt::syncfmtEditor(LiteApi::IEditor *editor, bool save, bool check, i
174
174
liteEditor->setNavigateHead (LiteApi::EditorNavigateNormal," go code format success" );
175
175
176
176
QByteArray data = process.readAllStandardOutput ();
177
- /*
178
- int vpos = -1;
179
- QScrollBar *bar = ed->verticalScrollBar();
180
- if (bar) {
181
- vpos = bar->sliderPosition();
182
- }
183
- */
177
+
184
178
QByteArray state = editor->saveState ();
185
179
QTextCursor cur = ed->textCursor ();
186
- // int pos = cur.position();
187
180
cur.beginEditBlock ();
181
+ bool bUseState = true ;
188
182
if (m_diff) {
189
- loadDiff (cur,codec->toUnicode (data));
183
+ bUseState = false ;
184
+ loadDiff (cur,codec->toUnicode (data));
190
185
} else {
191
186
cur.select (QTextCursor::Document);
192
187
cur.removeSelectedText ();
193
188
cur.insertText (codec->toUnicode (data));
194
189
}
195
- // cur.setPosition(pos);
196
190
cur.endEditBlock ();
197
191
ed->setTextCursor (cur);
198
- editor->restoreState (state);
192
+ if (bUseState) {
193
+ editor->restoreState (state);
194
+ }
199
195
200
- // ed->setTextCursor(cur);
201
- // if (vpos != -1) {
202
- // bar->setSliderPosition(vpos);
203
- // }
204
196
if (save) {
205
197
m_liteApp->editorManager ()->saveEditor (editor,false );
206
198
}
@@ -337,28 +329,25 @@ void GolangFmt::fmtFinish(bool error,int code,QString)
337
329
QTextCodec *codec = QTextCodec::codecForName (" utf-8" );
338
330
if (!error && code == 0 ) {
339
331
liteEditor->setNavigateHead (LiteApi::EditorNavigateNormal," go code format success" );
340
- int vpos = -1 ;
341
- QScrollBar *bar = ed->verticalScrollBar ();
342
- if (bar) {
343
- vpos = bar->sliderPosition ();
344
- }
332
+
333
+ QByteArray state = editor->saveState ();
345
334
QTextCursor cur = ed->textCursor ();
346
- int pos = cur.position ();
347
335
cur.beginEditBlock ();
336
+ bool bUseState = true ;
348
337
if (m_diff) {
338
+ bUseState = false ;
349
339
loadDiff (cur,codec->toUnicode (m_data));
350
340
} else {
351
341
cur.select (QTextCursor::Document);
352
342
cur.removeSelectedText ();
353
343
cur.insertText (codec->toUnicode (m_data));
354
344
}
355
- cur.setPosition (pos);
356
345
cur.endEditBlock ();
357
-
358
346
ed->setTextCursor (cur);
359
- if (vpos != - 1 ) {
360
- bar-> setSliderPosition (vpos );
347
+ if (bUseState ) {
348
+ editor-> restoreState (state );
361
349
}
350
+
362
351
if (save) {
363
352
m_liteApp->editorManager ()->saveEditor (editor,false );
364
353
}
@@ -386,13 +375,31 @@ void GolangFmt::fmtFinish(bool error,int code,QString)
386
375
m_data.clear ();
387
376
}
388
377
378
+ // TODO
379
+ int findBlockPos (const QString &orgText, const QString &newText, int pos )
380
+ {
381
+ if (pos > newText.length ()) {
382
+ return newText.length ();
383
+ }
384
+ return pos;
385
+ }
386
+
389
387
void GolangFmt::loadDiff (QTextCursor &cursor, const QString &diff)
390
388
{
389
+ // save org block
390
+ int orgBlockNumber = cursor.blockNumber ();
391
+ int orgPosInBlock = cursor.positionInBlock ();
392
+ QString orgBlockText = cursor.block ().text ();
393
+ int curBlockNumber = orgBlockNumber;
394
+ int lastBlockNumber = curBlockNumber;
395
+
396
+ // load diff
391
397
QRegExp reg (" @@\\ s+\\ -(\\ d+),?(\\ d+)?\\ s+\\ +(\\ d+),?(\\ d+)?\\ s+@@" );
392
398
QTextBlock block;
393
399
int line = -1 ;
394
400
int line_add = 0 ;
395
401
int block_number = 0 ;
402
+
396
403
foreach (QString s, diff.split (' \n ' )) {
397
404
if (s.length () == 0 ) {
398
405
continue ;
@@ -415,6 +422,10 @@ void GolangFmt::loadDiff(QTextCursor &cursor, const QString &diff)
415
422
continue ;
416
423
}
417
424
if (ch == ' +' ) {
425
+ if (lastBlockNumber >= block_number) {
426
+ lastBlockNumber = curBlockNumber;
427
+ curBlockNumber++;
428
+ }
418
429
block = cursor.document ()->findBlockByNumber (block_number);
419
430
if (!block.isValid ()) {
420
431
cursor.movePosition (QTextCursor::End);
@@ -427,6 +438,10 @@ void GolangFmt::loadDiff(QTextCursor &cursor, const QString &diff)
427
438
}
428
439
block_number++;
429
440
} else if (ch == ' -' ) {
441
+ if (lastBlockNumber >= (block_number)) {
442
+ lastBlockNumber = curBlockNumber;
443
+ curBlockNumber--;
444
+ }
430
445
block = cursor.document ()->findBlockByNumber (block_number);
431
446
cursor.setPosition (block.position ());
432
447
if (block.next ().isValid ()) {
@@ -444,4 +459,14 @@ void GolangFmt::loadDiff(QTextCursor &cursor, const QString &diff)
444
459
// skip comment
445
460
}
446
461
}
462
+ // load cur block
463
+ block = cursor.document ()->findBlockByNumber (curBlockNumber);
464
+ if (block.isValid ()) {
465
+ cursor.setPosition (block.position ());
466
+ int column = findBlockPos (orgBlockText,block.text (),orgPosInBlock);
467
+ if (column > 0 ) {
468
+ cursor.movePosition (QTextCursor::Right, QTextCursor::MoveAnchor, column);
469
+ }
470
+ }
447
471
}
472
+
0 commit comments