use of com.intellij.psi.PsiComment in project intellij-community by JetBrains.
the class AugmentedAssignmentQuickFix method applyFix.
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
PsiElement element = descriptor.getPsiElement();
if (element instanceof PyAssignmentStatement && element.isWritable()) {
final PyAssignmentStatement statement = (PyAssignmentStatement) element;
final PyExpression target = statement.getLeftHandSideExpression();
final PyBinaryExpression expression = (PyBinaryExpression) statement.getAssignedValue();
if (expression == null)
return;
PyExpression leftExpression = expression.getLeftExpression();
PyExpression rightExpression = expression.getRightExpression();
if (rightExpression instanceof PyParenthesizedExpression)
rightExpression = ((PyParenthesizedExpression) rightExpression).getContainedExpression();
if (target != null && rightExpression != null) {
final String targetText = target.getText();
final String rightText = rightExpression.getText();
if (rightText.equals(targetText)) {
final PyExpression tmp = rightExpression;
rightExpression = leftExpression;
leftExpression = tmp;
}
final List<PsiComment> comments = PsiTreeUtil.getChildrenOfTypeAsList(statement, PsiComment.class);
if ((leftExpression instanceof PyReferenceExpression || leftExpression instanceof PySubscriptionExpression)) {
if (leftExpression.getText().equals(targetText)) {
final PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
final StringBuilder stringBuilder = new StringBuilder();
final PsiElement psiOperator = expression.getPsiOperator();
if (psiOperator == null)
return;
stringBuilder.append(targetText).append(" ").append(psiOperator.getText()).append("= ").append(rightExpression.getText());
final PyAugAssignmentStatementImpl augAssignment = elementGenerator.createFromText(LanguageLevel.forElement(element), PyAugAssignmentStatementImpl.class, stringBuilder.toString());
for (PsiComment comment : comments) augAssignment.add(comment);
statement.replace(augAssignment);
}
}
}
}
}
use of com.intellij.psi.PsiComment in project intellij-community by JetBrains.
the class AddEncodingQuickFix method applyFix.
public void applyFix(@NotNull final Project project, @NotNull final ProblemDescriptor descriptor) {
final PsiElement element = descriptor.getPsiElement();
final PsiFile file = element.getContainingFile();
if (file == null)
return;
PsiElement firstLine = file.getFirstChild();
if (firstLine instanceof PsiComment && firstLine.getText().startsWith("#!")) {
firstLine = firstLine.getNextSibling();
}
final LanguageLevel languageLevel = LanguageLevel.forElement(file);
final String commentText = String.format(PyEncodingUtil.ENCODING_FORMAT_PATTERN[myEncodingFormatIndex], myDefaultEncoding);
final PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
PsiComment encodingComment = elementGenerator.createFromText(languageLevel, PsiComment.class, commentText);
encodingComment = (PsiComment) file.addBefore(encodingComment, firstLine);
final FileEditor fileEditor = FileEditorManager.getInstance(project).getSelectedEditor(element.getContainingFile().getVirtualFile());
if (fileEditor instanceof TextEditor) {
if (encodingComment.getNextSibling() == null || !encodingComment.getNextSibling().textContains('\n')) {
file.addAfter(elementGenerator.createFromText(languageLevel, PsiWhiteSpace.class, "\n"), encodingComment);
}
final Editor editor = ((TextEditor) fileEditor).getEditor();
final Document document = editor.getDocument();
final int insertedLineNumber = document.getLineNumber(encodingComment.getTextOffset());
editor.getCaretModel().moveToLogicalPosition(new LogicalPosition(insertedLineNumber + 1, 0));
}
}
use of com.intellij.psi.PsiComment in project intellij-community by JetBrains.
the class PyFunctionImpl method getTypeCommentAnnotation.
@Nullable
@Override
public String getTypeCommentAnnotation() {
final PyFunctionStub 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 PyFunctionImpl method getTypeComment.
@Nullable
@Override
public PsiComment getTypeComment() {
final PsiComment inlineComment = PyUtil.getCommentOnHeaderLine(this);
if (inlineComment != null && PyTypingTypeProvider.getTypeCommentValue(inlineComment.getText()) != null) {
return inlineComment;
}
final PyStatementList statements = getStatementList();
if (statements.getStatements().length != 0) {
final PsiComment comment = as(statements.getFirstChild(), PsiComment.class);
if (comment != null && PyTypingTypeProvider.getTypeCommentValue(comment.getText()) != null) {
return comment;
}
}
return null;
}
use of com.intellij.psi.PsiComment in project kotlin by JetBrains.
the class KotlinDeclarationMover method getElementSourceLineRange.
@Override
protected LineRange getElementSourceLineRange(@NotNull PsiElement element, @NotNull Editor editor, @NotNull LineRange oldRange) {
PsiElement first;
PsiElement last;
if (element instanceof KtDeclaration) {
first = element.getFirstChild();
last = element.getLastChild();
if (first == null || last == null)
return null;
} else {
first = last = element;
}
TextRange textRange1 = first.getTextRange();
TextRange textRange2 = last.getTextRange();
Document doc = editor.getDocument();
if (doc.getTextLength() < textRange2.getEndOffset())
return null;
int startLine = editor.offsetToLogicalPosition(textRange1.getStartOffset()).line;
int endLine = editor.offsetToLogicalPosition(textRange2.getEndOffset()).line + 1;
if (element instanceof PsiComment || startLine == oldRange.startLine || startLine == oldRange.endLine || endLine == oldRange.startLine || endLine == oldRange.endLine) {
return new LineRange(startLine, endLine);
}
TextRange lineTextRange = new TextRange(doc.getLineStartOffset(oldRange.startLine), doc.getLineEndOffset(oldRange.endLine));
if (element instanceof KtDeclaration) {
for (PsiElement anchor : getDeclarationAnchors((KtDeclaration) element)) {
TextRange suspectTextRange = anchor.getTextRange();
if (suspectTextRange != null && lineTextRange.intersects(suspectTextRange))
return new LineRange(startLine, endLine);
}
}
return null;
}
Aggregations