Search in sources :

Example 6 with Substring

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

the class SectionBasedDocStringUpdater method removeParameter.

@Override
public void removeParameter(@NotNull final String name) {
    for (Section section : myOriginalDocString.getParameterSections()) {
        final List<SectionField> sectionFields = section.getFields();
        for (SectionField field : sectionFields) {
            final Substring nameSub = ContainerUtil.find(field.getNamesAsSubstrings(), substring -> substring.toString().equals(name));
            if (nameSub != null) {
                if (field.getNamesAsSubstrings().size() == 1) {
                    final int endLine = getFieldEndLine(field);
                    if (sectionFields.size() == 1) {
                        removeLinesAndSpacesAfter(getSectionStartLine(section), endLine + 1);
                    } else {
                        final int startLine = getFieldStartLine(field);
                        if (ContainerUtil.getLastItem(sectionFields) == field) {
                            removeLines(startLine, endLine + 1);
                        } else {
                            removeLinesAndSpacesAfter(startLine, endLine + 1);
                        }
                    }
                } else {
                    final Substring wholeParamName = expandParamNameSubstring(nameSub);
                    remove(wholeParamName.getStartOffset(), wholeParamName.getEndOffset());
                }
                break;
            }
        }
    }
}
Also used : Substring(com.jetbrains.python.toolbox.Substring) SectionField(com.jetbrains.python.documentation.docstrings.SectionBasedDocString.SectionField) Section(com.jetbrains.python.documentation.docstrings.SectionBasedDocString.Section)

Example 7 with Substring

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

the class TagBasedDocString method parseTag.

protected int parseTag(int lineno, String tagPrefix) {
    final Substring lineWithPrefix = getLine(lineno).trimLeft();
    if (lineWithPrefix.startsWith(tagPrefix)) {
        final Substring line = lineWithPrefix.substring(tagPrefix.length());
        final Matcher strictTagMatcher = RE_STRICT_TAG_LINE.matcher(line);
        final Matcher looseTagMatcher = RE_LOOSE_TAG_LINE.matcher(line);
        Matcher tagMatcher = null;
        if (strictTagMatcher.matches()) {
            tagMatcher = strictTagMatcher;
        } else if (looseTagMatcher.matches()) {
            tagMatcher = looseTagMatcher;
        }
        if (tagMatcher != null) {
            final Substring tagName = line.getMatcherGroup(tagMatcher, 1);
            final Substring argName = line.getMatcherGroup(tagMatcher, 2).trim();
            final TextRange firstArgLineRange = line.getMatcherGroup(tagMatcher, 3).trim().getTextRange();
            final int linesCount = getLineCount();
            final int argStart = firstArgLineRange.getStartOffset();
            int argEnd = firstArgLineRange.getEndOffset();
            while (lineno + 1 < linesCount) {
                final Substring nextLine = getLine(lineno + 1).trim();
                if (nextLine.isEmpty() || nextLine.startsWith(tagPrefix)) {
                    break;
                }
                argEnd = nextLine.getTextRange().getEndOffset();
                lineno++;
            }
            final Substring argValue = new Substring(argName.getSuperString(), argStart, argEnd);
            final String tagNameString = tagName.toString();
            if (argName.isEmpty()) {
                mySimpleTagValues.put(tagNameString, argValue);
            } else {
                if ("param".equals(tagNameString) || "parameter".equals(tagNameString) || "arg".equals(tagNameString) || "argument".equals(tagNameString)) {
                    final Matcher argTypeMatcher = RE_ARG_TYPE.matcher(argName);
                    if (argTypeMatcher.matches()) {
                        final Substring type = argName.getMatcherGroup(argTypeMatcher, 1).trim();
                        final Substring arg = argName.getMatcherGroup(argTypeMatcher, 2);
                        getTagValuesMap(TYPE).put(arg, type);
                    } else {
                        getTagValuesMap(tagNameString).put(argName, argValue);
                    }
                } else {
                    getTagValuesMap(tagNameString).put(argName, argValue);
                }
            }
        }
    }
    return lineno;
}
Also used : Substring(com.jetbrains.python.toolbox.Substring) Matcher(java.util.regex.Matcher) TextRange(com.intellij.openapi.util.TextRange) StructuredDocString(com.jetbrains.python.psi.StructuredDocString)

Example 8 with Substring

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

the class TagBasedDocStringUpdater method removeParameter.

@Override
public void removeParameter(@NotNull String name) {
    final List<Substring> nameSubs = myOriginalDocString.getParameterSubstrings();
    for (Substring sub : nameSubs) {
        if (sub.toString().equals(name)) {
            final int startLine = sub.getStartLine();
            final int nextAfterBlock = myOriginalDocString.consumeIndentedBlock(startLine + 1, getLineIndentSize(startLine));
            removeLinesAndSpacesAfter(startLine, nextAfterBlock);
        }
    }
}
Also used : Substring(com.jetbrains.python.toolbox.Substring)

Example 9 with Substring

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

the class DocStringReferenceProvider method referencesFromFields.

@NotNull
private static List<PsiReference> referencesFromFields(@NotNull PyStringLiteralExpression element, int offset, @NotNull List<SectionBasedDocString.SectionField> fields, @Nullable ReferenceType nameRefType) {
    final List<PsiReference> result = new ArrayList<>();
    for (SectionBasedDocString.SectionField field : fields) {
        for (Substring nameSub : field.getNamesAsSubstrings()) {
            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;
}
Also used : Substring(com.jetbrains.python.toolbox.Substring) ArrayList(java.util.ArrayList) PsiReference(com.intellij.psi.PsiReference) TextRange(com.intellij.openapi.util.TextRange) NotNull(org.jetbrains.annotations.NotNull)

Example 10 with Substring

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

the class DocStringUpdater method insertAfterLine.

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

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