FakeVim: Aspectify settings

Change-Id: Ic73edce82e192779ed3efe86a5cb747a52d94d36
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
hjk
2021-02-26 17:55:55 +01:00
parent 3e08210998
commit 50f93710b7
8 changed files with 401 additions and 815 deletions

View File

@@ -31,7 +31,7 @@
// Qt Creator. The idea is to keep this file here in a "clean" state that
// allows easy reuse with any QTextEdit or QPlainTextEdit derived class.
#include <utils/layoutbuilder.h>
#include <utils/qtcassert.h>
#include <QDebug>
@@ -41,168 +41,171 @@ using namespace Utils;
namespace FakeVim {
namespace Internal {
DummyAction::DummyAction(void *)
#ifdef FAKEVIM_STANDALONE
FvBaseAspect::FvBaseAspect()
{
}
void DummyAction::setValue(const QVariant &value)
void FvBaseAspect::setValue(const QVariant &value)
{
m_value = value;
}
QVariant DummyAction::value() const
QVariant FvBaseAspect::value() const
{
return m_value;
}
void DummyAction::setDefaultValue(const QVariant &value)
void FvBaseAspect::setDefaultValue(const QVariant &value)
{
m_defaultValue = value;
}
QVariant DummyAction::defaultValue() const
QVariant FvBaseAspect::defaultValue() const
{
return m_defaultValue;
}
void DummyAction::setSettingsKey(const QString &group, const QString &key)
void FvBaseAspect::setSettingsKey(const QString &group, const QString &key)
{
m_settingsGroup = group;
m_settingsKey = key;
}
QString DummyAction::settingsKey() const
QString FvBaseAspect::settingsKey() const
{
return m_settingsKey;
}
#endif
FakeVimSettings::FakeVimSettings()
{
// Specific FakeVim settings
createAction(ConfigReadVimRc, false, "ReadVimRc");
createAction(ConfigVimRcPath, QString(), "VimRcPath");
#ifndef FAKEVIM_STANDALONE
createAction(ConfigUseFakeVim, false, "UseFakeVim");
item(ConfigUseFakeVim)->setText(tr("Use Vim-style Editing"));
item(ConfigReadVimRc)->setText(tr("Read .vimrc"));
item(ConfigVimRcPath)->setText(tr("Path to .vimrc"));
setup(&useFakeVim, false, "UseFakeVim", {}, tr("Use FakeVim"));
#endif
createAction(ConfigShowMarks, false, "ShowMarks", "sm");
createAction(ConfigPassControlKey, false, "PassControlKey", "pck");
createAction(ConfigPassKeys, true, "PassKeys", "pk");
// Specific FakeVim settings
setup(&readVimRc, false, "ReadVimRc", {}, tr("Read .vimrc from location:"));
setup(&vimRcPath, QString(), "VimRcPath", {}, {}); // tr("Path to .vimrc")
setup(&showMarks, false, "ShowMarks", "sm", tr("Show position of text marks"));
setup(&passControlKey, false, "PassControlKey", "pck", tr("Pass control keys"));
setup(&passKeys, true, "PassKeys", "pk", tr("Pass keys in insert mode"));
// Emulated Vsetting
createAction(ConfigStartOfLine, true, "StartOfLine", "sol");
createAction(ConfigTabStop, 8, "TabStop", "ts");
createAction(ConfigSmartTab, false, "SmartTab", "sta");
createAction(ConfigHlSearch, true, "HlSearch", "hls");
createAction(ConfigShiftWidth, 8, "ShiftWidth", "sw");
createAction(ConfigExpandTab, false, "ExpandTab", "et");
createAction(ConfigAutoIndent, false, "AutoIndent", "ai");
createAction(ConfigSmartIndent, false, "SmartIndent", "si");
createAction(ConfigIncSearch, true, "IncSearch", "is");
createAction(ConfigUseCoreSearch, false, "UseCoreSearch", "ucs");
createAction(ConfigSmartCase, false, "SmartCase", "scs");
createAction(ConfigIgnoreCase, false, "IgnoreCase", "ic");
createAction(ConfigWrapScan, true, "WrapScan", "ws");
createAction(ConfigTildeOp, false, "TildeOp", "top");
createAction(ConfigShowCmd, true, "ShowCmd", "sc");
createAction(ConfigRelativeNumber, false, "RelativeNumber", "rnu");
createAction(ConfigBlinkingCursor, false, "BlinkingCursor", "bc");
createAction(ConfigScrollOff, 0, "ScrollOff", "so");
createAction(ConfigBackspace, QString("indent,eol,start"), "ConfigBackspace", "bs");
createAction(ConfigIsKeyword, QString("@,48-57,_,192-255,a-z,A-Z"), "IsKeyword", "isk");
createAction(ConfigClipboard, QString(), "Clipboard", "cb");
createAction(ConfigFormatOptions, QString(), "formatoptions", "fo");
setup(&startOfLine, true, "StartOfLine", "sol", tr("Start of line"));
setup(&tabStop, 8, "TabStop", "ts", tr("Tabulator size:"));
setup(&smartTab, false, "SmartTab", "sta", tr("Smart tabulators"));
setup(&hlSearch, true, "HlSearch", "hls", tr("Highlight search results"));
setup(&shiftWidth, 8, "ShiftWidth", "sw", tr("Shift width:"));
setup(&expandTab, false, "ExpandTab", "et", tr("Expand tabulators"));
setup(&autoIndent, false, "AutoIndent", "ai", tr("Automatic indentation"));
setup(&smartIndent, false, "SmartIndent", "si", tr("Smart tabulators"));
setup(&incSearch, true, "IncSearch", "is", tr("Incremental search"));
setup(&useCoreSearch, false, "UseCoreSearch", "ucs", tr("Use search dialog"));
setup(&smartCase, false, "SmartCase", "scs", tr("Use smartcase"));
setup(&ignoreCase, false, "IgnoreCase", "ic", tr("Use ignorecase"));
setup(&wrapScan, true, "WrapScan", "ws", tr("Use wrapscan"));
setup(&tildeOp, false, "TildeOp", "top", tr("Use tildeop"));
setup(&showCmd, true, "ShowCmd", "sc", tr("Show partial command"));
setup(&relativeNumber, false, "RelativeNumber", "rnu", tr("Show line numbers relative to cursor"));
setup(&blinkingCursor, false, "BlinkingCursor", "bc", tr("Blinking cursor"));
setup(&scrollOff, 0, "ScrollOff", "so", tr("Scroll offset:"));
setup(&backspace, "indent,eol,start",
"ConfigBackspace","bs", tr("Backspace:"));
setup(&isKeyword, "@,48-57,_,192-255,a-z,A-Z",
"IsKeyword", "isk", tr("Keyword characters:"));
setup(&clipboard, {}, "Clipboard", "cb", tr(""));
setup(&formatOptions, {}, "formatoptions", "fo", tr(""));
// Emulated plugins
createAction(ConfigEmulateVimCommentary, false, "commentary");
createAction(ConfigEmulateReplaceWithRegister, false, "ReplaceWithRegister");
createAction(ConfigEmulateExchange, false, "exchange");
createAction(ConfigEmulateArgTextObj, false, "argtextobj");
createAction(ConfigEmulateSurround, false, "surround");
setup(&emulateVimCommentary, false, "commentary", {}, "vim-commentary");
setup(&emulateReplaceWithRegister, false, "ReplaceWithRegister", {}, "ReplaceWithRegister");
setup(&emulateExchange, false, "exchange", {}, "vim-exchange");
setup(&emulateArgTextObj, false, "argtextobj", {}, "argtextobj.vim");
setup(&emulateSurround, false, "surround", {}, "vim-surround");
// Some polish
useFakeVim.setDisplayName(tr("Use Vim-style Editing"));
relativeNumber.setToolTip(tr("Displays line numbers relative to the line containing "
"text cursor."));
passControlKey.setToolTip(tr("Does not interpret key sequences like Ctrl-S in FakeVim "
"but handles them as regular shortcuts. This gives easier access to core functionality "
"at the price of losing some features of FakeVim."));
passKeys.setToolTip(tr("Does not interpret some key presses in insert mode so that "
"code can be properly completed and expanded."));
tabStop.setToolTip(tr("Vim tabstop option."));
#ifndef FAKEVIM_STANDALONE
backspace.setDisplayStyle(FvStringAspect::LineEditDisplay);
isKeyword.setDisplayStyle(FvStringAspect::LineEditDisplay);
const QString vimrcDefault = QLatin1String(HostOsInfo::isAnyUnixHost()
? "$HOME/.vimrc" : "%USERPROFILE%\\_vimrc");
vimRcPath.setExpectedKind(PathChooser::File);
vimRcPath.setToolTip(tr("Keep empty to use the default path, i.e. "
"%USERPROFILE%\\_vimrc on Windows, ~/.vimrc otherwise."));
vimRcPath.setPlaceHolderText(tr("Default: %1").arg(vimrcDefault));
vimRcPath.setDisplayStyle(FvStringAspect::PathChooserDisplay);
#endif
}
FakeVimSettings::~FakeVimSettings()
{
qDeleteAll(m_items);
}
FakeVimSettings::~FakeVimSettings() = default;
void FakeVimSettings::insertItem(int code, FakeVimAction *item,
const QString &longName, const QString &shortName)
FvBaseAspect *FakeVimSettings::item(const QString &name)
{
QTC_ASSERT(!m_items.contains(code), qDebug() << code; return);
m_items[code] = item;
if (!longName.isEmpty()) {
m_nameToCode[longName] = code;
m_codeToName[code] = longName;
}
if (!shortName.isEmpty())
m_nameToCode[shortName] = code;
}
void FakeVimSettings::readSettings(QSettings *settings)
{
foreach (FakeVimAction *item, m_items)
item->readSettings(settings);
}
void FakeVimSettings::writeSettings(QSettings *settings)
{
foreach (FakeVimAction *item, m_items)
item->writeSettings(settings);
}
FakeVimAction *FakeVimSettings::item(int code)
{
QTC_ASSERT(m_items.value(code, 0), qDebug() << "CODE: " << code; return nullptr);
return m_items.value(code, 0);
}
FakeVimAction *FakeVimSettings::item(const QString &name)
{
return m_items.value(m_nameToCode.value(name, -1), 0);
return m_nameToAspect.value(name, nullptr);
}
QString FakeVimSettings::trySetValue(const QString &name, const QString &value)
{
int code = m_nameToCode.value(name, -1);
if (code == -1)
FvBaseAspect *aspect = m_nameToAspect.value(name, nullptr);
if (!aspect)
return tr("Unknown option: %1").arg(name);
if (code == ConfigTabStop || code == ConfigShiftWidth) {
if (aspect == &tabStop || aspect == &shiftWidth) {
if (value.toInt() <= 0)
return tr("Argument must be positive: %1=%2")
.arg(name).arg(value);
}
FakeVimAction *act = item(code);
if (!act)
return tr("Unknown option: %1").arg(name);
act->setValue(value);
aspect->setValue(value);
return QString();
}
void FakeVimSettings::createAction(int code, const QVariant &value,
const QString &settingsKey,
const QString &shortKey)
void FakeVimSettings::setup(FvBaseAspect *aspect,
const QVariant &value,
const QString &settingsKey,
const QString &shortName,
const QString &labelText)
{
auto item = new FakeVimAction(nullptr);
item->setValue(value);
item->setSettingsKey("FakeVim", settingsKey);
item->setDefaultValue(value);
item->setCheckable(value.canConvert<bool>());
insertItem(code, item, settingsKey.toLower(), shortKey);
aspect->setSettingsKey("FakeVim", settingsKey);
aspect->setDefaultValue(value);
#ifndef FAKEVIM_STANDALONE
aspect->setLabelText(labelText);
aspect->setAutoApply(false);
registerAspect(aspect);
if (auto boolAspect = dynamic_cast<FvBoolAspect *>(aspect))
boolAspect->setLabelPlacement(FvBoolAspect::LabelPlacement::AtCheckBoxWithoutDummyLabel);
#else
Q_UNUSED(labelText)
#endif
const QString longName = settingsKey.toLower();
if (!longName.isEmpty()) {
m_nameToAspect[longName] = aspect;
m_aspectToName[aspect] = longName;
}
if (!shortName.isEmpty())
m_nameToAspect[shortName] = aspect;
}
FakeVimSettings *theFakeVimSettings()
FakeVimSettings *fakeVimSettings()
{
static FakeVimSettings s;
return &s;
}
FakeVimAction *theFakeVimSetting(int code)
{
return theFakeVimSettings()->item(code);
}
} // namespace Internal
} // namespace FakeVim