move params
authoranna <Anna.Kozlova@jetbrains.com>
Mon, 13 Sep 2010 09:57:16 +0000 (13:57 +0400)
committeranna <Anna.Kozlova@jetbrains.com>
Mon, 13 Sep 2010 11:45:30 +0000 (15:45 +0400)
java/java-impl/src/com/intellij/refactoring/changeSignature/JavaChangeSignatureDetector.java
platform/lang-impl/src/com/intellij/refactoring/changeSignature/ChangeSignatureGestureDetector.java
platform/lang-impl/src/com/intellij/refactoring/changeSignature/LanguageChangeSignatureDetector.java
platform/lang-impl/src/com/intellij/refactoring/changeSignature/MoveParameterAction.java [new file with mode: 0644]
platform/lang-impl/src/com/intellij/refactoring/changeSignature/MoveParameterLeftAction.java [new file with mode: 0644]
platform/lang-impl/src/com/intellij/refactoring/changeSignature/MoveParameterRightAction.java [new file with mode: 0644]
platform/platform-resources-en/src/messages/ActionsBundle.properties
resources/src/idea/IdeaActions.xml

index c71f7674f9b4aec737f8d4292ec04179b1f4c799..c9102b3fb4c4d78fa75f184610e0dc82cc30912f 100644 (file)
 package com.intellij.refactoring.changeSignature;
 
 import com.intellij.openapi.application.ApplicationManager;
+import com.intellij.openapi.application.Result;
 import com.intellij.openapi.command.CommandProcessor;
+import com.intellij.openapi.command.WriteCommandAction;
 import com.intellij.openapi.diagnostic.Logger;
 import com.intellij.openapi.editor.Document;
+import com.intellij.openapi.editor.Editor;
 import com.intellij.openapi.util.Comparing;
 import com.intellij.openapi.util.TextRange;
 import com.intellij.psi.*;
@@ -254,6 +257,52 @@ public class JavaChangeSignatureDetector implements LanguageChangeSignatureDetec
     return method != null && isInsideMethodSignature(element, method) && Comparing.equal(method, bannedInfo.getMethod());
   }
 
