use of com.intellij.openapi.editor.highlighter.HighlighterIterator in project intellij-community by JetBrains.
the class EnterBetweenXmlTagsHandler method isBetweenXmlTags.
private static boolean isBetweenXmlTags(Project project, Editor editor, PsiFile file, int offset) {
if (offset == 0)
return false;
CharSequence chars = editor.getDocument().getCharsSequence();
if (chars.charAt(offset - 1) != '>')
return false;
EditorHighlighter highlighter = ((EditorEx) editor).getHighlighter();
HighlighterIterator iterator = highlighter.createIterator(offset - 1);
if (iterator.getTokenType() != XmlTokenType.XML_TAG_END)
return false;
if (isAtTheEndOfEmptyTag(project, editor, file, iterator)) {
return false;
}
iterator.retreat();
int retrieveCount = 1;
while (!iterator.atEnd()) {
final IElementType tokenType = iterator.getTokenType();
if (tokenType == XmlTokenType.XML_END_TAG_START)
return false;
if (tokenType == XmlTokenType.XML_START_TAG_START)
break;
++retrieveCount;
iterator.retreat();
}
for (int i = 0; i < retrieveCount; ++i) iterator.advance();
iterator.advance();
return !iterator.atEnd() && iterator.getTokenType() == XmlTokenType.XML_END_TAG_START;
}
use of com.intellij.openapi.editor.highlighter.HighlighterIterator in project intellij-plugins by JetBrains.
the class HbEnterHandler method isBetweenHbTags.
/**
* Checks to see if {@code Enter} has been typed while the caret is between an open and close tag pair.
*
* @return true if between open and close tags, false otherwise
*/
private static boolean isBetweenHbTags(Editor editor, PsiFile file, int offset) {
if (offset == 0)
return false;
CharSequence chars = editor.getDocument().getCharsSequence();
if (chars.charAt(offset - 1) != '}')
return false;
EditorHighlighter highlighter = ((EditorEx) editor).getHighlighter();
HighlighterIterator iterator = highlighter.createIterator(offset - 1);
PsiDocumentManager.getInstance(file.getProject()).commitDocument(editor.getDocument());
final PsiElement openerElement = file.findElementAt(iterator.getStart());
PsiElement openTag = HbPsiUtil.findParentOpenTagElement(openerElement);
if (openTag == null) {
return false;
}
iterator.advance();
if (iterator.atEnd()) {
// no more tokens, so certainly no close tag
return false;
}
final PsiElement closerElement = file.findElementAt(iterator.getStart());
PsiElement closeTag = HbPsiUtil.findParentCloseTagElement(closerElement);
// if we got this far, we're between open and close tags iff this is a close tag
return closeTag != null;
}
use of com.intellij.openapi.editor.highlighter.HighlighterIterator in project oxy-template-support-plugin by mutant-industries.
the class MatchingTagsAndJsBlock method buildBraceMatcherBasedFolding.
private static void buildBraceMatcherBasedFolding(@NotNull List<FoldingDescriptor> descriptors, @NotNull PsiElement root, @NotNull Document document, @NotNull SyntaxHighlighter highlighter) {
LexerEditorHighlighter editorHighlighter = new LexerEditorHighlighter(highlighter, EditorColorsManager.getInstance().getGlobalScheme());
editorHighlighter.setText(document.getText());
FileType fileType = root.getContainingFile().getFileType();
BraceMatcher braceMatcher = BraceMatchingUtil.getBraceMatcher(fileType, root.getLanguage());
TextRange totalRange = root.getTextRange();
final HighlighterIterator iterator = editorHighlighter.createIterator(totalRange.getStartOffset());
final LinkedList<Trinity<Integer, Integer, IElementType>> stack = new LinkedList<>();
String editorText = document.getText();
TextRange range;
while (!iterator.atEnd() && iterator.getStart() < totalRange.getEndOffset()) {
final Trinity<Integer, Integer, IElementType> last;
if (OxyTemplateParserDefinition.PARAMETER_QUOTES.contains(iterator.getTokenType())) {
iterator.advance();
continue;
}
if (braceMatcher.isLBraceToken(iterator, editorText, fileType)) {
if (iterator.getTokenType() == OxyTemplateTypes.T_XML_TAG_START) {
IElementType tokenType = iterator.getTokenType();
do {
iterator.advance();
} while (!iterator.atEnd() && iterator.getTokenType() != OxyTemplateTypes.T_XML_OPEN_TAG_END && iterator.getTokenType() != OxyTemplateTypes.T_XML_CLOSE_TAG_END && iterator.getTokenType() != OxyTemplateTypes.T_XML_EMPTY_TAG_END);
if (iterator.atEnd()) {
return;
}
stack.addLast(Trinity.create(iterator.getStart(), iterator.getEnd(), tokenType));
iterator.retreat();
} else {
stack.addLast(Trinity.create(iterator.getStart() + 2, iterator.getEnd(), iterator.getTokenType()));
}
} else if (braceMatcher.isRBraceToken(iterator, editorText, fileType) && !stack.isEmpty() && braceMatcher.isPairBraces((last = stack.getLast()).third, iterator.getTokenType())) {
stack.removeLast();
range = new TextRange(last.first, iterator.getEnd() - (iterator.getTokenType() == OxyTemplateTypes.T_XML_CLOSE_TAG_END ? 1 : 2));
if (last.third != OxyTemplateTypes.T_XML_EMPTY_TAG_END && StringUtil.countChars(document.getText(range), '\n') >= 2) {
descriptors.add(new FoldingDescriptor(root, range));
}
}
iterator.advance();
}
}
use of com.intellij.openapi.editor.highlighter.HighlighterIterator in project Perl5-IDEA by Camelcade.
the class PerlEnterInCommentHandler 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) {
if (!file.getLanguage().is(PerlLanguage.INSTANCE)) {
return Result.Continue;
}
int currentOffset = caretOffset.get();
assert editor instanceof EditorEx;
HighlighterIterator highlighterIterator = ((EditorEx) editor).getHighlighter().createIterator(currentOffset);
if (highlighterIterator.atEnd()) {
return Result.Continue;
}
IElementType currentTokenType = highlighterIterator.getTokenType();
int currentTokenStart = highlighterIterator.getStart();
// noinspection ConstantConditions
if (currentTokenType == COMMENT_LINE && currentOffset > currentTokenStart) {
Document document = editor.getDocument();
int lineNumber = document.getLineNumber(currentOffset);
int lineEnd = document.getLineEndOffset(lineNumber);
if (lineEnd > currentOffset) {
document.insertString(currentOffset, "# ");
}
}
return Result.Continue;
}
use of com.intellij.openapi.editor.highlighter.HighlighterIterator in project Perl5-IDEA by Camelcade.
the class PerlTypedHandler method charTyped.
@NotNull
@Override
public Result charTyped(char typedChar, @NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) {
final int offset = editor.getCaretModel().getOffset() - 1;
if (offset < 0) {
return Result.CONTINUE;
}
EditorHighlighter highlighter = ((EditorEx) editor).getHighlighter();
HighlighterIterator iterator = highlighter.createIterator(offset);
IElementType elementTokenType = iterator.getTokenType();
Document document = editor.getDocument();
if (QUOTE_OPEN_ANY.contains(elementTokenType) && CodeInsightSettings.getInstance().AUTOINSERT_PAIR_QUOTE) {
IElementType quotePrefixType = offset > 0 ? PerlEditorUtil.getPreviousTokenType(highlighter.createIterator(offset - 1)) : null;
CharSequence text = document.getCharsSequence();
if (offset > text.length() - 1 || text.charAt(offset) != typedChar) {
return Result.CONTINUE;
}
if (elementTokenType == QUOTE_DOUBLE_OPEN || elementTokenType == QUOTE_SINGLE_OPEN) {
AutoPopupController.getInstance(project).scheduleAutoPopup(editor);
}
char openChar = text.charAt(offset);
char closeChar = PerlBaseLexer.getQuoteCloseChar(openChar);
iterator.advance();
IElementType possibleCloseQuoteType = iterator.atEnd() ? null : iterator.getTokenType();
if (QUOTE_CLOSE_FIRST_ANY.contains(possibleCloseQuoteType) && closeChar == text.charAt(iterator.getStart())) {
if (DOUBLE_QUOTE_OPENERS.contains(quotePrefixType) && StringUtil.containsChar(HANDLED_BY_BRACE_MATCHER, openChar)) {
iterator.advance();
if (iterator.atEnd() || !QUOTE_OPEN_ANY.contains(iterator.getTokenType())) {
EditorModificationUtil.insertStringAtCaret(editor, Character.toString(closeChar) + openChar, false, false);
}
}
return Result.CONTINUE;
}
StringBuilder textToAppend = new StringBuilder();
textToAppend.append(closeChar);
if (DOUBLE_QUOTE_OPENERS.contains(quotePrefixType)) {
textToAppend.append(openChar);
if (openChar != closeChar) {
textToAppend.append(closeChar);
}
}
EditorModificationUtil.insertStringAtCaret(editor, textToAppend.toString(), false, false);
} else if (elementTokenType == LEFT_BRACE) {
AutoPopupController.getInstance(project).scheduleAutoPopup(editor, CompletionType.BASIC, psiFile -> {
PsiElement newElement = psiFile.findElementAt(offset);
return PsiUtilCore.getElementType(newElement) == elementTokenType && newElement.getParent() instanceof PsiPerlHashIndex;
});
}
return Result.CONTINUE;
}
Aggregations