use of com.intellij.openapi.editor.ex.EditorEx in project intellij-community by JetBrains.
the class GroovyEnterHandler method handleBetweenSquareBraces.
private static boolean handleBetweenSquareBraces(Editor editor, int caret, DataContext context, Project project, EditorActionHandler originalHandler) {
String text = editor.getDocument().getText();
if (text == null || text.isEmpty())
return false;
final EditorHighlighter highlighter = ((EditorEx) editor).getHighlighter();
if (caret < 1 || caret > text.length() - 1) {
return false;
}
HighlighterIterator iterator = highlighter.createIterator(caret - 1);
if (GroovyTokenTypes.mLBRACK == iterator.getTokenType()) {
if (text.length() > caret) {
iterator = highlighter.createIterator(caret);
if (GroovyTokenTypes.mRBRACK == iterator.getTokenType()) {
originalHandler.execute(editor, context);
originalHandler.execute(editor, context);
editor.getCaretModel().moveCaretRelatively(0, -1, false, false, true);
insertSpacesByGroovyContinuationIndent(editor, project);
return true;
}
}
}
return false;
}
use of com.intellij.openapi.editor.ex.EditorEx 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.ex.EditorEx in project intellij-community by JetBrains.
the class PyDataViewerPanel method createEditorField.
@NotNull
private EditorTextField createEditorField() {
return new EditorTextField(EditorFactory.getInstance().createDocument(""), myProject, PythonFileType.INSTANCE, false, true) {
@Override
protected EditorEx createEditor() {
EditorEx editor = super.createEditor();
editor.getContentComponent().addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
apply(mySliceTextField.getText());
}
}
});
return editor;
}
};
}
use of com.intellij.openapi.editor.ex.EditorEx in project kotlin by JetBrains.
the class KotlinTypedHandler method indentBrace.
/**
* Copied from
* @see com.intellij.codeInsight.editorActions.TypedHandler#indentBrace(Project, Editor, char)
*/
private static void indentBrace(@NotNull final Project project, @NotNull final Editor editor, char braceChar) {
final int offset = editor.getCaretModel().getOffset() - 1;
Document document = editor.getDocument();
CharSequence chars = document.getCharsSequence();
if (offset < 0 || chars.charAt(offset) != braceChar)
return;
int spaceStart = CharArrayUtil.shiftBackward(chars, offset - 1, " \t");
if (spaceStart < 0 || chars.charAt(spaceStart) == '\n' || chars.charAt(spaceStart) == '\r') {
PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
documentManager.commitDocument(document);
final PsiFile file = documentManager.getPsiFile(document);
if (file == null || !file.isWritable())
return;
PsiElement element = file.findElementAt(offset);
if (element == null)
return;
EditorHighlighter highlighter = ((EditorEx) editor).getHighlighter();
HighlighterIterator iterator = highlighter.createIterator(offset);
FileType fileType = file.getFileType();
BraceMatcher braceMatcher = BraceMatchingUtil.getBraceMatcher(fileType, iterator);
boolean isBrace = braceMatcher.isLBraceToken(iterator, chars, fileType) || braceMatcher.isRBraceToken(iterator, chars, fileType);
if (element.getNode() != null && isBrace) {
ApplicationManager.getApplication().runWriteAction(new Runnable() {
@Override
public void run() {
int newOffset = CodeStyleManager.getInstance(project).adjustLineIndent(file, offset);
editor.getCaretModel().moveToOffset(newOffset + 1);
editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
editor.getSelectionModel().removeSelection();
}
});
}
}
}
use of com.intellij.openapi.editor.ex.EditorEx in project intellij-community by JetBrains.
the class IpnbEditorUtil method createPlainCodeEditor.
public static Editor createPlainCodeEditor(@NotNull final Project project, @NotNull final String text) {
final EditorFactory editorFactory = EditorFactory.getInstance();
assert editorFactory != null;
final Document document = editorFactory.createDocument(text);
EditorEx editor = (EditorEx) editorFactory.createEditor(document, project);
setupEditor(editor);
return editor;
}
Aggregations