+  @Override
+  public boolean isMoveParameterAvailable(PsiElement element, boolean left) {
+    if (element instanceof PsiParameter) {
+      final PsiParameter parameter = (PsiParameter)element;
+      final PsiElement declarationScope = parameter.getDeclarationScope();
+      if (declarationScope instanceof PsiMethod) {
+        final PsiMethod method = (PsiMethod)declarationScope;
+        final int parameterIndex = method.getParameterList().getParameterIndex(parameter);
+        if (left) {
+          return parameterIndex > 0;
+        } else {
+          return parameterIndex < method.getParameterList().getParametersCount() - 1;
+        }
+      }
+    }
+    return false;
+  }
+
+  @Override
+  public void moveParameter(final PsiElement element, final Editor editor, final boolean left) {
+    final PsiParameter parameter = (PsiParameter)element;
+    final PsiMethod method = (PsiMethod)parameter.getDeclarationScope();
+    final int parameterIndex = method.getParameterList().getParameterIndex(parameter);
+    new WriteCommandAction(element.getProject(), MOVE_PARAMETER){
+      @Override
+      protected void run(Result result) throws Throwable {
+        final PsiParameterList parameterList = method.getParameterList();
+        final PsiParameter[] parameters = parameterList.getParameters();
+        final int deltaOffset = editor.getCaretModel().getOffset() - parameter.getTextRange().getStartOffset();
+        final PsiParameter frst = left ? parameters[parameterIndex - 1] : parameter;
+        final PsiParameter scnd = left ? parameter : parameters[parameterIndex + 1];
+        final int startOffset = frst.getTextRange().getStartOffset();
+        final int endOffset = scnd.getTextRange().getEndOffset();
+
+        final PsiFile file = method.getContainingFile();
+        final Document document = PsiDocumentManager.getInstance(getProject()).getDocument(file);
+        if (document != null) {
+          final String comma_whitespace_between =
+            document.getText().substring(frst.getTextRange().getEndOffset(), scnd.getTextRange().getStartOffset());
+          document.replaceString(startOffset, endOffset, scnd.getText() + comma_whitespace_between + frst.getText());
+          editor.getCaretModel().moveToOffset(document.getText().indexOf(parameter.getText(), startOffset) + deltaOffset);
+        }
+      }
+    }.execute();
+  }
+
   private static boolean isInsideMethodSignature(PsiElement element, @NotNull PsiMethod method) {
     final PsiCodeBlock body = method.getBody();
     if (body != null) {
index a9bce25662fc7e504607e67d99d37cacbc3400e2..a350fd739b122352278f9ecdf54f008c2190a429 100644 (file)
@@ -216,6 +216,7 @@ public class ChangeSignatureGestureDetector extends PsiTreeChangeAdapter impleme
           final String currentCommandName = processor.getCurrentCommandName();
           if (!Comparing.strEqual(EditorBundle.message("typing.in.editor.command.name"), currentCommandName) &&
               !Comparing.strEqual(EditorBundle.message("paste.command.name"), currentCommandName) &&
+              !Comparing.strEqual(LanguageChangeSignatureDetector.MOVE_PARAMETER, currentCommandName) &&
               !Comparing.equal(EditorActionUtil.DELETE_COMMAND_GROUP, processor.getCurrentCommandGroupId())) {
             return;
           }
index 58f8cd5f4829be882401ed99897c2051b5022f72..3ad1dadf9d138e2ae510b6484afeb7fc169f63a8 100644 (file)
@@ -15,6 +15,7 @@
  */
 package com.intellij.refactoring.changeSignature;
 
+import com.intellij.openapi.editor.Editor;
 import com.intellij.openapi.util.TextRange;
 import com.intellij.psi.PsiElement;
 import org.jetbrains.annotations.NotNull;
@@ -25,7 +26,7 @@ import org.jetbrains.annotations.Nullable;
  * Date: Sep 6, 2010
  */
 public interface LanguageChangeSignatureDetector {
-
+  String MOVE_PARAMETER = "Parameter Move";
 
   @Nullable
   ChangeInfo createCurrentChangeSignature(final @NotNull PsiElement element,
@@ -39,4 +40,8 @@ public interface LanguageChangeSignatureDetector {
   TextRange getHighlightingRange(PsiElement element);
 
   boolean wasBanned(PsiElement element, @NotNull ChangeInfo bannedInfo);
+
+  boolean isMoveParameterAvailable(PsiElement parameter, boolean left);
+
+  void moveParameter(PsiElement parameter, Editor editor, boolean left);
 }
diff --git a/platform/lang-impl/src/com/intellij/refactoring/changeSignature/MoveParameterAction.java b/platform/lang-impl/src/com/intellij/refactoring/changeSignature/MoveParameterAction.java
new file mode 100644 (file)
index 0000000..b43f4bf
--- /dev/null
@@ -0,0 +1,47 @@
+package com.intellij.refactoring.changeSignature;
+
+import com.intellij.openapi.actionSystem.*;
+import com.intellij.openapi.diagnostic.Logger;
+import com.intellij.openapi.editor.Editor;
+import com.intellij.psi.PsiElement;
+
+/**
+ * User: anna
+ * Date: Sep 10, 2010
+ */
+public abstract class MoveParameterAction extends AnAction{
+  private final boolean myLeft;
+  private static final Logger LOG = Logger.getInstance("#" + MoveParameterAction.class.getName());
+
+  public MoveParameterAction(boolean left) {
+    super();
+    myLeft = left;
+  }
+
+  @Override
+  public void actionPerformed(AnActionEvent e) {
+    final DataContext dataContext = e.getDataContext();
+    final PsiElement psiElement = LangDataKeys.PSI_ELEMENT.getData(dataContext);
+    LOG.assertTrue(psiElement != null);
+    final Editor editor = PlatformDataKeys.EDITOR.getData(dataContext);
+    LanguageChangeSignatureDetectors.INSTANCE.forLanguage(psiElement.getLanguage()).moveParameter(psiElement, editor, myLeft);
+  }
+
+
+  @Override
+  public void update(AnActionEvent e) {
+    final Presentation presentation = e.getPresentation();
+    presentation.setEnabled(false);
+    final DataContext dataContext = e.getDataContext();
+    final Editor editor = PlatformDataKeys.EDITOR.getData(dataContext);
+    if (editor != null) {
+      final PsiElement psiElement = LangDataKeys.PSI_ELEMENT.getData(dataContext);
+      if (psiElement != null) {
+        final LanguageChangeSignatureDetector detector = LanguageChangeSignatureDetectors.INSTANCE.forLanguage(psiElement.getLanguage());
+        if (detector != null) {
+          presentation.setEnabled(detector.isMoveParameterAvailable(psiElement, myLeft));
+        }
+      }
+    }
+  }
+}
diff --git a/platform/lang-impl/src/com/intellij/refactoring/changeSignature/MoveParameterLeftAction.java b/platform/lang-impl/src/com/intellij/refactoring/changeSignature/MoveParameterLeftAction.java
new file mode 100644 (file)
index 0000000..c380d37
--- /dev/null
@@ -0,0 +1,26 @@
+/*
+ * Copyright 2000-2010 JetBrains s.r.o.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.intellij.refactoring.changeSignature;
+
+/**
+ * User: anna
+ * Date: Sep 10, 2010
+ */
+public class MoveParameterLeftAction extends MoveParameterAction {
+  public MoveParameterLeftAction() {
+    super(true);
+  }
+}
diff --git a/platform/lang-impl/src/com/intellij/refactoring/changeSignature/MoveParameterRightAction.java b/platform/lang-impl/src/com/intellij/refactoring/changeSignature/MoveParameterRightAction.java
new file mode 100644 (file)
index 0000000..8f2eb78
--- /dev/null
@@ -0,0 +1,26 @@
+/*
+ * Copyright 2000-2010 JetBrains s.r.o.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.intellij.refactoring.changeSignature;
+
+/**
+ * User: anna
+ * Date: Sep 10, 2010
+ */
+public class MoveParameterRightAction extends MoveParameterAction {
+  public MoveParameterRightAction() {
+    super(false);
+  }
+}
index 44c952d3b7d23f63e06e0c5d3632ec7784d9d503..3d6c6d60217b70e67b215075640c0b907493b832 100644 (file)
@@ -477,6 +477,10 @@ action.RenameElement.text=_Rename...
 action.RenameElement.description=Rename the selected symbol and correct all references
 action.ChangeSignature.text=Change Si_gnature...
 action.ChangeSignature.description=Change signature of the selected method or class and correct all references
+action.MoveToTheLeft.text=Move parameter left
+action.MoveToTheLeft.description=Move parameter left
+action.MoveToTheRight.text=Move parameter right
+action.MoveToTheRight.description=Move parameter right
 action.ChangeTypeSignature.text=T_ype Migration...
 action.ChangeTypeSignature.description=Change type of the return type of the method, field, parameter, variable or class type argumeants and correct all references
 action.MakeStatic.text=Make S_tatic...
index d6d023194d465476322307309c6d9c679e4e579e..7de13202c34941e131ad4c5bd72d8d007dd0393f 100644 (file)
 
     <group id="RefactoringMenu1">
       <action id="ChangeSignature" class="com.intellij.refactoring.actions.ChangeSignatureAction"/>
+      <action id="MoveToTheLeft" class="com.intellij.refactoring.changeSignature.MoveParameterLeftAction"/>
+      <action id="MoveToTheRight" class="com.intellij.refactoring.changeSignature.MoveParameterRightAction"/>
       <action id="ChangeTypeSignature"
               class="com.intellij.refactoring.typeMigration.actions.ChangeTypeSignatureAction"
               text="T_ype Migration..."