Stubs - Extract interface for JetNamedDeclaration. Old implementation renamed to JetNotStubbedNamedDeclaration.

This commit is contained in:
Nikolay Krasko
2012-06-14 18:56:37 +04:00
parent 0d87012783
commit 7b5c9af33c
12 changed files with 196 additions and 91 deletions
@@ -16,17 +16,8 @@
package org.jetbrains.jet.lang.psi;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lexer.JetToken;
/**
* @author Nikolay Krasko
*/
public interface JetDeclaration extends JetExpression, JetModifierListOwner {
@Override
@Nullable
JetModifierList getModifierList();
@Override
boolean hasModifier(JetToken modifier);
}
@@ -1,3 +1,19 @@
/*
* Copyright 2010-2012 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 org.jetbrains.jet.lang.psi;
import com.intellij.psi.NavigatablePsiElement;
@@ -69,7 +69,7 @@ public class JetElementImpl extends ASTWrapperPsiElement implements JetElement {
public <D> void acceptChildren(@NotNull JetTreeVisitor<D> visitor, D data) {
PsiElement child = getFirstChild();
while (child != null) {
if (child instanceof JetElementImpl) {
if (child instanceof JetElement) {
((JetElement) child).accept(visitor, data);
}
child = child.getNextSibling();
@@ -1,89 +1,19 @@
/*
* Copyright 2010-2012 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 org.jetbrains.jet.lang.psi;
import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiNameIdentifierOwner;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.lexer.JetTokens;
/**
* @author max
* @author Nikolay Krasko
*/
public abstract class JetNamedDeclaration extends JetDeclarationImpl implements PsiNameIdentifierOwner, JetStatementExpression, JetNamed {
public JetNamedDeclaration(@NotNull ASTNode node) {
super(node);
}
@Override
public String getName() {
PsiElement identifier = getNameIdentifier();
if (identifier != null) {
String text = identifier.getText();
return text != null ? JetPsiUtil.unquoteIdentifier(text) : null;
}
else {
return null;
}
}
@Override
public Name getNameAsName() {
String name = getName();
return name != null ? Name.identifier(name) : null;
}
public interface JetNamedDeclaration extends JetDeclaration, PsiNameIdentifierOwner, JetStatementExpression, JetNamed {
@NotNull
public Name getNameAsSafeName() {
return JetPsiUtil.safeName(getName());
}
Name getNameAsSafeName();
@Override
public PsiElement getNameIdentifier() {
return findChildByType(JetTokens.IDENTIFIER);
}
@Override
public PsiElement setName(@NonNls @NotNull String name) throws IncorrectOperationException {
return getNameIdentifier().replace(JetPsiFactory.createNameIdentifier(getProject(), name));
}
@Override
public int getTextOffset() {
PsiElement identifier = getNameIdentifier();
return identifier != null ? identifier.getTextRange().getStartOffset() : getTextRange().getStartOffset();
}
public boolean isScriptDeclaration() {
return getScript() != null;
}
boolean isScriptDeclaration();
@Nullable
public JetScript getScript() {
if (getParent() != null && getParent().getParent() instanceof JetScript) {
return (JetScript) getParent().getParent();
}
else {
return null;
}
}
JetScript getScript();
}
@@ -0,0 +1,92 @@
/*
* Copyright 2010-2012 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 org.jetbrains.jet.lang.psi;
import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiElement;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.lexer.JetTokens;
/**
* @author max
*/
@Deprecated
abstract class JetNotStubbedNamedDeclaration extends JetDeclarationImpl implements JetNamedDeclaration {
public JetNotStubbedNamedDeclaration(@NotNull ASTNode node) {
super(node);
}
@Override
public String getName() {
PsiElement identifier = getNameIdentifier();
if (identifier != null) {
String text = identifier.getText();
return text != null ? JetPsiUtil.unquoteIdentifier(text) : null;
}
else {
return null;
}
}
@Override
public Name getNameAsName() {
String name = getName();
return name != null ? Name.identifier(name) : null;
}
@Override
@NotNull
public Name getNameAsSafeName() {
return JetPsiUtil.safeName(getName());
}
@Override
public PsiElement getNameIdentifier() {
return findChildByType(JetTokens.IDENTIFIER);
}
@Override
public PsiElement setName(@NonNls @NotNull String name) throws IncorrectOperationException {
return getNameIdentifier().replace(JetPsiFactory.createNameIdentifier(getProject(), name));
}
@Override
public int getTextOffset() {
PsiElement identifier = getNameIdentifier();
return identifier != null ? identifier.getTextRange().getStartOffset() : getTextRange().getStartOffset();
}
@Override
public boolean isScriptDeclaration() {
return getScript() != null;
}
@Override
@Nullable
public JetScript getScript() {
if (getParent() != null && getParent().getParent() instanceof JetScript) {
return (JetScript) getParent().getParent();
}
else {
return null;
}
}
}
@@ -31,7 +31,7 @@ import java.util.List;
/**
* @author abreslav
*/
public class JetObjectDeclaration extends JetNamedDeclaration implements JetClassOrObject {
public class JetObjectDeclaration extends JetNotStubbedNamedDeclaration implements JetClassOrObject {
public JetObjectDeclaration(@NotNull ASTNode node) {
super(node);
}
@@ -123,4 +123,9 @@ public class JetObjectDeclaration extends JetNamedDeclaration implements JetClas
public void delete() throws IncorrectOperationException {
JetPsiUtil.deleteClass(this);
}
//@Override
//public ItemPresentation getPresentation() {
// return ItemPresentationProviders.getItemPresentation(this);
//}
}
@@ -22,7 +22,7 @@ import org.jetbrains.annotations.NotNull;
/**
* @author abreslav
*/
public class JetObjectDeclarationName extends JetNamedDeclaration {
public class JetObjectDeclarationName extends JetNotStubbedNamedDeclaration {
public JetObjectDeclarationName(@NotNull ASTNode node) {
super(node);
@@ -25,7 +25,7 @@ import org.jetbrains.jet.lexer.JetTokens;
/**
* @author max
*/
public class JetParameter extends JetNamedDeclaration {
public class JetParameter extends JetNotStubbedNamedDeclaration {
public JetParameter(@NotNull ASTNode node) {
super(node);
}
@@ -180,7 +180,7 @@ public class JetPsiUtil {
if (parent instanceof JetFile) {
firstPart = getFQName((JetFile) parent);
}
else if (parent instanceof JetNamedDeclaration) {
else if (parent instanceof JetNotStubbedNamedDeclaration) {
firstPart = getFQName((JetNamedDeclaration) parent);
}
@@ -0,0 +1,71 @@
/*
* Copyright 2010-2012 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 org.jetbrains.jet.lang.psi;
import com.intellij.extapi.psi.StubBasedPsiElementBase;
import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiElement;
import com.intellij.psi.stubs.IStubElementType;
import com.intellij.psi.stubs.StubElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.JetNodeTypes;
import org.jetbrains.jet.lexer.JetToken;
/**
* @author Nikolay Krasko
*/
abstract class JetStubDeclaration<T extends StubElement> extends StubBasedPsiElementBase<T> implements JetDeclaration {
public JetStubDeclaration(@NotNull T stub, @NotNull IStubElementType nodeType) {
super(stub, nodeType);
}
public JetStubDeclaration(@NotNull ASTNode node) {
super(node);
}
@Override
public JetModifierList getModifierList() {
return (JetModifierList) findChildByType(JetNodeTypes.MODIFIER_LIST);
}
@Override
public boolean hasModifier(JetToken modifier) {
JetModifierList modifierList = getModifierList();
return modifierList != null && modifierList.hasModifier(modifier);
}
@Override
public <D> void acceptChildren(@NotNull JetTreeVisitor<D> visitor, D data) {
PsiElement child = getFirstChild();
while (child != null) {
if (child instanceof JetElement) {
((JetElement) child).accept(visitor, data);
}
child = child.getNextSibling();
}
}
@Override
public void accept(@NotNull JetVisitorVoid visitor) {
visitor.visitExpression(this);
}
@Override
public <R, D> R accept(@NotNull JetVisitor<R, D> visitor, D data) {
return visitor.visitExpression(this, data);
}
}
@@ -26,7 +26,7 @@ import org.jetbrains.jet.lexer.JetTokens;
/**
* @author max
*/
public class JetTypeParameter extends JetNamedDeclaration {
public class JetTypeParameter extends JetNotStubbedNamedDeclaration {
public JetTypeParameter(@NotNull ASTNode node) {
super(node);
}
@@ -27,7 +27,7 @@ import java.util.List;
/**
* @author max
*/
public class JetTypeParameterListOwner extends JetNamedDeclaration {
public class JetTypeParameterListOwner extends JetNotStubbedNamedDeclaration {
public JetTypeParameterListOwner(@NotNull ASTNode node) {
super(node);
}