Search in sources :

Example 11 with Substring

use of com.jetbrains.python.toolbox.Substring in project intellij-community by JetBrains.

the class DocStringUpdater method insertBeforeLine.

protected final void insertBeforeLine(int lineNumber, @NotNull String text) {
    final Substring line = myOriginalDocString.getLines().get(lineNumber);
    insert(line.getStartOffset(), text + '\n');
}
Also used : Substring(com.jetbrains.python.toolbox.Substring)

Example 12 with Substring

use of com.jetbrains.python.toolbox.Substring in project intellij-community by JetBrains.

the class GoogleCodeStyleDocString method parseSectionHeader.

@NotNull
@Override
protected Pair<Substring, Integer> parseSectionHeader(int lineNum) {
    final Substring line = getLine(lineNum);
    final Matcher matcher = SECTION_HEADER.matcher(line);
    if (matcher.matches()) {
        final Substring title = line.getMatcherGroup(matcher, 1).trim();
        if (isValidSectionTitle(title.toString())) {
            return Pair.create(title, lineNum + 1);
        }
    }
    return Pair.create(null, lineNum);
}
Also used : Substring(com.jetbrains.python.toolbox.Substring) Matcher(java.util.regex.Matcher) NotNull(org.jetbrains.annotations.NotNull)

Example 13 with Substring

use of com.jetbrains.python.toolbox.Substring in project intellij-community by JetBrains.

the class NumpyDocString method parseHeader.

@Override
protected int parseHeader(int startLine) {
    final int nextNonEmptyLineNum = consumeEmptyLines(startLine);
    final Substring line = getLineOrNull(nextNonEmptyLineNum);
    if (line != null && SIGNATURE.matcher(line).matches()) {
        mySignature = line.trim();
        return nextNonEmptyLineNum + 1;
    }
    return nextNonEmptyLineNum;
}
Also used : Substring(com.jetbrains.python.toolbox.Substring)

Example 14 with Substring

use of com.jetbrains.python.toolbox.Substring in project intellij-community by JetBrains.

the class PyDocstringGenerator method startTemplate.

public void startTemplate() {
    Preconditions.checkNotNull(myDocStringOwner, "For this action docstring owner must be supplied");
    final PyStringLiteralExpression docStringExpression = getDocStringExpression();
    assert docStringExpression != null;
    final TemplateBuilder builder = TemplateBuilderFactory.getInstance().createTemplateBuilder(docStringExpression);
    if (myAddedParams.size() > 1) {
        throw new IllegalArgumentException("TemplateBuilder can be created only for one parameter");
    }
    final DocstringParam paramToEdit = getParamToEdit();
    final DocStringFormat format = myDocStringFormat;
    if (format == DocStringFormat.PLAIN) {
        return;
    }
    final StructuredDocString parsed = DocStringUtil.parseDocString(format, docStringExpression);
    final Substring substring;
    if (paramToEdit.isReturnValue()) {
        substring = parsed.getReturnTypeSubstring();
    } else {
        final String paramName = paramToEdit.getName();
        substring = parsed.getParamTypeSubstring(paramName);
    }
    if (substring == null) {
        return;
    }
    builder.replaceRange(substring.getTextRange(), getDefaultType(getParamToEdit()));
    Template template = ((TemplateBuilderImpl) builder).buildInlineTemplate();
    final VirtualFile virtualFile = myDocStringOwner.getContainingFile().getVirtualFile();
    if (virtualFile == null)
        return;
    final Project project = myDocStringOwner.getProject();
    OpenFileDescriptor descriptor = new OpenFileDescriptor(project, virtualFile, docStringExpression.getTextOffset());
    Editor targetEditor = FileEditorManager.getInstance(project).openTextEditor(descriptor, true);
    if (targetEditor != null) {
        TemplateManager.getInstance(project).startTemplate(targetEditor, template);
    }
}
Also used : Substring(com.jetbrains.python.toolbox.Substring) VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) Editor(com.intellij.openapi.editor.Editor)

Example 15 with Substring

use of com.jetbrains.python.toolbox.Substring in project intellij-community by JetBrains.

the class DocStringReferenceProvider method referencesFromNames.

private static List<PsiReference> referencesFromNames(@NotNull PyStringLiteralExpression element, int offset, @NotNull StructuredDocString docString, @NotNull List<Substring> paramNames, @NotNull ReferenceType refType) {
    List<PsiReference> result = new ArrayList<>();
    for (Substring name : paramNames) {
        final String s = name.toString();
        if (PyNames.isIdentifier(s)) {
            final TextRange range = name.getTextRange().shiftRight(offset);
            result.add(new DocStringParameterReference(element, range, refType));
        }
        if (refType.equals(ReferenceType.PARAMETER_TYPE)) {
            final Substring type = docString.getParamTypeSubstring(s);
            if (type != null) {
                result.addAll(parseTypeReferences(element, type, offset));
            }
        }
    }
    return result;
}
Also used : Substring(com.jetbrains.python.toolbox.Substring) ArrayList(java.util.ArrayList) PsiReference(com.intellij.psi.PsiReference) TextRange(com.intellij.openapi.util.TextRange) StructuredDocString(com.jetbrains.python.psi.StructuredDocString)

Aggregations

Substring (com.jetbrains.python.toolbox.Substring)24 NotNull (org.jetbrains.annotations.NotNull)6 ArrayList (java.util.ArrayList)5 StructuredDocString (com.jetbrains.python.psi.StructuredDocString)4 TextRange (com.intellij.openapi.util.TextRange)3 PsiReference (com.intellij.psi.PsiReference)3 EpydocString (com.jetbrains.python.documentation.docstrings.EpydocString)3 Section (com.jetbrains.python.documentation.docstrings.SectionBasedDocString.Section)3 SectionField (com.jetbrains.python.documentation.docstrings.SectionBasedDocString.SectionField)3 Matcher (java.util.regex.Matcher)3 ImmutableList (com.google.common.collect.ImmutableList)2 List (java.util.List)2 ProblemsHolder (com.intellij.codeInspection.ProblemsHolder)1 Editor (com.intellij.openapi.editor.Editor)1 OpenFileDescriptor (com.intellij.openapi.fileEditor.OpenFileDescriptor)1 Project (com.intellij.openapi.project.Project)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PlainDocString (com.jetbrains.python.documentation.docstrings.PlainDocString)1 DocstringQuickFix (com.jetbrains.python.inspections.quickfix.DocstringQuickFix)1