Skip to content

Commit

Permalink
fix: [editor] The cursor is abnormal after completion
Browse files Browse the repository at this point in the history
as title

Log: fix issue
  • Loading branch information
Kakueeen authored and deepin-mozart committed Nov 5, 2024
1 parent 5ab29d1 commit ae83965
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ void CodeCompletionWidget::executeWithTextEdit(lsp::CompletionItem *item)
textToBeInserted += extraCharacters;
auto range = item->textEdit.range;
editor()->replaceRange(range.start.line, range.start.character,
curLine, curIndex, textToBeInserted);
curLine, curIndex, textToBeInserted, true);
if (cursorOffset) {
editor()->lineIndexFromPosition(editor()->cursorPosition(), &curLine, &curIndex);
editor()->setCursorPosition(curLine, curIndex + cursorOffset);
Expand Down Expand Up @@ -234,7 +234,8 @@ void CodeCompletionWidget::executeWithoutTextEdit(lsp::CompletionItem *item)
QRegularExpressionMatch match = identifier.match(text);
int matchLength = match.hasMatch() ? match.capturedLength(0) : 0;
length = qMax(length, matchLength);
editor()->replaceRange(pos - length, pos, textToInsert);
int startPos = pos - length;
editor()->replaceRange(startPos, pos, textToInsert, true);
}

void CodeCompletionWidget::modelContentChanged()
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/codeeditor/gui/tabwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ void TabWidget::replaceAll(const QString &fileName, const QString &oldText,
void TabWidget::replaceText(const QString &fileName, int line, int index, int length, const QString &after)
{
if (auto editor = d->findEditor(fileName))
editor->replaceRange(line, index, line, index + length, after);
editor->replaceRange(line, index, line, index + length, after, true);
}

bool TabWidget::replaceRange(const QString &fileName, const dpfservice::Edit::Range &range, const QString &newText)
Expand Down
8 changes: 5 additions & 3 deletions src/plugins/codeeditor/gui/texteditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -650,19 +650,21 @@ int TextEditor::positionFromPoint(int x, int y)
return pos;
}

void TextEditor::replaceRange(int lineFrom, int indexFrom, int lineTo, int indexTo, const QString &text)
void TextEditor::replaceRange(int lineFrom, int indexFrom, int lineTo, int indexTo, const QString &text, bool changePos)
{
int startPos = positionFromLineIndex(lineFrom, indexFrom);
int endPos = positionFromLineIndex(lineTo, indexTo);
replaceRange(startPos, endPos, text);
replaceRange(startPos, endPos, text, changePos);
}

void TextEditor::replaceRange(int startPosition, int endPosition, const QString &text)
void TextEditor::replaceRange(int startPosition, int endPosition, const QString &text, bool changePos)
{
SendScintilla(SCI_CLEARSELECTIONS);
SendScintilla(SCI_SETTARGETSTART, startPosition);
SendScintilla(SCI_SETTARGETEND, endPosition);
SendScintilla(SCI_REPLACETARGET, -1, textAsBytes(text).constData());
if (changePos)
SendScintilla(SCI_GOTOPOS, startPosition + text.length());
}

void TextEditor::insertText(const QString &text)
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/codeeditor/gui/texteditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ class TextEditor : public QsciScintilla
bool selectionStatus(const int &lineFrom, const int &lineTo, const int &indexFrom, const int &indexTo);
QPoint pointFromPosition(int position);
int positionFromPoint(int x, int y);
void replaceRange(int lineFrom, int indexFrom, int lineTo, int indexTo, const QString &text);
void replaceRange(int startPosition, int endPosition, const QString &text);
void replaceRange(int lineFrom, int indexFrom, int lineTo, int indexTo, const QString &text, bool changePos = false);
void replaceRange(int startPosition, int endPosition, const QString &text, bool changePos = false);
void insertText(const QString &text);
LanguageClientHandler *languageClient() const;
int wordStartPositoin(int position);
Expand Down

0 comments on commit ae83965

Please sign in to comment.