PY-16877 Complete section names in Google and Numpy docstrings

This commit is contained in:
Mikhail Golubev
2015-09-14 22:32:52 +03:00
parent bb1039e02d
commit ec3c403fbf
6 changed files with 93 additions and 4 deletions
+1 -4
View File
@@ -481,6 +481,7 @@
serviceImplementation="com.jetbrains.python.documentation.PyDocumentationSettings"/>
<psi.referenceContributor implementation="com.jetbrains.python.documentation.docstrings.DocStringReferenceContributor"/>
<completion.contributor language="Python" implementationClass="com.jetbrains.python.documentation.docstrings.DocStringTagCompletionContributor"/>
<completion.contributor language="Python" implementationClass="com.jetbrains.python.documentation.docstrings.DocStringSectionHeaderCompletionContributor"/>
<projectService serviceInterface="com.intellij.psi.search.ProjectScopeBuilder"
serviceImplementation="com.jetbrains.python.psi.search.PyProjectScopeBuilder"
@@ -790,10 +791,6 @@
<add-to-group group-id="XDebugger.ToolWindow.TopToolbar" relative-to-action="StepInto" anchor="after"/>
</action>
<action class="com.jetbrains.python.codeInsight.PyProbeAction" id="PyProbeAction" text="Python Probe Action" internal="true">
<add-to-group group-id="Internal"/>
</action>
</actions>
<extensions defaultExtensionNs="com.intellij.spellchecker">
@@ -0,0 +1,59 @@
/*
* 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.documentation.docstrings;
import com.intellij.codeInsight.completion.*;
import com.intellij.codeInsight.lookup.LookupElementBuilder;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.util.ProcessingContext;
import org.jetbrains.annotations.NotNull;
import static com.intellij.patterns.PlatformPatterns.psiElement;
/**
* @author Mikhail Golubev
*/
public class DocStringSectionHeaderCompletionContributor extends CompletionContributor {
public DocStringSectionHeaderCompletionContributor() {
extend(CompletionType.BASIC, psiElement().withParent(DocStringTagCompletionContributor.DOCSTRING_PATTERN),
new CompletionProvider<CompletionParameters>() {
@Override
protected void addCompletions(@NotNull CompletionParameters parameters,
ProcessingContext context,
@NotNull CompletionResultSet result) {
final PsiFile file = parameters.getOriginalFile();
final PsiElement stringNode = parameters.getOriginalPosition();
assert stringNode != null;
final int offset = parameters.getOffset();
final DocStringFormat format = DocStringUtil.getConfiguredDocStringFormat(file);
if (!(format == DocStringFormat.GOOGLE || format == DocStringFormat.NUMPY)) {
return;
}
final Document document = parameters.getEditor().getDocument();
final TextRange linePrefixRange = new TextRange(document.getLineStartOffset(document.getLineNumber(offset)), offset);
final String prefix = StringUtil.trimLeading(document.getText(linePrefixRange));
result = result.withPrefixMatcher(prefix).caseInsensitive();
for (String tag : SectionBasedDocString.SECTION_NAMES) {
result.addElement(LookupElementBuilder.create(StringUtil.capitalize(tag)));
}
}
});
}
}
@@ -0,0 +1,4 @@
def f():
"""
<caret>
"""
@@ -0,0 +1,4 @@
def f():
"""
Keyword arguments:
"""
@@ -0,0 +1,4 @@
def f():
"""
Keyword argu<caret>:
"""
@@ -405,6 +405,27 @@ public class PythonCompletionTest extends PyTestCase {
LookupElementBuilder.create("bar").withAutoCompletionPolicy(AutoCompletionPolicy.NEVER_AUTOCOMPLETE));
}
// PY-16877
public void testSectionNamesInGoogleDocstring() {
runWithDocStringFormat(DocStringFormat.GOOGLE, new Runnable() {
public void run() {
final List<String> variants = doTestByFile();
assertNotNull(variants);
assertContainsElements(variants, "Args", "Parameters", "Keyword arguments", "Returns");
}
});
}
// PY-16877
public void testTwoWordsSectionNameInGoogleDocstring() throws Exception {
runWithDocStringFormat(DocStringFormat.GOOGLE, new Runnable() {
@Override
public void run() {
doTest();
}
});
}
public void testPep328Completion() { // PY-3409
myFixture.copyDirectoryToProject("pep328", "pep328");
myFixture.configureByFile("pep328/package/subpackage1/moduleX.py");