use of com.intellij.openapi.editor.CaretModel in project intellij-community by JetBrains.
the class GroovyEnterHandler method preprocessEnter.
@Override
public Result preprocessEnter(@NotNull PsiFile file, @NotNull Editor editor, @NotNull Ref<Integer> caretOffset, @NotNull Ref<Integer> caretAdvance, @NotNull DataContext dataContext, EditorActionHandler originalHandler) {
Document document = editor.getDocument();
Project project = file.getProject();
CaretModel caretModel = editor.getCaretModel();
if (!(file instanceof GroovyFileBase)) {
return Result.Continue;
}
int docLength = document.getTextLength();
if (docLength == 0) {
return Result.Continue;
}
final int caret = caretModel.getOffset();
final EditorHighlighter highlighter = ((EditorEx) editor).getHighlighter();
if (caret >= 1 && caret < docLength && CodeInsightSettings.getInstance().SMART_INDENT_ON_ENTER) {
HighlighterIterator iterator = highlighter.createIterator(caret);
iterator.retreat();
while (!iterator.atEnd() && TokenType.WHITE_SPACE == iterator.getTokenType()) {
iterator.retreat();
}
boolean afterArrow = !iterator.atEnd() && iterator.getTokenType() == GroovyTokenTypes.mCLOSABLE_BLOCK_OP;
if (afterArrow) {
originalHandler.execute(editor, dataContext);
PsiDocumentManager.getInstance(project).commitDocument(document);
CodeStyleManager.getInstance(project).adjustLineIndent(file, caretModel.getOffset());
}
iterator = highlighter.createIterator(caretModel.getOffset());
while (!iterator.atEnd() && TokenType.WHITE_SPACE == iterator.getTokenType()) {
iterator.advance();
}
if (!iterator.atEnd() && GroovyTokenTypes.mRCURLY == iterator.getTokenType()) {
PsiDocumentManager.getInstance(project).commitDocument(document);
final PsiElement element = file.findElementAt(iterator.getStart());
if (element != null && element.getNode().getElementType() == GroovyTokenTypes.mRCURLY && element.getParent() instanceof GrClosableBlock && docLength > caret && afterArrow) {
return Result.DefaultForceIndent;
}
}
if (afterArrow) {
return Result.Stop;
}
if (editor.isInsertMode() && !HandlerUtils.isReadOnly(editor) && !editor.getSelectionModel().hasSelection() && handleFlyingGeese(editor, caret, dataContext, originalHandler, file)) {
return Result.DefaultForceIndent;
}
}
if (handleEnter(editor, dataContext, project, originalHandler))
return Result.Stop;
return Result.Continue;
}
use of com.intellij.openapi.editor.CaretModel in project intellij-community by JetBrains.
the class GroovyEnterHandler method handleInString.
private static boolean handleInString(Editor editor, int caretOffset, DataContext dataContext, EditorActionHandler originalHandler) {
Project project = CommonDataKeys.PROJECT.getData(dataContext);
if (project == null)
return false;
final VirtualFile vfile = FileDocumentManager.getInstance().getFile(editor.getDocument());
assert vfile != null;
PsiFile file = PsiManager.getInstance(project).findFile(vfile);
Document document = editor.getDocument();
String fileText = document.getText();
if (fileText.length() == caretOffset)
return false;
if (!checkStringApplicable(editor, caretOffset))
return false;
if (file == null)
return false;
PsiDocumentManager.getInstance(project).commitDocument(document);
final PsiElement stringElement = inferStringPair(file, caretOffset);
if (stringElement == null)
return false;
ASTNode node = stringElement.getNode();
final IElementType nodeElementType = node.getElementType();
boolean isInsertIndent = isInsertIndent(caretOffset, stringElement.getTextRange().getStartOffset(), fileText);
// For simple String literals like 'abc'
CaretModel caretModel = editor.getCaretModel();
if (nodeElementType == GroovyTokenTypes.mSTRING_LITERAL) {
if (isSingleQuoteString(stringElement)) {
//the case of print '\<caret>'
if (isSlashBeforeCaret(caretOffset, fileText)) {
EditorModificationUtil.insertStringAtCaret(editor, "\n");
} else if (stringElement.getParent() instanceof GrReferenceExpression) {
TextRange range = stringElement.getTextRange();
convertEndToMultiline(range.getEndOffset(), document, fileText, '\'');
document.insertString(range.getStartOffset(), "''");
caretModel.moveToOffset(caretOffset + 2);
EditorModificationUtil.insertStringAtCaret(editor, "\n");
} else {
EditorModificationUtil.insertStringAtCaret(editor, "'+");
originalHandler.execute(editor, dataContext);
EditorModificationUtil.insertStringAtCaret(editor, "'");
PsiDocumentManager.getInstance(project).commitDocument(document);
CodeStyleManager.getInstance(project).reformatRange(file, caretOffset, caretModel.getOffset());
}
} else {
insertLineFeedInString(editor, dataContext, originalHandler, isInsertIndent);
}
return true;
}
if (GSTRING_TOKENS.contains(nodeElementType) || nodeElementType == GroovyElementTypes.GSTRING_CONTENT && GSTRING_TOKENS.contains(node.getFirstChildNode().getElementType()) || nodeElementType == GroovyTokenTypes.mDOLLAR && node.getTreeParent().getTreeParent().getElementType() == GroovyElementTypes.GSTRING) {
PsiElement parent = stringElement.getParent();
if (nodeElementType == GroovyTokenTypes.mGSTRING_LITERAL) {
parent = stringElement;
} else {
while (parent != null && !(parent instanceof GrLiteral)) {
parent = parent.getParent();
}
}
if (parent == null)
return false;
if (isDoubleQuotedString(parent)) {
PsiElement exprSibling = stringElement.getNextSibling();
boolean rightFromDollar = exprSibling instanceof GrExpression && exprSibling.getTextRange().getStartOffset() == caretOffset;
if (rightFromDollar)
caretOffset--;
TextRange parentRange = parent.getTextRange();
if (rightFromDollar || parent.getParent() instanceof GrReferenceExpression) {
convertEndToMultiline(parent.getTextRange().getEndOffset(), document, fileText, '"');
document.insertString(parentRange.getStartOffset(), "\"\"");
caretModel.moveToOffset(caretOffset + 2);
EditorModificationUtil.insertStringAtCaret(editor, "\n");
if (rightFromDollar) {
caretModel.moveCaretRelatively(1, 0, false, false, true);
}
} else if (isSlashBeforeCaret(caretOffset, fileText)) {
EditorModificationUtil.insertStringAtCaret(editor, "\n");
} else {
EditorModificationUtil.insertStringAtCaret(editor, "\"+");
originalHandler.execute(editor, dataContext);
EditorModificationUtil.insertStringAtCaret(editor, "\"");
PsiDocumentManager.getInstance(project).commitDocument(document);
CodeStyleManager.getInstance(project).reformatRange(file, caretOffset, caretModel.getOffset());
}
} else {
insertLineFeedInString(editor, dataContext, originalHandler, isInsertIndent);
}
return true;
}
if (REGEX_TOKENS.contains(nodeElementType) || nodeElementType == GroovyElementTypes.GSTRING_CONTENT && REGEX_TOKENS.contains(node.getFirstChildNode().getElementType()) || nodeElementType == GroovyTokenTypes.mDOLLAR && node.getTreeParent().getTreeParent().getElementType() == GroovyElementTypes.REGEX) {
PsiElement parent = stringElement.getParent();
if (nodeElementType == GroovyTokenTypes.mREGEX_LITERAL || nodeElementType == GroovyTokenTypes.mDOLLAR_SLASH_REGEX_LITERAL) {
parent = stringElement;
} else {
while (parent != null && !(parent instanceof GrLiteral)) {
parent = parent.getParent();
}
}
if (parent == null || parent.getLastChild() instanceof PsiErrorElement)
return false;
PsiElement exprSibling = stringElement.getNextSibling();
boolean rightFromDollar = exprSibling instanceof GrExpression && exprSibling.getTextRange().getStartOffset() == caretOffset;
if (rightFromDollar) {
caretModel.moveToOffset(caretOffset - 1);
}
insertLineFeedInString(editor, dataContext, originalHandler, isInsertIndent);
if (rightFromDollar) {
caretModel.moveCaretRelatively(1, 0, false, false, true);
}
return true;
}
return false;
}
use of com.intellij.openapi.editor.CaretModel in project intellij-community by JetBrains.
the class GroovyDocMethodHandler method handleInsert.
@Override
public void handleInsert(InsertionContext context, int startOffset, LookupElement item) {
Editor editor = context.getEditor();
Object o = item.getObject();
assert o instanceof PsiMethod;
PsiMethod method = (PsiMethod) o;
StringBuffer buffer = new StringBuffer();
buffer.append("(");
PsiParameterList params = method.getParameterList();
int count = params.getParametersCount();
int i = 0;
for (PsiParameter parameter : params.getParameters()) {
PsiType type = parameter.getType();
String text = type.getCanonicalText();
if (type instanceof PsiEllipsisType) {
text = text.substring(0, text.length() - 3) + "[]";
}
buffer.append(PsiTypesUtil.unboxIfPossible(text));
if (i < count - 1) {
buffer.append(", ");
}
i++;
}
buffer.append(") ");
CaretModel caretModel = editor.getCaretModel();
int endOffset = shortenParamterReferences(context, startOffset, method, buffer);
caretModel.moveToOffset(endOffset);
}
use of com.intellij.openapi.editor.CaretModel in project intellij-community by JetBrains.
the class XPathInsertHandler method handleInsert.
public void handleInsert(InsertionContext context, LookupElement lookupItem) {
final Object object = lookupItem.getObject();
LOG.debug("object = " + object);
handleInsertImpl(context, lookupItem, context.getCompletionChar());
final Editor editor = context.getEditor();
final CharSequence charsSequence = editor.getDocument().getCharsSequence();
final CaretModel caretModel = editor.getCaretModel();
int offset = caretModel.getOffset();
if (object instanceof Lookup) {
final Lookup item = (Lookup) object;
if (item.isFunction()) {
if (charAt(charsSequence, offset) != '(') {
EditorModificationUtil.insertStringAtCaret(editor, "()");
if (item.hasParameters()) {
caretModel.moveCaretRelatively(-1, 0, false, false, true);
}
} else {
caretModel.moveCaretRelatively(1, 0, false, false, true);
}
} else if (item instanceof NamespaceLookup) {
if (charAt(charsSequence, offset) != ':') {
EditorModificationUtil.insertStringAtCaret(editor, ":");
return;
}
}
}
if (context.getCompletionChar() == '\t') {
if (charAt(charsSequence, offset) == ',') {
offset++;
caretModel.moveCaretRelatively(1, 0, false, false, true);
while (charAt(charsSequence, offset++) == ' ') {
caretModel.moveCaretRelatively(1, 0, false, false, true);
}
} else if (isIdentifier(charAt(charsSequence, offset)) && isIdentifier(charAt(charsSequence, offset - 1))) {
EditorModificationUtil.insertStringAtCaret(editor, " ");
} else if (charAt(charsSequence, offset) == ':') {
caretModel.moveCaretRelatively(1, 0, false, false, true);
}
}
}
use of com.intellij.openapi.editor.CaretModel in project intellij-community by JetBrains.
the class PyStatementMover method beforeMove.
@Override
public void beforeMove(@NotNull final Editor editor, @NotNull final MoveInfo info, final boolean down) {
final LineRange toMove = info.toMove;
final LineRange toMove2 = info.toMove2;
if (toMove instanceof MyLineRange && toMove2 instanceof ScopeRange) {
PostprocessReformattingAspect.getInstance(editor.getProject()).disablePostprocessFormattingInside(() -> {
final PsiElement startToMove = ((MyLineRange) toMove).myStartElement;
final PsiElement endToMove = ((MyLineRange) toMove).myEndElement;
final PsiFile file = startToMove.getContainingFile();
final SelectionModel selectionModel = editor.getSelectionModel();
final CaretModel caretModel = editor.getCaretModel();
final int selectionStart = selectionModel.getSelectionStart();
boolean isSelectionStartAtCaret = caretModel.getOffset() == selectionStart;
final SelectionContainer selectionLen = getSelectionLenContainer(editor, ((MyLineRange) toMove));
int shift = getCaretShift(startToMove, endToMove, caretModel, isSelectionStartAtCaret);
final boolean hasSelection = selectionModel.hasSelection();
int offset;
if (((ScopeRange) toMove2).isTheSameLevel()) {
offset = moveTheSameLevel((ScopeRange) toMove2, (MyLineRange) toMove);
} else {
offset = moveInOut(((MyLineRange) toMove), editor, info);
}
restoreCaretAndSelection(file, editor, isSelectionStartAtCaret, hasSelection, selectionLen, shift, offset, (MyLineRange) toMove);
//do not move further
info.toMove2 = info.toMove;
});
}
}
Aggregations