Skip to content

Commit

Permalink
fix: [completion] Optimize intelligent completion
Browse files Browse the repository at this point in the history
as title

Log: Optimize intelligent completion
  • Loading branch information
Kakueeen authored and deepin-mozart committed Oct 28, 2024
1 parent 4cebe9b commit 844a10f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
9 changes: 4 additions & 5 deletions src/plugins/codeeditor/gui/private/texteditor_p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -583,11 +583,10 @@ void TextEditorPrivate::updateCacheInfo(int pos, int added)
q->lineIndexFromPosition(pos, &line, &index);
editor.lineChanged(fileName, line, added);
if (lineWidgetContainer->isVisible()) {
if (showAtLine > line && q->lines() != 1) {
if (added != q->lines() - 1) {
showAtLine += added;
showAtLine = qMax(showAtLine, 1);
}
if (showAtLine > line) {
showAtLine += added;
showAtLine = qMax(showAtLine, 1);

updateLineWidgetPosition();
}
}
Expand Down
37 changes: 31 additions & 6 deletions src/plugins/codeeditor/gui/tabwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -613,14 +613,39 @@ bool TabWidget::replaceRange(const QString &fileName, const dpfservice::Edit::Ra
if (auto editor = d->findEditor(fileName)) {
// When using the `lineLength()` function to get the length, it is calculated in bytes.
// When there are Chinese characters in the text, the length obtained is not correct.
int indexTo = range.end.column;
if (indexTo == -1) {
auto lineText = editor->text(range.end.line);
indexTo = lineText.length();
auto lineLength = [editor](int line, bool rmN) {
auto lineText = editor->text(line);
int len = lineText.length();
if (rmN && lineText.endsWith('\n'))
len -= 1;
return len;
};

editor->beginUndoAction();
auto lineTextList = newText.split('\n');
for (int i = 0; i < lineTextList.size(); ++i) {
int line = range.start.line + i;
int indexFrom = 0;
if (i == 0)
indexFrom = range.start.column == -1 ? 0 : range.start.column;
int indexTo = lineLength(line, true);
if (line == range.end.line && indexTo != range.end.column)
indexTo = range.end.column;

if (i == lineTextList.size() - 1 || line == range.end.line) {
indexTo = range.end.column;
if (indexTo == -1)
indexTo = lineLength(range.end.line, false);
QStringList remainderLines;
std::copy(lineTextList.begin() + i, lineTextList.end(), std::back_inserter(remainderLines));
editor->replaceRange(line, indexFrom, range.end.line, indexTo, remainderLines.join('\n'));
break;
} else {
editor->replaceRange(line, indexFrom, line, indexTo, lineTextList[i]);
}
}

editor->replaceRange(range.start.line, range.start.column == -1 ? 0 : range.start.column,
range.end.line, indexTo, newText);
editor->endUndoAction();
return true;
}

Expand Down
3 changes: 3 additions & 0 deletions src/plugins/codegeex/copilot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ Copilot::Copilot(QObject *parent)
completion = response;
}

if (completion.endsWith('\n'))
completion.chop(1);

generatedCode = completion;
completionProvider->setInlineCompletions({ completion });
emit completionProvider->finished();
Expand Down

0 comments on commit 844a10f

Please sign in to comment.