rename initial
authoralexey.ivanov <alexey.ivanov@jetbrains.com>
Thu, 28 Oct 2010 15:31:14 +0000 (19:31 +0400)
committeralexey.ivanov <alexey.ivanov@jetbrains.com>
Thu, 28 Oct 2010 15:31:14 +0000 (19:31 +0400)
src/META-INF/plugin.xml
src/org/jetbrains/javafx/lang/psi/impl/JavaFxClassDefinitionImpl.java
src/org/jetbrains/javafx/lang/psi/impl/JavaFxFunctionDefinitionImpl.java
src/org/jetbrains/javafx/lang/psi/impl/JavaFxParameterImpl.java
src/org/jetbrains/javafx/lang/psi/impl/JavaFxPresentableElementImpl.java
src/org/jetbrains/javafx/lang/psi/impl/JavaFxVariableDeclarationImpl.java
src/org/jetbrains/javafx/lang/psi/impl/resolve/JavaFxReference.java
src/org/jetbrains/javafx/refactoring/JavaFxChangeUtil.java
src/org/jetbrains/javafx/refactoring/rename/JavaFxNamesValidator.java [new file with mode: 0644]
src/org/jetbrains/javafx/refactoring/rename/JavaFxRenameProcessor.java [new file with mode: 0644]

index 7ecf3d299415b44f642532c1effb98624b01c398..00cc6f14ee033911b66af07a4336f09254402ad1 100644 (file)
                              implementationClass="org.jetbrains.javafx.lang.psi.impl.resolve.JavaFxReferenceElementManipulator"/>
     <lang.surroundDescriptor language="JavaFx"
                              implementationClass="org.jetbrains.javafx.refactoring.surround.JavaFxExpressionSurroundDescriptor"/>
-      <lang.findUsagesProvider language="JavaFx" implementationClass="org.jetbrains.javafx.findUsages.JavaFxFindUsagesProvider"/>
+    <lang.findUsagesProvider language="JavaFx" implementationClass="org.jetbrains.javafx.findUsages.JavaFxFindUsagesProvider"/>
+    <lang.namesValidator language="JavaFx" implementationClass="org.jetbrains.javafx.refactoring.rename.JavaFxNamesValidator"/>
+
+    <renamePsiElementProcessor implementation="org.jetbrains.javafx.refactoring.rename.JavaFxRenameProcessor" order="last"/>
 
     <frameworkSupport implementation="org.jetbrains.javafx.JavaFxSupportProvider"/>
     <sdkType implementation="org.jetbrains.javafx.sdk.JavaFxSdkType"/>
index 5253523f3bb8b9af5913964a4c3d0d3d85a07fba..13ffba2e851acf4b52a1693f5d9d0fa8f1aea48f 100644 (file)
@@ -23,8 +23,6 @@ import com.intellij.psi.PsiReference;
 import com.intellij.psi.ResolveState;
 import com.intellij.psi.scope.PsiScopeProcessor;
 import com.intellij.util.Icons;
-import com.intellij.util.IncorrectOperationException;
-import org.jetbrains.annotations.NonNls;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
 import org.jetbrains.javafx.lang.parser.JavaFxElementTypes;
@@ -91,12 +89,6 @@ public class JavaFxClassDefinitionImpl extends JavaFxPresentableElementImpl<Java
     return Icons.CLASS_ICON;
   }
 
-// TODO: !!!
-
-  public PsiElement setName(@NonNls @NotNull String name) throws IncorrectOperationException {
-    throw new IncorrectOperationException();
-  }
-
   @Override
   public boolean processDeclarations(@NotNull final PsiScopeProcessor processor,
                                      @NotNull final ResolveState state,
index b930f94425d28a93568988b9298c2983761301d7..69ac2c0941069e050bef4d0c3836f59c8a2ed44c 100644 (file)
@@ -22,8 +22,6 @@ import com.intellij.psi.ResolveState;
 import com.intellij.psi.scope.PsiScopeProcessor;
 import com.intellij.psi.stubs.StubElement;
 import com.intellij.util.Icons;
-import com.intellij.util.IncorrectOperationException;
-import org.jetbrains.annotations.NonNls;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
 import org.jetbrains.javafx.lang.parser.JavaFxElementTypes;
@@ -118,12 +116,6 @@ public class JavaFxFunctionDefinitionImpl extends JavaFxPresentableElementImpl<J
     return locationString;
   }
 
