Resolve references inside *.pyi files (PY-15206)

This commit is contained in:
Andrey Vlasovskikh
2015-07-08 01:17:53 +03:00
parent 8d82508fc6
commit de7a0d6143
10 changed files with 209 additions and 2 deletions
+2
View File
@@ -560,6 +560,8 @@
<!-- typing -->
<multiHostInjector implementation="com.jetbrains.python.codeInsight.PyTypingAnnotationInjector"/>
<lang.parserDefinition language="PythonStub" implementationClass="com.jetbrains.python.pyi.PyiParserDefinition"/>
<fileTypeFactory implementation="com.jetbrains.python.pyi.PyiFileTypeFactory"/>
<lang.inspectionSuppressor language="Python" implementationClass="com.jetbrains.python.inspections.PyInspectionsSuppressor"/>
</extensions>
@@ -15,16 +15,24 @@
*/
package com.jetbrains.python.pyi;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.psi.FileViewProvider;
import com.jetbrains.python.psi.LanguageLevel;
import com.jetbrains.python.psi.impl.PyFileImpl;
import org.jetbrains.annotations.NotNull;
/**
* @author vlan
*/
public class PyiFile extends PyFileImpl {
public PyiFile(FileViewProvider viewProvider) {
super(viewProvider);
super(viewProvider, PyiLanguageDialect.getInstance());
}
@NotNull
@Override
public FileType getFileType() {
return PyiFileType.INSTANCE;
}
@Override
@@ -0,0 +1,35 @@
/*
* Copyright 2000-2015 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 com.jetbrains.python.pyi;
import com.intellij.lang.Language;
import com.jetbrains.python.psi.PyFileElementType;
import org.jetbrains.annotations.NotNull;
/**
* @author vlan
*/
public class PyiFileElementType extends PyFileElementType {
protected PyiFileElementType(Language language) {
super(language);
}
@NotNull
@Override
public String getExternalId() {
return "PythonStub.FILE";
}
}
@@ -0,0 +1,48 @@
/*
* Copyright 2000-2015 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 com.jetbrains.python.pyi;
import com.jetbrains.python.PythonFileType;
import org.jetbrains.annotations.NotNull;
/**
* @author vlan
*/
public class PyiFileType extends PythonFileType {
public static PythonFileType INSTANCE = new PyiFileType();
protected PyiFileType() {
super(new PyiLanguageDialect());
}
@NotNull
@Override
public String getName() {
return PyiLanguageDialect.ID;
}
@NotNull
@Override
public String getDescription() {
return "Python stub";
}
@NotNull
@Override
public String getDefaultExtension() {
return "pyi";
}
}
@@ -0,0 +1,32 @@
/*
* Copyright 2000-2015 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 com.jetbrains.python.pyi;
import com.intellij.openapi.fileTypes.FileTypeConsumer;
import com.intellij.openapi.fileTypes.FileTypeFactory;
import com.jetbrains.python.PythonFileType;
import org.jetbrains.annotations.NotNull;
/**
* @author vlan
*/
public class PyiFileTypeFactory extends FileTypeFactory {
@Override
public void createFileTypes(@NotNull FileTypeConsumer consumer) {
final PythonFileType instance = PyiFileType.INSTANCE;
consumer.consume(instance, instance.getDefaultExtension());
}
}
@@ -0,0 +1,34 @@
/*
* Copyright 2000-2015 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 com.jetbrains.python.pyi;
import com.intellij.lang.Language;
import com.jetbrains.python.PythonLanguage;
/**
* @author vlan
*/
public class PyiLanguageDialect extends Language {
public static final String ID = "PythonStub";
protected PyiLanguageDialect() {
super(PythonLanguage.getInstance(), ID);
}
public static PyiLanguageDialect getInstance() {
return (PyiLanguageDialect)PyiFileType.INSTANCE.getLanguage();
}
}
@@ -17,14 +17,22 @@ package com.jetbrains.python.pyi;
import com.intellij.psi.FileViewProvider;
import com.intellij.psi.PsiFile;
import com.intellij.psi.tree.IFileElementType;
import com.jetbrains.python.PythonParserDefinition;
/**
* @author vlan
*/
public class PyiParserDefinition extends PythonParserDefinition {
public static final IFileElementType PYTHON_STUB_FILE = new PyiFileElementType(PyiLanguageDialect.getInstance());
@Override
public PsiFile createFile(FileViewProvider viewProvider) {
return new PyiFile(viewProvider);
}
@Override
public IFileElementType getFileNodeType() {
return PYTHON_STUB_FILE;
}
}
@@ -0,0 +1,6 @@
class C:
def f() -> None: ...
def g() -> C: ...
# <ref>
@@ -63,7 +63,7 @@ public abstract class PyMultiFileResolveTestCase extends PyResolveTestCase {
protected PsiFile prepareFile() {
prepareTestDirectory();
VirtualFile sourceFile = null;
for (String ext : new String[]{".py", ".pyx"}) {
for (String ext : new String[]{".py", ".pyx", ".pyi"}) {
final String fileName = myTestFileName != null ? myTestFileName : getTestName(false) + ext;
sourceFile = myFixture.findFileInTempDir(fileName);
if (sourceFile != null) {
@@ -0,0 +1,34 @@
/*
* Copyright 2000-2015 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 com.jetbrains.python.pyi;
import com.jetbrains.python.PythonTestUtil;
import com.jetbrains.python.fixtures.PyMultiFileResolveTestCase;
import com.jetbrains.python.psi.PyClass;
/**
* @author vlan
*/
public class PyiResolveTest extends PyMultiFileResolveTestCase {
@Override
protected String getTestDataPath() {
return PythonTestUtil.getTestDataPath() + "/pyi/resolve";
}
public void testClassInsidePyiFile() {
assertResolvesTo(PyClass.class, "C");
}
}