use of com.intellij.openapi.editor.highlighter.HighlighterIterator in project intellij-community by JetBrains.
the class RParenthTailType method calcParensBalance.
private static int calcParensBalance(Document document, EditorHighlighter highlighter, int rangeStart, int rangeEnd) {
LOG.assertTrue(0 <= rangeStart);
LOG.assertTrue(rangeStart <= rangeEnd);
LOG.assertTrue(rangeEnd <= document.getTextLength());
HighlighterIterator iterator = highlighter.createIterator(rangeStart);
int balance = 0;
while (!iterator.atEnd() && iterator.getStart() < rangeEnd) {
IElementType tokenType = iterator.getTokenType();
if (tokenType == JavaTokenType.LPARENTH) {
balance++;
} else if (tokenType == JavaTokenType.RPARENTH) {
balance--;
}
iterator.advance();
}
return balance;
}
use of com.intellij.openapi.editor.highlighter.HighlighterIterator in project intellij-community by JetBrains.
the class RParenthTailType method getExistingRParenthOffset.
private static int getExistingRParenthOffset(final Editor editor, final int tailOffset) {
final Document document = editor.getDocument();
if (tailOffset >= document.getTextLength())
return -1;
final CharSequence charsSequence = document.getCharsSequence();
EditorHighlighter highlighter = ((EditorEx) editor).getHighlighter();
int existingRParenthOffset = -1;
for (HighlighterIterator iterator = highlighter.createIterator(tailOffset); !iterator.atEnd(); iterator.advance()) {
final IElementType tokenType = iterator.getTokenType();
if ((!(tokenType instanceof IJavaElementType) || !ElementType.JAVA_COMMENT_OR_WHITESPACE_BIT_SET.contains(tokenType)) && tokenType != TokenType.WHITE_SPACE) {
final int start = iterator.getStart();
if (iterator.getEnd() == start + 1 && ')' == charsSequence.charAt(start)) {
existingRParenthOffset = start;
}
break;
}
}
if (existingRParenthOffset >= 0) {
final PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(editor.getProject());
psiDocumentManager.commitDocument(document);
TextRange range = getRangeToCheckParensBalance(psiDocumentManager.getPsiFile(document), document, editor.getCaretModel().getOffset());
int balance = calcParensBalance(document, highlighter, range.getStartOffset(), range.getEndOffset());
if (balance > 0) {
return -1;
}
}
return existingRParenthOffset;
}
use of com.intellij.openapi.editor.highlighter.HighlighterIterator in project intellij-community by JetBrains.
the class JavaTypedHandler method isAfterClassLikeIdentifierOrDot.
public static boolean isAfterClassLikeIdentifierOrDot(final int offset, final Editor editor) {
HighlighterIterator iterator = ((EditorEx) editor).getHighlighter().createIterator(offset);
if (iterator.atEnd())
return false;
if (iterator.getStart() > 0)
iterator.retreat();
final IElementType tokenType = iterator.getTokenType();
if (tokenType == JavaTokenType.DOT)
return true;
return isClassLikeIdentifier(offset, editor, iterator, JavaTokenType.IDENTIFIER);
}
use of com.intellij.openapi.editor.highlighter.HighlighterIterator in project intellij-community by JetBrains.
the class JavaTypedHandler method handleAfterJavaLT.
//need custom handler, since brace matcher cannot be used
public static void handleAfterJavaLT(final Editor editor, final IElementType lt, final IElementType gt, final TokenSet invalidInsideReference) {
if (!CodeInsightSettings.getInstance().AUTOINSERT_PAIR_BRACKET)
return;
int offset = editor.getCaretModel().getOffset();
HighlighterIterator iterator = ((EditorEx) editor).getHighlighter().createIterator(offset);
while (iterator.getStart() > 0 && !invalidInsideReference.contains(iterator.getTokenType())) {
iterator.retreat();
}
if (invalidInsideReference.contains(iterator.getTokenType()))
iterator.advance();
int balance = 0;
while (!iterator.atEnd() && balance >= 0) {
final IElementType tokenType = iterator.getTokenType();
if (tokenType == lt) {
balance++;
} else if (tokenType == gt) {
balance--;
} else if (invalidInsideReference.contains(tokenType)) {
break;
}
iterator.advance();
}
if (balance == 1) {
editor.getDocument().insertString(offset, ">");
}
}
use of com.intellij.openapi.editor.highlighter.HighlighterIterator in project intellij-community by JetBrains.
the class JavaTypedHandler method handleJavaGT.
//need custom handler, since brace matcher cannot be used
public static boolean handleJavaGT(final Editor editor, final IElementType lt, final IElementType gt, final TokenSet invalidInsideReference) {
if (!CodeInsightSettings.getInstance().AUTOINSERT_PAIR_BRACKET)
return false;
int offset = editor.getCaretModel().getOffset();
if (offset == editor.getDocument().getTextLength())
return false;
HighlighterIterator iterator = ((EditorEx) editor).getHighlighter().createIterator(offset);
if (iterator.getTokenType() != gt)
return false;
while (!iterator.atEnd() && !invalidInsideReference.contains(iterator.getTokenType())) {
iterator.advance();
}
if (!iterator.atEnd() && invalidInsideReference.contains(iterator.getTokenType()))
iterator.retreat();
int balance = 0;
while (!iterator.atEnd() && balance >= 0) {
final IElementType tokenType = iterator.getTokenType();
if (tokenType == lt) {
balance--;
} else if (tokenType == gt) {
balance++;
} else if (invalidInsideReference.contains(tokenType)) {
break;
}
iterator.retreat();
}
if (balance == 0) {
EditorModificationUtil.moveCaretRelatively(editor, 1);
return true;
}
return false;
}
Aggregations