Commit d44bac6f authored by hjk's avatar hjk
Browse files

FakeVim: Make plugin standalone if necessary



It's possible to use FakeVim as standalone plugin for a QTextEdit or
QPlainTextEdit widget, so there should be minimum dependencies on
Qt Creator code.

Change-Id: I415ed87f5e4d97ea78d9d25a8f0c82701ef1f70d
Reviewed-by: default avatarhjk <hjk121@nokiamail.com>
parent e1f08408
......@@ -42,7 +42,11 @@
#include <QObject>
#include <QCoreApplication>
#ifdef FAKEVIM_STANDALONE
using namespace FakeVim::Internal::Utils;
#else
using namespace Utils;
#endif
///////////////////////////////////////////////////////////////////////
//
......@@ -53,6 +57,29 @@ using namespace Utils;
namespace FakeVim {
namespace Internal {
typedef QLatin1String _;
#ifdef FAKEVIM_STANDALONE
namespace Utils {
SavedAction::SavedAction(QObject *parent)
: QObject(parent)
{
}
void SavedAction::setValue(const QVariant &value)
{
m_value = value;
}
QVariant SavedAction::value() const
{
return m_value;
}
} // namespace Utils
#endif // FAKEVIM_STANDALONE
FakeVimSettings::FakeVimSettings()
{}
......@@ -64,7 +91,7 @@ FakeVimSettings::~FakeVimSettings()
void FakeVimSettings::insertItem(int code, SavedAction *item,
const QString &longName, const QString &shortName)
{
QTC_ASSERT(!m_items.contains(code), qDebug() << code << item->toString(); return);
QTC_ASSERT(!m_items.contains(code), qDebug() << code; return);
m_items[code] = item;
if (!longName.isEmpty()) {
m_nameToCode[longName] = code;
......@@ -74,6 +101,7 @@ void FakeVimSettings::insertItem(int code, SavedAction *item,
m_nameToCode[shortName] = code;
}
#ifndef FAKEVIM_STANDALONE
void FakeVimSettings::readSettings(QSettings *settings)
{
foreach (SavedAction *item, m_items)
......@@ -85,6 +113,7 @@ void FakeVimSettings::writeSettings(QSettings *settings)
foreach (SavedAction *item, m_items)
item->writeSettings(settings);
}
#endif // FAKEVIM_STANDALONE
SavedAction *FakeVimSettings::item(int code)
{
......@@ -114,162 +143,63 @@ QString FakeVimSettings::trySetValue(const QString &name, const QString &value)
return QString();
}
FakeVimSettings *theFakeVimSettings()
SavedAction *createAction(FakeVimSettings *instance, int code, const QVariant &value,
const QString &settingsKey = QString(),
const QString &shortKey = QString())
{
static FakeVimSettings *instance = 0;
if (instance)
return instance;
instance = new FakeVimSettings;
typedef QLatin1String _;
SavedAction *item = 0;
SavedAction *item = new SavedAction(instance);
item->setValue(value);
#ifndef FAKEVIM_STANDALONE
item->setSettingsKey(_("FakeVim"), settingsKey);
item->setDefaultValue(value);
item->setCheckable( value.canConvert<bool>() );
#endif
instance->insertItem(code, item, settingsKey.toLower(), shortKey);
return item;
}
const QString group = _("FakeVim");
item = new SavedAction(instance);
item->setText(QCoreApplication::translate("FakeVim::Internal",
FakeVimSettings *theFakeVimSettings()
{
static FakeVimSettings *s = 0;
if (s)
return s;
s = new FakeVimSettings;
// Specific FakeVim settings
createAction(s, ConfigUseFakeVim, true, _("UseFakeVim"));
createAction(s, ConfigReadVimRc, false, _("ReadVimRc"));
createAction(s, ConfigVimRcPath, QString(), _("VimRcPath"));
#ifndef FAKEVIM_STANDALONE
s->item(ConfigUseFakeVim)->setText(QCoreApplication::translate("FakeVim::Internal",
"Use Vim-style Editing"));
item->setSettingsKey(group, _("UseFakeVim"));
item->setCheckable(true);
item->setValue(false);
instance->insertItem(ConfigUseFakeVim, item);
item = new SavedAction(instance);
item->setText(QCoreApplication::translate("FakeVim::Internal",
s->item(ConfigReadVimRc)->setText(QCoreApplication::translate("FakeVim::Internal",
"Read .vimrc"));
item->setSettingsKey(group, _("ReadVimRc"));
item->setCheckable(true);
item->setValue(false);
instance->insertItem(ConfigReadVimRc, item);
item = new SavedAction(instance);
item->setText(QCoreApplication::translate("FakeVim::Internal",
s->item(ConfigVimRcPath)->setText(QCoreApplication::translate("FakeVim::Internal",
"Path to .vimrc"));
item->setDefaultValue(QString());
item->setSettingsKey(group, _("VimRcPath"));
instance->insertItem(ConfigVimRcPath, item);
item = new SavedAction(instance);
item->setValue(true);
item->setDefaultValue(true);
item->setSettingsKey(group, _("StartOfLine"));
item->setCheckable(true);
instance->insertItem(ConfigStartOfLine, item, _("startofline"), _("sol"));
item = new SavedAction(instance);
item->setDefaultValue(8);
item->setSettingsKey(group, _("TabStop"));
instance->insertItem(ConfigTabStop, item, _("tabstop"), _("ts"));
item = new SavedAction(instance);
item->setDefaultValue(false);
item->setValue(false);
item->setSettingsKey(group, _("SmartTab"));
instance->insertItem(ConfigSmartTab, item, _("smarttab"), _("sta"));
item = new SavedAction(instance);
item->setDefaultValue(true);
item->setValue(true);
item->setSettingsKey(group, _("HlSearch"));
item->setCheckable(true);
instance->insertItem(ConfigHlSearch, item, _("hlsearch"), _("hls"));
item = new SavedAction(instance);
item->setDefaultValue(8);
item->setSettingsKey(group, _("ShiftWidth"));
instance->insertItem(ConfigShiftWidth, item, _("shiftwidth"), _("sw"));
item = new SavedAction(instance);
item->setDefaultValue(false);
item->setValue(false);
item->setSettingsKey(group, _("ExpandTab"));
item->setCheckable(true);
instance->insertItem(ConfigExpandTab, item, _("expandtab"), _("et"));
item = new SavedAction(instance);
item->setDefaultValue(false);
item->setValue(false);
item->setSettingsKey(group, _("AutoIndent"));
item->setValue(false);
item->setCheckable(true);
instance->insertItem(ConfigAutoIndent, item, _("autoindent"), _("ai"));
item = new SavedAction(instance);
item->setDefaultValue(false);
item->setValue(false);
item->setSettingsKey(group, _("SmartIndent"));
item->setValue(false);
item->setCheckable(true);
instance->insertItem(ConfigSmartIndent, item, _("smartindent"), _("si"));
item = new SavedAction(instance);
item->setDefaultValue(true);
item->setValue(true);
item->setSettingsKey(group, _("IncSearch"));
item->setCheckable(true);
instance->insertItem(ConfigIncSearch, item, _("incsearch"), _("is"));
item = new SavedAction(instance);
item->setDefaultValue(false);
item->setValue(false);
item->setSettingsKey(group, _("UseCoreSearch")); item->setCheckable(true);
instance->insertItem(ConfigUseCoreSearch, item,
_("usecoresearch"), _("ucs"));
item = new SavedAction(instance);
item->setDefaultValue(false);
item->setValue(false);
item->setSettingsKey(group, _("SmartCase")); item->setCheckable(true);
item->setCheckable(true);
instance->insertItem(ConfigSmartCase, item, _("smartcase"), _("scs"));
item = new SavedAction(instance);
item->setDefaultValue(true);
item->setValue(true);
item->setSettingsKey(group, _("WrapScan")); item->setCheckable(true);
item->setCheckable(true);
instance->insertItem(ConfigWrapScan, item, _("wrapscan"), _("ws"));
item = new SavedAction(instance);
item->setDefaultValue(_("indent,eol,start"));
item->setSettingsKey(group, _("Backspace"));
instance->insertItem(ConfigBackspace, item, _("backspace"), _("bs"));
item = new SavedAction(instance);
item->setDefaultValue(_("@,48-57,_,192-255,a-z,A-Z"));
item->setSettingsKey(group, _("IsKeyword"));
instance->insertItem(ConfigIsKeyword, item, _("iskeyword"), _("isk"));
// Invented here.
item = new SavedAction(instance);
item->setDefaultValue(false);
item->setValue(false);
item->setSettingsKey(group, _("ShowMarks"));
item->setCheckable(true);
instance->insertItem(ConfigShowMarks, item, _("showmarks"), _("sm"));
item = new SavedAction(instance);
item->setDefaultValue(false);
item->setValue(false);
item->setSettingsKey(group, _("PassControlKey"));
item->setCheckable(true);
instance->insertItem(ConfigPassControlKey, item, _("passcontrolkey"), _("pck"));
item = new SavedAction(instance);
item->setDefaultValue(QString());
item->setValue(QString());
item->setSettingsKey(group, _("Clipboard"));
item->setCheckable(true);
instance->insertItem(ConfigClipboard, item, _("clipboard"), _("cb"));
item = new SavedAction(instance);
item->setDefaultValue(true);
item->setValue(true);
item->setSettingsKey(group, _("ShowCmd")); item->setCheckable(true);
item->setCheckable(true);
instance->insertItem(ConfigShowCmd, item, _("showcmd"), _("sc"));
return instance;
#endif
createAction(s, ConfigShowMarks, false, _("ShowMarks"), _("sm"));
createAction(s, ConfigPassControlKey, false, _("PassControlKey"), _("pck"));
// Emulated Vim setting
createAction(s, ConfigStartOfLine, true, _("StartOfLine"), _("sol"));
createAction(s, ConfigTabStop, 8, _("TabStop"), _("ts"));
createAction(s, ConfigSmartTab, false, _("SmartTab"), _("sta"));
createAction(s, ConfigHlSearch, true, _("HlSearch"), _("hls"));
createAction(s, ConfigShiftWidth, 8, _("ShiftWidth"), _("sw"));
createAction(s, ConfigExpandTab, false, _("ExpandTab"), _("et"));
createAction(s, ConfigAutoIndent, false, _("AutoIndent"), _("ai"));
createAction(s, ConfigSmartIndent, false, _("SmartIndent"), _("si"));
createAction(s, ConfigIncSearch, true, _("IncSearch"), _("is"));
createAction(s, ConfigUseCoreSearch, false, _("UseCoreSearch"), _("ucs"));
createAction(s, ConfigSmartCase, false, _("SmartCase"), _("scs"));
createAction(s, ConfigWrapScan, true, _("WrapScan"), _("ws"));
createAction(s, ConfigShowCmd, true, _("ShowCmd"), _("sc"));
createAction(s, ConfigBackspace, _("indent,eol,start"), _("ConfigBackspace"), _("bs"));
createAction(s, ConfigIsKeyword, _("@,48-57,_,192-255,a-z,A-Z"), _("IsKeyword"), _("isk"));
createAction(s, ConfigClipboard, QString(), _("Clipboard"), _("cb"));
return s;
}
SavedAction *theFakeVimSetting(int code)
......
......@@ -30,15 +30,33 @@
#ifndef FAKEVIM_ACTIONS_H
#define FAKEVIM_ACTIONS_H
#include <utils/savedaction.h>
#ifndef FAKEVIM_STANDALONE
# include <utils/savedaction.h>
#endif
#include <QHash>
#include <QObject>
#include <QString>
#include <QVariant>
namespace FakeVim {
namespace Internal {
#ifdef FAKEVIM_STANDALONE
namespace Utils {
class SavedAction : public QObject
{
public:
SavedAction(QObject *parent);
void setValue(const QVariant &value);
QVariant value() const;
QVariant m_value;
};
} // namespace Utils
#endif // FAKEVIM_STANDALONE
enum FakeVimSettingsCode
{
ConfigUseFakeVim,
......@@ -90,8 +108,10 @@ public:
Utils::SavedAction *item(const QString &name);
QString trySetValue(const QString &name, const QString &value);
#ifndef FAKEVIM_STANDALONE
void readSettings(QSettings *settings);
void writeSettings(QSettings *settings);
#endif
private:
QHash<int, Utils::SavedAction *> m_items;
......
......@@ -59,6 +59,8 @@
#include "fakevimhandler.h"
#include "fakevimactions.h"
#include <utils/hostosinfo.h>
#include <utils/qtcassert.h>
......@@ -105,6 +107,9 @@
#endif
using namespace Utils;
#ifdef FAKEVIM_STANDALONE
using namespace FakeVim::Internal::Utils;
#endif
namespace FakeVim {
namespace Internal {
......@@ -1768,7 +1773,7 @@ public:
// auto-indent
QString tabExpand(int len) const;
Column indentation(const QString &line) const;
void insertAutomaticIndentation(bool goingDown);
void insertAutomaticIndentation(bool goingDown, bool forceAutoIndent = false);
bool removeAutomaticIndentation(); // true if something removed
// number of autoindented characters
int m_justAutoIndented;
......@@ -2045,12 +2050,14 @@ EventResult FakeVimHandler::Private::handleEvent(QKeyEvent *ev)
return EventPassedToCore;
}
#ifndef FAKEVIM_STANDALONE
bool inSnippetMode = false;
QMetaObject::invokeMethod(editor(),
"inSnippetMode", Q_ARG(bool *, &inSnippetMode));
if (inSnippetMode)
return EventPassedToCore;
#endif
// Fake "End of line"
//m_tc = cursor();
......@@ -4983,23 +4990,15 @@ bool FakeVimHandler::Private::handleExChangeCommand(const ExCommand &cmd)
if (!cmd.matches(_("c"), _("change")))
return false;
const bool oldAutoIndent = hasConfig(ConfigAutoIndent);
// Temporarily set autoindent if ! is present.
if (cmd.hasBang)
theFakeVimSetting(ConfigAutoIndent)->setValue(true, false);
Range range = cmd.range;
range.rangemode = RangeLineModeExclusive;
removeText(range);
insertAutomaticIndentation(true);
insertAutomaticIndentation(true, cmd.hasBang);
// FIXME: In Vim same or less number of lines can be inserted and position after insertion is
// beginning of last inserted line.
enterInsertMode();
if (cmd.hasBang && !oldAutoIndent)
theFakeVimSetting(ConfigAutoIndent)->setValue(false, false);
return true;
}
......@@ -5189,7 +5188,7 @@ bool FakeVimHandler::Private::handleExBangCommand(const ExCommand &cmd) // :!
QProcess proc;
proc.start(command);
proc.waitForStarted();
if (Utils::HostOsInfo::isWindowsHost())
if (HostOsInfo::isWindowsHost())
text.replace(_("\n"), _("\r\n"));
proc.write(text.toUtf8());
proc.closeWriteChannel();
......@@ -6825,9 +6824,9 @@ QString FakeVimHandler::Private::tabExpand(int n) const
+ QString(n % ts, QLatin1Char(' '));
}
void FakeVimHandler::Private::insertAutomaticIndentation(bool goingDown)
void FakeVimHandler::Private::insertAutomaticIndentation(bool goingDown, bool forceAutoIndent)
{
if (!hasConfig(ConfigAutoIndent) && !hasConfig(ConfigSmartIndent))
if (!forceAutoIndent && !hasConfig(ConfigAutoIndent) && !hasConfig(ConfigSmartIndent))
return;
if (hasConfig(ConfigSmartIndent)) {
......
......@@ -30,8 +30,6 @@
#ifndef FAKEVIM_HANDLER_H
#define FAKEVIM_HANDLER_H
#include "fakevimactions.h"
#include <QObject>
#include <QTextEdit>
......
......@@ -29,6 +29,7 @@
#include "fakevimplugin.h"
#include "fakevimactions.h"
#include "fakevimhandler.h"
#include "ui_fakevimoptions.h"
......
include(../../auto/qttest.pri)
include($$IDE_SOURCE_TREE/src/libs/utils/utils.pri)
DEFINES += FAKEVIM_STANDALONE
FAKEVIMDIR = $$IDE_SOURCE_TREE/src/plugins/fakevim
LIBSDIR = $$IDE_SOURCE_TREE/src/libs
UTILSDIR = $$IDE_SOURCE_TREE/src/libs/utils
SOURCES += main.cpp \
$$FAKEVIMDIR/fakevimhandler.cpp \
$$FAKEVIMDIR/fakevimactions.cpp
$$FAKEVIMDIR/fakevimactions.cpp \
$$UTILSDIR/hostosinfo.cpp \
$$UTILSDIR/qtcassert.cpp
HEADERS += $$FAKEVIMDIR/fakevimhandler.h \
$$FAKEVIMDIR/fakevimactions.h
INCLUDEPATH += $$FAKEVIMDIR $$LIBSDIR
$$FAKEVIMDIR/fakevimactions.h \
$$UTILSDIR/hostosinfo.h \
$$UTILSDIR/qtcassert.h
INCLUDEPATH += $$FAKEVIMDIR $$UTILSDIR
......@@ -29,17 +29,56 @@
#include "fakevimhandler.h"
#include <QDebug>
#include <QApplication>
#include <QFontMetrics>
#include <QMainWindow>
#include <QMessageBox>
#include <QPainter>
#include <QPlainTextEdit>
#include <QStatusBar>
#include <QTextEdit>
using namespace FakeVim::Internal;
typedef QLatin1String _;
/**
* Simple editor widget.
* @tparam TextEdit QTextEdit or QPlainTextEdit as base class
*/
template <typename TextEdit>
class Editor : public TextEdit
{
public:
Editor(QWidget *parent = 0) : TextEdit(parent)
{
TextEdit::setCursorWidth(0);
}
void paintEvent(QPaintEvent *e)
{
TextEdit::paintEvent(e);
// Draw text cursor.
QRect rect = TextEdit::cursorRect();
if ( e->rect().contains(rect) ) {
QPainter painter(TextEdit::viewport());
if ( TextEdit::overwriteMode() ) {
QFontMetrics fm(TextEdit::font());
rect.setWidth(fm.width(QLatin1Char('m')));
painter.setPen(Qt::NoPen);
painter.setBrush(TextEdit::palette().color(QPalette::Base));
painter.setCompositionMode(QPainter::CompositionMode_Difference);
} else {
rect.setWidth(TextEdit::cursorWidth());
painter.setPen(TextEdit::palette().color(QPalette::Text));
}
painter.drawRect(rect);
}
}
};
class Proxy : public QObject
{
Q_OBJECT
......@@ -64,6 +103,44 @@ public slots:
updateStatusBar();
}
void highlightMatches(const QString &pattern)
{
QTextEdit *ed = qobject_cast<QTextEdit *>(m_widget);
if (!ed)
return;
// Clear previous highlights.
ed->selectAll();
QTextCursor cur = ed->textCursor();
QTextCharFormat fmt = cur.charFormat();
fmt.setBackground(Qt::transparent);
cur.setCharFormat(fmt);
// Highlight matches.
QTextDocument *doc = ed->document();
QRegExp re(pattern);
cur = doc->find(re);
int a = cur.position();
while ( !cur.isNull() ) {
if ( cur.hasSelection() ) {
fmt.setBackground(Qt::yellow);
cur.setCharFormat(fmt);
} else {
cur.movePosition(QTextCursor::NextCharacter);
}
cur = doc->find(re, cur);
int b = cur.position();
if (a == b) {
cur.movePosition(QTextCursor::NextCharacter);
cur = doc->find(re, cur);
b = cur.position();
if (a == b) break;
}
a = b;
}
}
void changeStatusMessage(const QString &contents, int cursorPos)
{
m_statusMessage = cursorPos == -1 ? contents
......@@ -83,6 +160,18 @@ public slots:
m_mainWindow->statusBar()->showMessage(msg);
}
void handleExCommand(bool *handled, const ExCommand &cmd)
{
if (cmd.matches(_("q"), _("quit")) || cmd.matches(_("qa"), _("qall"))) {
QApplication::quit();
} else {
*handled = false;
return;
}
*handled = true;
}
private:
QWidget *m_widget;
QMainWindow *m_mainWindow;
......@@ -90,55 +179,63 @@ private:
QString m_statusData;
};
int main(int argc, char *argv[])
QWidget *createEditorWidget(bool usePlainTextEdit)
{
QApplication app(argc, argv);
QStringList args = app.arguments();
(void) args.takeFirst();
QWidget *widget = 0;
QString title;
bool usePlainTextEdit = args.size() < 2;
QWidget *editor = 0;
if (usePlainTextEdit) {
QPlainTextEdit *w = new QPlainTextEdit;
Editor<QPlainTextEdit> *w = new Editor<QPlainTextEdit>;
w->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
title = QLatin1String("PlainTextEdit");
widget = w;
editor = w;