PY-4717 Detect Numpy docstring from its content

This commit is contained in:
Mikhail Golubev
2015-08-10 19:26:12 +03:00
parent cfa78401a1
commit 31e755b87a
3 changed files with 25 additions and 5 deletions

View File

@@ -15,6 +15,7 @@
*/
package com.jetbrains.python.documentation;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.PsiComment;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiWhiteSpace;
@@ -51,6 +52,9 @@ public class DocStringUtil {
if (isGoogleDocString(text)) {
return new GoogleCodeStyleDocString(text);
}
if (isNumpyDocstring(text)) {
return new NumpyDocString(text);
}
return new EpydocString(text);
}
@@ -61,7 +65,7 @@ public class DocStringUtil {
public static boolean isEpydocDocString(@NotNull String text) {
return text.contains("@param ") || text.contains("@rtype") || text.contains("@type");
}
public static boolean isGoogleDocString(@NotNull String text) {
final Matcher matcher = GoogleCodeStyleDocString.SECTION_HEADER_RE.matcher(text);
if (!matcher.find()) {
@@ -70,7 +74,21 @@ public class DocStringUtil {
@NonNls final String foundName = matcher.group(1).trim();
return SectionBasedDocString.SECTION_NAMES.contains(foundName.toLowerCase());
}
public static boolean isNumpyDocstring(@NotNull String text) {
final String[] lines = StringUtil.splitByLines(text, false);
for (int i = 0; i < lines.length; i++) {
final String line = lines[i];
if (NumpyDocString.SECTION_HEADER.matcher(line).matches() && i > 0) {
@NonNls final String lineBefore = lines[i - 1];
if (SectionBasedDocString.SECTION_NAMES.contains(lineBefore.trim().toLowerCase())) {
return true;
}
}
}
return false;
}
/**
* Looks for a doc string under given parent.
* @param parent where to look. For classes and functions, this would be PyStatementList, for modules, PyFile.

View File

@@ -28,8 +28,8 @@ import java.util.regex.Pattern;
* @see <a href="http://sphinxcontrib-napoleon.readthedocs.org/en/latest/example_numpy.html#example-numpy">Napoleon: Example NumPy Style Python Docstrings</a>
*/
public class NumpyDocString extends SectionBasedDocString {
private static final Pattern SIGNATURE = Pattern.compile("^\\s*([\\w., ]+=)?\\s*[\\w\\.]+\\(.*\\)\\s*$");
private static final Pattern SECTION_HEADER = Pattern.compile("^\\s*[-=]{2,}\\s*$");
private static final Pattern SIGNATURE = Pattern.compile("^\\s*([\\w., ]+=)?\\s*[\\w\\.]+\\(.*\\)\\s*$", Pattern.MULTILINE);
public static final Pattern SECTION_HEADER = Pattern.compile("^\\s*[-=]{2,}\\s*$", Pattern.MULTILINE);
private Substring mySignature;
public NumpyDocString(@NotNull String text) {

View File

@@ -1,2 +1,4 @@
def ones(shape, dtype=None, order='C')
Inferred type: (shape:&nbsp;Union[<a href="psi_element://#typename#int">int</a>,&nbsp;Iterable[<a href="psi_element://#typename#int">int</a>]],&nbsp;dtype:&nbsp;<a href="psi_element://#typename#object">object</a>,&nbsp;order:&nbsp;<a href="psi_element://#typename#str">str</a>)&nbsp;-&gt;&nbsp;ndarray<br>
Inferred type: (shape:&nbsp;Union[<a href="psi_element://#typename#int">int</a>,&nbsp;Iterable[<a href="psi_element://#typename#int">int</a>]],&nbsp;dtype:&nbsp;<a href="psi_element://#typename#object">object</a>,&nbsp;order:&nbsp;<a href="psi_element://#typename#str">str</a>)&nbsp;-&gt;&nbsp;ndarray<br>
**Test docstring**
Return a new array of given shape and type, filled with ones.