MCPcopy Create free account
hub / github.com/Codeya-IDE/deepin-ide / doReplaceAll

Method doReplaceAll

src/plugins/codeeditor/find/editordocumentfind.cpp:119–163  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

117}
118
119void EditorDocumentFindPrivate::doReplaceAll(TextEditor *editor, const QString &findText,
120 const QString &replaceText, bool caseSensitive, bool wholeWords)
121{
122 int srcPosition = editor->cursorPosition();
123 int firstDisLineNum = editor->SendScintilla(TextEditor::SCI_GETFIRSTVISIBLELINE);
124 editor->beginUndoAction();
125
126 int flags = buildSearchFlags(false, caseSensitive, wholeWords, false, true, FINDNEXTTYPE_REPLACENEXT, 0, 0);
127 editor->SendScintilla(TextEditor::SCI_SETSEARCHFLAGS, flags);
128
129 FindReplaceInfo findReplaceInfo;
130 findReplaceInfo.startRange = 0;
131 findReplaceInfo.endRange = editor->SendScintilla(TextEditor::SCI_GETLENGTH);
132
133 QByteArray textFind = findText.toUtf8();
134 QByteArray textReplace = replaceText.toUtf8();
135
136 intptr_t targetStart = 0;
137 intptr_t targetEnd = 0;
138 while (targetStart >= 0) {
139 targetStart = editor->searchInTarget(textFind, findReplaceInfo.startRange, findReplaceInfo.endRange);
140 // If we've not found anything, just break out of the loop
141 if (targetStart == -1 || targetStart == -2)
142 break;
143
144 targetEnd = editor->SendScintilla(TextEditor::SCI_GETTARGETEND);
145 if (targetEnd > findReplaceInfo.endRange)
146 break;
147
148 intptr_t foundTextLen = targetEnd - targetStart;
149 intptr_t replacedLength = editor->replaceTarget(textReplace);
150 intptr_t replaceDelta = replacedLength - foundTextLen;
151 // After the processing of the last string occurrence the search loop should be stopped
152 // This helps to avoid the endless replacement during the EOL ("$") searching
153 if (targetStart + foundTextLen == findReplaceInfo.endRange)
154 break;
155
156 findReplaceInfo.startRange = targetStart + foundTextLen + replaceDelta; //search from result onwards
157 findReplaceInfo.endRange += replaceDelta; //adjust end of range in case of replace
158 }
159
160 editor->endUndoAction();
161 editor->gotoPosition(srcPosition);
162 editor->setFirstVisibleLine(firstDisLineNum);
163}
164
165int EditorDocumentFindPrivate::buildSearchFlags(bool re, bool cs, bool wo, bool wrap, bool forward, FindNextType findNextType, bool posix, bool cxx11)
166{

Callers 1

replaceAllMethod · 0.80

Calls 8

beginUndoActionMethod · 0.80
searchInTargetMethod · 0.80
replaceTargetMethod · 0.80
endUndoActionMethod · 0.80
setFirstVisibleLineMethod · 0.80
cursorPositionMethod · 0.45
SendScintillaMethod · 0.45
gotoPositionMethod · 0.45

Tested by

no test coverage detected