/*
* Copyright 2011 Jon S Akhtar (Sylvanaar)
*
* 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.sylvanaar.idea.Lua.lang.psi.impl.statements;
import com.intellij.lang.ASTNode;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.*;
import com.intellij.psi.scope.PsiScopeProcessor;
import com.intellij.util.IncorrectOperationException;
import com.sylvanaar.idea.Lua.lang.psi.expressions.LuaExpression;
import com.sylvanaar.idea.Lua.lang.psi.expressions.LuaExpressionList;
import com.sylvanaar.idea.Lua.lang.psi.expressions.LuaFunctionCallExpression;
import com.sylvanaar.idea.Lua.lang.psi.expressions.LuaLiteralExpression;
import com.sylvanaar.idea.Lua.lang.psi.resolve.LuaResolveResult;
import com.sylvanaar.idea.Lua.lang.psi.resolve.LuaResolver;
import com.sylvanaar.idea.Lua.lang.psi.statements.LuaModuleStatement;
import com.sylvanaar.idea.Lua.lang.psi.symbols.LuaCompoundIdentifier;
import com.sylvanaar.idea.Lua.lang.psi.symbols.LuaGlobalIdentifier;
import com.sylvanaar.idea.Lua.lang.psi.symbols.LuaSymbol;
import com.sylvanaar.idea.Lua.lang.psi.types.LuaType;
import com.sylvanaar.idea.Lua.lang.psi.visitor.LuaElementVisitor;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* Created by IntelliJ IDEA.
* User: Jon S Akhtar
* Date: 3/7/11
* Time: 11:21 AM
*/
public class LuaModuleStatementImpl extends LuaFunctionCallStatementImpl implements LuaModuleStatement {
public LuaModuleStatementImpl(ASTNode node) {
super(node);
}
@Override
public void accept(LuaElementVisitor visitor) {
visitor.visitModuleStatement(this);
}
@Override
public void accept(@NotNull PsiElementVisitor visitor) {
if (visitor instanceof LuaElementVisitor) {
((LuaElementVisitor) visitor).visitModuleStatement(this);
} else {
visitor.visitElement(this);
}
}
@Override
public String toString() {
String name = getName();
return "Module: " + (name !=null? name :"err");
}
PsiElement getNameElement() {
LuaFunctionCallExpression invoked = getInvokedExpression();
if (invoked == null) return null;
LuaExpressionList args = invoked.getArgumentList();
if (args == null) return null;
return args.getLuaExpressions().get(0);
}
public String getName() {
LuaFunctionCallExpression invoked = getInvokedExpression();
if (invoked == null) return null;
LuaExpressionList args = invoked.getArgumentList();
if (args == null) return null;
LuaExpression expression = args.getLuaExpressions().get(0);
LuaLiteralExpression lit = null;
if (expression instanceof LuaLiteralExpression)
lit = (LuaLiteralExpression) expression;
if (lit != null && lit.getLuaType() == LuaType.STRING) {
return (String) lit.getValue();
}
if (expression instanceof LuaSymbol && StringUtil.notNullize(((LuaSymbol) expression).getName()).equals("..."
)) {
final VirtualFile virtualFile = getContainingFile().getVirtualFile();
if (virtualFile != null) {
return virtualFile.getNameWithoutExtension();
}
}
return null;
}
@Override
public PsiElement setName(@NonNls @NotNull String name) throws IncorrectOperationException {
return null;
}
@Override
public String getDefinedName() {
return getName();
}
public boolean processDeclarations(@NotNull PsiScopeProcessor processor,
@NotNull ResolveState resolveState,
PsiElement lastParent,
@NotNull PsiElement place) {
processor.execute(this, resolveState);
return true;
}
@Override
public boolean isSameKind(LuaSymbol symbol) {
return symbol instanceof LuaGlobalIdentifier || symbol instanceof LuaCompoundIdentifier;
}
@Override
public boolean isAssignedTo() {
return true;
}
@Override
public PsiElement replaceWithExpression(LuaExpression newCall, boolean b) {
return null;
}
@Override
public LuaType getLuaType() {
return LuaType.TABLE;
}
@Override
public TextRange getIncludedTextRange() {
return new TextRange(getTextOffset()+getTextLength(), getContainingFile().getTextRange().getEndOffset());
}
@Override
public PsiElement resolveWithoutCaching(boolean ingnoreAlias) {
boolean save = RESOLVER.getIgnoreAliasing();
RESOLVER.setIgnoreAliasing(ingnoreAlias);
LuaResolveResult[] results = RESOLVER.resolve(this, false);
RESOLVER.setIgnoreAliasing(save);
if (results != null && results.length > 0)
return results[0].getElement();
return null;
}
@Override
public PsiElement getElement() {
return getNameElement();
}
@NotNull
public TextRange getRangeInElement() {
PsiElement e = getElement();
if (e != null)
return e.getTextRange();
return TextRange.EMPTY_RANGE;
}
@Nullable
public PsiElement resolve() {
ResolveResult[] results = getManager().getResolveCache().resolveWithCaching(this, RESOLVER, true, false);
return results.length == 1 ? results[0].getElement() : null;
}
@NotNull
public ResolveResult[] multiResolve(final boolean incompleteCode) {
return getManager().getResolveCache().resolveWithCaching(this, RESOLVER, true, incompleteCode);
}
private static final LuaResolver RESOLVER = new LuaResolver();
@NotNull
public String getCanonicalText() {
return getText();
}
@Override
public PsiReference getReference() {
return this;
}
@Override
public PsiElement handleElementRename(String newElementName) throws IncorrectOperationException {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public PsiElement bindToElement(@NotNull PsiElement element) throws IncorrectOperationException {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public boolean isReferenceTo(PsiElement element) {
return false; //To change body of implemented methods use File | Settings | File Templates.
}
@NotNull
@Override
public Object[] getVariants() {
return new Object[0]; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public boolean isSoft() {
return false; //To change body of implemented methods use File | Settings | File Templates.
}
public PsiElement getNameIdentifier() {
return this;
}
}