summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorErlend Hamberg <ehamberg@gmail.com>2011-08-12 13:06:56 (GMT)
committerErlend Hamberg <ehamberg@gmail.com>2011-08-12 13:11:12 (GMT)
commit7cd0b5d3747c7bdaf2e5f2a08ec562dc9d97c6ad (patch)
tree13ec5d5362d33d941e5cd6285105578a933ebe8a
parenta326ee270fd4a22d7420fe91c7fc1afd5a8b3a6f (diff)
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.
-rw-r--r--part/tests/vimode_test.cpp18
-rw-r--r--part/vimode/katevinormalmode.cpp47
-rw-r--r--part/vimode/katevinormalmode.h6
-rw-r--r--part/vimode/katevivisualmode.cpp1
4 files changed, 64 insertions, 8 deletions
diff --git a/part/tests/vimode_test.cpp b/part/tests/vimode_test.cpp
index 745b476..2a404ff 100644
--- a/part/tests/vimode_test.cpp
+++ b/part/tests/vimode_test.cpp
@@ -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");
}
diff --git a/part/vimode/katevinormalmode.cpp b/part/vimode/katevinormalmode.cpp
index 79bae19..079ccfd 100644
--- a/part/vimode/katevinormalmode.cpp
+++ b/part/vimode/katevinormalmode.cpp
@@ -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 );
+}
diff --git a/part/vimode/katevinormalmode.h b/part/vimode/katevinormalmode.h
index 9cb5913..91a5215 100644
--- a/part/vimode/katevinormalmode.h
+++ b/part/vimode/katevinormalmode.h
@@ -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;
diff --git a/part/vimode/katevivisualmode.cpp b/part/vimode/katevivisualmode.cpp
index dbdd502..74a7cd4 100644
--- a/part/vimode/katevivisualmode.cpp
+++ b/part/vimode/katevivisualmode.cpp
@@ -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 );