Changeset View
Changeset View
Standalone View
Standalone View
src/vimode/emulatedcommandbar/completer.cpp
Show All 23 Lines | |||||
24 | using namespace KateVi; | 24 | using namespace KateVi; | ||
25 | 25 | | |||
26 | #include "kateview.h" | 26 | #include "kateview.h" | ||
27 | 27 | | |||
28 | #include <QCompleter> | 28 | #include <QCompleter> | ||
29 | #include <QLineEdit> | 29 | #include <QLineEdit> | ||
30 | #include <QStringListModel> | 30 | #include <QStringListModel> | ||
31 | #include <QAbstractItemView> | 31 | #include <QAbstractItemView> | ||
32 | #include <QRegularExpression> | ||||
32 | 33 | | |||
33 | namespace | 34 | namespace | ||
34 | { | 35 | { | ||
35 | bool caseInsensitiveLessThan(const QString &s1, const QString &s2) | 36 | bool caseInsensitiveLessThan(const QString &s1, const QString &s2) | ||
36 | { | 37 | { | ||
37 | return s1.toLower() < s2.toLower(); | 38 | return s1.toLower() < s2.toLower(); | ||
38 | } | 39 | } | ||
39 | } | 40 | } | ||
▲ Show 20 Lines • Show All 162 Lines • ▼ Show 20 Line(s) | 202 | { | |||
202 | const QString completionPrefix = m_edit->text().mid(m_currentCompletionStartParams.wordStartPos, m_edit->cursorPosition() - m_currentCompletionStartParams.wordStartPos); | 203 | const QString completionPrefix = m_edit->text().mid(m_currentCompletionStartParams.wordStartPos, m_edit->cursorPosition() - m_currentCompletionStartParams.wordStartPos); | ||
203 | m_completer->setCompletionPrefix(completionPrefix); | 204 | m_completer->setCompletionPrefix(completionPrefix); | ||
204 | // Seem to need a call to complete() else the size of the popup box is not altered appropriately. | 205 | // Seem to need a call to complete() else the size of the popup box is not altered appropriately. | ||
205 | m_completer->complete(); | 206 | m_completer->complete(); | ||
206 | } | 207 | } | ||
207 | 208 | | |||
208 | CompletionStartParams Completer::activateWordFromDocumentCompletion() | 209 | CompletionStartParams Completer::activateWordFromDocumentCompletion() | ||
209 | { | 210 | { | ||
210 | QRegExp wordRegEx(QLatin1String("\\w{1,}")); | 211 | static const QRegularExpression wordRegEx(QLatin1String("\\w{1,}")); | ||
212 | QRegularExpressionMatch match; | ||||
213 | | ||||
211 | QStringList foundWords; | 214 | QStringList foundWords; | ||
212 | // Narrow the range of lines we search around the cursor so that we don't die on huge files. | 215 | // Narrow the range of lines we search around the cursor so that we don't die on huge files. | ||
213 | const int startLine = qMax(0, m_view->cursorPosition().line() - 4096); | 216 | const int startLine = qMax(0, m_view->cursorPosition().line() - 4096); | ||
214 | const int endLine = qMin(m_view->document()->lines(), m_view->cursorPosition().line() + 4096); | 217 | const int endLine = qMin(m_view->document()->lines(), m_view->cursorPosition().line() + 4096); | ||
215 | for (int lineNum = startLine; lineNum < endLine; lineNum++) { | 218 | for (int lineNum = startLine; lineNum < endLine; lineNum++) { | ||
216 | const QString line = m_view->document()->line(lineNum); int wordSearchBeginPos = 0; | 219 | const QString line = m_view->document()->line(lineNum); | ||
217 | while (wordRegEx.indexIn(line, wordSearchBeginPos) != -1) { | 220 | int wordSearchBeginPos = 0; | ||
218 | const QString foundWord = wordRegEx.cap(0); | 221 | while ((match = wordRegEx.match(line, wordSearchBeginPos)).hasMatch()) { | ||
222 | const QString foundWord = match.captured(); | ||||
219 | foundWords << foundWord; | 223 | foundWords << foundWord; | ||
220 | wordSearchBeginPos = wordRegEx.indexIn(line, wordSearchBeginPos) + wordRegEx.matchedLength(); | 224 | wordSearchBeginPos = match.capturedEnd(); | ||
221 | } | 225 | } | ||
222 | } | 226 | } | ||
223 | foundWords = QSet<QString>::fromList(foundWords).toList(); | 227 | foundWords = QSet<QString>::fromList(foundWords).toList(); | ||
224 | std::sort(foundWords.begin(), foundWords.end(), caseInsensitiveLessThan); | 228 | std::sort(foundWords.begin(), foundWords.end(), caseInsensitiveLessThan); | ||
225 | CompletionStartParams completionStartParams; | 229 | CompletionStartParams completionStartParams; | ||
226 | completionStartParams.completionType = CompletionStartParams::WordFromDocument; | 230 | completionStartParams.completionType = CompletionStartParams::WordFromDocument; | ||
227 | completionStartParams.completions = foundWords; | 231 | completionStartParams.completions = foundWords; | ||
228 | completionStartParams.wordStartPos = wordBeforeCursorBegin(); | 232 | completionStartParams.wordStartPos = wordBeforeCursorBegin(); | ||
Show All 28 Lines |