use of com.intellij.openapi.editor.highlighter.HighlighterIterator in project intellij-community by JetBrains.
the class JavaTypedHandler method beforeCharTyped.
@Override
public Result beforeCharTyped(final char c, final Project project, final Editor editor, final PsiFile file, final FileType fileType) {
if (!(file instanceof PsiJavaFile))
return Result.CONTINUE;
if (c == '@') {
autoPopupJavadocLookup(project, editor);
} else if (c == '#' || c == '.') {
autoPopupMemberLookup(project, editor);
}
int offsetBefore = editor.getCaretModel().getOffset();
//important to calculate before inserting charTyped
myJavaLTTyped = '<' == c && !(file instanceof JspFile) && CodeInsightSettings.getInstance().AUTOINSERT_PAIR_BRACKET && PsiUtil.isLanguageLevel5OrHigher(file) && isAfterClassLikeIdentifierOrDot(offsetBefore, editor);
if ('>' == c) {
if (!(file instanceof JspFile) && CodeInsightSettings.getInstance().AUTOINSERT_PAIR_BRACKET && PsiUtil.isLanguageLevel5OrHigher(file)) {
if (handleJavaGT(editor, JavaTokenType.LT, JavaTokenType.GT, INVALID_INSIDE_REFERENCE))
return Result.STOP;
}
}
if (c == ';') {
if (handleSemicolon(editor, fileType))
return Result.STOP;
}
if (fileType == StdFileTypes.JAVA && c == '{') {
int offset = editor.getCaretModel().getOffset();
if (offset == 0) {
return Result.CONTINUE;
}
HighlighterIterator iterator = ((EditorEx) editor).getHighlighter().createIterator(offset - 1);
while (!iterator.atEnd() && iterator.getTokenType() == TokenType.WHITE_SPACE) {
iterator.retreat();
}
if (iterator.atEnd() || iterator.getTokenType() == JavaTokenType.RBRACKET || iterator.getTokenType() == JavaTokenType.EQ) {
return Result.CONTINUE;
}
Document doc = editor.getDocument();
PsiDocumentManager.getInstance(project).commitDocument(doc);
final PsiElement leaf = file.findElementAt(offset);
if (PsiTreeUtil.getParentOfType(leaf, PsiArrayInitializerExpression.class, false, PsiCodeBlock.class, PsiMember.class) != null) {
return Result.CONTINUE;
}
PsiElement st = leaf != null ? leaf.getParent() : null;
PsiElement prev = offset > 1 ? file.findElementAt(offset - 1) : null;
if (CodeInsightSettings.getInstance().AUTOINSERT_PAIR_BRACKET && isRparenth(leaf) && (st instanceof PsiWhileStatement || st instanceof PsiIfStatement) && shouldInsertStatementBody(st, doc, prev)) {
CommandProcessor.getInstance().executeCommand(project, () -> new JavaSmartEnterProcessor().process(project, editor, file), "Insert block statement", null);
return Result.STOP;
}
PsiElement prevLeaf = leaf == null ? null : PsiTreeUtil.prevVisibleLeaf(leaf);
if (PsiUtil.isJavaToken(prevLeaf, JavaTokenType.ARROW) || PsiTreeUtil.getParentOfType(prevLeaf, PsiNewExpression.class, true, PsiCodeBlock.class, PsiMember.class) != null) {
return Result.CONTINUE;
}
if (PsiTreeUtil.getParentOfType(leaf, PsiCodeBlock.class, false, PsiMember.class) != null) {
EditorModificationUtil.insertStringAtCaret(editor, "{");
TypedHandler.indentOpenedBrace(project, editor);
// use case: manually wrapping part of method's code in 'if', 'while', etc
return Result.STOP;
}
}
return Result.CONTINUE;
}
use of com.intellij.openapi.editor.highlighter.HighlighterIterator in project intellij-community by JetBrains.
the class BraceHighlightingHandler method updateBraces.
void updateBraces() {
ApplicationManager.getApplication().assertIsDispatchThread();
if (myPsiFile == null || !myPsiFile.isValid())
return;
clearBraceHighlighters();
if (!myCodeInsightSettings.HIGHLIGHT_BRACES)
return;
if (myEditor.getSelectionModel().hasSelection())
return;
if (myEditor.getSoftWrapModel().isInsideOrBeforeSoftWrap(myEditor.getCaretModel().getVisualPosition()))
return;
int offset = myEditor.getCaretModel().getOffset();
final CharSequence chars = myEditor.getDocument().getCharsSequence();
//if (myEditor.offsetToLogicalPosition(offset).column != myEditor.getCaretModel().getLogicalPosition().column) {
// // we are in virtual space
// final int caretLineNumber = myEditor.getCaretModel().getLogicalPosition().line;
// if (caretLineNumber >= myDocument.getLineCount()) return;
// offset = myDocument.getLineEndOffset(caretLineNumber) + myDocument.getLineSeparatorLength(caretLineNumber);
//}
final int originalOffset = offset;
EditorHighlighter highlighter = getEditorHighlighter();
HighlighterIterator iterator = highlighter.createIterator(offset);
FileType fileType = PsiUtilBase.getPsiFileAtOffset(myPsiFile, offset).getFileType();
if (iterator.atEnd()) {
offset--;
} else if (BraceMatchingUtil.isRBraceToken(iterator, chars, fileType)) {
offset--;
} else if (!BraceMatchingUtil.isLBraceToken(iterator, chars, fileType)) {
offset--;
if (offset >= 0) {
HighlighterIterator it = highlighter.createIterator(offset);
if (!BraceMatchingUtil.isRBraceToken(it, chars, getFileTypeByIterator(it)))
offset++;
}
}
if (offset < 0) {
removeLineMarkers();
return;
}
iterator = highlighter.createIterator(offset);
fileType = getFileTypeByIterator(iterator);
myAlarm.cancelAllRequests();
if (BraceMatchingUtil.isLBraceToken(iterator, chars, fileType) || BraceMatchingUtil.isRBraceToken(iterator, chars, fileType)) {
doHighlight(offset, originalOffset, fileType);
} else if (offset > 0 && offset < chars.length()) {
// There is a possible case that there are paired braces nearby the caret position and the document contains only white
// space symbols between them. We want to highlight such braces as well.
// Example:
// public void test() { <caret>
// }
char c = chars.charAt(offset);
boolean searchForward = c != '\n';
// Try to find matched brace backwards.
if (offset >= originalOffset && (c == ' ' || c == '\t' || c == '\n')) {
int backwardNonWsOffset = CharArrayUtil.shiftBackward(chars, offset - 1, "\t ");
if (backwardNonWsOffset >= 0) {
iterator = highlighter.createIterator(backwardNonWsOffset);
FileType newFileType = getFileTypeByIterator(iterator);
if (BraceMatchingUtil.isLBraceToken(iterator, chars, newFileType) || BraceMatchingUtil.isRBraceToken(iterator, chars, newFileType)) {
offset = backwardNonWsOffset;
searchForward = false;
doHighlight(backwardNonWsOffset, originalOffset, newFileType);
}
}
}
// Try to find matched brace forward.
if (searchForward) {
int forwardOffset = CharArrayUtil.shiftForward(chars, offset, "\t ");
if (forwardOffset > offset || c == ' ' || c == '\t') {
iterator = highlighter.createIterator(forwardOffset);
FileType newFileType = getFileTypeByIterator(iterator);
if (BraceMatchingUtil.isLBraceToken(iterator, chars, newFileType) || BraceMatchingUtil.isRBraceToken(iterator, chars, newFileType)) {
offset = forwardOffset;
doHighlight(forwardOffset, originalOffset, newFileType);
}
}
}
}
//highlight scope
if (!myCodeInsightSettings.HIGHLIGHT_SCOPE) {
removeLineMarkers();
return;
}
final int _offset = offset;
final FileType _fileType = fileType;
myAlarm.addRequest(() -> {
if (!myProject.isDisposed() && !myEditor.isDisposed()) {
highlightScope(_offset, _fileType);
}
}, 300);
}
use of com.intellij.openapi.editor.highlighter.HighlighterIterator in project kotlin by JetBrains.
the class KotlinTypedHandler method autoPopupParameterInfo.
private static void autoPopupParameterInfo(Project project, Editor editor) {
int offset = editor.getCaretModel().getOffset();
if (offset == 0)
return;
HighlighterIterator iterator = ((EditorEx) editor).getHighlighter().createIterator(offset - 1);
IElementType tokenType = iterator.getTokenType();
if (KtTokens.COMMENTS.contains(tokenType) || tokenType == KtTokens.REGULAR_STRING_PART || tokenType == KtTokens.OPEN_QUOTE || tokenType == KtTokens.CHARACTER_LITERAL)
return;
AutoPopupController.getInstance(project).autoPopupParameterInfo(editor, null);
}
use of com.intellij.openapi.editor.highlighter.HighlighterIterator in project kotlin by JetBrains.
the class KotlinTypedHandler method beforeCharTyped.
@Override
public Result beforeCharTyped(char c, Project project, Editor editor, PsiFile file, FileType fileType) {
if (!(file instanceof KtFile)) {
return Result.CONTINUE;
}
switch(c) {
case '<':
kotlinLTTyped = CodeInsightSettings.getInstance().AUTOINSERT_PAIR_BRACKET && LtGtTypingUtils.shouldAutoCloseAngleBracket(editor.getCaretModel().getOffset(), editor);
autoPopupParameterInfo(project, editor);
return Result.CONTINUE;
case '>':
if (CodeInsightSettings.getInstance().AUTOINSERT_PAIR_BRACKET) {
if (LtGtTypingUtils.handleKotlinGTInsert(editor)) {
return Result.STOP;
}
}
return Result.CONTINUE;
case '{':
// Returning Result.CONTINUE will cause inserting "{}" for unmatched '{'
int offset = editor.getCaretModel().getOffset();
if (offset == 0) {
return Result.CONTINUE;
}
HighlighterIterator iterator = ((EditorEx) editor).getHighlighter().createIterator(offset - 1);
while (!iterator.atEnd() && iterator.getTokenType() == TokenType.WHITE_SPACE) {
iterator.retreat();
}
if (iterator.atEnd() || !(SUPPRESS_AUTO_INSERT_CLOSE_BRACE_AFTER.contains(iterator.getTokenType()))) {
return Result.CONTINUE;
}
int tokenBeforeBraceOffset = iterator.getStart();
PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());
PsiElement leaf = file.findElementAt(offset);
if (leaf != null) {
PsiElement parent = leaf.getParent();
if (parent != null && CONTROL_FLOW_EXPRESSIONS.contains(parent.getNode().getElementType())) {
ASTNode nonWhitespaceSibling = FormatterUtil.getPreviousNonWhitespaceSibling(leaf.getNode());
if (nonWhitespaceSibling != null && nonWhitespaceSibling.getStartOffset() == tokenBeforeBraceOffset) {
EditorModificationUtil.insertStringAtCaret(editor, "{", false, true);
indentBrace(project, editor, '{');
return Result.STOP;
}
}
}
return Result.CONTINUE;
case '.':
autoPopupMemberLookup(project, editor);
return Result.CONTINUE;
case '@':
autoPopupLabelLookup(project, editor);
return Result.CONTINUE;
case ':':
autoPopupCallableReferenceLookup(project, editor);
return Result.CONTINUE;
case '[':
autoPopupParameterInfo(project, editor);
return Result.CONTINUE;
}
return Result.CONTINUE;
}
use of com.intellij.openapi.editor.highlighter.HighlighterIterator in project intellij-community by JetBrains.
the class PyTripleQuoteBackspaceDelegate method beforeCharDeleted.
@Override
public void beforeCharDeleted(char c, PsiFile file, Editor editor) {
isTripleQuote = false;
if (c == '"' || c == '\'' && CodeInsightSettings.getInstance().AUTOINSERT_PAIR_QUOTE) {
final QuoteHandler quoteHandler = TypedHandler.getQuoteHandler(file, editor);
if (quoteHandler == null || !(quoteHandler instanceof BaseQuoteHandler))
return;
final int offset = editor.getCaretModel().getCurrentCaret().getOffset();
String text = editor.getDocument().getText();
boolean mayBeTripleQuote = offset >= 3 && offset + 2 < text.length();
if (mayBeTripleQuote) {
HighlighterIterator iterator = ((EditorEx) editor).getHighlighter().createIterator(offset);
boolean hasTripleQuoteAfter = offset + 2 < text.length() && text.charAt(offset) == c && text.charAt(offset + 1) == c && text.charAt(offset + 2) == c;
isTripleQuote = quoteHandler.isOpeningQuote(iterator, offset - 1) && hasTripleQuoteAfter;
}
}
}
Aggregations