use of com.intellij.openapi.editor.highlighter.HighlighterIterator in project intellij-plugins by JetBrains.
the class CfmlAttributeNamesCompletionProvider method addCompletions.
public void addCompletions(@NotNull final CompletionParameters parameters, final ProcessingContext context, @NotNull final CompletionResultSet result) {
PsiElement element = parameters.getPosition();
String tagName = "";
while (element != null && !(element instanceof CfmlTag) && !(element instanceof CfmlComponent) && !(element instanceof CfmlPropertyImpl)) {
PsiElement prevNode = element.getPrevSibling();
PsiElement superPrevNode = prevNode != null ? prevNode.getPrevSibling() : null;
if (superPrevNode != null && superPrevNode.getText().equalsIgnoreCase("property")) {
tagName = "cfproperty";
break;
}
element = element.getParent();
}
if (element == null) {
return;
}
if (tagName.isEmpty()) {
tagName = element instanceof CfmlTag ? ((CfmlTag) element).getTagName() : element instanceof CfmlPropertyImpl ? "cfproperty" : "cfcomponent";
}
Set<String> excluded = new HashSet<>();
final CfmlAttributeImpl[] attributes = PsiTreeUtil.getChildrenOfType(element, CfmlAttributeImpl.class);
if (attributes != null) {
for (CfmlAttributeImpl attribute : attributes) {
excluded.add(attribute.getAttributeName());
}
}
for (CfmlAttributeDescription s : CfmlUtil.getAttributes(tagName, element.getProject())) {
if (s.getName() == null) {
continue;
}
if (excluded.contains(s.getName())) {
continue;
}
result.addElement(TailTypeDecorator.withTail(LookupElementBuilder.create(s.getName()).withCaseSensitivity(false), new TailType() {
public int processTail(Editor editor, int tailOffset) {
HighlighterIterator iterator = ((EditorEx) editor).getHighlighter().createIterator(tailOffset);
if (!iterator.atEnd() && iterator.getTokenType() == CfmlTokenTypes.WHITE_SPACE)
iterator.advance();
if (!iterator.atEnd() && iterator.getTokenType() == CfmlTokenTypes.ASSIGN) {
iterator.advance();
} else {
editor.getDocument().insertString(tailOffset, "=\"\"");
return moveCaret(editor, tailOffset, 2);
}
int offset = iterator.getStart();
if (!iterator.atEnd() && iterator.getTokenType() == CfmlTokenTypes.WHITE_SPACE)
iterator.advance();
if (!iterator.atEnd() && CfmlTokenTypes.STRING_ELEMENTS.contains(iterator.getTokenType())) {
return tailOffset;
}
editor.getDocument().insertString(offset, "\"\"");
return moveCaret(editor, tailOffset, offset - tailOffset + 1);
}
}));
}
}
use of com.intellij.openapi.editor.highlighter.HighlighterIterator in project intellij-plugins by JetBrains.
the class CfmlTypedHandler method beforeCharTyped.
public Result beforeCharTyped(final char c, final Project project, final Editor editor, final PsiFile file, final FileType fileType) {
PsiFile cfmlFile = file.getViewProvider().getPsi(CfmlLanguage.INSTANCE);
if (isNotCfmlFile(cfmlFile, editor)) {
return Result.CONTINUE;
}
int offset = editor.getCaretModel().getOffset();
if (c == '{') {
CfmlBraceMatcher braceMatcher = new CfmlBraceMatcher();
HighlighterIterator iterator = ((EditorEx) editor).getHighlighter().createIterator(offset);
if (!braceMatcher.isLBraceToken(iterator, editor.getDocument().getCharsSequence(), fileType)) {
EditorModificationUtil.insertStringAtCaret(editor, "}", true, 0);
// return Result.STOP;
}
return Result.CONTINUE;
}
if (c == '#') {
if (ourEnableDoublePoundInsertion && CfmlEditorUtil.countSharpsBalance(editor) == 0) {
char charAtOffset = DocumentUtils.getCharAt(editor.getDocument(), offset);
if (charAtOffset == '#') {
EditorModificationUtil.moveCaretRelatively(editor, 1);
return Result.STOP;
}
EditorModificationUtil.insertStringAtCaret(editor, "#", true, 0);
}
} else if (c == '>') {
if (((EditorEx) editor).getHighlighter().createIterator(editor.getCaretModel().getOffset()).getTokenType() == CfmlTokenTypes.COMMENT || ((EditorEx) editor).getHighlighter().createIterator(editor.getCaretModel().getOffset()).getTokenType().getLanguage() != CfmlLanguage.INSTANCE) {
return Result.CONTINUE;
}
insertCloseTagIfNeeded(editor, cfmlFile, project);
return Result.STOP;
}
return Result.CONTINUE;
}
use of com.intellij.openapi.editor.highlighter.HighlighterIterator in project intellij-plugins by JetBrains.
the class DartTypeHandler method handleDartGT.
// todo extract helper method, duplicated code in com.intellij.codeInsight.editorActions.JavaTypedHandler.handleJavaGT()
private static boolean handleDartGT(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;
}
use of com.intellij.openapi.editor.highlighter.HighlighterIterator in project intellij-community by JetBrains.
the class BraceMatchingUtil method getMatchedBraceOffset.
@TestOnly
public static int getMatchedBraceOffset(@NotNull Editor editor, boolean forward, @NotNull PsiFile file) {
Document document = editor.getDocument();
int offset = editor.getCaretModel().getOffset();
EditorHighlighter editorHighlighter = BraceHighlightingHandler.getLazyParsableHighlighterIfAny(file.getProject(), editor, file);
HighlighterIterator iterator = editorHighlighter.createIterator(offset);
boolean matched = matchBrace(document.getCharsSequence(), file.getFileType(), iterator, forward);
assert matched;
return iterator.getStart();
}
use of com.intellij.openapi.editor.highlighter.HighlighterIterator in project intellij-community by JetBrains.
the class TodoItemNode method update.
@Override
public void update(PresentationData presentation) {
TodoItem todoItem = getValue().getTodoItem();
RangeMarker myRangeMarker = getValue().getRangeMarker();
if (!todoItem.getFile().isValid() || !myRangeMarker.isValid() || myRangeMarker.getStartOffset() == myRangeMarker.getEndOffset()) {
myRangeMarker.dispose();
setValue(null);
return;
}
myHighlightedRegions.clear();
// Update name
Document document = getValue().getDocument();
CharSequence chars = document.getCharsSequence();
int startOffset = myRangeMarker.getStartOffset();
int endOffset = myRangeMarker.getEndOffset();
LOG.assertTrue(startOffset > -1);
LOG.assertTrue(startOffset <= document.getTextLength());
LOG.assertTrue(endOffset > -1);
LOG.assertTrue(endOffset < document.getTextLength() + 1);
int lineNumber = document.getLineNumber(startOffset);
LOG.assertTrue(lineNumber > -1);
LOG.assertTrue(lineNumber < document.getLineCount());
int lineStartOffset = document.getLineStartOffset(lineNumber);
LOG.assertTrue(lineStartOffset > -1);
LOG.assertTrue(lineStartOffset <= startOffset);
LOG.assertTrue(lineStartOffset <= document.getTextLength());
int columnNumber = startOffset - lineStartOffset;
LOG.assertTrue(columnNumber > -1);
while (lineStartOffset < document.getTextLength() && (chars.charAt(lineStartOffset) == '\t' || chars.charAt(lineStartOffset) == ' ')) {
lineStartOffset++;
}
int lineEndOffset = document.getLineEndOffset(lineNumber);
LOG.assertTrue(lineEndOffset >= 0);
LOG.assertTrue(lineEndOffset <= document.getTextLength());
String lineColumnPrefix = "(" + (lineNumber + 1) + ", " + (columnNumber + 1) + ") ";
String highlightedText = chars.subSequence(lineStartOffset, Math.min(lineEndOffset, chars.length())).toString();
String newName = lineColumnPrefix + highlightedText;
// Update icon
Icon newIcon = todoItem.getPattern().getAttributes().getIcon();
// Update highlighted regions
myHighlightedRegions.clear();
EditorHighlighter highlighter = myBuilder.getHighlighter(todoItem.getFile(), document);
HighlighterIterator iterator = highlighter.createIterator(lineStartOffset);
while (!iterator.atEnd()) {
int start = Math.max(iterator.getStart(), lineStartOffset);
int end = Math.min(iterator.getEnd(), lineEndOffset);
if (lineEndOffset < start || lineEndOffset < end) {
break;
}
TextAttributes attributes = iterator.getTextAttributes();
int fontType = attributes.getFontType();
if ((fontType & Font.BOLD) != 0) {
// suppress bold attribute
attributes = attributes.clone();
attributes.setFontType(fontType & ~Font.BOLD);
}
HighlightedRegion region = new HighlightedRegion(lineColumnPrefix.length() + start - lineStartOffset, lineColumnPrefix.length() + end - lineStartOffset, attributes);
myHighlightedRegions.add(region);
iterator.advance();
}
TextAttributes attributes = todoItem.getPattern().getAttributes().getTextAttributes();
HighlightedRegion region = new HighlightedRegion(lineColumnPrefix.length() + startOffset - lineStartOffset, lineColumnPrefix.length() + endOffset - lineStartOffset, attributes);
myHighlightedRegions.add(region);
//
presentation.setPresentableText(newName);
presentation.setIcon(newIcon);
}
Aggregations