Search in sources :

Example 6 with PyExpressionCodeFragmentImpl

use of com.jetbrains.python.psi.impl.PyExpressionCodeFragmentImpl in project intellij-community by JetBrains.

the class PythonCopyPasteProcessor method addLeadingSpacesToNormalizeSelection.

@NotNull
private static String addLeadingSpacesToNormalizeSelection(@NotNull Project project, @NotNull String text) {
    if (!fragmentBeginsWithBlockStatement(text)) {
        return text;
    }
    final PyExpressionCodeFragmentImpl fragment = new PyExpressionCodeFragmentImpl(project, "dummy.py", text, false);
    //fragment.setContext(file);
    final PyStatementListContainer statement = as(fragment.getFirstChild(), PyStatementListContainer.class);
    if (statement == null) {
        return text;
    }
    final String statementIndent = PyIndentUtil.getElementIndent(statement);
    if (!statementIndent.isEmpty()) {
        return text;
    }
    final String indentStep = PyIndentUtil.getIndentFromSettings(project);
    final String bodyIndent = PyIndentUtil.getElementIndent(statement.getStatementList());
    final String expectedBodyIndent = statementIndent + indentStep;
    if (bodyIndent.startsWith(expectedBodyIndent)) {
        return bodyIndent.substring(0, bodyIndent.length() - indentStep.length()) + text;
    }
    return text;
}
Also used : PyExpressionCodeFragmentImpl(com.jetbrains.python.psi.impl.PyExpressionCodeFragmentImpl) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

PyExpressionCodeFragmentImpl (com.jetbrains.python.psi.impl.PyExpressionCodeFragmentImpl)6 PsiElement (com.intellij.psi.PsiElement)3 NotNull (org.jetbrains.annotations.NotNull)3 Nullable (org.jetbrains.annotations.Nullable)2 Project (com.intellij.openapi.project.Project)1 PyBuiltinCache (com.jetbrains.python.psi.impl.PyBuiltinCache)1