-// TODO: !!!
-
-  public PsiElement setName(@NonNls @NotNull String name) throws IncorrectOperationException {
-    throw new IncorrectOperationException();
-  }
-
   @Override
   @Nullable
   public JavaFxQualifiedName getQualifiedName() {
index 56757e761900ec52e06f447904ab5bc34f08d201..9e213551995046d9027adaef01cec741a929e79e 100644 (file)
@@ -19,8 +19,6 @@ import com.intellij.lang.ASTNode;
 import com.intellij.psi.PsiArrayType;
 import com.intellij.psi.PsiElement;
 import com.intellij.psi.PsiType;
-import com.intellij.util.IncorrectOperationException;
-import org.jetbrains.annotations.NonNls;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.javafx.lang.parser.JavaFxElementTypes;
 import org.jetbrains.javafx.lang.psi.JavaFxExpression;
@@ -59,10 +57,6 @@ public class JavaFxParameterImpl extends JavaFxPresentableElementImpl<JavaFxPara
     return super.getName();
   }
 
-  public PsiElement setName(@NonNls @NotNull String name) throws IncorrectOperationException {
-    throw new IncorrectOperationException();
-  }
-
   @Override
   public PsiType getType() {
     final JavaFxTypeElement typeElement = getTypeElement();
index e817e1cf786fd32d062664e0db077d9d59b079e1..6e4ad61fa9b6a78c1928c0519a14333831745935 100644 (file)
@@ -18,11 +18,15 @@ package org.jetbrains.javafx.lang.psi.impl;
 import com.intellij.lang.ASTNode;
 import com.intellij.navigation.ItemPresentation;
 import com.intellij.openapi.editor.colors.TextAttributesKey;
+import com.intellij.psi.PsiElement;
 import com.intellij.psi.PsiNamedElement;
 import com.intellij.psi.stubs.IStubElementType;
 import com.intellij.psi.stubs.StubElement;
+import com.intellij.util.IncorrectOperationException;
+import org.jetbrains.annotations.NonNls;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.javafx.lang.psi.JavaFxPsiUtil;
+import org.jetbrains.javafx.refactoring.JavaFxChangeUtil;
 
 import javax.swing.*;
 
@@ -62,6 +66,15 @@ public abstract class JavaFxPresentableElementImpl<T extends StubElement> extend
     };
   }
 
+  public PsiElement setName(@NonNls @NotNull String name) throws IncorrectOperationException {
+    final ASTNode oldNameIdentifier = getNameNode();
+    if (oldNameIdentifier != null) {
+      final ASTNode nameElement = JavaFxChangeUtil.createNameIdentifier(getProject(), name);
+      getNode().replaceChild(oldNameIdentifier, nameElement);
+    }
+    return this;
+  }
+
   @Override
   public int getTextOffset() {
     final ASTNode nameNode = getNameNode();
index a908b49b64b6fa5cbb5457587a79b88840e695f1..16e243a4c5624db92c319173bcc92ecc51b01968 100644 (file)
@@ -23,8 +23,6 @@ import com.intellij.psi.scope.PsiScopeProcessor;
 import com.intellij.psi.stubs.StubElement;
 import com.intellij.psi.util.PsiTreeUtil;
 import com.intellij.util.Icons;
-import com.intellij.util.IncorrectOperationException;
-import org.jetbrains.annotations.NonNls;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
 import org.jetbrains.javafx.lang.lexer.JavaFxTokenTypes;
@@ -106,10 +104,6 @@ public class JavaFxVariableDeclarationImpl extends JavaFxPresentableElementImpl<
     return locationString;
   }
 
