store imported Qname in ImportElement stub

This commit is contained in:
Dmitry Jemerov
2010-03-04 21:26:40 +03:00
parent b24114cc2b
commit 65bd485984
10 changed files with 50 additions and 29 deletions
@@ -24,7 +24,7 @@ public class PyFileElementType extends IStubFileElementType {
@Override
public int getStubVersion() {
return 15;
return 16;
}
@Override
@@ -4,14 +4,18 @@ import com.intellij.psi.StubBasedPsiElement;
import com.jetbrains.python.psi.stubs.PyImportElementStub;
import org.jetbrains.annotations.Nullable;
import java.util.List;
/**
* @author yole
*/
public interface PyImportElement extends PyElement, NameDefiner, StubBasedPsiElement<PyImportElementStub> {
@Nullable
PyReferenceExpression getImportReference();
@Nullable
List<String> getImportedQName();
@Nullable
PyTargetExpression getAsName();
@@ -114,7 +114,8 @@ public class PyFromImportStatementImpl extends PyBaseElementImpl<PyFromImportSta
if (isStarImport()) {
PyReferenceExpression expr = getImportSource();
if (expr != null) {
final PsiElement importedFile = PyReferenceExpressionImpl.turnDirIntoInit(ResolveImportUtil.resolveImportReference(expr));
final PsiElement target = ResolveImportUtil.resolveImportReference(expr);
final PsiElement importedFile = PyReferenceExpressionImpl.turnDirIntoInit(target);
if (importedFile != null) {
return importedFile.processDeclarations(processor, state, null, place);
}
@@ -43,6 +43,14 @@ public class PyImportElementImpl extends PyBaseElementImpl<PyImportElementStub>
return (PyReferenceExpression)importRefNode.getPsi();
}
public List<String> getImportedQName() {
final PyImportElementStub stub = getStub();
if (stub != null) {
return stub.getImportedQName();
}
return ResolveImportUtil.getQualifiedName(getImportReference());
}
public PyTargetExpression getAsName() {
final ASTNode asNameNode = getNode().findChildByType(PyElementTypes.TARGET_EXPRESSION);
if (asNameNode == null) return null;
@@ -57,9 +65,9 @@ public class PyImportElementImpl extends PyBaseElementImpl<PyImportElementStub>
if (!StringUtil.isEmpty(asName)) {
return asName;
}
final String importedName = stub.getImportedName();
if (!StringUtil.isEmpty(importedName)) {
return importedName;
final List<String> importedName = stub.getImportedQName();
if (importedName.size() > 0) {
return importedName.get(importedName.size()-1);
}
return null;
}
@@ -184,9 +184,8 @@ public class PyReferenceExpressionImpl extends PyElementImpl implements PyRefere
* @return resolution result.
* @see #resolve()
*/
private
@NotNull
List<RatedResolveResult> resolveInner() {
private List<RatedResolveResult> resolveInner() {
//List<PsiElement> ret = new ArrayList<PsiElement>();
ResultList ret = new ResultList();
@@ -7,13 +7,14 @@ import com.intellij.psi.stubs.StubInputStream;
import com.intellij.psi.stubs.StubOutputStream;
import com.intellij.util.io.StringRef;
import com.jetbrains.python.psi.PyImportElement;
import com.jetbrains.python.psi.PyReferenceExpression;
import com.jetbrains.python.psi.PyStubElementType;
import com.jetbrains.python.psi.PyTargetExpression;
import com.jetbrains.python.psi.impl.PyImportElementImpl;
import com.jetbrains.python.psi.stubs.PyImportElementStub;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* @author yole
@@ -35,21 +36,27 @@ public class PyImportElementElementType extends PyStubElementType<PyImportElemen
@Override
public PyImportElementStub createStub(PyImportElement psi, StubElement parentStub) {
final PyReferenceExpression importReference = psi.getImportReference();
final PyTargetExpression asName = psi.getAsName();
return new PyImportElementStubImpl(importReference != null ? importReference.getText() : "",
return new PyImportElementStubImpl(psi.getImportedQName(),
asName != null ? asName.getText() : "",
parentStub);
}
public void serialize(PyImportElementStub stub, StubOutputStream dataStream) throws IOException {
dataStream.writeName(stub.getImportedName());
final List<String> qName = stub.getImportedQName();
dataStream.writeVarInt(qName.size());
for (String s : qName) {
dataStream.writeName(s);
}
dataStream.writeName(stub.getAsName());
}
public PyImportElementStub deserialize(StubInputStream dataStream, StubElement parentStub) throws IOException {
StringRef importedName = dataStream.readName();
int size = dataStream.readVarInt();
List<String> qName = new ArrayList<String>(size);
for (int i = 0; i < size; i++) {
qName.add(dataStream.readName().getString());
}
StringRef asName = dataStream.readName();
return new PyImportElementStubImpl(importedName.getString(), asName.getString(), parentStub);
}
return new PyImportElementStubImpl(qName, asName.getString(), parentStub); }
}
@@ -1,33 +1,31 @@
package com.jetbrains.python.psi.impl.stubs;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.stubs.StubBase;
import com.intellij.psi.stubs.StubElement;
import com.jetbrains.python.PyElementTypes;
import com.jetbrains.python.psi.PyImportElement;
import com.jetbrains.python.psi.stubs.PyImportElementStub;
import java.util.List;
/**
* @author yole
*/
public class PyImportElementStubImpl extends StubBase<PyImportElement> implements PyImportElementStub {
private final String myImportedName;
private final List<String> myImportedQName;
private final String myAsName;
public PyImportElementStubImpl(String importedName, String asName, final StubElement parent) {
public PyImportElementStubImpl(List<String> importedQName, String asName, final StubElement parent) {
super(parent, PyElementTypes.IMPORT_ELEMENT);
myImportedName = importedName;
myImportedQName = importedQName;
myAsName = asName;
}
public String getImportedName() {
return myImportedName;
public List<String> getImportedQName() {
return myImportedQName;
}
public String getAsName() {
if (!StringUtil.isEmpty(myAsName)) {
return myAsName;
}
return myImportedName;
return myAsName;
}
}
@@ -3,10 +3,12 @@ package com.jetbrains.python.psi.stubs;
import com.intellij.psi.stubs.StubElement;
import com.jetbrains.python.psi.PyImportElement;
import java.util.List;
/**
* @author yole
*/
public interface PyImportElementStub extends StubElement<PyImportElement> {
String getImportedName();
List<String> getImportedQName();
String getAsName();
}
+1 -1
View File
@@ -1,2 +1,2 @@
from sys import argv
import os
import os.path
@@ -172,11 +172,13 @@ public class PyStubsTest extends PyLightFixtureTestCase {
assertFalse(fromImport.isStarImport());
assertEquals(0, fromImport.getRelativeLevel());
final List<String> qName = fromImport.getImportSourceQName();
assertEquals(1, qName.size());
assertEquals("sys", qName.get(0));
assertSameElements(qName, "sys");
final List<PyImportElement> importTargets = file.getImportTargets();
assertEquals(1, importTargets.size());
final PyImportElement importElement = importTargets.get(0);
final List<String> importQName = importElement.getImportedQName();
assertSameElements(importQName, "os", "path");
assertNotParsed(file);
}