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.*;
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) {
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;
}
*/
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;
* Date: Sep 6, 2010
*/
public interface LanguageChangeSignatureDetector {
-
+ String MOVE_PARAMETER = "Parameter Move";
@Nullable
ChangeInfo createCurrentChangeSignature(final @NotNull PsiElement element,
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);
}
--- /dev/null
+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));
+ }
+ }
+ }
+ }
+}
--- /dev/null
+/*
+ * 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);
+ }
+}
--- /dev/null
+/*
+ * 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);
+ }
+}
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...
<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..."