String (@NotNull PsiFile testFile, boolean allowUnhandledTokens) { TestCase.assertNotNull("Fixture has no file", testFile); final SyntaxHighlighter syntaxHighlighter = SyntaxHighlighterFactory.getSyntaxHighlighter(testFile.getFileType(), testFile.getProject(), testFile.getVirtualFile()); TestCase.assertNotNull("Syntax highlighter not found", syntaxHighlighter); final Lexer highlightingLexer = syntaxHighlighter.getHighlightingLexer(); TestCase.assertNotNull("Highlighting lexer not found", highlightingLexer); final String fileText = testFile.getText(); highlightingLexer.start(fileText); IElementType tokenType; final StringBuilder sb = new StringBuilder(); Set<IElementType> notHighlightedTokens = new HashSet<>(); while ((tokenType = highlightingLexer.getTokenType()) != null) { final TextAttributesKey[] highlights = syntaxHighlighter.getTokenHighlights(tokenType); if (highlights.length > 0) { if (sb.length() > 0) { sb.append("\n"); } String token = fileText.substring(highlightingLexer.getTokenStart(), highlightingLexer.getTokenEnd()); token = token.replace(' ', '␣'); if (StringUtil.isEmptyOrSpaces(token)) { token = token.replace("\n", "\\n"); } sb.append(token).append("\n"); final List<String> attrNames = new SmartList<>(); for (final TextAttributesKey attributesKey : highlights) { attrNames.add(" " + serializeTextAttributeKey(attributesKey)); } sb.append(StringUtil.join(attrNames, "\n")); } else if (!StringUtil.isEmptyOrSpaces(highlightingLexer.getTokenText())) { notHighlightedTokens.add(tokenType); } highlightingLexer.advance(); } if (!allowUnhandledTokens && !notHighlightedTokens.isEmpty()) { TestCase.fail("Some tokens have no highlighting: " + notHighlightedTokens); } return sb.toString(); }