Patch #3304 2012-07-10 01:41

alpha0010

Move line(s) up/down (shortcut)
Download
3304-Move_line_s_up.patch (4.5 KB)
Category
Application::FeatureAdd
Status
Accepted
Close date
2012-07-14 07:44
Assigned to
mortenmacfly
Index: src/src/main.h
===================================================================
--- src/src/main.h    (revision 8097)
+++ src/src/main.h    (working copy)
@@ -165,6 +165,7 @@
         void OnEditLineTranspose(wxCommandEvent& event);
         void OnEditLineCopy(wxCommandEvent& event);
         void OnEditLinePaste(wxCommandEvent& event);
+        void OnEditLineMove(wxCommandEvent& event);
         void OnEditUpperCase(wxCommandEvent& event);
         void OnEditLowerCase(wxCommandEvent& event);
         void OnEditInsertNewLine(wxCommandEvent& event);
Index: src/src/resources/main_menu.xrc
===================================================================
--- src/src/resources/main_menu.xrc    (revision 8097)
+++ src/src/resources/main_menu.xrc    (working copy)
@@ -418,6 +418,16 @@
             <label>Paste</label>
             <help>Paste before current line</help>
           </object>
+          <object class="wxMenuItem" name="idEditLineUp">
+            <label>Move up</label>
+            <accel>Alt-UP</accel>
+            <help>Transpose current line(s) up</help>
+          </object>
+          <object class="wxMenuItem" name="idEditLineDown">
+            <label>Move down</label>
+            <accel>Alt-DOWN</accel>
+            <help>Transpose current line(s) down</help>
+          </object>
         </object>
         <object class="wxMenu" name="idEditSpecialCommandsCase">
           <label>Case</label>
Index: src/src/main.cpp
===================================================================
--- src/src/main.cpp    (revision 8097)
+++ src/src/main.cpp    (working copy)
@@ -226,6 +226,8 @@
 int idEditLineTranspose           = XRCID("idEditLineTranspose");
 int idEditLineCopy                = XRCID("idEditLineCopy");
 int idEditLinePaste               = XRCID("idEditLinePaste");
+int idEditLineUp                  = XRCID("idEditLineUp");
+int idEditLineDown                = XRCID("idEditLineDown");
 int idEditSpecialCommandsCase     = XRCID("idEditSpecialCommandsCase");
 int idEditUpperCase               = XRCID("idEditUpperCase");
 int idEditLowerCase               = XRCID("idEditLowerCase");
@@ -444,6 +446,8 @@
     EVT_MENU(idEditLineTranspose,         MainFrame::OnEditLineTranspose)
     EVT_MENU(idEditLineCopy,              MainFrame::OnEditLineCopy)
     EVT_MENU(idEditLinePaste,             MainFrame::OnEditLinePaste)
+    EVT_MENU(idEditLineUp,                MainFrame::OnEditLineMove)
+    EVT_MENU(idEditLineDown,              MainFrame::OnEditLineMove)
     EVT_MENU(idEditUpperCase,             MainFrame::OnEditUpperCase)
     EVT_MENU(idEditLowerCase,             MainFrame::OnEditLowerCase)
     EVT_MENU(idEditInsertNewLine,         MainFrame::OnEditInsertNewLine)
@@ -3239,6 +3243,47 @@
     }
 }
 
+void MainFrame::OnEditLineMove(wxCommandEvent& event)
+{
+    cbEditor* ed = Manager::Get()->GetEditorManager()->GetBuiltinActiveEditor();
+    if (ed)
+    {
+        cbStyledTextCtrl* ctrl = ed->GetControl();
+        if (ctrl->GetSelections() > 1)
+            return;
+        int startPos = ctrl->PositionFromLine(ctrl->LineFromPosition(ctrl->GetSelectionStart()));
+        int endPos = ctrl->PositionFromLine(ctrl->LineFromPosition(ctrl->GetSelectionEnd()) + 1) - 1;
+        if (event.GetId() == idEditLineUp)
+        {
+            if (startPos < 2)
+                return;
+            ctrl->BeginUndoAction();
+            wxString line = ctrl->GetTextRange(ctrl->PositionFromLine(ctrl->LineFromPosition(startPos) - 1),
+                                               startPos);
+            ctrl->InsertText(endPos + 1, line);
+            ctrl->DeleteRange(startPos - line.Length(), line.Length());
+            startPos -= line.Length();
+            endPos -= line.Length();
+            ctrl->EndUndoAction();
+        }
+        else
+        {
+            if (ctrl->LineFromPosition(ctrl->GetSelectionEnd()) == ctrl->GetLineCount())
+                return;
+            ctrl->BeginUndoAction();
+            wxString line = ctrl->GetTextRange(endPos + 1,
+                                               ctrl->PositionFromLine(ctrl->LineFromPosition(endPos + 1) + 1));
+            ctrl->DeleteRange(endPos + 1, line.Length());
+            ctrl->InsertText(startPos, line);
+            startPos += line.Length();
+            endPos += line.Length();
+            ctrl->EndUndoAction();
+        }
+        ctrl->SetSelectionStart(startPos);
+        ctrl->SetSelectionEnd(endPos);
+    }
+}
+
 void MainFrame::OnEditUpperCase(wxCommandEvent& /*event*/)
 {
     cbEditor* ed = Manager::Get()->GetEditorManager()->GetBuiltinActiveEditor();
mortenmacfly 2012-07-10 05:52

Nice one!

But you have to be careful here:

- Did you try with rectangle selection mode?

- Did you try with virtual space mode?

mortenmacfly 2012-07-10 06:09

...maybe it can be as simple as that you just return, if the selection is rectangular or virtual space is selected (not sure about the second on). In rectangle selection mode you cannot do anything meaningful anyways IMHO...

alpha0010 2012-07-11 12:28

Virtual space and rectangular select already work (all lines that have selection somewhere on them are treated as selected).

This revision fixes the multiple selection case (by avoiding it with a return).

mortenmacfly 2012-07-11 12:38

I know it was "somewhat" working with rectangular. My point is, does the user expect the whole lines move when (s)he has selected a rectangular area? I believe not. I would expect only the rectangular area moves which is not the case... what about you?

Will try the update though...

alpha0010 2012-07-13 00:28

This is the behavior I would expect (it is under the 'Line' menu, and therefore effects lines as whole units), however that is only me. I am not certain what other users would think of this.

mortenmacfly 2012-07-13 08:18

Hmmm I see. I wasn't even aware its under the "Line" menu, as I always use short-cuts a lot. I also see that is might be feasible (because easier) to just roughly select the lines for the movement, so not exactly the whole line. Is it worth an option? I think not... I would wonder what behaviour the majority of users expects and go for that... when committing, I'll place a ToDo in the code accordingly.

alpha0010 2012-07-13 20:44

I am not sure if what you said is a feature request or a comment, but current behavior expands rough selections to the nearest whole line.