Skip to content

Commit

Permalink
Add support for some basic rename refactoring
Browse files Browse the repository at this point in the history
It does not work perfectly yet:
1. references are not linked together after rename
2. renaming on reference works well, but renaming on declaration renames only the declaration
  • Loading branch information
Darkyenus committed Oct 4, 2015
1 parent 13ddae6 commit 66a5336
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/glslplugin/lang/elements/GLSLIdentifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public PsiElement setName(@NotNull String name) throws IncorrectOperationExcepti
assert oldName != null; // we've already checked this isn't null (and thrown if it is) in checkSetName
PsiElement newName = GLSLPsiElementFactory.createLeafElement(getProject(), name);
getNode().replaceChild(oldName.getNode(), newName.getNode());
return this;
return newName;
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
package glslplugin.lang.elements.declarations;

import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiCheckedRenameElement;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiNameIdentifierOwner;
import com.intellij.util.IncorrectOperationException;
import glslplugin.lang.elements.GLSLIdentifier;
import glslplugin.lang.elements.types.GLSLBasicFunctionType;
import glslplugin.lang.elements.types.GLSLFunctionType;
import org.jetbrains.annotations.NotNull;
Expand All @@ -28,7 +33,7 @@
/**
* GLSLFunctionDeclarationImpl is the psi implementation of a function declaration.
*/
public class GLSLFunctionDeclarationImpl extends GLSLSingleDeclarationImpl implements GLSLFunctionDeclaration {
public class GLSLFunctionDeclarationImpl extends GLSLSingleDeclarationImpl implements GLSLFunctionDeclaration, PsiNameIdentifierOwner, PsiCheckedRenameElement {
private GLSLFunctionType type;

public GLSLFunctionDeclarationImpl(@NotNull ASTNode astNode) {
Expand Down Expand Up @@ -95,4 +100,26 @@ private GLSLFunctionType createType() {
public String getDeclarationDescription() {
return "function";
}

@Nullable
@Override
public GLSLIdentifier getNameIdentifier() {
final GLSLDeclarator declarator = getDeclarator();
if(declarator == null)return null;
return declarator.getNameIdentifier();
}

@Override
public PsiElement setName(@NotNull String name) throws IncorrectOperationException {
final GLSLIdentifier nameIdentifier = getNameIdentifier();
if(nameIdentifier == null)throw new IncorrectOperationException("GLSLDeclarator is null");
return nameIdentifier.setName(name);
}

@Override
public void checkSetName(String name) throws IncorrectOperationException {
final GLSLIdentifier nameIdentifier = getNameIdentifier();
if(nameIdentifier == null)throw new IncorrectOperationException("GLSLDeclarator is null");
nameIdentifier.checkSetName(name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
package glslplugin.lang.elements.declarations;

import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiNameIdentifierOwner;
import com.intellij.util.IncorrectOperationException;
import glslplugin.lang.elements.statements.GLSLCompoundStatement;
import org.jetbrains.annotations.Nullable;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@
package glslplugin.lang.elements.expressions;

import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiCheckedRenameElement;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiNameIdentifierOwner;
import com.intellij.util.IncorrectOperationException;
import glslplugin.lang.elements.GLSLElement;
import glslplugin.lang.elements.GLSLIdentifier;
import glslplugin.lang.elements.GLSLReferenceElement;
Expand Down Expand Up @@ -271,4 +274,13 @@ public String toString() {
return "Function call: " + getFunctionName();
}
}

@Override
public String getName() {
if(isConstructor()){
return getType().getTypename();
} else {
return getFunctionName();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public PsiElement handleElementRename(String newElementName) throws IncorrectOpe
if (source instanceof PsiNamedElement) {
return ((PsiNamedElement) source).setName(newElementName);
}
throw new IncorrectOperationException("Not supported!");
return resolve();
}

public PsiElement bindToElement(@NotNull PsiElement element) throws IncorrectOperationException {
Expand Down

0 comments on commit 66a5336

Please sign in to comment.