Commit 7cd0b5d3 authored by Erlend Hamberg's avatar Erlend Hamberg
Browse files

added “gq” command (re-format text)

this command re-formats (joins the lines and then wrap them) according to
the text width set.

tests are included.
parent a326ee27
......@@ -182,6 +182,10 @@ void ViModeTest::VisualModeTests() {
DoTest("foobar","Vra","aaaaaa");
DoTest("foo\nbar","jlvklrx","fox\nxxr");
DoTest("123\n123","l\\ctrl-vljrx","1xx\n1xx");
// Testing "gq"
DoTest("foo\nbar\nbaz","Vgq","foo\nbar\nbaz");
DoTest("foo\nbar\nbaz","Vjgq","foo bar\nbaz");
}
void ViModeTest::InsertModeTests() {
......@@ -519,6 +523,20 @@ void ViModeTest::NormalModeCommandsTest() {
"lmajlmb`a`bj\\ctrl-o\\ctrl-ix",
"Foo foo.\nBar bar.\nBa baz.");
// Testing "gq" (reformat) text
DoTest("foo\nbar", "gqq", "foo\nbar");
DoTest("foo\nbar", "2gqq", "foo bar");
DoTest("foo\nbar\nbaz", "jgqj", "foo\nbar baz");
// when setting the text to wrap at column 10, this should be re-formatted to
// span several lines ...
kate_document->setWordWrapAt( 10 );
DoTest("foo bar foo bar foo bar", "gqq", "foo bar\nfoo bar\nfoo bar");
// ... and when re-setting it to column 80 again, they should be joined again
kate_document->setWordWrapAt( 80 );
DoTest("foo bar\nfoo bar\nfoo bar", "gqG", "foo bar foo bar foo bar");
}
......
......@@ -915,21 +915,17 @@ bool KateViNormalMode::commandJoinLines()
// remember line length so the cursor can be put between the joined lines
int l = doc()->lineLength( c.line() );
int n = getCount();
unsigned int from = c.line();
unsigned int to = c.line()+getCount();
// if we were given a range of lines, this information overrides the previous
if ( m_commandRange.startLine != -1 && m_commandRange.endLine != -1 ) {
m_commandRange.normalize();
c.setLine ( m_commandRange.startLine );
n = m_commandRange.endLine-m_commandRange.startLine;
to = m_commandRange.endLine;
}
// make sure we don't try to join lines past the document end
if ( n > doc()->lines()-1-c.line() ) {
n = doc()->lines()-1-c.line();
}
doc()->joinLines( c.line(), c.line()+n );
joinLines( from, to );
// position cursor between the joined lines
c.setColumn( l );
......@@ -1596,6 +1592,20 @@ bool KateViNormalMode::commandSwitchToPrevTab() {
return true;
}
bool KateViNormalMode::commandFormatLine()
{
Cursor c( m_view->cursorPosition() );
reformatLines( c.line(), c.line()+getCount()-1 );
return true;
}
bool KateViNormalMode::commandFormatLines()
{
reformatLines( m_commandRange.startLine, m_commandRange.endLine );
return true;
}
////////////////////////////////////////////////////////////////////////////////
......@@ -2798,6 +2808,8 @@ void KateViNormalMode::initializeCommands()
ADDCMD("gt", commandSwitchToNextTab,0);
ADDCMD("gT", commandSwitchToPrevTab,0);
ADDCMD("gqq", commandFormatLine, IS_CHANGE);
ADDCMD("gq", commandFormatLines, IS_CHANGE | NEEDS_MOTION);
// regular motions
......@@ -2937,3 +2949,22 @@ OperationMode KateViNormalMode::getOperationMode() const
return m;
}
void KateViNormalMode::joinLines(unsigned int from, unsigned int to) const
{
// make sure we don't try to join lines past the document end
if ( to >= (unsigned int)(doc()->lines()) ) {
to = doc()->lines()-1;
}
// joining one line is a no-op
if ( from == to ) return;
doc()->joinLines( from, to );
}
void KateViNormalMode::reformatLines(unsigned int from, unsigned int to) const
{
joinLines( from, to );
doc()->wrapText( from, to );
}
......@@ -152,6 +152,9 @@ class KATEPART_TESTS_EXPORT KateViNormalMode : public KateViModeBase
bool commandSwitchToNextTab();
bool commandSwitchToPrevTab();
bool commandFormatLine();
bool commandFormatLines();
// MOTIONS
KateViRange motionLeft();
......@@ -260,6 +263,9 @@ class KATEPART_TESTS_EXPORT KateViNormalMode : public KateViModeBase
void executeCommand( const KateViCommand* cmd );
OperationMode getOperationMode() const;
void joinLines(unsigned int from, unsigned int to) const;
void reformatLines(unsigned int from, unsigned int to) const;
QString m_keys;
unsigned int m_countTemp;
bool m_findWaitingForChar;
......
......@@ -325,6 +325,7 @@ void KateViVisualMode::initializeCommands()
ADDCMD("~", commandChangeCase, IS_CHANGE );
ADDCMD("I", commandPrependToBlock, IS_CHANGE );
ADDCMD("A", commandAppendToBlock, IS_CHANGE );
ADDCMD("gq", commandFormatLines, IS_CHANGE );
// regular motions
ADDMOTION("h", motionLeft, 0 );
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment