private boolean isComment(int offset) { final HighlighterIterator it = myEditor.getHighlighter().createIterator(offset); IElementType tokenType = it.getTokenType(); Language language = tokenType.getLanguage(); TokenSet comments = myComments.get(language); if (comments == null) { ParserDefinition definition = LanguageParserDefinitions.INSTANCE.forLanguage(language); if (definition != null) { comments = definition.getCommentTokens(); } if (comments == null) { return false; } else { myComments.put(language, comments); } } return comments.contains(tokenType); }
/** * @return children of specified type, taken from stubs (if this element is currently stub-based) or AST (otherwise). */ public <Psi extends PsiElement> Psi[] getStubOrPsiChildren(TokenSet filter, ArrayFactory<Psi> f) { T stub = myStub; if (stub != null) { //noinspection unchecked return (Psi[])stub.getChildrenByType(filter, f); } else { final ASTNode[] nodes = getNode().getChildren(filter); Psi[] psiElements = f.create(nodes.length); for (int i = 0; i < nodes.length; i++) { //noinspection unchecked psiElements[i] = (Psi)nodes[i].getPsi(); } return psiElements; } }
private static IElementType parseEnumBody(@NotNull PsiBuilder builder, @NotNull String enumName, @NotNull GroovyParser parser) { PsiBuilder.Marker ebMarker = builder.mark(); if (!ParserUtils.getToken(builder, GroovyTokenTypes.mLCURLY)) { ebMarker.rollbackTo(); return GroovyElementTypes.WRONGWAY; } ParserUtils.getToken(builder, GroovyTokenTypes.mNLS); if (EnumConstant.parseConstantList(builder, parser)) { if (!ParserUtils.lookAhead(builder, GroovyTokenTypes.mRCURLY)) { ParserUtils.getToken(builder, TokenSet.create(GroovyTokenTypes.mNLS, GroovyTokenTypes.mSEMI), GroovyBundle.message("separator.or.rcurly.expected")); } } parseMembers(builder, enumName, parser, false); ParserUtils.getToken(builder, GroovyTokenTypes.mRCURLY, GroovyBundle.message("rcurly.expected")); ebMarker.done(GroovyElementTypes.ENUM_BODY); return GroovyElementTypes.ENUM_BODY; }
private static boolean isInsideStringLiteral(@NotNull Editor editor, @NotNull PsiFile file) { int offset = editor.getCaretModel().getOffset(); PsiElement element = file.findElementAt(offset); if (element == null) return false; final ParserDefinition definition = LanguageParserDefinitions.INSTANCE.forLanguage(element.getLanguage()); if (definition != null) { final TokenSet stringLiteralElements = definition.getStringLiteralElements(); final ASTNode node = element.getNode(); if (node == null) return false; final IElementType elementType = node.getElementType(); if (stringLiteralElements.contains(elementType)) { return true; } PsiElement parent = element.getParent(); if (parent != null) { ASTNode parentNode = parent.getNode(); if (parentNode != null && stringLiteralElements.contains(parentNode.getElementType())) { return true; } } } return false; }
@Nullable private static ASTNode checkNodeAndSiblings(@Nullable ASTNode node, TokenSet tokens, ArrayList<FoldingDescriptor> regions, Document document) { if (node != null && tokens.contains(node.getElementType())) { final ASTNode start = node; ASTNode end = start; node = node.getTreeNext(); if (node != null) { do { end = node; node = node.getTreeNext(); } while (node != null && tokens.contains(node.getElementType())); } if (end != start) { while (end.getPsi() instanceof PsiWhiteSpace) { end = end.getTreePrev(); } if (isOnDifferentLine(start, end, document)) { regions.add(new FoldingDescriptor(start, new TextRange(start.getStartOffset(), end.getTextRange().getEndOffset()))); } } } return node; }
@Override public void doApply(@NotNull Editor editor, @NotNull PySmartEnterProcessor processor, @NotNull PyConditionalStatementPart statementPart) throws IncorrectOperationException { final PyExpression condition = statementPart.getCondition(); final Document document = editor.getDocument(); final PsiElement colon = PyPsiUtils.getFirstChildOfType(statementPart, PyTokenTypes.COLON); if (colon == null) { if (condition != null) { final PsiElement firstNonComment = PyPsiUtils.getNextNonCommentSibling(condition.getNextSibling(), false); if (firstNonComment != null && !":".equals(firstNonComment.getNode().getText())) { document.insertString(firstNonComment.getTextRange().getEndOffset(), ":"); } } else { final TokenSet keywords = TokenSet.create(PyTokenTypes.IF_KEYWORD, PyTokenTypes.ELIF_KEYWORD, PyTokenTypes.WHILE_KEYWORD); final PsiElement keywordToken = PyPsiUtils.getChildByFilter(statementPart, keywords, 0); final int offset = sure(keywordToken).getTextRange().getEndOffset(); document.insertString(offset, " :"); processor.registerUnresolvedError(offset + 1); } } else if (condition == null) { processor.registerUnresolvedError(colon.getTextRange().getStartOffset()); } }
@Override public boolean acceptInput(@NotNull final VirtualFile file) { if (!file.isInLocalFileSystem()) { return false; // do not index TODOs in library sources } final FileType fileType = file.getFileType(); if (fileType instanceof LanguageFileType) { final Language lang = ((LanguageFileType)fileType).getLanguage(); final ParserDefinition parserDef = LanguageParserDefinitions.INSTANCE.forLanguage(lang); final TokenSet commentTokens = parserDef != null ? parserDef.getCommentTokens() : null; return commentTokens != null; } return PlatformIdTableBuilding.isTodoIndexerRegistered(fileType) || fileType instanceof CustomSyntaxTableFileType; }
@Nullable @Override public WordsScanner getWordsScanner() { return new DefaultWordsScanner( new TSLexerAdapter(), TokenSet.create(TSTypes.ID), TokenSet.create(TSTypes.COMMENT), TokenSet.EMPTY ); }
@Nullable @Override public WordsScanner getWordsScanner() { return new DefaultWordsScanner(new AppleScriptLexerAdapter(), TokenSet.create(AppleScriptTypes.IDENTIFIER), TokenSet.create(AppleScriptTypes.COMMENT), TokenSet.create(AppleScriptTypes.STRING_LITERAL)); }
@Nullable @Override public WordsScanner getWordsScanner() { return new DefaultWordsScanner( new SoyLexer(), TokenSet.create(SoyTypes.IDENTIFIER_WORD), TokenSet.create(SoyTypes.COMMENT_BLOCK), TokenSet.EMPTY); }
@Override public void annotate(@NotNull PsiElement psiElement, @NotNull AnnotationHolder annotationHolder) { if (psiElement instanceof TagElement) { TagElement tagElement = (TagElement) psiElement; TokenSet allowedRBraces = SoyTokenTypes.RIGHT_BRACES; if (mustCloseRBraceTags.contains(tagElement.getClass())) { allowedRBraces = TokenSet.andNot(allowedRBraces, SoyTokenTypes.SLASH_R_BRACES); } else if (mustCloseSlashRBraceTags.contains(tagElement.getClass())) { allowedRBraces = TokenSet.andSet(allowedRBraces, SoyTokenTypes.SLASH_R_BRACES); } if (tagElement.isDoubleBraced()) { allowedRBraces = TokenSet.andSet(allowedRBraces, SoyTokenTypes.DOUBLE_BRACES); } else { allowedRBraces = TokenSet.andNot(allowedRBraces, SoyTokenTypes.DOUBLE_BRACES); } if (!allowedRBraces.contains(tagElement.getClosingBraceType())) { annotationHolder.createErrorAnnotation(tagElement, "Must close by " + Stream.of(allowedRBraces.getTypes()) .map(SoyTokenTypes.BRACE_TYPE_TO_STRING::get) .sorted() .collect(Collectors.joining(" or "))); } } }
private static SpacingBuilder createSpaceBuilder(final CodeStyleSettings settings) { return new SpacingBuilder(settings, FlexibleSearchLanguage.getInstance()) .after(FlexibleSearchTypes.COMP_OP) .spaceIf(true) .before(FlexibleSearchTypes.COMP_OP) .spaceIf(true) .after(FlexibleSearchTypes.LEFT_DOUBLE_BRACE) .spaceIf(true) .before(FlexibleSearchTypes.RIGHT_DOUBLE_BRACE) .spaceIf(true) .before(FlexibleSearchTypes.LEFT_BRACE) .spaceIf(true) .after(FlexibleSearchTypes.RIGHT_BRACE) .spaceIf(true) .before(FlexibleSearchTypes.LEFT_PAREN) .spaceIf(true) .after(FlexibleSearchTypes.RIGHT_PAREN) .spaceIf(true) .around(TokenSet.create(FlexibleSearchTypes.WHERE, FlexibleSearchTypes.WHERE_CLAUSE)) .spaceIf(true) ; }
@Nullable @Override public WordsScanner getWordsScanner() { return new DefaultWordsScanner( new ImpexLexerAdapter(), orSet( create(ImpexTypes.MACRO_NAME_DECLARATION), create(ImpexTypes.MACRO_DECLARATION), create(ImpexTypes.MACRO_USAGE) ), create(ImpexTypes.COMMENT), TokenSet.ANY ); }
public List<YAMLDocument> getDocuments() { final ArrayList<YAMLDocument> result = new ArrayList<>(); for (ASTNode node : getNode().getChildren(TokenSet.create(YAMLElementTypes.DOCUMENT))) { result.add((YAMLDocument) node.getPsi()); } return result; }
@Nullable @Override public WordsScanner getWordsScanner() { return new DefaultWordsScanner(new YAMLFlexLexer(), TokenSet.create(YAMLTokenTypes.SCALAR_KEY), TokenSet.create(YAMLTokenTypes.COMMENT), TokenSet.create(YAMLTokenTypes.SCALAR_TEXT, YAMLTokenTypes.SCALAR_DSTRING, YAMLTokenTypes.SCALAR_STRING)); }
@Nullable @Override public WordsScanner getWordsScanner() { return new DefaultWordsScanner(new CrystalLexerAdapter(), TokenSet.create(CrystalTypes.KEY), TokenSet.create(CrystalTypes.COMMENT), TokenSet.EMPTY); }
/** * Tries to update the map by associating given keys with a given value. * Throws error if the map already contains different mapping for one of given keys. */ protected static void safeMap(@NotNull final Map<IElementType, TextAttributesKey> map, @NotNull final TokenSet keys, @NotNull final TextAttributesKey value) { for (final IElementType type : keys.getTypes()) { safeMap(map, type, value); } }
@Override public <E extends PsiElement> E[] getChildrenByType(final TokenSet filter, final ArrayFactory<E> f) { final int count = countChildren(filter); E[] array = f.create(count); if (count == 0) return array; fillFilteredChildren(filter, array); return array; }
@NotNull public static List<LighterASTNode> getChildrenOfType(@NotNull LighterAST tree, @NotNull LighterASTNode node, @NotNull TokenSet types) { List<LighterASTNode> result = null; List<LighterASTNode> children = tree.getChildren(node); for (int i = 0, size = children.size(); i < size; ++i) { LighterASTNode child = children.get(i); if (types.contains(child.getTokenType())) { if (result == null) result = new SmartList<LighterASTNode>(); result.add(child); } } return result != null ? result: Collections.<LighterASTNode>emptyList(); }
@Nullable public static ASTNode findSibling(ASTNode start, TokenSet types) { ASTNode child = start; while (true) { if (child == null) return null; if (types.contains(child.getElementType())) return child; child = child.getTreeNext(); } }
@NotNull protected <T extends PyElement> T childToPsiNotNull(TokenSet filterSet, int index) { final PyElement child = childToPsi(filterSet, index); if (child == null) { throw new RuntimeException("child must not be null: expression text " + getText()); } //noinspection unchecked return (T)child; }
public RegExpOptions getOffOptions() { final ASTNode[] nodes = getNode().getChildren(TokenSet.create(RegExpElementTypes.OPTIONS)); for (ASTNode node : nodes) { if (node.textContains('-')) { return (RegExpOptions)node.getPsi(); } } return null; }
public boolean compareAndEat(final TokenSet types) { boolean found = compare(types); if (found) { advanceLexer(); } return found; }
@Override @NotNull public ASTNode parse(final IElementType root, final PsiBuilder builder) { builder.enforceCommentTokens(TokenSet.EMPTY); builder.putUserDataUnprotected(PsiBuilderImpl.CUSTOM_COMPARATOR, REPARSE_XML_TAG_BY_NAME); final PsiBuilder.Marker file = builder.mark(); new XmlParsing(builder).parseDocument(); file.done(root); return builder.getTreeBuilt(); }
private static List<IElementType> findSubNodeTypes(ASTNode node, TokenSet types) { List<IElementType> foundTypes = new SmartList<IElementType>(); for (ASTNode child = node.getFirstChildNode(); child != null && child.getTreeParent() == node; child = child.getTreeNext()) { IElementType type = child.getElementType(); if (types.contains(type)) { foundTypes.add(type); } } return foundTypes; }
private static boolean lookingAt(Lexer lexer, TokenSet tokenSet) { final LexerPosition position = lexer.getCurrentPosition(); try { lexer.advance(); skipWhitespaceAnComments(lexer); return tokenSet.contains(lexer.getTokenType()); } finally { lexer.restore(position); } }
private static PsiElement skipSet(PsiElement elem, boolean forward, TokenSet set, boolean skipNLs) { while (elem != null && elem.getNode() != null && set.contains(elem.getNode().getElementType()) && (skipNLs || elem.getText().indexOf('\n') == -1)) { if (forward) { elem = elem.getNextSibling(); } else { elem = elem.getPrevSibling(); } } return elem; }
@Override public TokenSet getCommentTokenSet(@NotNull final PsiFile file) { final JspFile jspFile = JspPsiUtil.getJspFile(file); TokenSet commentTokens = TokenSet.orSet(JavaIndexPatternBuilder.XML_COMMENT_BIT_SET, StdTokenSets.COMMENT_BIT_SET); final ParserDefinition parserDefinition = LanguageParserDefinitions.INSTANCE.forLanguage(jspFile.getViewProvider().getTemplateDataLanguage()); if (parserDefinition != null) { commentTokens = TokenSet.orSet(commentTokens, parserDefinition.getCommentTokens()); } return commentTokens; }
private int countChildren(final TokenSet types) { int count = 0; List<StubElement> childrenStubs = getChildrenStubs(); //noinspection ForLoopReplaceableByForEach for (int i = 0, childrenStubsSize = childrenStubs.size(); i < childrenStubsSize; i++) { StubElement childStub = childrenStubs.get(i); if (types.contains(childStub.getStubType())) count++; } return count; }
private static String createReplacementText(PyBinaryExpression expression) { PyExpression resultExpression; final PyExpression leftExpression = expression.getLeftExpression(); final PyExpression rightExpression = expression.getRightExpression(); boolean positiveCondition = !TokenSet.create(PyTokenTypes.NE, PyTokenTypes.NE_OLD).contains(expression.getOperator()); positiveCondition ^= isFalse(leftExpression) || isFalse(rightExpression) || isNull(rightExpression) || isNull(leftExpression) || isEmpty(rightExpression) || isEmpty(leftExpression); if (isTrue(leftExpression) || isFalse(leftExpression) || isNull(leftExpression) || isEmpty(leftExpression)) { resultExpression = rightExpression; } else { resultExpression = leftExpression; } return ((positiveCondition) ? "" : "not ") + resultExpression.getText(); }
@Nullable public static LighterASTNode firstChildOfType(@NotNull List<LighterASTNode> children, @NotNull TokenSet types) { for (int i = 0; i < children.size(); ++i) { LighterASTNode child = children.get(i); if (types.contains(child.getTokenType())) return child; } return null; }
@Override @Nullable public ASTNode findChildByType(@NotNull TokenSet typesSet, ASTNode anchor) { if (DebugUtil.CHECK_INSIDE_ATOMIC_ACTION_ENABLED){ ApplicationManager.getApplication().assertReadAccessAllowed(); } ASTNode child = anchor; while (true) { if (child == null) return null; if (typesSet.contains(child.getElementType())) return child; child = child.getTreeNext(); } }
public CommentsLiteralsSearchData(VirtualFile lastFile, Set<Language> relevantLanguages, SyntaxHighlighterOverEditorHighlighter highlighter, TokenSet tokensOfInterest, StringSearcher searcher, Matcher matcher, FindModel model) { this.lastFile = lastFile; this.highlighter = highlighter; this.tokensOfInterest = tokensOfInterest; this.searcher = searcher; this.matcher = matcher; this.relevantLanguages = relevantLanguages; this.model = model; }
@NotNull @Override public TokenSet getStatementTokens() { return TokenSet.create(EXPRESSION_STATEMENT, ASSIGNMENT_STATEMENT, AUG_ASSIGNMENT_STATEMENT, ASSERT_STATEMENT, BREAK_STATEMENT, CONTINUE_STATEMENT, DEL_STATEMENT, EXEC_STATEMENT, FOR_STATEMENT, FROM_IMPORT_STATEMENT, GLOBAL_STATEMENT, IMPORT_STATEMENT, IF_STATEMENT, PASS_STATEMENT, PRINT_STATEMENT, RAISE_STATEMENT, RETURN_STATEMENT, TRY_EXCEPT_STATEMENT, WITH_STATEMENT, WHILE_STATEMENT, NONLOCAL_STATEMENT, CLASS_DECLARATION, FUNCTION_DECLARATION); }
private boolean parseGenericStatement(PsiBuilder builder, TokenSet until) { boolean plainStatement = parseStatement(builder, false); if (plainStatement || parseExtendedStatement(builder)) { if (parseSeparatorsWithoutLastNls(builder, plainStatement, until)) { return false; } } else { builder.error(GroovyBundle.message("wrong.statement")); assert builder.getTokenType() != GroovyTokenTypes.mLCURLY && builder.getTokenType() != GroovyTokenTypes.mRCURLY; builder.advanceLexer(); } return true; }
@NotNull @Override public TokenSet getWhitespaceTokens() { return WHITE_SPACES; }
@NotNull @Override public TokenSet getCommentTokens() { return COMMENTS; }
@NotNull @Override public TokenSet getStringLiteralElements() { return STRINGS; }
@NotNull public TokenSet getWhitespaceTokens() { return WHITE_SPACES; }
@NotNull public TokenSet getCommentTokens() { return COMMENTS; }