use of com.intellij.openapi.editor.highlighter.HighlighterIterator in project intellij-community by JetBrains.
the class CustomFileTypeCompletionContributor method inCommentOrLiteral.
private static boolean inCommentOrLiteral(CompletionParameters parameters) {
HighlighterIterator iterator = ((EditorEx) parameters.getEditor()).getHighlighter().createIterator(parameters.getOffset());
if (iterator.atEnd())
return false;
IElementType elementType = iterator.getTokenType();
if (elementType == CustomHighlighterTokenType.WHITESPACE) {
iterator.retreat();
elementType = iterator.getTokenType();
}
return elementType == CustomHighlighterTokenType.LINE_COMMENT || elementType == CustomHighlighterTokenType.MULTI_LINE_COMMENT || elementType == CustomHighlighterTokenType.STRING || elementType == CustomHighlighterTokenType.SINGLE_QUOTED_STRING;
}
use of com.intellij.openapi.editor.highlighter.HighlighterIterator in project intellij-community by JetBrains.
the class HTMLTextPainter method writeStyles.
private void writeStyles(@NonNls final Writer writer) throws IOException {
EditorColorsScheme scheme = EditorColorsManager.getInstance().getGlobalScheme();
writer.write("<style type=\"text/css\">\n");
final Color lineNumbers = scheme.getColor(EditorColors.LINE_NUMBERS_COLOR);
writer.write(String.format(".ln { color: #%s; font-weight: normal; font-style: normal; }\n", ColorUtil.toHex(lineNumbers == null ? Gray.x00 : lineNumbers)));
HighlighterIterator hIterator = myHighlighter.createIterator(myOffset);
while (!hIterator.atEnd()) {
TextAttributes textAttributes = hIterator.getTextAttributes();
if (!myStyleMap.containsKey(textAttributes)) {
@NonNls String styleName = "s" + myStyleMap.size();
myStyleMap.put(textAttributes, styleName);
writer.write("." + styleName + " { ");
Color foreColor = textAttributes.getForegroundColor();
if (foreColor == null)
foreColor = scheme.getDefaultForeground();
writer.write("color: " + colorToHtml(foreColor) + "; ");
if (BitUtil.isSet(textAttributes.getFontType(), Font.BOLD)) {
writer.write("font-weight: bold; ");
}
if (BitUtil.isSet(textAttributes.getFontType(), Font.ITALIC)) {
writer.write("font-style: italic; ");
}
writer.write("}\n");
}
hIterator.advance();
}
for (LineMarkerInfo separator : myMethodSeparators) {
Color color = separator.separatorColor;
if (color != null && !mySeparatorStyles.containsKey(color)) {
@NonNls String styleName = "ls" + mySeparatorStyles.size();
mySeparatorStyles.put(color, styleName);
String htmlColor = colorToHtml(color);
writer.write("." + styleName + " { height: 1px; border-width: 0; color: " + htmlColor + "; background-color:" + htmlColor + "}\n");
}
}
writer.write("</style>\n");
}
use of com.intellij.openapi.editor.highlighter.HighlighterIterator in project intellij-community by JetBrains.
the class HTMLTextPainter method paint.
@SuppressWarnings({ "HardCodedStringLiteral" })
public void paint(TreeMap refMap, FileType fileType) throws FileNotFoundException {
HighlighterIterator hIterator = myHighlighter.createIterator(myOffset);
if (hIterator.atEnd())
return;
OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(myHTMLFileName), CharsetToolkit.UTF8_CHARSET);
lineCount = myFirstLineNumber;
TextAttributes prevAttributes = null;
Iterator refKeys = null;
int refOffset = -1;
PsiReference ref = null;
if (refMap != null) {
refKeys = refMap.keySet().iterator();
if (refKeys.hasNext()) {
Integer key = (Integer) refKeys.next();
ref = (PsiReference) refMap.get(key);
refOffset = key.intValue();
}
}
int referenceEnd = -1;
try {
writeHeader(writer, new File(myFileName).getName());
if (myFirstLineNumber == 0) {
writeLineNumber(writer);
}
String closeTag = null;
getMethodSeparator(hIterator.getStart());
while (!hIterator.atEnd()) {
TextAttributes textAttributes = hIterator.getTextAttributes();
int hStart = hIterator.getStart();
int hEnd = hIterator.getEnd();
if (hEnd > mySegmentEnd)
break;
boolean haveNonWhiteSpace = false;
for (int offset = hStart; offset < hEnd; offset++) {
char c = myText.charAt(offset);
if (c != ' ' && c != '\t') {
haveNonWhiteSpace = true;
break;
}
}
if (!haveNonWhiteSpace) {
// don't write separate spans for whitespace-only text fragments
writeString(writer, myText, hStart, hEnd - hStart, fileType);
hIterator.advance();
continue;
}
if (refOffset > 0 && hStart <= refOffset && hEnd > refOffset) {
referenceEnd = writeReferenceTag(writer, ref);
}
// if(myForceFonts || !equals(prevAttributes, textAttributes)) {
if (!equals(prevAttributes, textAttributes) && referenceEnd < 0) {
if (closeTag != null) {
writer.write(closeTag);
}
closeTag = writeFontTag(writer, textAttributes);
prevAttributes = textAttributes;
}
writeString(writer, myText, hStart, hEnd - hStart, fileType);
// }
if (referenceEnd > 0 && hEnd >= referenceEnd) {
writer.write("</a>");
referenceEnd = -1;
if (refKeys.hasNext()) {
Integer key = (Integer) refKeys.next();
ref = (PsiReference) refMap.get(key);
refOffset = key.intValue();
}
}
hIterator.advance();
}
if (closeTag != null) {
writer.write(closeTag);
}
writeFooter(writer);
} catch (IOException e) {
LOG.error(e.getMessage(), e);
} finally {
try {
writer.close();
} catch (IOException e) {
LOG.error(e.getMessage(), e);
}
}
}
use of com.intellij.openapi.editor.highlighter.HighlighterIterator 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.openapi.editor.highlighter.HighlighterIterator in project intellij-community by JetBrains.
the class EnterInLineCommentHandler method isInLineComment.
private static boolean isInLineComment(@NotNull Editor editor, int offset, IElementType lineCommentType) {
if (offset < 1)
return false;
EditorHighlighter highlighter = ((EditorEx) editor).getHighlighter();
HighlighterIterator iterator = highlighter.createIterator(offset - 1);
return iterator.getTokenType() == lineCommentType;
}
Aggregations