diff options
Diffstat (limited to 'python/src/com/jetbrains/python/psi/impl')
7 files changed, 70 insertions, 46 deletions
diff --git a/python/src/com/jetbrains/python/psi/impl/PyElementGeneratorImpl.java b/python/src/com/jetbrains/python/psi/impl/PyElementGeneratorImpl.java index 744dc9731b41..bb0e29f800a7 100644 --- a/python/src/com/jetbrains/python/psi/impl/PyElementGeneratorImpl.java +++ b/python/src/com/jetbrains/python/psi/impl/PyElementGeneratorImpl.java @@ -24,6 +24,7 @@ import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiFile; import com.intellij.psi.PsiFileFactory; +import com.intellij.psi.PsiWhiteSpace; import com.intellij.psi.impl.PsiFileFactoryImpl; import com.intellij.psi.impl.source.tree.LeafPsiElement; import com.intellij.psi.tree.TokenSet; @@ -86,8 +87,9 @@ public class PyElementGeneratorImpl extends PyElementGenerator { } + @Override public PyStringLiteralExpression createStringLiteralFromString(@NotNull String unescaped) { - return createStringLiteralFromString(null, unescaped); + return createStringLiteralFromString(null, unescaped, true); } public PyStringLiteralExpression createStringLiteral(@NotNull PyStringLiteralExpression oldElement, @NotNull String unescaped) { @@ -100,7 +102,11 @@ public class PyElementGeneratorImpl extends PyElementGenerator { } } - public PyStringLiteralExpression createStringLiteralFromString(@Nullable PsiFile destination, @NotNull String unescaped) { + + @Override + public PyStringLiteralExpression createStringLiteralFromString(@Nullable PsiFile destination, + @NotNull String unescaped, + final boolean preferUTF8) { boolean useDouble = !unescaped.contains("\""); boolean useMulti = unescaped.matches(".*(\r|\n).*"); String quotes; @@ -115,7 +121,7 @@ public class PyElementGeneratorImpl extends PyElementGenerator { VirtualFile vfile = destination == null ? null : destination.getVirtualFile(); Charset charset; if (vfile == null) { - charset = Charset.forName("US-ASCII"); + charset = (preferUTF8 ? Charset.forName("UTF-8") : Charset.forName("US-ASCII")); } else { charset = vfile.getCharset(); @@ -191,7 +197,7 @@ public class PyElementGeneratorImpl extends PyElementGenerator { final LeafPsiElement[] leafs = PsiTreeUtil.getChildrenOfType(list, LeafPsiElement.class); if (leafs != null) { final Deque<LeafPsiElement> commas = Queues.newArrayDeque(Collections2.filter(Arrays.asList(leafs), COMMAS_ONLY)); - if (! commas.isEmpty()) { + if (!commas.isEmpty()) { final LeafPsiElement lastComma = commas.getLast(); if (PsiTreeUtil.getNextSiblingOfType(lastComma, PyExpression.class) == null) { //Comma has no expression after it lastComma.delete(); @@ -297,7 +303,7 @@ public class PyElementGeneratorImpl extends PyElementGenerator { AccessDirection accessDirection) { String propertyText; if (accessDirection == AccessDirection.DELETE) { - propertyText = "@" + propertyName +".deleter\ndef " + propertyName + "(self):\n del self." + fieldName; + propertyText = "@" + propertyName + ".deleter\ndef " + propertyName + "(self):\n del self." + fieldName; } else if (accessDirection == AccessDirection.WRITE) { propertyText = "@" + propertyName + ".setter\ndef " + propertyName + "(self, value):\n self." + fieldName + " = value"; @@ -415,6 +421,12 @@ public class PyElementGeneratorImpl extends PyElementGenerator { PyExpressionStatement.class, content + "\n"); } + @NotNull + @Override + public PsiElement createNewLine() { + return createFromText(LanguageLevel.getDefault(), PsiWhiteSpace.class, " \n\n "); + } + private static class CommasOnly extends NotNullPredicate<LeafPsiElement> { @Override protected boolean applyNotNull(@NotNull final LeafPsiElement input) { diff --git a/python/src/com/jetbrains/python/psi/impl/PyElsePartImpl.java b/python/src/com/jetbrains/python/psi/impl/PyElsePartImpl.java index ddd02d5310a3..8c69893c62a8 100644 --- a/python/src/com/jetbrains/python/psi/impl/PyElsePartImpl.java +++ b/python/src/com/jetbrains/python/psi/impl/PyElsePartImpl.java @@ -15,26 +15,16 @@ */ package com.jetbrains.python.psi.impl; -import com.jetbrains.python.psi.PyElsePart; -import com.jetbrains.python.psi.PyStatementList; -import com.jetbrains.python.PyElementTypes; import com.intellij.lang.ASTNode; +import com.jetbrains.python.psi.PyElsePart; /** * User: dcheryasov * Date: Mar 15, 2009 9:40:35 PM */ -public class PyElsePartImpl extends PyElementImpl implements PyElsePart { +public class PyElsePartImpl extends PyStatementPartImpl implements PyElsePart { public PyElsePartImpl(ASTNode astNode) { super(astNode); } - - public PyStatementList getStatementList() { - ASTNode n = getNode().findChildByType(PyElementTypes.STATEMENT_LISTS); - if (n != null) { - return (PyStatementList)n.getPsi(); - } - return null; - } } diff --git a/python/src/com/jetbrains/python/psi/impl/PyFileImpl.java b/python/src/com/jetbrains/python/psi/impl/PyFileImpl.java index 030fcfe3d7e5..02b790622fe7 100644 --- a/python/src/com/jetbrains/python/psi/impl/PyFileImpl.java +++ b/python/src/com/jetbrains/python/psi/impl/PyFileImpl.java @@ -477,8 +477,9 @@ public class PyFileImpl extends PsiFileBase implements PyFile, PyExpression { if (starImportSource != null) { starImportSource = PyUtil.turnDirIntoInit(starImportSource); if (starImportSource instanceof PyFile) { - final PsiElement result = ((PyFile)starImportSource).getElementNamed(name); - if (result != null) { + final PyFile file = (PyFile)starImportSource; + final PsiElement result = file.getElementNamed(name); + if (result != null && PyUtil.isStarImportableFrom(name, file)) { return result; } } diff --git a/python/src/com/jetbrains/python/psi/impl/PyFunctionBuilder.java b/python/src/com/jetbrains/python/psi/impl/PyFunctionBuilder.java index e0cdf87b2aab..9d2be0f62205 100644 --- a/python/src/com/jetbrains/python/psi/impl/PyFunctionBuilder.java +++ b/python/src/com/jetbrains/python/psi/impl/PyFunctionBuilder.java @@ -23,6 +23,7 @@ import com.intellij.psi.codeStyle.CodeStyleSettingsManager; import com.intellij.util.ArrayUtil; import com.jetbrains.python.PyNames; import com.jetbrains.python.PythonFileType; +import com.jetbrains.python.documentation.StructuredDocStringBase; import com.jetbrains.python.psi.*; import org.jetbrains.annotations.NotNull; @@ -82,14 +83,21 @@ public class PyFunctionBuilder { /** * Adds docstring to function. Provide doc with out of comment blocks. * + * * @param docString doc */ public void docString(@NotNull final String docString) { - myDocStringLines = StringUtil.splitByLines(removeIndent(docString)); + final String[] stringsToAdd = StringUtil.splitByLines(removeIndent(docString)); + if (myDocStringLines == null) { + myDocStringLines = stringsToAdd; + } + else { + myDocStringLines = ArrayUtil.mergeArrays(myDocStringLines, stringsToAdd); + } } @NotNull - private String removeIndent(@NotNull final String string) { + private static String removeIndent(@NotNull final String string) { return INDENT_REMOVE_PATTERN.matcher(string).replaceAll(""); } @@ -97,6 +105,21 @@ public class PyFunctionBuilder { myName = name; } + /** + * Adds param and its type to doc + * @param name param name + * @param type param type + * @param docStyle what docstyle to use to doc param type + */ + @NotNull + public PyFunctionBuilder parameterWithType(@NotNull final String name, + @NotNull final String type, + @NotNull final StructuredDocStringBase docStyle) { + parameter(name); + docString(docStyle.createParameterType(name, type)); + return this; + } + public PyFunctionBuilder parameter(String baseName) { String name = baseName; int uniqueIndex = 0; @@ -173,8 +196,9 @@ public class PyFunctionBuilder { /** * Adds decorator with argument + * * @param decoratorName decorator name - * @param value its argument + * @param value its argument */ public void decorate(@NotNull final String decoratorName, @NotNull final String value) { decorate(decoratorName); diff --git a/python/src/com/jetbrains/python/psi/impl/PyStarImportElementImpl.java b/python/src/com/jetbrains/python/psi/impl/PyStarImportElementImpl.java index 4011c233e38d..88c3134481a8 100644 --- a/python/src/com/jetbrains/python/psi/impl/PyStarImportElementImpl.java +++ b/python/src/com/jetbrains/python/psi/impl/PyStarImportElementImpl.java @@ -52,11 +52,8 @@ public class PyStarImportElementImpl extends PyElementImpl implements PyStarImpo for (PsiElement importedFile : new HashSet<PsiElement>(importedFiles)) { // resolver gives lots of duplicates final PsiElement source = PyUtil.turnDirIntoInit(importedFile); if (source instanceof PyFile) { - Iterable<PyElement> declaredNames = ((PyFile)source).iterateNames(); - if (((PyFile)source).getDunderAll() == null) { - declaredNames = excludeUnderscoredNames(declaredNames); - } - chain.add(declaredNames); + final PyFile sourceFile = (PyFile)source; + chain.add(filterStarImportableNames(sourceFile.iterateNames(), sourceFile)); } } return chain; @@ -64,15 +61,13 @@ public class PyStarImportElementImpl extends PyElementImpl implements PyStarImpo return Collections.emptyList(); } - private static Iterable<PyElement> excludeUnderscoredNames(Iterable<PyElement> declaredNames) { + @NotNull + private static Iterable<PyElement> filterStarImportableNames(@NotNull Iterable<PyElement> declaredNames, @NotNull final PyFile file) { return Iterables.filter(declaredNames, new Predicate<PyElement>() { @Override public boolean apply(@Nullable PyElement input) { final String name = input != null ? input.getName() : null; - if (name != null && name.startsWith("_")) { - return false; - } - return true; + return name != null && PyUtil.isStarImportableFrom(name, file); } }); } @@ -93,11 +88,7 @@ public class PyStarImportElementImpl extends PyElementImpl implements PyStarImpo final List<? extends RatedResolveResult> results = moduleType.resolveMember(name, null, AccessDirection.READ, PyResolveContext.defaultContext()); final PsiElement result = results != null && !results.isEmpty() ? results.get(0).getElement() : null; - if (result != null) { - final List<String> all = sourceFile.getDunderAll(); - if (all != null ? !all.contains(name) : name.startsWith("_")) { - continue; - } + if (result != null && PyUtil.isStarImportableFrom(name, sourceFile) ) { return result; } } diff --git a/python/src/com/jetbrains/python/psi/impl/PyStatementPartImpl.java b/python/src/com/jetbrains/python/psi/impl/PyStatementPartImpl.java index 46e9a785a9d4..7454d6741382 100644 --- a/python/src/com/jetbrains/python/psi/impl/PyStatementPartImpl.java +++ b/python/src/com/jetbrains/python/psi/impl/PyStatementPartImpl.java @@ -15,10 +15,11 @@ */ package com.jetbrains.python.psi.impl; -import com.jetbrains.python.psi.PyStatementPart; -import com.jetbrains.python.psi.PyStatementList; -import com.jetbrains.python.PyElementTypes; import com.intellij.lang.ASTNode; +import com.jetbrains.python.PyElementTypes; +import com.jetbrains.python.psi.PyStatementList; +import com.jetbrains.python.psi.PyStatementPart; +import org.jetbrains.annotations.NotNull; /** * Abstract statement part implementation; extracts the statements list. @@ -30,11 +31,8 @@ public abstract class PyStatementPartImpl extends PyElementImpl implements PySta super(astNode); } + @NotNull public PyStatementList getStatementList() { - ASTNode n = getNode().findChildByType(PyElementTypes.STATEMENT_LISTS); - if (n != null) { - return (PyStatementList)n.getPsi(); - } - return null; + return childToPsiNotNull(PyElementTypes.STATEMENT_LIST); } } diff --git a/python/src/com/jetbrains/python/psi/impl/PyWithStatementImpl.java b/python/src/com/jetbrains/python/psi/impl/PyWithStatementImpl.java index 98cd8e5703d5..3af3771e8ecd 100644 --- a/python/src/com/jetbrains/python/psi/impl/PyWithStatementImpl.java +++ b/python/src/com/jetbrains/python/psi/impl/PyWithStatementImpl.java @@ -63,6 +63,14 @@ public class PyWithStatementImpl extends PyElementImpl implements PyWithStatemen } public PyWithItem[] getWithItems() { - return childrenToPsi(WITH_ITEM, PyWithItem.EMPTY_ARRAY); + return childrenToPsi(WITH_ITEM, PyWithItem.EMPTY_ARRAY); + } + + @Override + @NotNull + public PyStatementList getStatementList() { + final PyStatementList statementList = childToPsi(PyElementTypes.STATEMENT_LIST); + assert statementList != null : "Statement list missing for with statement " + getText(); + return statementList; } } |