FakeVim: Merge comments when joining lines with 'J'

This is the default behavior in vim as well

Change-Id: Ia4d56e3cfc7f91fc353078daefaabeada9a86bed
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
Tom Praschan
2021-02-06 20:09:23 +01:00
parent b6f26ed67a
commit 509d5895f8
5 changed files with 34 additions and 0 deletions

View File

@@ -108,6 +108,8 @@
\li \c :undo, \c :redo
\li \c :normal
\li \c :<, \c :>
\li \c{set formatoptions=}, see \c{:h fo-table} in the Vim documentation.
Currently supported letters: \c fo-j
\endlist
\section2 Plugin Emulation

View File

@@ -4071,6 +4071,17 @@ void FakeVimPlugin::test_vim_command_J()
KEYS("3J", lmid(0, 5) + " " + lmid(5, 1) + " " + lmid(6, 1).mid(4) + "| " + lmid(7));
KEYS("uu", lmid(0, 4) + "\nint |main(int argc, char *argv[])\n" + lmid(5));
COMMAND("redo", lmid(0, 4) + "\nint |main(int argc, char *argv[]) " + lmid(5));
// Joining comments
data.doCommand("set formatoptions=f");
data.setText("// abc" N "// def");
KEYS("J", "// abc def");
data.setText("/*" N X "* abc" N "* def" N "*/");
KEYS("J", "/*" N "* abc def" N "*/");
data.setText("# abc" N "# def");
KEYS("J", "# abc def");
}
void FakeVimPlugin::test_vim_command_put_at_eol()

View File

@@ -113,6 +113,7 @@ FakeVimSettings::FakeVimSettings()
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");
// Emulated plugins
createAction(ConfigEmulateVimCommentary, false, "commentary");

View File

@@ -107,6 +107,7 @@ enum FakeVimSettingsCode
ConfigShowCmd,
ConfigScrollOff,
ConfigRelativeNumber,
ConfigFormatOptions,
// Plugin emulation
ConfigEmulateVimCommentary,

View File

@@ -7615,6 +7615,13 @@ void FakeVimHandler::Private::joinLines(int count, bool preserveSpace)
{
int pos = position();
const int blockNumber = m_cursor.blockNumber();
const QString currentLine = lineContents(blockNumber + 1);
const bool startingLineIsComment
= currentLine.contains(QRegularExpression("^\\s*\\/\\/")) // Cpp-style
|| currentLine.contains(QRegularExpression("^\\s*\\/?\\*")) // C-style
|| currentLine.contains(QRegularExpression("^\\s*#")); // Python/Shell-style
for (int i = qMax(count - 2, 0); i >= 0 && blockNumber < document()->blockCount(); --i) {
moveBehindEndOfLine();
pos = position();
@@ -7625,6 +7632,18 @@ void FakeVimHandler::Private::joinLines(int count, bool preserveSpace)
} else {
while (characterAtCursor() == ' ' || characterAtCursor() == '\t')
moveRight();
// If the line we started from is a comment, remove the comment string from the next line
if (startingLineIsComment && config(ConfigFormatOptions).toString().contains('f')) {
if (characterAtCursor() == '/' && characterAt(position() + 1) == '/')
moveRight(2);
else if (characterAtCursor() == '*' || characterAtCursor() == '#')
moveRight(1);
if (characterAtCursor() == ' ')
moveRight();
}
m_cursor.insertText(QString(' '));
}
}