use of com.intellij.psi.PsiComment in project intellij-community by JetBrains.
the class PyTargetExpressionImpl method getTypeCommentAnnotation.
@Nullable
@Override
public String getTypeCommentAnnotation() {
final PyTargetExpressionStub stub = getStub();
if (stub != null) {
return stub.getTypeComment();
}
final PsiComment comment = getTypeComment();
if (comment != null) {
return PyTypingTypeProvider.getTypeCommentValue(comment.getText());
}
return null;
}
use of com.intellij.psi.PsiComment in project intellij-community by JetBrains.
the class PyTargetExpressionImpl method getTypeComment.
@Nullable
@Override
public PsiComment getTypeComment() {
PsiComment comment = null;
final PyAssignmentStatement assignment = PsiTreeUtil.getParentOfType(this, PyAssignmentStatement.class);
if (assignment != null) {
final PyExpression assignedValue = assignment.getAssignedValue();
if (assignedValue != null) {
comment = as(PyPsiUtils.getNextNonWhitespaceSiblingOnSameLine(assignedValue), PsiComment.class);
}
} else {
final PyStatementListContainer forOrWith = PsiTreeUtil.getParentOfType(this, PyForPart.class, PyWithStatement.class);
if (forOrWith != null) {
comment = PyUtil.getCommentOnHeaderLine(forOrWith);
}
}
return comment != null && PyTypingTypeProvider.getTypeCommentValue(comment.getText()) != null ? comment : null;
}
use of com.intellij.psi.PsiComment in project intellij-community by JetBrains.
the class PyExtractMethodHandler method getStatementsRange.
@Nullable
private static Couple<PsiElement> getStatementsRange(final PsiElement element1, final PsiElement element2) {
final PsiElement parent = PsiTreeUtil.findCommonParent(element1, element2);
if (parent == null) {
return null;
}
final PyElement statementList = PyPsiUtils.getStatementList(parent);
if (statementList == null) {
return null;
}
final PsiElement statement1 = PyPsiUtils.getParentRightBefore(element1, statementList);
final PsiElement statement2 = PyPsiUtils.getParentRightBefore(element2, statementList);
if (statement1 == null || statement2 == null) {
return null;
}
// return elements if they are really first and last elements of statements
if (element1 == PsiTreeUtil.getDeepestFirst(statement1) && element2 == PyPsiUtils.getPrevSignificantLeaf(PsiTreeUtil.getDeepestLast(statement2), !(element2 instanceof PsiComment))) {
return Couple.of(statement1, statement2);
}
return null;
}
use of com.intellij.psi.PsiComment in project intellij-community by JetBrains.
the class PyJoinIfIntention method doInvoke.
public void doInvoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
PyIfStatement expression = PsiTreeUtil.getParentOfType(file.findElementAt(editor.getCaretModel().getOffset()), PyIfStatement.class);
PyIfStatement ifStatement = getIfStatement(expression);
PyStatement firstStatement = getFirstStatement(ifStatement);
if (ifStatement == null)
return;
if (firstStatement != null && firstStatement instanceof PyIfStatement) {
PyExpression condition = ((PyIfStatement) firstStatement).getIfPart().getCondition();
PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
PyExpression ifCondition = ifStatement.getIfPart().getCondition();
if (ifCondition == null || condition == null)
return;
StringBuilder replacementText = new StringBuilder(ifCondition.getText() + " and ");
if (condition instanceof PyBinaryExpression && ((PyBinaryExpression) condition).getOperator() == PyTokenTypes.OR_KEYWORD) {
replacementText.append("(").append(condition.getText()).append(")");
} else
replacementText.append(condition.getText());
PyExpression newCondition = elementGenerator.createExpressionFromText(replacementText.toString());
ifCondition.replace(newCondition);
PyStatementList stList = ((PyIfStatement) firstStatement).getIfPart().getStatementList();
PyStatementList ifStatementList = ifStatement.getIfPart().getStatementList();
if (ifStatementList == null || stList == null)
return;
List<PsiComment> comments = PsiTreeUtil.getChildrenOfTypeAsList(ifStatement.getIfPart(), PsiComment.class);
comments.addAll(PsiTreeUtil.getChildrenOfTypeAsList(((PyIfStatement) firstStatement).getIfPart(), PsiComment.class));
comments.addAll(PsiTreeUtil.getChildrenOfTypeAsList(ifStatementList, PsiComment.class));
comments.addAll(PsiTreeUtil.getChildrenOfTypeAsList(stList, PsiComment.class));
for (PsiElement comm : comments) {
ifStatement.getIfPart().addBefore(comm, ifStatementList);
comm.delete();
}
ifStatementList.replace(stList);
}
}
use of com.intellij.psi.PsiComment in project intellij-community by JetBrains.
the class DocStringUtil method getAttributeDocComment.
@Nullable
public static String getAttributeDocComment(@NotNull PyTargetExpression attr) {
if (attr.getParent() instanceof PyAssignmentStatement) {
final PyAssignmentStatement assignment = (PyAssignmentStatement) attr.getParent();
final PsiElement prevSibling = PyPsiUtils.getPrevNonWhitespaceSibling(assignment);
if (prevSibling instanceof PsiComment && prevSibling.getText().startsWith("#:")) {
return prevSibling.getText().substring(2);
}
}
return null;
}
Aggregations