FakeVim: Emulate search more precisely

Submitting search highlights (if 'hlsearch' enabled) in visible editors.

Update highlights after 'nohlsearch' command, 'set hlsearch' and
changing document.

Change-Id: I66ed8c6e9c6e99f3c64f91cdec5ac5feb636d2cb
Reviewed-by: hjk <hjk121@nokiamail.com>
This commit is contained in:
hluk
2013-08-06 17:13:53 +02:00
committed by hjk
parent 91e6d27a73
commit d15fb5a75c
2 changed files with 36 additions and 18 deletions

View File

@@ -1901,6 +1901,7 @@ public:
QTextCursor m_searchCursor; QTextCursor m_searchCursor;
int m_searchStartPosition; int m_searchStartPosition;
int m_searchFromScreenLine; int m_searchFromScreenLine;
QString m_highlighted; // currently highlighted text
bool handleExCommandHelper(ExCommand &cmd); // Returns success. bool handleExCommandHelper(ExCommand &cmd); // Returns success.
bool handleExPluginCommand(const ExCommand &cmd); // Handled by plugin? bool handleExPluginCommand(const ExCommand &cmd); // Handled by plugin?
@@ -1954,6 +1955,7 @@ public:
, mapDepth(0) , mapDepth(0)
, currentMessageLevel(MessageInfo) , currentMessageLevel(MessageInfo)
, lastSearchForward(false) , lastSearchForward(false)
, highlightsCleared(false)
, findPending(false) , findPending(false)
, returnToMode(CommandMode) , returnToMode(CommandMode)
, currentRegister(0) , currentRegister(0)
@@ -2001,7 +2003,6 @@ public:
// Command line buffers. // Command line buffers.
CommandBuffer commandBuffer; CommandBuffer commandBuffer;
CommandBuffer searchBuffer; CommandBuffer searchBuffer;
QString oldNeedle;
// Current mini buffer message. // Current mini buffer message.
QString currentMessage; QString currentMessage;
@@ -2009,9 +2010,11 @@ public:
QString currentCommand; QString currentCommand;
// Search state. // Search state.
QString lastSearch; QString lastSearch; // last search expression as entered by user
bool lastSearchForward; QString lastNeedle; // last search expression translated with vimPatternToQtPattern()
bool findPending; bool lastSearchForward; // last search command was '/' or '*'
bool highlightsCleared; // ':nohlsearch' command is active until next search
bool findPending; // currently searching using external tool (until editor is focused again)
// Last substitution command. // Last substitution command.
QString lastSubstituteFlags; QString lastSubstituteFlags;
@@ -2087,6 +2090,7 @@ void FakeVimHandler::Private::focus()
updateCursorShape(); updateCursorShape();
if (!g.inFakeVim || g.mode != CommandMode) if (!g.inFakeVim || g.mode != CommandMode)
updateMiniBuffer(); updateMiniBuffer();
updateHighlights();
} }
void FakeVimHandler::Private::enterFakeVim() void FakeVimHandler::Private::enterFakeVim()
@@ -3177,8 +3181,17 @@ void FakeVimHandler::Private::updateSelection()
void FakeVimHandler::Private::updateHighlights() void FakeVimHandler::Private::updateHighlights()
{ {
if (!hasConfig(ConfigUseCoreSearch)) if (hasConfig(ConfigUseCoreSearch) || !hasConfig(ConfigHlSearch) || g.highlightsCleared) {
emit q->highlightMatches(g.oldNeedle); if (m_highlighted.isEmpty())
return;
m_highlighted.clear();
} else if (m_highlighted != g.lastNeedle) {
m_highlighted = g.lastNeedle;
} else {
return;
}
emit q->highlightMatches(m_highlighted);
} }
void FakeVimHandler::Private::updateMiniBuffer() void FakeVimHandler::Private::updateMiniBuffer()
@@ -5319,6 +5332,8 @@ bool FakeVimHandler::Private::handleExSetCommand(const ExCommand &cmd)
act->setValue(true); act->setValue(true);
else if (oldValue == true) else if (oldValue == true)
{} // nothing to do {} // nothing to do
if (g.highlightsCleared && (cmd.args == _("hls") || cmd.args == _("hlsearch")))
highlightMatches(g.lastNeedle);
} else if (act) { } else if (act) {
// Non-boolean to show. // Non-boolean to show.
showMessage(MessageInfo, cmd.args + QLatin1Char('=') + act->value().toString()); showMessage(MessageInfo, cmd.args + QLatin1Char('=') + act->value().toString());
@@ -5654,11 +5669,12 @@ bool FakeVimHandler::Private::handleExShiftCommand(const ExCommand &cmd)
bool FakeVimHandler::Private::handleExNohlsearchCommand(const ExCommand &cmd) bool FakeVimHandler::Private::handleExNohlsearchCommand(const ExCommand &cmd)
{ {
// :nohlsearch // :noh, :nohl, ..., :nohlsearch
if (!cmd.cmd.startsWith(_("noh"))) if (cmd.cmd.size() < 3 || !QString(_("nohlsearch")).startsWith(cmd.cmd))
return false; return false;
highlightMatches(QString()); g.highlightsCleared = true;
updateHighlights();
return true; return true;
} }
@@ -5955,10 +5971,8 @@ void FakeVimHandler::Private::searchNext(bool forward)
void FakeVimHandler::Private::highlightMatches(const QString &needle) void FakeVimHandler::Private::highlightMatches(const QString &needle)
{ {
if (!hasConfig(ConfigHlSearch) || needle == g.oldNeedle) g.lastNeedle = needle;
return; g.highlightsCleared = false;
g.oldNeedle = needle;
updateHighlights(); updateHighlights();
} }
@@ -7230,6 +7244,9 @@ void FakeVimHandler::Private::onContentsChanged(int position, int charsRemoved,
m_oldPosition); m_oldPosition);
} }
} }
if (!m_highlighted.isEmpty())
emit q->highlightMatches(m_highlighted);
} }
void FakeVimHandler::Private::onUndoCommandAdded() void FakeVimHandler::Private::onUndoCommandAdded()

View File

@@ -1987,11 +1987,12 @@ void FakeVimPluginPrivate::changeSelection(const QList<QTextEdit::ExtraSelection
void FakeVimPluginPrivate::highlightMatches(const QString &needle) void FakeVimPluginPrivate::highlightMatches(const QString &needle)
{ {
IEditor *editor = EditorManager::currentEditor(); foreach (IEditor *editor, EditorManager::instance()->visibleEditors()) {
QWidget *w = editor->widget(); QWidget *w = editor->widget();
Find::IFindSupport *find = Aggregation::query<Find::IFindSupport>(w); Find::IFindSupport *find = Aggregation::query<Find::IFindSupport>(w);
if (find != 0) if (find != 0)
find->highlightAll(needle, Find::FindRegularExpression | Find::FindCaseSensitively); find->highlightAll(needle, Find::FindRegularExpression | Find::FindCaseSensitively);
}
} }
int FakeVimPluginPrivate::currentFile() const int FakeVimPluginPrivate::currentFile() const