diff --git a/python/src/META-INF/python-core.xml b/python/src/META-INF/python-core.xml index 476948f3a9bc..12e677ff5852 100644 --- a/python/src/META-INF/python-core.xml +++ b/python/src/META-INF/python-core.xml @@ -560,6 +560,8 @@ + + diff --git a/python/src/com/jetbrains/python/pyi/PyiFile.java b/python/src/com/jetbrains/python/pyi/PyiFile.java index 52e4f05c7af9..a83c3de08872 100644 --- a/python/src/com/jetbrains/python/pyi/PyiFile.java +++ b/python/src/com/jetbrains/python/pyi/PyiFile.java @@ -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 diff --git a/python/src/com/jetbrains/python/pyi/PyiFileElementType.java b/python/src/com/jetbrains/python/pyi/PyiFileElementType.java new file mode 100644 index 000000000000..d05f0793ecf9 --- /dev/null +++ b/python/src/com/jetbrains/python/pyi/PyiFileElementType.java @@ -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"; + } +} diff --git a/python/src/com/jetbrains/python/pyi/PyiFileType.java b/python/src/com/jetbrains/python/pyi/PyiFileType.java new file mode 100644 index 000000000000..8d629d4951e2 --- /dev/null +++ b/python/src/com/jetbrains/python/pyi/PyiFileType.java @@ -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"; + } +} diff --git a/python/src/com/jetbrains/python/pyi/PyiFileTypeFactory.java b/python/src/com/jetbrains/python/pyi/PyiFileTypeFactory.java new file mode 100644 index 000000000000..52b4c066e8c1 --- /dev/null +++ b/python/src/com/jetbrains/python/pyi/PyiFileTypeFactory.java @@ -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()); + } +} diff --git a/python/src/com/jetbrains/python/pyi/PyiLanguageDialect.java b/python/src/com/jetbrains/python/pyi/PyiLanguageDialect.java new file mode 100644 index 000000000000..38b07d0258d2 --- /dev/null +++ b/python/src/com/jetbrains/python/pyi/PyiLanguageDialect.java @@ -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(); + } +} diff --git a/python/src/com/jetbrains/python/pyi/PyiParserDefinition.java b/python/src/com/jetbrains/python/pyi/PyiParserDefinition.java index ff3c9f5479a7..a62a6a7762c2 100644 --- a/python/src/com/jetbrains/python/pyi/PyiParserDefinition.java +++ b/python/src/com/jetbrains/python/pyi/PyiParserDefinition.java @@ -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; + } } diff --git a/python/testData/pyi/resolve/classInsidePyiFile/ClassInsidePyiFile.pyi b/python/testData/pyi/resolve/classInsidePyiFile/ClassInsidePyiFile.pyi new file mode 100644 index 000000000000..724f1150f250 --- /dev/null +++ b/python/testData/pyi/resolve/classInsidePyiFile/ClassInsidePyiFile.pyi @@ -0,0 +1,6 @@ +class C: + def f() -> None: ... + + +def g() -> C: ... +# diff --git a/python/testSrc/com/jetbrains/python/fixtures/PyMultiFileResolveTestCase.java b/python/testSrc/com/jetbrains/python/fixtures/PyMultiFileResolveTestCase.java index e3279805a97f..936e2abfdc92 100644 --- a/python/testSrc/com/jetbrains/python/fixtures/PyMultiFileResolveTestCase.java +++ b/python/testSrc/com/jetbrains/python/fixtures/PyMultiFileResolveTestCase.java @@ -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) { diff --git a/python/testSrc/com/jetbrains/python/pyi/PyiResolveTest.java b/python/testSrc/com/jetbrains/python/pyi/PyiResolveTest.java new file mode 100644 index 000000000000..ff99d0803fd3 --- /dev/null +++ b/python/testSrc/com/jetbrains/python/pyi/PyiResolveTest.java @@ -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"); + } +}