diff --git a/python/src/com/jetbrains/python/documentation/DocStringParameterReference.java b/python/src/com/jetbrains/python/documentation/DocStringParameterReference.java index d8d0844393f3..8b291c65d798 100644 --- a/python/src/com/jetbrains/python/documentation/DocStringParameterReference.java +++ b/python/src/com/jetbrains/python/documentation/DocStringParameterReference.java @@ -41,13 +41,15 @@ import java.util.Set; * @author yole */ public class DocStringParameterReference extends PsiReferenceBase implements PsiReferenceEx { - private final TagBasedDocString.ReferenceType myType; + private final ReferenceType myType; - public DocStringParameterReference(PyStringLiteralExpression element, TextRange range, TagBasedDocString.ReferenceType refType) { + public DocStringParameterReference(PyStringLiteralExpression element, TextRange range, ReferenceType refType) { super(element, range); myType = refType; } + public enum ReferenceType {PARAMETER, PARAMETER_TYPE, KEYWORD, VARIABLE, CLASS_VARIABLE, INSTANCE_VARIABLE} + @Override public PsiElement resolve() { PyDocStringOwner owner = PsiTreeUtil.getParentOfType(getElement(), PyDocStringOwner.class); @@ -58,20 +60,20 @@ public class DocStringParameterReference extends PsiReferenceBase referencesFromNames(PyStringLiteralExpression element, + private static List referencesFromNames(@NotNull PyStringLiteralExpression element, int offset, - StructuredDocString docString, - List paramNames, - TagBasedDocString.ReferenceType refType) { + @NotNull StructuredDocString docString, + @NotNull List paramNames, + @NotNull ReferenceType refType) { List result = new ArrayList(); for (Substring name : paramNames) { final String s = name.toString(); @@ -109,7 +118,7 @@ public class DocStringReferenceProvider extends PsiReferenceProvider { final TextRange range = name.getTextRange().shiftRight(offset); result.add(new DocStringParameterReference(element, range, refType)); } - if (refType.equals(TagBasedDocString.ReferenceType.PARAMETER_TYPE)) { + if (refType.equals(ReferenceType.PARAMETER_TYPE)) { final Substring type = docString.getParamTypeSubstring(s); if (type != null) { result.addAll(parseTypeReferences(element, type, offset)); @@ -119,7 +128,28 @@ public class DocStringReferenceProvider extends PsiReferenceProvider { return result; } - private static List parseTypeReferences(PsiElement anchor, Substring s, int offset) { + @NotNull + private static List referencesFromFields(@NotNull PyStringLiteralExpression element, + int offset, + @NotNull List fields, + @Nullable ReferenceType nameRefType) { + final List result = new ArrayList(); + for (SectionField field : fields) { + final Substring nameSub = field.getNameAsSubstring(); + if (nameRefType != null && nameSub != null && !nameSub.isEmpty()) { + final TextRange range = nameSub.getTextRange().shiftRight(offset); + result.add(new DocStringParameterReference(element, range, nameRefType)); + } + final Substring typeSub = field.getTypeAsSubstring(); + if (typeSub != null && !typeSub.isEmpty()) { + result.addAll(parseTypeReferences(element, typeSub, offset)); + } + } + return result; + } + + @NotNull + private static List parseTypeReferences(@NotNull PsiElement anchor, @NotNull Substring s, int offset) { final List result = new ArrayList(); final PyTypeParser.ParseResult parseResult = PyTypeParser.parse(anchor, s.toString()); final Map types = parseResult.getTypes(); diff --git a/python/src/com/jetbrains/python/documentation/DocStringUtil.java b/python/src/com/jetbrains/python/documentation/DocStringUtil.java index 724d9f8df197..320392860333 100644 --- a/python/src/com/jetbrains/python/documentation/DocStringUtil.java +++ b/python/src/com/jetbrains/python/documentation/DocStringUtil.java @@ -15,7 +15,6 @@ */ package com.jetbrains.python.documentation; -import com.google.common.base.Preconditions; import com.intellij.lang.ASTNode; import com.intellij.openapi.module.Module; import com.intellij.openapi.module.ModuleManager; @@ -29,7 +28,6 @@ import com.intellij.psi.PsiFile; import com.intellij.psi.PsiWhiteSpace; import com.intellij.psi.util.PsiTreeUtil; import com.intellij.util.ArrayUtil; -import com.jetbrains.python.PyTokenTypes; import com.jetbrains.python.codeInsight.controlflow.ScopeOwner; import com.jetbrains.python.psi.*; import com.jetbrains.python.psi.impl.PyPsiUtils; @@ -80,7 +78,7 @@ public class DocStringUtil { @NotNull public static StructuredDocString parseDocString(@NotNull DocStringFormat format, @NotNull ASTNode node) { - Preconditions.checkArgument(node.getElementType() == PyTokenTypes.DOCSTRING); + //Preconditions.checkArgument(node.getElementType() == PyTokenTypes.DOCSTRING); return parseDocString(format, node.getText()); } diff --git a/python/src/com/jetbrains/python/documentation/SectionBasedDocString.java b/python/src/com/jetbrains/python/documentation/SectionBasedDocString.java index 08f8f52455e8..87bceb127f13 100644 --- a/python/src/com/jetbrains/python/documentation/SectionBasedDocString.java +++ b/python/src/com/jetbrains/python/documentation/SectionBasedDocString.java @@ -400,14 +400,10 @@ public abstract class SectionBasedDocString extends DocStringLineParser implemen } @NotNull - private List getParameterFields() { + public List getParameterFields() { final List result = new ArrayList(); - for (Section section : mySections) { - if (section.getNormalizedTitle().equals(PARAMETERS_SECTION)) { - for (SectionField field : section.getFields()) { - result.add(field); - } - } + for (Section section : getSectionsWithNormalizedTitle(PARAMETERS_SECTION)) { + result.addAll(section.getFields()); } return result; } @@ -445,14 +441,10 @@ public abstract class SectionBasedDocString extends DocStringLineParser implemen } @NotNull - private List getKeywordArgumentFields() { + public List getKeywordArgumentFields() { final List result = new ArrayList(); - for (Section section : mySections) { - if (section.getNormalizedTitle().equals(KEYWORD_ARGUMENTS_SECTION)) { - for (SectionField field : section.getFields()) { - result.add(field); - } - } + for (Section section : getSectionsWithNormalizedTitle(KEYWORD_ARGUMENTS_SECTION)) { + result.addAll(section.getFields()); } return result; } @@ -488,14 +480,19 @@ public abstract class SectionBasedDocString extends DocStringLineParser implemen return field != null ? field.getDescription() : null; } + + @NotNull + public List getReturnFields() { + final List result = new ArrayList(); + for (Section section : getSectionsWithNormalizedTitle(RETURNS_SECTION)) { + result.addAll(section.getFields()); + } + return result; + } + @Nullable private SectionField getFirstReturnField() { - for (Section section : mySections) { - if (section.getNormalizedTitle().equals(RETURNS_SECTION) && !section.getFields().isEmpty()) { - return section.getFields().get(0); - } - } - return null; + return ContainerUtil.getFirstItem(getReturnFields()); } @Override @@ -521,12 +518,10 @@ public abstract class SectionBasedDocString extends DocStringLineParser implemen } @NotNull - private List getExceptionFields() { + public List getExceptionFields() { final List result = new ArrayList(); - for (Section section : mySections) { - if (section.getNormalizedTitle().equals(RAISES_SECTION)) { - result.addAll(section.getFields()); - } + for (Section section : getSectionsWithNormalizedTitle(RAISES_SECTION)) { + result.addAll(section.getFields()); } return result; } @@ -541,6 +536,25 @@ public abstract class SectionBasedDocString extends DocStringLineParser implemen }); } + @NotNull + public List getAttributeFields() { + final List result = new ArrayList(); + for (Section section : getSectionsWithNormalizedTitle(ATTRIBUTES_SECTION)) { + result.addAll(section.getFields()); + } + return result; + } + + @NotNull + private List
getSectionsWithNormalizedTitle(@NotNull final String title) { + return ContainerUtil.mapNotNull(mySections, new Function() { + @Override + public Section fun(Section section) { + return section.getNormalizedTitle().equals(title) ? section : null; + } + }); + } + @Nullable @Override public String getAttributeDescription() { diff --git a/python/src/com/jetbrains/python/documentation/TagBasedDocString.java b/python/src/com/jetbrains/python/documentation/TagBasedDocString.java index 09f18c78fffa..432d1fc0c18b 100644 --- a/python/src/com/jetbrains/python/documentation/TagBasedDocString.java +++ b/python/src/com/jetbrains/python/documentation/TagBasedDocString.java @@ -52,8 +52,6 @@ public abstract class TagBasedDocString extends DocStringLineParser implements S @NotNull private final String myTagPrefix; - public enum ReferenceType {PARAMETER, PARAMETER_TYPE, KEYWORD, VARIABLE, CLASS_VARIABLE, INSTANCE_VARIABLE} - public static String TYPE = "type"; protected TagBasedDocString(@NotNull Substring docStringText, @NotNull String tagPrefix) {