use of com.intellij.openapi.editor.Document in project intellij-community by JetBrains.
the class PyConditionalStatementPartFixer method doApply.
@Override
public void doApply(@NotNull Editor editor, @NotNull PySmartEnterProcessor processor, @NotNull PyConditionalStatementPart statementPart) throws IncorrectOperationException {
final PyExpression condition = statementPart.getCondition();
final Document document = editor.getDocument();
final PsiElement colon = PyPsiUtils.getFirstChildOfType(statementPart, PyTokenTypes.COLON);
if (colon == null) {
if (condition != null) {
final PsiElement firstNonComment = PyPsiUtils.getNextNonCommentSibling(condition.getNextSibling(), false);
if (firstNonComment != null && !":".equals(firstNonComment.getNode().getText())) {
document.insertString(firstNonComment.getTextRange().getEndOffset(), ":");
}
} else {
final TokenSet keywords = TokenSet.create(PyTokenTypes.IF_KEYWORD, PyTokenTypes.ELIF_KEYWORD, PyTokenTypes.WHILE_KEYWORD);
final PsiElement keywordToken = PyPsiUtils.getChildByFilter(statementPart, keywords, 0);
final int offset = sure(keywordToken).getTextRange().getEndOffset();
document.insertString(offset, " :");
processor.registerUnresolvedError(offset + 1);
}
} else if (condition == null) {
processor.registerUnresolvedError(colon.getTextRange().getStartOffset());
}
}
use of com.intellij.openapi.editor.Document in project intellij-community by JetBrains.
the class PyForPartFixer method doApply.
@Override
public void doApply(@NotNull Editor editor, @NotNull PySmartEnterProcessor processor, @NotNull PyForPart forPart) {
final PsiElement colon = PyPsiUtils.getFirstChildOfType(forPart, PyTokenTypes.COLON);
final Document document = editor.getDocument();
final PsiElement forToken = PyPsiUtils.getFirstChildOfType(forPart, PyTokenTypes.FOR_KEYWORD);
if (colon == null) {
String textToInsert = ":";
PsiElement sourceOrTarget = forPart.getSource();
PsiElement positionToInsert = sourceOrTarget;
if (sourceOrTarget == null) {
sourceOrTarget = forPart.getTarget();
final PsiElement inToken = PyPsiUtils.getFirstChildOfType(forPart, PyTokenTypes.IN_KEYWORD);
if (inToken == null) {
if (sourceOrTarget == null) {
positionToInsert = sure(forToken);
textToInsert = " in :";
processor.registerUnresolvedError(positionToInsert.getTextRange().getEndOffset() + 1);
} else {
positionToInsert = sourceOrTarget;
textToInsert = " in :";
processor.registerUnresolvedError(positionToInsert.getTextRange().getEndOffset() + 4);
}
} else {
positionToInsert = inToken;
textToInsert = " :";
processor.registerUnresolvedError(positionToInsert.getTextRange().getEndOffset() + 1);
}
}
document.insertString(positionToInsert.getTextRange().getEndOffset(), textToInsert);
}
}
use of com.intellij.openapi.editor.Document in project intellij-community by JetBrains.
the class PyParameterListFixer method doApply.
@Override
public void doApply(@NotNull Editor editor, @NotNull PySmartEnterProcessor processor, @NotNull PyParameterList parameters) throws IncorrectOperationException {
final PsiElement lBrace = PyPsiUtils.getChildByFilter(parameters, PyTokenTypes.OPEN_BRACES, 0);
final PsiElement rBrace = PyPsiUtils.getChildByFilter(parameters, PyTokenTypes.CLOSE_BRACES, 0);
final PyFunction pyFunction = as(parameters.getParent(), PyFunction.class);
if (pyFunction != null && !PyFunctionFixer.isFakeFunction(pyFunction) && (lBrace == null || rBrace == null)) {
final Document document = editor.getDocument();
if (lBrace == null) {
final String textToInsert = pyFunction.getNameNode() == null ? " (" : "(";
document.insertString(parameters.getTextOffset(), textToInsert);
} else if (parameters.getParameters().length == 0) {
document.insertString(lBrace.getTextRange().getEndOffset(), ")");
} else {
document.insertString(parameters.getTextRange().getEndOffset(), ")");
}
}
}
use of com.intellij.openapi.editor.Document in project intellij-community by JetBrains.
the class OverwriteEqualsInsertHandler method handleInsert.
@Override
public void handleInsert(InsertionContext context, LookupElement item) {
if (context.getCompletionChar() != Lookup.REPLACE_SELECT_CHAR) {
return;
}
Document doc = context.getDocument();
int tailOffset = context.getTailOffset();
if (tailOffset < doc.getCharsSequence().length() && doc.getCharsSequence().charAt(tailOffset) == '=') {
doc.deleteString(tailOffset, tailOffset + 1);
}
}
use of com.intellij.openapi.editor.Document in project intellij-community by JetBrains.
the class PyClassInsertHandler method handleInsert.
public void handleInsert(InsertionContext context, LookupElement item) {
final Editor editor = context.getEditor();
final Document document = editor.getDocument();
if (context.getCompletionChar() == '(') {
context.setAddCompletionChar(false);
final int offset = context.getTailOffset();
document.insertString(offset, "()");
PyClass pyClass = PyUtil.as(item.getPsiElement(), PyClass.class);
PyFunction init = pyClass != null ? pyClass.findInitOrNew(true, null) : null;
if (init != null && PyFunctionInsertHandler.hasParams(context, init)) {
editor.getCaretModel().moveToOffset(offset + 1);
AutoPopupController.getInstance(context.getProject()).autoPopupParameterInfo(context.getEditor(), init);
} else {
editor.getCaretModel().moveToOffset(offset + 2);
}
}
}
Aggregations