use of com.intellij.codeInsight.highlighting.BraceMatcher 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.codeInsight.highlighting.BraceMatcher in project intellij-plugins by JetBrains.
the class CfmlBraceMatcher method isLBraceToken.
public boolean isLBraceToken(HighlighterIterator iterator, CharSequence fileText, FileType fileType) {
final IElementType tokenType = iterator.getTokenType();
PairedBraceMatcher matcher = LanguageBraceMatching.INSTANCE.forLanguage(tokenType.getLanguage());
if (matcher != null) {
BracePair[] pairs = matcher.getPairs();
for (BracePair pair : pairs) {
if (pair.getLeftBraceType() == tokenType)
return true;
}
}
if (!tokenType.getLanguage().equals(CfmlLanguage.INSTANCE)) {
FileType tokenFileType = iterator.getTokenType().getLanguage().getAssociatedFileType();
if (tokenFileType != null && tokenFileType != CfmlFileType.INSTANCE) {
for (FileTypeExtensionPoint<BraceMatcher> ext : Extensions.getExtensions(BraceMatcher.EP_NAME)) {
if (ext.filetype != null && ext.filetype.equals(tokenFileType.getName())) {
return ext.getInstance().isLBraceToken(iterator, fileText, tokenFileType instanceof XmlFileType ? StdFileTypes.HTML : tokenFileType);
}
}
}
}
for (BracePair pair : PAIRS) {
if (pair.getLeftBraceType() == tokenType) {
return true;
}
}
return tokenType.equals(CfmlTokenTypes.OPENER) && (!CfmlUtil.isEndTagRequired(getTagName(fileText, iterator), null) || findEndTag(fileText, iterator));
}
use of com.intellij.codeInsight.highlighting.BraceMatcher in project intellij-community by JetBrains.
the class EnterAfterUnmatchedBraceHandler method getUnmatchedLBracesNumberBefore.
/**
* Calculates number of unmatched left braces before the given offset.
*
* @param editor target editor
* @param offset target offset
* @param fileType target file type
* @return number of unmatched braces before the given offset;
* negative value if it's not possible to perform the calculation or if there are no unmatched left braces before
* the given offset
*/
protected static int getUnmatchedLBracesNumberBefore(Editor editor, int offset, FileType fileType) {
if (offset == 0) {
return -1;
}
CharSequence chars = editor.getDocument().getCharsSequence();
if (chars.charAt(offset - 1) != '{') {
return -1;
}
EditorHighlighter highlighter = ((EditorEx) editor).getHighlighter();
HighlighterIterator iterator = highlighter.createIterator(offset - 1);
BraceMatcher braceMatcher = BraceMatchingUtil.getBraceMatcher(fileType, iterator);
if (!braceMatcher.isLBraceToken(iterator, chars, fileType) || !braceMatcher.isStructuralBrace(iterator, chars, fileType)) {
return -1;
}
Language language = iterator.getTokenType().getLanguage();
iterator = highlighter.createIterator(0);
int lBracesBeforeOffset = 0;
int lBracesAfterOffset = 0;
int rBracesBeforeOffset = 0;
int rBracesAfterOffset = 0;
for (; !iterator.atEnd(); iterator.advance()) {
IElementType tokenType = iterator.getTokenType();
if (!tokenType.getLanguage().equals(language) || !braceMatcher.isStructuralBrace(iterator, chars, fileType)) {
continue;
}
boolean beforeOffset = iterator.getStart() < offset;
if (braceMatcher.isLBraceToken(iterator, chars, fileType)) {
if (beforeOffset) {
lBracesBeforeOffset++;
} else {
lBracesAfterOffset++;
}
} else if (braceMatcher.isRBraceToken(iterator, chars, fileType)) {
if (beforeOffset) {
rBracesBeforeOffset++;
} else {
rBracesAfterOffset++;
}
}
}
return lBracesBeforeOffset - rBracesBeforeOffset - (rBracesAfterOffset - lBracesAfterOffset);
}
use of com.intellij.codeInsight.highlighting.BraceMatcher in project intellij-community by JetBrains.
the class CustomFileTypeFoldingBuilder method buildBraceMatcherBasedFolding.
public static void buildBraceMatcherBasedFolding(List<FoldingDescriptor> descriptors, PsiElement root, Document document, 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();
while (!iterator.atEnd() && iterator.getStart() < totalRange.getEndOffset()) {
final Trinity<Integer, Integer, IElementType> last;
if (braceMatcher.isLBraceToken(iterator, editorText, fileType) && braceMatcher.isStructuralBrace(iterator, editorText, fileType)) {
stack.addLast(Trinity.create(iterator.getStart(), iterator.getEnd(), iterator.getTokenType()));
} else if (braceMatcher.isRBraceToken(iterator, editorText, fileType) && braceMatcher.isStructuralBrace(iterator, editorText, fileType) && !stack.isEmpty() && braceMatcher.isPairBraces((last = stack.getLast()).third, iterator.getTokenType())) {
stack.removeLast();
TextRange range = new TextRange(last.first, iterator.getEnd());
if (StringUtil.countChars(document.getText(range), '\n') >= 3) {
descriptors.add(new FoldingDescriptor(root, range));
}
}
iterator.advance();
}
}
use of com.intellij.codeInsight.highlighting.BraceMatcher in project intellij-community by JetBrains.
the class TypedHandler method handleAfterLParen.
private static void handleAfterLParen(@NotNull Editor editor, @NotNull FileType fileType, char lparenChar) {
int offset = editor.getCaretModel().getOffset();
HighlighterIterator iterator = ((EditorEx) editor).getHighlighter().createIterator(offset);
boolean atEndOfDocument = offset == editor.getDocument().getTextLength();
if (!atEndOfDocument)
iterator.retreat();
if (iterator.atEnd())
return;
BraceMatcher braceMatcher = BraceMatchingUtil.getBraceMatcher(fileType, iterator);
if (iterator.atEnd())
return;
IElementType braceTokenType = iterator.getTokenType();
final CharSequence fileText = editor.getDocument().getCharsSequence();
if (!braceMatcher.isLBraceToken(iterator, fileText, fileType))
return;
if (!iterator.atEnd()) {
iterator.advance();
if (!iterator.atEnd() && !BraceMatchingUtil.isPairedBracesAllowedBeforeTypeInFileType(braceTokenType, iterator.getTokenType(), fileType)) {
return;
}
iterator.retreat();
}
int lparenOffset = BraceMatchingUtil.findLeftmostLParen(iterator, braceTokenType, fileText, fileType);
if (lparenOffset < 0)
lparenOffset = 0;
iterator = ((EditorEx) editor).getHighlighter().createIterator(lparenOffset);
boolean matched = BraceMatchingUtil.matchBrace(fileText, fileType, iterator, true, true);
if (!matched) {
String text;
if (lparenChar == '(') {
text = ")";
} else if (lparenChar == '[') {
text = "]";
} else if (lparenChar == '<') {
text = ">";
} else if (lparenChar == '{') {
text = "}";
} else {
throw new AssertionError("Unknown char " + lparenChar);
}
editor.getDocument().insertString(offset, text);
}
}
Aggregations