public void testCpp() throws Exception { EditorHighlighter highlighter = HighlighterFactory.createHighlighter(getProject(), "A.cpp"); // 0123456789012345678 9 0123 45 6 7 highlighter.setText("#include try enum \"\\xff\\z\\\"xxx\""); HighlighterIterator iterator = highlighter.createIterator(2); assertEquals(CustomHighlighterTokenType.KEYWORD_1, iterator.getTokenType()); iterator = highlighter.createIterator(9); assertEquals(CustomHighlighterTokenType.KEYWORD_2, iterator.getTokenType()); iterator = highlighter.createIterator(15); assertEquals(CustomHighlighterTokenType.KEYWORD_1, iterator.getTokenType()); iterator = highlighter.createIterator(19); assertEquals(StringEscapesTokenTypes.VALID_STRING_ESCAPE_TOKEN, iterator.getTokenType()); iterator = highlighter.createIterator(23); assertEquals(StringEscapesTokenTypes.INVALID_CHARACTER_ESCAPE_TOKEN, iterator.getTokenType()); iterator = highlighter.createIterator(25); assertEquals(StringEscapesTokenTypes.VALID_STRING_ESCAPE_TOKEN, iterator.getTokenType()); iterator = highlighter.createIterator(27); assertEquals(CustomHighlighterTokenType.STRING, iterator.getTokenType()); }
public boolean hasToken(int position, CharSequence myBuffer, @Nullable TokenInfo tokenInfo) { int index = 0; int offset = position; while (offset < myBuffer.length()) { char c = myBuffer.charAt(offset++); int nextIndex = myTrie.findSubNode(index, myIgnoreCase ? Character.toUpperCase(c) : c); if (nextIndex == 0) { break; } index = nextIndex; if (myHashCodes.contains(index) && isWordEnd(offset, myBuffer)) { String keyword = myBuffer.subSequence(position, offset).toString(); String testKeyword = myIgnoreCase ? StringUtil.toUpperCase(keyword) : keyword; for (int i = 0; i < CustomHighlighterTokenType.KEYWORD_TYPE_COUNT; i++) { if (myKeywordSets.get(i).contains(testKeyword)) { if (tokenInfo != null) { tokenInfo.updateData(position, position + keyword.length(), getToken(i)); } return true; } } } } return false; }
public boolean hasToken(int position, CharSequence myBuffer, TokenInfo myTokenInfo) { int index = 0; int offset = position; boolean found = false; while (offset < myBuffer.length()) { char c = myBuffer.charAt(offset++); int nextIndex = myTrie.findSubNode(index, myIgnoreCase ? Character.toUpperCase(c) : c); if (nextIndex == 0) { break; } index = nextIndex; if (myHashCodes.contains(index) && isWordEnd(offset, myBuffer)) { String keyword = myBuffer.subSequence(position, offset).toString(); String testKeyword = myIgnoreCase ? StringUtil.toUpperCase(keyword) : keyword; for (int i = 0; i < CustomHighlighterTokenType.KEYWORD_TYPE_COUNT; i++) { if (myKeywordSets.get(i).contains(testKeyword)) { myTokenInfo.updateData(position, position + keyword.length(), getToken(i)); found = true; break; } } } } return found; }
@Override public IElementType getOppositeBraceTokenType(@NotNull IElementType type) { if (!(type instanceof CustomHighlighterTokenType.CustomElementType)) { return null; } if (type == CustomHighlighterTokenType.L_BRACE) return CustomHighlighterTokenType.R_BRACE; if (type == CustomHighlighterTokenType.R_BRACE) return CustomHighlighterTokenType.L_BRACE; if (type == CustomHighlighterTokenType.L_BRACKET) return CustomHighlighterTokenType.R_BRACKET; if (type == CustomHighlighterTokenType.R_BRACKET) return CustomHighlighterTokenType.L_BRACKET; if (type == CustomHighlighterTokenType.L_ANGLE) return CustomHighlighterTokenType.R_ANGLE; if (type == CustomHighlighterTokenType.R_ANGLE) return CustomHighlighterTokenType.L_ANGLE; if (type == CustomHighlighterTokenType.L_PARENTH) return CustomHighlighterTokenType.R_PARENTH; if (type == CustomHighlighterTokenType.R_PARENTH) return CustomHighlighterTokenType.L_PARENTH; return null; }
public void advance() { if (myPosition >= myEndOffset) { myCurrentToken.updateData(myPosition, myPosition, null); return; } boolean tokenFound = false; for (TokenParser tokenParser : myTokenParsers) { if (tokenParser.hasToken(myPosition)) { tokenParser.getTokenInfo(myCurrentToken); if (myCurrentToken.getEnd() <= myCurrentToken.getStart()) { throw new AssertionError(tokenParser); } tokenFound = true; break; } } if (!tokenFound) { myCurrentToken.updateData(myPosition, myPosition + 1, CustomHighlighterTokenType.CHARACTER); } myPosition = myCurrentToken.getEnd(); }
public void testHaskel() throws Exception { EditorHighlighter highlighter = HighlighterFactory.createHighlighter(getProject(), "A.hs"); // 0123456789012345678 9 0123 45 6 7 highlighter.setText("{-# #-} module"); HighlighterIterator iterator = highlighter.createIterator(2); assertEquals(CustomHighlighterTokenType.MULTI_LINE_COMMENT, iterator.getTokenType()); iterator = highlighter.createIterator(12); assertEquals(CustomHighlighterTokenType.KEYWORD_1, iterator.getTokenType()); }
public static WordsScanner createCustomFileTypeScanner(SyntaxTable syntaxTable) { return new DefaultWordsScanner(new CustomFileTypeLexer(syntaxTable, true), TokenSet.create(CustomHighlighterTokenType.IDENTIFIER), TokenSet.create(CustomHighlighterTokenType.LINE_COMMENT, CustomHighlighterTokenType.MULTI_LINE_COMMENT), TokenSet.create(CustomHighlighterTokenType.STRING, CustomHighlighterTokenType.SINGLE_QUOTED_STRING)); }
@Override public boolean hasToken(int position) { final char c = myBuffer.charAt(position); if (".,:;".indexOf(c) >= 0) { myTokenInfo.updateData(position, position+1, CustomHighlighterTokenType.PUNCTUATION); return true; } return false; }
@Override public boolean hasToken(int position) { if (!Character.isJavaIdentifierStart(myBuffer.charAt(position))) return false; final int start = position; for (position++; position < myEndOffset; position++) { if (!isIdentifierPart(position)) break; } IElementType tokenType = CustomHighlighterTokenType.IDENTIFIER; myTokenInfo.updateData(start, position, tokenType); return true; }
@Override public boolean hasToken(int position) { final int start = position; final char startChar = myBuffer.charAt(start); if(!isDigit(startChar)) return false; for (position++; position < myEndOffset; position++) { if (!isDigit(myBuffer.charAt(position))) break; } if (position < myEndOffset && myBuffer.charAt(position) == '.') { final int dotPosition = position; position++; if (position < myEndOffset && !isDigit(myBuffer.charAt(position))) { position = dotPosition; } else { // after decimal point for (; position < myEndOffset; position++) { if (!isDigit(myBuffer.charAt(position))) break; } if (position < myEndOffset) { final char finalChar = myBuffer.charAt(position); if (!isNumberTail(finalChar) && !isDelimiter(finalChar)) { position = dotPosition; } } } } while(position < myEndOffset && isNumberTail(myBuffer.charAt(position))) { position++; } myTokenInfo.updateData(start, position, CustomHighlighterTokenType.NUMBER); return true; }
public KeywordParser(List<Set<String>> keywordSets, boolean ignoreCase) { myIgnoreCase = ignoreCase; LOG.assertTrue(keywordSets.size() == CustomHighlighterTokenType.KEYWORD_TYPE_COUNT); for (Set<String> keywordSet : keywordSets) { Set<String> normalized = normalizeKeywordSet(keywordSet); myKeywordSets.add(normalized); for (String s : normalized) { myHashCodes.add(myTrie.getHashCode(s)); } } }
private static IElementType getToken(int keywordSetIndex) { switch(keywordSetIndex) { case 0: return CustomHighlighterTokenType.KEYWORD_1; case 1: return CustomHighlighterTokenType.KEYWORD_2; case 2: return CustomHighlighterTokenType.KEYWORD_3; case 3: return CustomHighlighterTokenType.KEYWORD_4; } throw new AssertionError(keywordSetIndex); }
@Override public boolean hasToken(int position) { if (!Character.isWhitespace(myBuffer.charAt(position))) return false; int start = position; for (position++; position < myEndOffset && Character.isWhitespace(myBuffer.charAt(position)); position++) ; myTokenInfo.updateData(start, position, CustomHighlighterTokenType.WHITESPACE); return true; }
private static boolean inCommentOrLiteral(CompletionParameters parameters) { HighlighterIterator iterator = ((EditorEx)parameters.getEditor()).getHighlighter().createIterator(parameters.getOffset()); 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; }
public static Lexer createPlainTextLexer() { ArrayList<TokenParser> tokenParsers = new ArrayList<TokenParser>(); tokenParsers.add(new WhitespaceParser()); tokenParsers.addAll(BraceTokenParser.getBraces()); tokenParsers.addAll(BraceTokenParser.getParens()); tokenParsers.addAll(BraceTokenParser.getBrackets()); tokenParsers.addAll(BraceTokenParser.getAngleBrackets()); return new MergingLexerAdapter(new AbstractCustomLexer(tokenParsers), TokenSet.create(CustomHighlighterTokenType.CHARACTER)); }
private static WordsScanner createWordScanner(final CustomSyntaxTableFileType customSyntaxTableFileType) { return new DefaultWordsScanner(new CustomFileTypeLexer(customSyntaxTableFileType.getSyntaxTable(), true), TokenSet.create(CustomHighlighterTokenType.IDENTIFIER), TokenSet.create(CustomHighlighterTokenType.LINE_COMMENT, CustomHighlighterTokenType.MULTI_LINE_COMMENT), TokenSet.create(CustomHighlighterTokenType.STRING, CustomHighlighterTokenType.SINGLE_QUOTED_STRING)); }
public boolean hasToken(int position) { if (!Character.isJavaIdentifierStart(myBuffer.charAt(position))) return false; final int start = position; for (position++; position < myEndOffset; position++) { final char c = myBuffer.charAt(position); if (!isIdentifierPart(c)) break; } IElementType tokenType = CustomHighlighterTokenType.IDENTIFIER; myTokenInfo.updateData(start, position, tokenType); return true; }
public boolean hasToken(int position) { final int start = position; final char startChar = myBuffer.charAt(start); if(!isDigit(startChar)) return false; for (position++; position < myEndOffset; position++) { if (!isDigit(myBuffer.charAt(position))) break; } if (position < myEndOffset && myBuffer.charAt(position) == '.') { final int dotPosition = position; position++; if (position < myEndOffset && !isDigit(myBuffer.charAt(position))) { position = dotPosition; } else { // after decimal point for (; position < myEndOffset; position++) { if (!isDigit(myBuffer.charAt(position))) break; } if (position < myEndOffset) { final char finalChar = myBuffer.charAt(position); if (!isNumberTail(finalChar) && !isDelimiter(finalChar)) { position = dotPosition; } } } } while(position < myEndOffset && isNumberTail(myBuffer.charAt(position))) { position++; } myTokenInfo.updateData(start, position, CustomHighlighterTokenType.NUMBER); return true; }
public boolean hasToken(int position) { if (!Character.isWhitespace(myBuffer.charAt(position))) return false; int start = position; for (position++; position < myEndOffset && Character.isWhitespace(myBuffer.charAt(position)); position++) ; myTokenInfo.updateData(start, position, CustomHighlighterTokenType.WHITESPACE); return true; }
@Override public boolean isLBraceToken(HighlighterIterator iterator, CharSequence fileText, FileType fileType) { final IElementType tokenType = iterator.getTokenType(); return tokenType == CustomHighlighterTokenType.L_BRACKET || tokenType == CustomHighlighterTokenType.L_ANGLE || tokenType == CustomHighlighterTokenType.L_PARENTH || tokenType == CustomHighlighterTokenType.L_BRACE; }
@Override public boolean isPairBraces(IElementType tokenType, IElementType tokenType2) { return (tokenType == CustomHighlighterTokenType.L_BRACE && tokenType2 == CustomHighlighterTokenType.R_BRACE) || (tokenType == CustomHighlighterTokenType.R_BRACE && tokenType2 == CustomHighlighterTokenType.L_BRACE) || (tokenType == CustomHighlighterTokenType.L_BRACKET && tokenType2 == CustomHighlighterTokenType.R_BRACKET) || (tokenType == CustomHighlighterTokenType.R_BRACKET && tokenType2 == CustomHighlighterTokenType.L_BRACKET) || (tokenType == CustomHighlighterTokenType.L_ANGLE && tokenType2 == CustomHighlighterTokenType.R_ANGLE) || (tokenType == CustomHighlighterTokenType.R_ANGLE && tokenType2 == CustomHighlighterTokenType.L_ANGLE) || (tokenType == CustomHighlighterTokenType.L_PARENTH && tokenType2 == CustomHighlighterTokenType.R_PARENTH) || (tokenType == CustomHighlighterTokenType.R_PARENTH && tokenType2 == CustomHighlighterTokenType.L_PARENTH); }
@Override public boolean isClosingQuote(HighlighterIterator iterator, int offset) { final IElementType tokenType = iterator.getTokenType(); if (tokenType == CustomHighlighterTokenType.STRING || tokenType == CustomHighlighterTokenType.SINGLE_QUOTED_STRING || tokenType == CustomHighlighterTokenType.CHARACTER){ int start = iterator.getStart(); int end = iterator.getEnd(); return end - start >= 1 && offset == end - 1; } return false; }
@Override public boolean isOpeningQuote(HighlighterIterator iterator, int offset) { final IElementType tokenType = iterator.getTokenType(); if (tokenType == CustomHighlighterTokenType.STRING || tokenType == CustomHighlighterTokenType.SINGLE_QUOTED_STRING || tokenType == CustomHighlighterTokenType.CHARACTER){ int start = iterator.getStart(); return offset == start; } return false; }
@Override public boolean hasNonClosedLiteral(Editor editor, HighlighterIterator iterator, int offset) { try { Document doc = editor.getDocument(); CharSequence chars = doc.getCharsSequence(); int lineEnd = doc.getLineEndOffset(doc.getLineNumber(offset)); while (!iterator.atEnd() && iterator.getStart() < lineEnd) { IElementType tokenType = iterator.getTokenType(); if (tokenType == CustomHighlighterTokenType.STRING || tokenType == CustomHighlighterTokenType.SINGLE_QUOTED_STRING || tokenType == CustomHighlighterTokenType.CHARACTER) { if (iterator.getStart() >= iterator.getEnd() - 1 || chars.charAt(iterator.getEnd() - 1) != '\"' && chars.charAt(iterator.getEnd() - 1) != '\'') { return true; } } iterator.advance(); } } finally { while (!iterator.atEnd() && iterator.getStart() != offset) iterator.retreat(); } return false; }
@Override public boolean isInsideLiteral(HighlighterIterator iterator) { final IElementType tokenType = iterator.getTokenType(); return tokenType == CustomHighlighterTokenType.STRING || tokenType == CustomHighlighterTokenType.SINGLE_QUOTED_STRING || tokenType == CustomHighlighterTokenType.CHARACTER; }