Skip to content

Commit 6f9ead8

Browse files
committed
golangfmt editor loaddiff try to keep the original location
1 parent 01ee80d commit 6f9ead8

File tree

1 file changed

+50
-25
lines changed

1 file changed

+50
-25
lines changed

liteidex/src/plugins/golangfmt/golangfmt.cpp

Lines changed: 50 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -174,33 +174,25 @@ void GolangFmt::syncfmtEditor(LiteApi::IEditor *editor, bool save, bool check, i
174174
liteEditor->setNavigateHead(LiteApi::EditorNavigateNormal,"go code format success");
175175

176176
QByteArray data = process.readAllStandardOutput();
177-
/*
178-
int vpos = -1;
179-
QScrollBar *bar = ed->verticalScrollBar();
180-
if (bar) {
181-
vpos = bar->sliderPosition();
182-
}
183-
*/
177+
184178
QByteArray state = editor->saveState();
185179
QTextCursor cur = ed->textCursor();
186-
//int pos = cur.position();
187180
cur.beginEditBlock();
181+
bool bUseState = true;
188182
if (m_diff) {
189-
loadDiff(cur,codec->toUnicode(data));
183+
bUseState = false;
184+
loadDiff(cur,codec->toUnicode(data));
190185
} else {
191186
cur.select(QTextCursor::Document);
192187
cur.removeSelectedText();
193188
cur.insertText(codec->toUnicode(data));
194189
}
195-
//cur.setPosition(pos);
196190
cur.endEditBlock();
197191
ed->setTextCursor(cur);
198-
editor->restoreState(state);
192+
if (bUseState) {
193+
editor->restoreState(state);
194+
}
199195

200-
//ed->setTextCursor(cur);
201-
//if (vpos != -1) {
202-
// bar->setSliderPosition(vpos);
203-
//}
204196
if (save) {
205197
m_liteApp->editorManager()->saveEditor(editor,false);
206198
}
@@ -337,28 +329,25 @@ void GolangFmt::fmtFinish(bool error,int code,QString)
337329
QTextCodec *codec = QTextCodec::codecForName("utf-8");
338330
if (!error && code == 0) {
339331
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();
345334
QTextCursor cur = ed->textCursor();
346-
int pos = cur.position();
347335
cur.beginEditBlock();
336+
bool bUseState = true;
348337
if (m_diff) {
338+
bUseState = false;
349339
loadDiff(cur,codec->toUnicode(m_data));
350340
} else {
351341
cur.select(QTextCursor::Document);
352342
cur.removeSelectedText();
353343
cur.insertText(codec->toUnicode(m_data));
354344
}
355-
cur.setPosition(pos);
356345
cur.endEditBlock();
357-
358346
ed->setTextCursor(cur);
359-
if (vpos != -1) {
360-
bar->setSliderPosition(vpos);
347+
if (bUseState) {
348+
editor->restoreState(state);
361349
}
350+
362351
if (save) {
363352
m_liteApp->editorManager()->saveEditor(editor,false);
364353
}
@@ -386,13 +375,31 @@ void GolangFmt::fmtFinish(bool error,int code,QString)
386375
m_data.clear();
387376
}
388377

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+
389387
void GolangFmt::loadDiff(QTextCursor &cursor, const QString &diff)
390388
{
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
391397
QRegExp reg("@@\\s+\\-(\\d+),?(\\d+)?\\s+\\+(\\d+),?(\\d+)?\\s+@@");
392398
QTextBlock block;
393399
int line = -1;
394400
int line_add = 0;
395401
int block_number = 0;
402+
396403
foreach(QString s, diff.split('\n')) {
397404
if (s.length() == 0) {
398405
continue;
@@ -415,6 +422,10 @@ void GolangFmt::loadDiff(QTextCursor &cursor, const QString &diff)
415422
continue;
416423
}
417424
if (ch == '+') {
425+
if (lastBlockNumber >= block_number) {
426+
lastBlockNumber = curBlockNumber;
427+
curBlockNumber++;
428+
}
418429
block = cursor.document()->findBlockByNumber(block_number);
419430
if (!block.isValid()) {
420431
cursor.movePosition(QTextCursor::End);
@@ -427,6 +438,10 @@ void GolangFmt::loadDiff(QTextCursor &cursor, const QString &diff)
427438
}
428439
block_number++;
429440
} else if (ch == '-') {
441+
if (lastBlockNumber >= (block_number)) {
442+
lastBlockNumber = curBlockNumber;
443+
curBlockNumber--;
444+
}
430445
block = cursor.document()->findBlockByNumber(block_number);
431446
cursor.setPosition(block.position());
432447
if (block.next().isValid()) {
@@ -444,4 +459,14 @@ void GolangFmt::loadDiff(QTextCursor &cursor, const QString &diff)
444459
//skip comment
445460
}
446461
}
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+
}
447471
}
472+

0 commit comments

Comments
 (0)