Bug 1077515 - part 8 - Minor refactoring of editor's handling of selection-move commands. r=roc
authorJonathan Kew <jkew@mozilla.com>
Sat, 22 Nov 2014 14:39:04 +0000
changeset 217002 3260d0792a327498596855295ec32f604c8164a2
parent 217001 e8d6b1cf145c6ccc3badd355f2ccebb76cb96bba
child 217003 228cc7984e2f89cd09cc62aec86c477a61e69364
push id27871
push usercbook@mozilla.com
push dateMon, 24 Nov 2014 12:32:57 +0000
treeherdermozilla-central@b8e1741b7d5c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersroc
bugs1077515
milestone36.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1077515 - part 8 - Minor refactoring of editor's handling of selection-move commands. r=roc
editor/libeditor/nsEditorCommands.cpp
--- a/editor/libeditor/nsEditorCommands.cpp
+++ b/editor/libeditor/nsEditorCommands.cpp
@@ -678,16 +678,56 @@ nsSelectionMoveCommands::IsCommandEnable
   nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
   if (editor)
     return editor->GetIsSelectionEditable(outCmdEnabled);
 
   *outCmdEnabled = false;
   return NS_OK;
 }
 
+static const struct ScrollCommand {
+  const char *reverseScroll;
+  const char *forwardScroll;
+  nsresult (NS_STDCALL nsISelectionController::*scroll)(bool);
+} scrollCommands[] = {
+  { "cmd_scrollTop", "cmd_scrollBottom",
+    &nsISelectionController::CompleteScroll },
+  { "cmd_scrollPageUp", "cmd_scrollPageDown",
+    &nsISelectionController::ScrollPage },
+  { "cmd_scrollLineUp", "cmd_scrollLineDown",
+    &nsISelectionController::ScrollLine }
+};
+
+static const struct MoveCommand {
+  const char *reverseMove;
+  const char *forwardMove;
+  const char *reverseSelect;
+  const char *forwardSelect;
+  nsresult (NS_STDCALL nsISelectionController::*move)(bool, bool);
+} moveCommands[] = {
+  { "cmd_charPrevious", "cmd_charNext",
+    "cmd_selectCharPrevious", "cmd_selectCharNext",
+    &nsISelectionController::CharacterMove },
+  { "cmd_linePrevious", "cmd_lineNext",
+    "cmd_selectLinePrevious", "cmd_selectLineNext",
+    &nsISelectionController::LineMove },
+  { "cmd_wordPrevious", "cmd_wordNext",
+    "cmd_selectWordPrevious", "cmd_selectWordNext",
+    &nsISelectionController::WordMove },
+  { "cmd_beginLine", "cmd_endLine",
+    "cmd_selectBeginLine", "cmd_selectEndLine",
+    &nsISelectionController::IntraLineMove },
+  { "cmd_movePageUp", "cmd_movePageDown",
+    "cmd_selectPageUp", "cmd_selectPageDown",
+    &nsISelectionController::PageMove },
+  { "cmd_moveTop", "cmd_moveBottom",
+    "cmd_selectTop", "cmd_selectBottom",
+    &nsISelectionController::CompleteMove }
+};
+
 NS_IMETHODIMP
 nsSelectionMoveCommands::DoCommand(const char *aCommandName,
                                    nsISupports *aCommandRefCon)
 {
   nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
   NS_ENSURE_TRUE(editor, NS_ERROR_FAILURE);
 
   nsCOMPtr<nsIDOMDocument> domDoc;
@@ -699,94 +739,40 @@ nsSelectionMoveCommands::DoCommand(const
     doc->FlushPendingNotifications(Flush_Layout);
   }
 
   nsCOMPtr<nsISelectionController> selCont;
   nsresult rv = editor->GetSelectionController(getter_AddRefs(selCont)); 
   NS_ENSURE_SUCCESS(rv, rv);
   NS_ENSURE_TRUE(selCont, NS_ERROR_FAILURE);
 
-  // complete scroll commands
-  if (!nsCRT::strcmp(aCommandName,"cmd_scrollTop"))
-    return selCont->CompleteScroll(false);
-  else if (!nsCRT::strcmp(aCommandName,"cmd_scrollBottom"))
-    return selCont->CompleteScroll(true);
-
-  // complete move commands
-  else if (!nsCRT::strcmp(aCommandName,"cmd_moveTop"))
-    return selCont->CompleteMove(false, false);
-  else if (!nsCRT::strcmp(aCommandName,"cmd_moveBottom"))
-    return selCont->CompleteMove(true, false);
-  else if (!nsCRT::strcmp(aCommandName,"cmd_selectTop"))
-    return selCont->CompleteMove(false, true);
-  else if (!nsCRT::strcmp(aCommandName,"cmd_selectBottom"))
-    return selCont->CompleteMove(true, true);
-
-  // line move commands
-  else if (!nsCRT::strcmp(aCommandName,"cmd_lineNext"))
-    return selCont->LineMove(true, false);
-  else if (!nsCRT::strcmp(aCommandName,"cmd_linePrevious"))
-    return selCont->LineMove(false, false);
-  else if (!nsCRT::strcmp(aCommandName,"cmd_selectLineNext"))
-    return selCont->LineMove(true, true);
-  else if (!nsCRT::strcmp(aCommandName,"cmd_selectLinePrevious"))
-    return selCont->LineMove(false, true);
-
-  // character move commands
-  else if (!nsCRT::strcmp(aCommandName,"cmd_charPrevious"))
-    return selCont->CharacterMove(false, false);
-  else if (!nsCRT::strcmp(aCommandName,"cmd_charNext"))
-    return selCont->CharacterMove(true, false);
-  else if (!nsCRT::strcmp(aCommandName,"cmd_selectCharPrevious"))
-    return selCont->CharacterMove(false, true);
-  else if (!nsCRT::strcmp(aCommandName,"cmd_selectCharNext"))
-    return selCont->CharacterMove(true, true);
+  // scroll commands
+  for (size_t i = 0; i < ArrayLength(scrollCommands); i++) {
+    const ScrollCommand& cmd = scrollCommands[i];
+    if (!nsCRT::strcmp(aCommandName, cmd.reverseScroll)) {
+      return (selCont->*(cmd.scroll))(false);
+    } else if (!nsCRT::strcmp(aCommandName, cmd.forwardScroll)) {
+      return (selCont->*(cmd.scroll))(true);
+    }
+  }
 
-  // intra line move commands
-  else if (!nsCRT::strcmp(aCommandName,"cmd_beginLine"))
-    return selCont->IntraLineMove(false, false);
-  else if (!nsCRT::strcmp(aCommandName,"cmd_endLine"))
-    return selCont->IntraLineMove(true, false);
-  else if (!nsCRT::strcmp(aCommandName,"cmd_selectBeginLine"))
-    return selCont->IntraLineMove(false, true);
-  else if (!nsCRT::strcmp(aCommandName,"cmd_selectEndLine"))
-    return selCont->IntraLineMove(true, true);
-  
-  // word move commands
-  else if (!nsCRT::strcmp(aCommandName,"cmd_wordPrevious"))
-    return selCont->WordMove(false, false);
-  else if (!nsCRT::strcmp(aCommandName,"cmd_wordNext"))
-    return selCont->WordMove(true, false);
-  else if (!nsCRT::strcmp(aCommandName,"cmd_selectWordPrevious"))
-    return selCont->WordMove(false, true);
-  else if (!nsCRT::strcmp(aCommandName,"cmd_selectWordNext"))
-    return selCont->WordMove(true, true);
-  
-  // scroll page commands
-  else if (!nsCRT::strcmp(aCommandName,"cmd_scrollPageUp"))
-    return selCont->ScrollPage(false);
-  else if (!nsCRT::strcmp(aCommandName,"cmd_scrollPageDown"))
-    return selCont->ScrollPage(true);
-  
-  // scroll line commands
-  else if (!nsCRT::strcmp(aCommandName,"cmd_scrollLineUp"))
-    return selCont->ScrollLine(false);
-  else if (!nsCRT::strcmp(aCommandName,"cmd_scrollLineDown"))
-    return selCont->ScrollLine(true);
-  
-  // page move commands
-  else if (!nsCRT::strcmp(aCommandName,"cmd_movePageUp"))
-    return selCont->PageMove(false, false);
-  else if (!nsCRT::strcmp(aCommandName,"cmd_movePageDown"))
-    return selCont->PageMove(true, false);
-  else if (!nsCRT::strcmp(aCommandName,"cmd_selectPageUp"))
-    return selCont->PageMove(false, true);
-  else if (!nsCRT::strcmp(aCommandName,"cmd_selectPageDown"))
-    return selCont->PageMove(true, true);
-    
+  // caret movement/selection commands
+  for (size_t i = 0; i < ArrayLength(moveCommands); i++) {
+    const MoveCommand& cmd = moveCommands[i];
+    if (!nsCRT::strcmp(aCommandName, cmd.reverseMove)) {
+      return (selCont->*(cmd.move))(false, false);
+    } else if (!nsCRT::strcmp(aCommandName, cmd.forwardMove)) {
+      return (selCont->*(cmd.move))(true, false);
+    } else if (!nsCRT::strcmp(aCommandName, cmd.reverseSelect)) {
+      return (selCont->*(cmd.move))(false, true);
+    } else if (!nsCRT::strcmp(aCommandName, cmd.forwardSelect)) {
+      return (selCont->*(cmd.move))(true, true);
+    }
+  }
+
   return NS_ERROR_FAILURE;
 }
 
 NS_IMETHODIMP 
 nsSelectionMoveCommands::DoCommandParams(const char *aCommandName,
                                          nsICommandParams *aParams,
                                          nsISupports *aCommandRefCon)
 {