@Override protected PsiFile createFile(@NotNull Language lang) { ParserDefinition parserDefinition = getDefinition(lang); if (parserDefinition == null) { return null; } if (lang.is(templateDataLanguage)) { PsiFileImpl file = (PsiFileImpl) parserDefinition.createFile(this); file.setContentElementType(templateDataLanguageType); return file; } else if (lang.isKindOf(baseLanguage)) { return parserDefinition.createFile(this); } else { return null; } }
@Override protected PsiFile createFile(@NotNull Language lang) { ParserDefinition parserDefinition = getDefinition(lang); if (parserDefinition == null) { return null; } if (lang.is(getTemplateDataLanguage())) { PsiFileImpl file = (PsiFileImpl) parserDefinition.createFile(this); file.setContentElementType(getTemplateDataElementType(getBaseLanguage())); return file; } else if (lang.isKindOf(getBaseLanguage())) { return parserDefinition.createFile(this); } else { return null; } }
@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; }
/** * Finds out whether the file minified by using common (not language-specific) heuristics. * Can be used for checking of css/less/scss/sass and js files. * * @param fileContent target file content * @param parserDefinition Parser definition of target language * @param noWSRequireAfterTokenSet TokenSet of types that doesn't require whitespaces after them. */ public static boolean isMinified(@NotNull CharSequence fileContent, @NotNull ParserDefinition parserDefinition, @NotNull TokenSet noWSRequireAfterTokenSet) { Lexer lexer = parserDefinition.createLexer(null); lexer.start(fileContent); if (!isMinified(lexer, parserDefinition, noWSRequireAfterTokenSet)) { return false; } else if (lexer.getTokenType() == null) { // whole file had been considered return true; } int startOffset = fileContent.length() - COUNT_OF_CONSIDERING_CHARACTERS_FROM_END_OF_FILE; if (startOffset <= 0) { return true; } lexer.start(fileContent, startOffset, fileContent.length()); return isMinified(lexer, parserDefinition, noWSRequireAfterTokenSet); }
@Override public boolean canHaveStub(VirtualFile file) { final FileType fileType = file.getFileType(); if (fileType instanceof LanguageFileType) { Language l = ((LanguageFileType)fileType).getLanguage(); ParserDefinition parserDefinition = LanguageParserDefinitions.INSTANCE.forLanguage(l); if (parserDefinition == null) return false; final IFileElementType elementType = parserDefinition.getFileNodeType(); return elementType instanceof IStubFileElementType && ((IStubFileElementType)elementType).shouldBuildStubFor(file); } else if (fileType.isBinary()) { final BinaryFileStubBuilder builder = BinaryFileStubBuilders.INSTANCE.forFileType(fileType); return builder != null && builder.acceptsFile(file); } return false; }
public static int getCumulativeVersion() { int version = VERSION; for (final FileType fileType : FileTypeRegistry.getInstance().getRegisteredFileTypes()) { if (fileType instanceof LanguageFileType) { Language l = ((LanguageFileType)fileType).getLanguage(); ParserDefinition parserDefinition = LanguageParserDefinitions.INSTANCE.forLanguage(l); if (parserDefinition != null) { final IFileElementType type = parserDefinition.getFileNodeType(); if (type instanceof IStubFileElementType) { version += ((IStubFileElementType)type).getStubVersion(); } } } BinaryFileStubBuilder builder = BinaryFileStubBuilders.INSTANCE.forFileType(fileType); if (builder != null) { version += builder.getStubVersion(); } } return version; }
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); }
public static boolean canHaveStub(@NotNull VirtualFile file) { final FileType fileType = file.getFileType(); if (fileType instanceof LanguageFileType) { final Language l = ((LanguageFileType)fileType).getLanguage(); final ParserDefinition parserDefinition = LanguageParserDefinitions.INSTANCE.forLanguage(l); if (parserDefinition == null) { return false; } final IFileElementType elementType = parserDefinition.getFileNodeType(); if (elementType instanceof IStubFileElementType) { if (((IStubFileElementType)elementType).shouldBuildStubFor(file)) { return true; } if (IndexingStamp.isFileIndexedStateCurrent(file, INDEX_ID)) { return true; } } } final BinaryFileStubBuilder builder = BinaryFileStubBuilders.INSTANCE.forFileType(fileType); return builder != null && builder.acceptsFile(file); }
private static boolean processStringLiteralsContainingIdentifier(@NotNull String identifier, @NotNull SearchScope searchScope, PsiSearchHelper helper, final Processor<PsiElement> processor) { TextOccurenceProcessor occurenceProcessor = new TextOccurenceProcessor() { @Override public boolean execute(@NotNull PsiElement element, int offsetInElement) { final ParserDefinition definition = LanguageParserDefinitions.INSTANCE.forLanguage(element.getLanguage()); final ASTNode node = element.getNode(); if (definition != null && node != null && definition.getStringLiteralElements().contains(node.getElementType())) { return processor.process(element); } return true; } }; return helper.processElementsWithWord(occurenceProcessor, searchScope, identifier, UsageSearchContext.IN_STRINGS, true); }
private static void registerProblemStart(@NotNull final AnnotationHolder holder, @NotNull final XmlTag tag, @NotNull final XmlToken start, @NotNull final XmlToken end) { PsiElement context = tag.getContainingFile().getContext(); if (context != null) { ParserDefinition parserDefinition = LanguageParserDefinitions.INSTANCE.forLanguage(context.getLanguage()); if (parserDefinition != null) { ASTNode contextNode = context.getNode(); if (contextNode != null && contextNode.getChildren(parserDefinition.getStringLiteralElements()) != null) { // TODO: we should check for concatenations here return; } } } final String tagName = tag.getName(); final String endTokenText = end.getText(); final RenameTagBeginOrEndIntentionAction renameEndAction = new RenameTagBeginOrEndIntentionAction(tagName, endTokenText, false); final RenameTagBeginOrEndIntentionAction renameStartAction = new RenameTagBeginOrEndIntentionAction(endTokenText, tagName, true); final Annotation annotation = holder.createErrorAnnotation(start, XmlErrorMessages.message("tag.has.wrong.closing.tag.name")); annotation.registerFix(renameEndAction); annotation.registerFix(renameStartAction); }
private static void registerProblemEnd(@NotNull final AnnotationHolder holder, @NotNull final XmlTag tag, @NotNull final XmlToken end) { PsiElement context = tag.getContainingFile().getContext(); if (context != null) { ParserDefinition parserDefinition = LanguageParserDefinitions.INSTANCE.forLanguage(context.getLanguage()); if (parserDefinition != null) { ASTNode contextNode = context.getNode(); if (contextNode != null && contextNode.getChildren(parserDefinition.getStringLiteralElements()) != null) { // TODO: we should check for concatenations here return; } } } final String tagName = tag.getName(); final String endTokenText = end.getText(); final RenameTagBeginOrEndIntentionAction renameEndAction = new RenameTagBeginOrEndIntentionAction(tagName, endTokenText, false); final RenameTagBeginOrEndIntentionAction renameStartAction = new RenameTagBeginOrEndIntentionAction(endTokenText, tagName, true); final Annotation annotation = holder.createErrorAnnotation(end, XmlErrorMessages.message("wrong.closing.tag.name")); annotation.registerFix(new RemoveExtraClosingTagIntentionAction()); annotation.registerFix(renameEndAction); annotation.registerFix(renameStartAction); }
private ASTNode createAST(String text) { ParserDefinition definition = new BuildParserDefinition(); PsiParser parser = definition.createParser(getProject()); Lexer lexer = definition.createLexer(getProject()); PsiBuilderImpl psiBuilder = new PsiBuilderImpl( getProject(), null, definition, lexer, new CharTableImpl(), text, null, null); PsiBuilderAdapter adapter = new PsiBuilderAdapter(psiBuilder) { @Override public void error(String messageText) { super.error(messageText); errors.add(messageText); } }; return parser.parse(definition.getFileNodeType(), adapter); }
/** * Creates the IDEA language object associated with the specified language. * * @param language * The language. * @return The created IDEA language. */ private LanguageBindings createLanguageBindings(final ILanguage language) { final MetaborgIdeaLanguage ideaLanguage = createIdeaLanguage(language); final MetaborgLanguageFileType fileType = createFileType(ideaLanguage); final SpoofaxTokenTypeManager tokenTypeManager = createTokenTypeManager(ideaLanguage); final IFileElementType fileElementType = createFileElementType(ideaLanguage, tokenTypeManager); final ParserDefinition parserDefinition = createParserDefinition(fileType, fileElementType); final SpoofaxSyntaxHighlighterFactory syntaxHighlighterFactory = createSyntaxHighlighterFactory(); final InstanceLanguageExtensionPoint<?> parserDefinitionExtension = new InstanceLanguageExtensionPoint<>(ExtensionIds.ParserDefinition, ideaLanguage, parserDefinition); final InstanceSyntaxHighlighterFactoryExtensionPoint syntaxHighlighterFactoryExtension = new InstanceSyntaxHighlighterFactoryExtensionPoint(ideaLanguage, syntaxHighlighterFactory); final InstanceLanguageExtensionPoint<?> externalAnnotatorExtension = new InstanceLanguageExtensionPoint<>( ExtensionIds.ExternalAnnotator, ideaLanguage, this.metaborgSourceAnnotator); return new LanguageBindings(tokenTypeManager, fileType, parserDefinitionExtension, syntaxHighlighterFactoryExtension, externalAnnotatorExtension); }
private static boolean isInsideStringLiteral(final Editor editor, final 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; }
public static boolean canHaveStub(@NotNull VirtualFile file) { final FileType fileType = file.getFileType(); if (fileType instanceof LanguageFileType) { final Language l = ((LanguageFileType)fileType).getLanguage(); final ParserDefinition parserDefinition = LanguageParserDefinitions.INSTANCE.forLanguage(l); if (parserDefinition == null) { return false; } final IFileElementType elementType = parserDefinition.getFileNodeType(); if (elementType instanceof IStubFileElementType) { if (((IStubFileElementType)elementType).shouldBuildStubFor(file)) { return true; } final ID indexId = IndexInfrastructure.getStubId(INDEX_ID, file.getFileType()); if (IndexingStamp.isFileIndexed(file, indexId, IndexInfrastructure.getIndexCreationStamp(indexId))) { return true; } } } final BinaryFileStubBuilder builder = BinaryFileStubBuilders.INSTANCE.forFileType(fileType); return builder != null && builder.acceptsFile(file); }
private static Map<FileType, Integer> computeVersionMap() { Map<FileType, Integer> map = new HashMap<FileType, Integer>(); for (final FileType fileType : FileTypeManager.getInstance().getRegisteredFileTypes()) { if (fileType instanceof LanguageFileType) { Language l = ((LanguageFileType)fileType).getLanguage(); ParserDefinition parserDefinition = LanguageParserDefinitions.INSTANCE.forLanguage(l); if (parserDefinition != null) { final IFileElementType type = parserDefinition.getFileNodeType(); if (type instanceof IStubFileElementType) { map.put(fileType, ((IStubFileElementType)type).getStubVersion()); } } } final BinaryFileStubBuilder builder = BinaryFileStubBuilders.INSTANCE.forFileType(fileType); if (builder != null) { map.put(fileType, builder.getStubVersion()); } } return map; }
@Override public boolean acceptInput(final VirtualFile file) { if (!(file.getFileSystem() instanceof LocalFileSystem)) { 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; }
private static boolean processStringLiteralsContainingIdentifier(@NotNull String identifier, @NotNull SearchScope searchScope, PsiSearchHelper helper, final Processor<PsiElement> processor) { TextOccurenceProcessor occurenceProcessor = new TextOccurenceProcessor() { @Override public boolean execute(PsiElement element, int offsetInElement) { final ParserDefinition definition = LanguageParserDefinitions.INSTANCE.forLanguage(element.getLanguage()); final ASTNode node = element.getNode(); if (definition != null && node != null && definition.getStringLiteralElements().contains(node.getElementType())) { return processor.process(element); } return true; } }; return helper.processElementsWithWord(occurenceProcessor, searchScope, identifier, UsageSearchContext.IN_STRINGS, true); }
/** Builds a new instance of {@link IgnoreFile}. */ public IgnoreFile(@NotNull FileViewProvider viewProvider, @NotNull IgnoreFileType fileType) { super(viewProvider); this.fileType = fileType; this.language = findLanguage(fileType.getLanguage(), viewProvider); final ParserDefinition parserDefinition = LanguageParserDefinitions.INSTANCE.forLanguage(this.language); if (parserDefinition == null) { throw new RuntimeException( "PsiFileBase: language.getParserDefinition() returned null for: " + this.language ); } this.parserDefinition = parserDefinition; final IFileElementType nodeType = parserDefinition.getFileNodeType(); init(nodeType, nodeType); }
@Override protected PsiFile createFile(@NotNull Language lang) { ParserDefinition parserDefinition = LanguageParserDefinitions.INSTANCE.forLanguage(lang); if (parserDefinition == null) { return null; } Language templateDataLanguage = getTemplateDataLanguage(myManager, myFile); if (lang == templateDataLanguage) { PsiFileImpl file = (PsiFileImpl) parserDefinition.createFile(this); file.setContentElementType(new TemplateDataElementType("Dot_TEMPLATE_DATA", templateDataLanguage, DotTokenTypes.CONTENT, DotTokenTypes.OUTER_ELEMENT_TYPE)); return file; } else if (lang == DotLanguage.INSTANCE) { return parserDefinition.createFile(this); } else { return null; } }
@Nonnull @Override public LeafElement createLeaf(@Nonnull IElementType type, @Nonnull LanguageVersion languageVersion, @Nonnull CharSequence text) { final ParserDefinition parserDefinition = LanguageParserDefinitions.INSTANCE.forLanguage(type.getLanguage()); if(parserDefinition != null) { if(parserDefinition.getCommentTokens(languageVersion).contains(type)) { return new PsiCoreCommentImpl(type, text); } } // this is special case, then type is WHITE_SPACE, but no parser definition if(type == TokenType.WHITE_SPACE) { return new PsiWhiteSpaceImpl(text); } if (type instanceof ILeafElementType) { return (LeafElement)((ILeafElementType)type).createLeafNode(text); } return new LeafPsiElement(type, text); }
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(LanguageVersionUtil.findLanguageVersion(language, myFile)); } if (comments == null) { return false; } else { myComments.put(language, comments); } } return comments.contains(tokenType); }
public static boolean canHaveStub(@Nonnull VirtualFile file) { final FileType fileType = file.getFileType(); if (fileType instanceof LanguageFileType) { final Language l = ((LanguageFileType)fileType).getLanguage(); final ParserDefinition parserDefinition = LanguageParserDefinitions.INSTANCE.forLanguage(l); if (parserDefinition == null) { return false; } final IFileElementType elementType = parserDefinition.getFileNodeType(); if (elementType instanceof IStubFileElementType) { if (((IStubFileElementType)elementType).shouldBuildStubFor(file)) { return true; } FileBasedIndex fileBasedIndex = FileBasedIndex.getInstance(); if (file instanceof NewVirtualFile && fileBasedIndex instanceof FileBasedIndexImpl && ((FileBasedIndexImpl)fileBasedIndex).getIndex(INDEX_ID).isIndexedStateForFile(((NewVirtualFile)file).getId(), file)) { return true; } } } final BinaryFileStubBuilder builder = BinaryFileStubBuilders.INSTANCE.forFileType(fileType); return builder != null && builder.acceptsFile(file); }
private static Object getVersionOwner(FileType fileType) { Object owner = null; if (fileType instanceof LanguageFileType) { Language l = ((LanguageFileType)fileType).getLanguage(); ParserDefinition parserDefinition = LanguageParserDefinitions.INSTANCE.forLanguage(l); if (parserDefinition != null) { final IFileElementType type = parserDefinition.getFileNodeType(); if (type instanceof IStubFileElementType) { owner = type; } } } BinaryFileStubBuilder builder = BinaryFileStubBuilders.INSTANCE.forFileType(fileType); if (builder != null) { owner = builder; } return owner; }
private static boolean processStringLiteralsContainingIdentifier(@Nonnull String identifier, @Nonnull SearchScope searchScope, PsiSearchHelper helper, final Processor<PsiElement> processor) { TextOccurenceProcessor occurenceProcessor = new TextOccurenceProcessor() { @Override public boolean execute(PsiElement element, int offsetInElement) { final ParserDefinition definition = LanguageParserDefinitions.INSTANCE.forLanguage(element.getLanguage()); final ASTNode node = element.getNode(); if (definition != null && node != null && definition.getStringLiteralElements(element.getLanguageVersion()).contains(node.getElementType())) { return processor.process(element); } return true; } }; return helper.processElementsWithWord(occurenceProcessor, searchScope, identifier, UsageSearchContext.IN_STRINGS, true); }
private static ASTNode insertWhitespaceIfNeeded(ASTNode anchorNode, final ASTNode elementNode, final ASTNode parentNode, final ASTNode insertionPlaceNode) throws IncorrectOperationException { ParserDefinition parserDef = LanguageParserDefinitions.INSTANCE.forLanguage(parentNode.getPsi().getLanguage()); final TokenSet comments = parserDef.getCommentTokens(parentNode.getPsi().getLanguage().getVersions()[0]); final TokenSet whitespaces = parserDef.getWhitespaceTokens(parentNode.getPsi().getLanguage().getVersions()[0]); if(anchorNode != null && ((!whitespaces.contains(anchorNode.getElementType()) && !whitespaces.contains(elementNode.getElementType())) || comments.contains(anchorNode.getElementType()) || comments.contains(elementNode.getElementType()) || elementNode.getPsi() instanceof PsiComment)) { String commentString = " "; if(comments.contains(anchorNode.getElementType()) || comments.contains(elementNode.getElementType()) || elementNode.getPsi() instanceof PsiComment) { commentString = "\n"; } final ASTNode wsNode = PsiParserFacade.SERVICE.getInstance(parentNode.getPsi().getProject()).createWhiteSpaceFromText(commentString).getNode(); parentNode.addChild(wsNode, insertionPlaceNode); anchorNode = wsNode; } return anchorNode; }
private ParserDefinition getDefinition(Language lang) { ParserDefinition parserDefinition; if (lang.isKindOf(getBaseLanguage())) { parserDefinition = LanguageParserDefinitions.INSTANCE.forLanguage(lang.is(getBaseLanguage()) ? lang : getBaseLanguage()); } else { parserDefinition = LanguageParserDefinitions.INSTANCE.forLanguage(lang); } return parserDefinition; }
@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 static boolean isComment(final ASTNode node) { final PsiElement psiElement = SourceTreeToPsiMap.treeElementToPsi(node); if (psiElement instanceof PsiComment) return true; final ParserDefinition parserDefinition = LanguageParserDefinitions.INSTANCE.forLanguage(psiElement.getLanguage()); if (parserDefinition == null) return false; final TokenSet commentTokens = parserDefinition.getCommentTokens(); return commentTokens.contains(node.getElementType()); }
@Nullable public static DataIndexer<TodoIndexEntry, Integer, FileContent> getTodoIndexer(FileType fileType, final VirtualFile virtualFile) { final DataIndexer<TodoIndexEntry, Integer, FileContent> indexer = ourTodoIndexers.get(fileType); if (indexer != null) { return indexer; } final DataIndexer<TodoIndexEntry, Integer, FileContent> extIndexer; if (fileType instanceof SubstitutedFileType && !((SubstitutedFileType)fileType).isSameFileType()) { SubstitutedFileType sft = (SubstitutedFileType)fileType; extIndexer = new CompositeTodoIndexer(getTodoIndexer(sft.getOriginalFileType(), virtualFile), getTodoIndexer(sft.getFileType(), virtualFile)); } else { extIndexer = TodoIndexers.INSTANCE.forFileType(fileType); } if (extIndexer != null) { return extIndexer; } 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; if (commentTokens != null) { return new TokenSetTodoIndexer(commentTokens, virtualFile); } } if (fileType instanceof CustomSyntaxTableFileType) { return new TokenSetTodoIndexer(ABSTRACT_FILE_COMMENT_TOKENS, virtualFile); } return null; }
public static boolean isInStringLiteral(PsiElement element) { ParserDefinition definition = LanguageParserDefinitions.INSTANCE.forLanguage(PsiUtilCore.findLanguageFromElement(element)); if (definition == null) { return false; } return isStringLiteral(element, definition) || isStringLiteral(element.getParent(), definition) || isStringLiteralWithError(element, definition) || isStringLiteralWithError(element.getParent(), definition); }
protected boolean isStringLiteral(PsiElement element) { if (element == null) return false; final ASTNode astNode = element.getNode(); if (astNode == null) { return false; } final IElementType elementType = astNode.getElementType(); final ParserDefinition parserDefinition = LanguageParserDefinitions.INSTANCE.forLanguage(element.getLanguage()); if (parserDefinition != null) { final TokenSet literals = parserDefinition.getStringLiteralElements(); return literals.contains(elementType); } return false; }
private void doVisitElement(PsiElement element) { CompiledPattern pattern = myGlobalVisitor.getContext().getPattern(); if (myGlobalVisitor.getCodeBlockLevel() == 0) { initTopLevelElement(element); return; } if (canBePatternVariable(element) && pattern.isRealTypedVar(element)) { myGlobalVisitor.handle(element); final MatchingHandler handler = pattern.getHandler(element); handler.setFilter(new NodeFilter() { public boolean accepts(PsiElement other) { return canBePatternVariableValue(other); } }); super.visitElement(element); return; } super.visitElement(element); if (myGlobalVisitor.getContext().getSearchHelper().doOptimizing() && element instanceof LeafElement) { ParserDefinition parserDefinition = LanguageParserDefinitions.INSTANCE.forLanguage(element.getLanguage()); if (parserDefinition != null) { String text = element.getText(); // todo: support variables inside comments boolean flag = true; if (StringUtil.isJavaIdentifier(text) && flag) { myGlobalVisitor.processTokenizedName(text, true, GlobalCompilingVisitor.OccurenceKind.CODE); } } } }
@Nullable protected PsiFile createFile(@NotNull Language lang) { if (lang != getBaseLanguage()) return null; final ParserDefinition parserDefinition = LanguageParserDefinitions.INSTANCE.forLanguage(lang); if (parserDefinition != null) { return parserDefinition.createFile(this); } return null; }
public static boolean isInComments(final IElementType tokenType) { final Language language = tokenType.getLanguage(); boolean inComments = false; final ParserDefinition parserDefinition = LanguageParserDefinitions.INSTANCE.forLanguage(language); if (parserDefinition != null) { final TokenSet commentTokens = parserDefinition.getCommentTokens(); if (commentTokens.contains(tokenType)) { inComments = true; } } return inComments; }
private static TokenSet addTokenTypesForLanguage(FindModel model, Language lang, TokenSet tokensOfInterest) { ParserDefinition definition = LanguageParserDefinitions.INSTANCE.forLanguage(lang); if (definition != null) { tokensOfInterest = TokenSet.orSet(tokensOfInterest, model.isInCommentsOnly() ? definition.getCommentTokens(): TokenSet.EMPTY); tokensOfInterest = TokenSet.orSet(tokensOfInterest, model.isInStringLiteralsOnly() ? definition.getStringLiteralElements() : TokenSet.EMPTY); } return tokensOfInterest; }
public ASTNode generateWhitespaceBetweenTokens(ASTNode left, ASTNode right) { PsiManager manager = right.getTreeParent().getPsi().getManager(); if (left.getElementType() == PyTokenTypes.END_OF_LINE_COMMENT) { return createLineBreak(manager); } if (left.getPsi().isValid() && right.getPsi().isValid()) { final PsiElement commonParent = PsiTreeUtil.findCommonParent(left.getPsi(), right.getPsi()); if (commonParent == null) return null; final PsiElement leftPrevAncestor = PsiTreeUtil.findPrevParent(commonParent, left.getPsi()); final PsiElement rightPrevAncestor = PsiTreeUtil.findPrevParent(commonParent, right.getPsi()); if (isStatementOrFunction(leftPrevAncestor) && isStatementOrFunction(rightPrevAncestor)) { int leftIndent = PyPsiUtils.getElementIndentation(leftPrevAncestor); int rightIndent = PyPsiUtils.getElementIndentation(rightPrevAncestor); int maxIndent = Math.max(leftIndent, rightIndent); return createWhitespace(manager, "\n" + StringUtil.repeatSymbol(' ', maxIndent)); } } if (right.getElementType() == PyTokenTypes.DEF_KEYWORD || right.getElementType() == PyTokenTypes.CLASS_KEYWORD) { return createLineBreak(manager); } if (left.getElementType() == TokenType.WHITE_SPACE || right.getElementType() == TokenType.WHITE_SPACE) { return null; } final PyStatement leftStatement = PsiTreeUtil.getParentOfType(left.getPsi(), PyStatement.class); if (leftStatement != null && !PsiTreeUtil.isAncestor(leftStatement, right.getPsi(), false)) { return createLineBreak(manager); } final Lexer lexer = new PythonIndentingLexer(); if (LanguageUtil.canStickTokensTogetherByLexer(left, right, lexer) == ParserDefinition.SpaceRequirements.MUST) { return createSpace(manager); } return null; }
public static boolean isLiteralExpression(@Nullable PsiElement context) { if (context == null) return false; final ASTNode astNode = context.getNode(); if (astNode == null) { return false; } final IElementType elementType = astNode.getElementType(); final ParserDefinition parserDefinition = LanguageParserDefinitions.INSTANCE.forLanguage(context.getLanguage()); return parserDefinition.getStringLiteralElements().contains(elementType); }