-  public PsiElement setName(@NonNls @NotNull String name) throws IncorrectOperationException {
-    throw new IncorrectOperationException();
-  }
-
   @Override
   public JavaFxQualifiedName getQualifiedName() {
     final JavaFxVariableStub stub = getStub();
index dd765b4ae64cc4528b77ff8e1ad73704edfde162..1353f5031927aa56646630c2ec6f58e62466113c 100644 (file)
  */
 package org.jetbrains.javafx.lang.psi.impl.resolve;
 
+import com.intellij.lang.ASTNode;
 import com.intellij.psi.PsiElement;
 import com.intellij.psi.PsiPolyVariantReferenceBase;
 import com.intellij.psi.ResolveResult;
 import com.intellij.psi.impl.PsiManagerEx;
 import com.intellij.psi.impl.source.resolve.ResolveCache;
+import com.intellij.util.IncorrectOperationException;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.javafx.lang.psi.JavaFxCallExpression;
 import org.jetbrains.javafx.lang.psi.JavaFxNewExpression;
 import org.jetbrains.javafx.lang.psi.JavaFxReferenceElement;
+import org.jetbrains.javafx.refactoring.JavaFxChangeUtil;
 
 /**
  * Created by IntelliJ IDEA.
@@ -73,6 +76,16 @@ public class JavaFxReference extends PsiPolyVariantReferenceBase<JavaFxReference
     return new JavaFxResolveProcessor(name);
   }
 
+  @Override
+  public PsiElement handleElementRename(final String newElementName) throws IncorrectOperationException {
+    final ASTNode nameElement = myElement.getNameNode();
+    if (nameElement != null) {
+      final ASTNode newNameElement = JavaFxChangeUtil.createNameIdentifier(myElement.getProject(), newElementName);
+      myElement.getNode().replaceChild(nameElement, newNameElement);
+    }
+    return myElement;
+  }
+
   @NotNull
   @Override
   public Object[] getVariants() {
index 4de9b7e8d80ceeb61ed8a12f48134b87444418b0..deb59d420be433edebb2c9297efe7e9346878021 100644 (file)
@@ -15,6 +15,7 @@
  */
 package org.jetbrains.javafx.refactoring;
 
+import com.intellij.lang.ASTNode;
 import com.intellij.openapi.diagnostic.Logger;
 import com.intellij.openapi.project.Project;
 import com.intellij.psi.PsiFile;
@@ -22,6 +23,7 @@ import com.intellij.psi.PsiFileFactory;
 import org.jetbrains.annotations.Nullable;
 import org.jetbrains.javafx.JavaFxFileType;
 import org.jetbrains.javafx.lang.psi.JavaFxExpression;
+import org.jetbrains.javafx.lang.psi.JavaFxReferenceExpression;
 
 /**
  * Created by IntelliJ IDEA.
@@ -43,4 +45,11 @@ public class JavaFxChangeUtil {
     final PsiFile dummyFile = createDummyFile(project, text);
     return (JavaFxExpression)dummyFile.getFirstChild();
   }
+
+  public static ASTNode createNameIdentifier(final Project project, final String name) {
+    final PsiFile dummyFile = createDummyFile(project, name);
+    final JavaFxReferenceExpression referenceExpression = (JavaFxReferenceExpression)dummyFile.getFirstChild();
+
+    return referenceExpression.getNode().getFirstChildNode();
+  }
 }
diff --git a/src/org/jetbrains/javafx/refactoring/rename/JavaFxNamesValidator.java b/src/org/jetbrains/javafx/refactoring/rename/JavaFxNamesValidator.java
new file mode 100644 (file)
index 0000000..384c75f
--- /dev/null
@@ -0,0 +1,46 @@
+package org.jetbrains.javafx.refactoring.rename;
+
+import com.intellij.lang.refactoring.NamesValidator;
+import com.intellij.lexer.Lexer;
+import com.intellij.openapi.project.Project;
+import org.jetbrains.javafx.lang.lexer.JavaFxFlexLexer;
+import org.jetbrains.javafx.lang.lexer.JavaFxTokenTypes;
+
+/**
+ * Created by IntelliJ IDEA.
+ *
+ * @author: Alexey.Ivanov
+ */
+public class JavaFxNamesValidator implements NamesValidator {
+  private static final Lexer ourLexer = new JavaFxFlexLexer();
+
+  @Override
+  public synchronized boolean isKeyword(final String name, final Project project) {
+    try {
+      ourLexer.start(name);
+      if (!JavaFxTokenTypes.RESERVED_WORDS.contains(ourLexer.getTokenType())) {
+        return false;
+      }
+      ourLexer.advance();
+      return (ourLexer.getTokenType() == null);
+    }
+    catch (StringIndexOutOfBoundsException e) {
+      return false;
+    }
+  }
+
+  @Override
+  public synchronized boolean isIdentifier(final String name, final Project project) {
+    try {
+      ourLexer.start(name);
+      if (!JavaFxTokenTypes.NAME_ALL.contains(ourLexer.getTokenType())) {
+        return false;
+      }
+      ourLexer.advance();
+      return (ourLexer.getTokenType() == null);
+    }
+    catch (StringIndexOutOfBoundsException e) {
+      return false;
+    }
+  }
+}
diff --git a/src/org/jetbrains/javafx/refactoring/rename/JavaFxRenameProcessor.java b/src/org/jetbrains/javafx/refactoring/rename/JavaFxRenameProcessor.java
new file mode 100644 (file)
index 0000000..db5bdeb
--- /dev/null
@@ -0,0 +1,17 @@
+package org.jetbrains.javafx.refactoring.rename;
+
+import com.intellij.psi.PsiElement;
+import com.intellij.refactoring.rename.RenamePsiElementProcessor;
+import org.jetbrains.javafx.lang.psi.JavaFxElement;
+
+/**
+ * Created by IntelliJ IDEA.
+ *
+ * @author: Alexey.Ivanov
+ */
+public class JavaFxRenameProcessor extends RenamePsiElementProcessor {
+  @Override
+  public boolean canProcessElement(PsiElement element) {
+    return element instanceof JavaFxElement;
+  }
+}