@NotNull @Override public Map<String, Set<String>> map(@NotNull FileContent inputData) { final Map<String, Set<String>> indexMap = new HashMap<>(); if (inputData.getFileType().isBinary()) { return indexMap; } final PsiFile file = inputData.getPsiFile(); if (fileDetector.isMainOpenApiFile(file)) { Set<String> partialOpenApiFileNames = getPartialOpenApiFileNames(file); indexMap.put(OpenApiFileIndex.PARTIAL_OPEN_API_FILES, partialOpenApiFileNames); indexMap.put(OpenApiFileIndex.MAIN_OPEN_API_FILE, ImmutableSet.of(file.getName() + DELIMITER + OpenApiFileType.MAIN)); } return indexMap; }
@NotNull @Override public Map<String, Set<String>> map(@NotNull FileContent inputData) { final Map<String, Set<String>> indexMap = new HashMap<>(); if (inputData.getFileType().isBinary()) { return indexMap; } final PsiFile file = inputData.getPsiFile(); if (fileDetector.isMainSwaggerFile(file)) { Set<String> partialSwaggerFileNames = getPartialSwaggerFileNames(file); indexMap.put(SwaggerFileIndex.PARTIAL_SWAGGER_FILES, partialSwaggerFileNames); indexMap.put(SwaggerFileIndex.MAIN_SWAGGER_FILE, ImmutableSet.of(file.getName() + DELIMITER + SwaggerFileType.MAIN)); } return indexMap; }
@NotNull @Override public Map<TodoIndexEntry, Integer> map(@NotNull FileContent inputData) { Map<TodoIndexEntry, Integer> result = ContainerUtil.newTroveMap(); for (DataIndexer<TodoIndexEntry, Integer, FileContent> indexer : indexers) { for (Map.Entry<TodoIndexEntry, Integer> entry : indexer.map(inputData).entrySet()) { TodoIndexEntry key = entry.getKey(); if (result.containsKey(key)) { result.put(key, result.get(key) + entry.getValue()); } else { result.put(key, entry.getValue()); } } } return result; }
@Override @NotNull public Map<IdIndexEntry, Integer> map(@NotNull final FileContent inputData) { final IdDataConsumer consumer = new IdDataConsumer(); final CharSequence chars = inputData.getContentAsText(); IdTableBuilding.scanWords(new IdTableBuilding.ScanWordProcessor() { @Override public void run(final CharSequence chars11, @Nullable char[] charsArray, final int start, final int end) { if (charsArray != null) { consumer.addOccurrence(charsArray, start, end, (int)UsageSearchContext.IN_PLAIN_TEXT); } else { consumer.addOccurrence(chars11, start, end, (int)UsageSearchContext.IN_PLAIN_TEXT); } } }, chars, 0, chars.length()); return consumer.getResult(); }
@Override public ObjectStubTree readOrBuild(Project project, VirtualFile vFile, @Nullable PsiFile psiFile) { if (!canHaveStub(vFile)) { return null; } try { final FileContent fc = new FileContentImpl(vFile, vFile.contentsToByteArray()); fc.putUserData(IndexingDataKeys.PROJECT, project); final Stub element = StubTreeBuilder.buildStubTree(fc); if (element instanceof PsiFileStub) { return new StubTree((PsiFileStub)element); } } catch (IOException e) { throw new RuntimeException(e); } return null; }
private AppEngineFlexPattern withAppEngineFlexYamlContent() { return with( new PatternCondition<FileContent>("with-appengine-java-flexible") { @Override public boolean accepts(@NotNull FileContent fileContent, ProcessingContext context) { // checks for flex engine file names and then checks for required configuration line // inside. boolean nameMatch = APP_ENGINE_FLEX_CONFIG_FILES.contains(fileContent.getFileName()); if (nameMatch) { Scanner scanner = new Scanner(fileContent.getContentAsText().toString()); while (scanner.hasNextLine()) { if (scanner.nextLine().startsWith(APP_ENGINE_FLEX_REQUIRED_YAML_CONTENT)) { return true; } } } return false; } }); }
@Override @NotNull public Map<String, Void> map(@NotNull final FileContent inputData) { if (!PantsUtil.isPantsProject(inputData.getProject())) { return Collections.emptyMap(); } final VirtualFile file = inputData.getFile(); Optional<VirtualFile> buildRoot = PantsUtil.findBuildRoot(file); if (!buildRoot.isPresent()) { return Collections.emptyMap(); } String relative = FileUtil.getRelativePath(buildRoot.get().getPath(), file.getParent().getPath(), File.separatorChar); if (relative == null || relative.isEmpty()) { return Collections.emptyMap(); } else { return Collections.singletonMap(relative, null); } }
public static boolean isValidForIndex(@NotNull FileContent inputData, @NotNull PsiFile psiFile) { String fileName = psiFile.getName(); if(fileName.startsWith(".") || fileName.endsWith("Test")) { return false; } VirtualFile baseDir = inputData.getProject().getBaseDir(); if(baseDir == null) { return false; } // is Test file in path name String relativePath = VfsUtil.getRelativePath(inputData.getFile(), baseDir, '/'); if(relativePath != null && (relativePath.contains("/Test/") || relativePath.contains("/Fixtures/"))) { return false; } return true; }
@Override public StubElement buildStubTree(FileContent fileContent) { try { VirtualFile file = fileContent.getFile(); Project project = fileContent.getProject(); byte[] content = fileContent.getContent(); final ClsStubBuilderFactory[] factories = Extensions.getExtensions(ClsStubBuilderFactory.EP_NAME); for (ClsStubBuilderFactory factory : factories) { if (!factory.isInnerClass(file) && factory.canBeProcessed(file, content)) { PsiFileStub stub = factory.buildFileStub(file, content, project); if (stub != null) return stub; } } if (!fileContent.getFileName().contains("$")) { LOG.info("No stub built for file " + fileContent); } return null; } catch (ClsFormatException e) { return null; } }
@Override @NotNull public Map<IdIndexEntry, Integer> map(final FileContent inputData) { final IdDataConsumer consumer = new IdDataConsumer(); final CharSequence chars = inputData.getContentAsText(); scanWords(new ScanWordProcessor() { @Override public void run(final CharSequence chars11, @Nullable char[] charsArray, final int start, final int end) { if (charsArray != null) { consumer.addOccurrence(charsArray, start, end, (int)UsageSearchContext.IN_PLAIN_TEXT); } else { consumer.addOccurrence(chars11, start, end, (int)UsageSearchContext.IN_PLAIN_TEXT); } } }, chars, 0, chars.length()); return consumer.getResult(); }
@NotNull @Override public Map<TodoIndexEntry, Integer> map(FileContent inputData) { Map<TodoIndexEntry, Integer> result = ContainerUtil.newTroveMap(); for (DataIndexer<TodoIndexEntry, Integer, FileContent> indexer : indexers) { for (Map.Entry<TodoIndexEntry, Integer> entry : indexer.map(inputData).entrySet()) { TodoIndexEntry key = entry.getKey(); if (result.containsKey(key)) { result.put(key, result.get(key) + entry.getValue()); } else { result.put(key, entry.getValue()); } } } return result; }
/** * Maps indexed files content to the {@link IgnoreEntryOccurrence}. * * @param inputData indexed file data * @return {@link IgnoreEntryOccurrence} data mapped with {@link IgnoreFileTypeKey} */ @NotNull @Override public Map<IgnoreFileTypeKey, IgnoreEntryOccurrence> map(@NotNull final FileContent inputData) { if (!(inputData.getPsiFile() instanceof IgnoreFile)) { return Collections.emptyMap(); } final IgnoreEntryOccurrence result = new IgnoreEntryOccurrence(inputData.getFile()); final IgnoreFileType type = (IgnoreFileType) inputData.getFileType(); IgnoreFile ignoreFile = (IgnoreFile) inputData.getPsiFile(); ignoreFile.acceptChildren(new IgnoreVisitor() { @Override public void visitEntry(@NotNull IgnoreEntry entry) { final Pattern pattern = Glob.createPattern(entry); if (pattern != null) { result.add(pattern.matcher(""), entry.isNegated()); } } }); return Collections.singletonMap(new IgnoreFileTypeKey(type), result); }
public static boolean isValidForIndex(FileContent inputData) { String fileName = inputData.getPsiFile().getName(); if(fileName.startsWith(".") || fileName.contains("Test")) { return false; } // we check for project path, on no match we are properly inside external library paths String relativePath = VfsUtil.getRelativePath(inputData.getFile(), inputData.getProject().getBaseDir(), '/'); if(relativePath == null) { return true; } // is Test file in path name return !(relativePath.contains("\\Test\\") || relativePath.contains("\\Fixtures\\")); }
@NotNull @Override public Map<String, String> map(FileContent inputData) { Map<String, String> result = new THashMap<String, String>(); for (PsiElement child : inputData.getPsiFile().getChildren()) { if (child instanceof ThriftTopLevelDeclaration) { String topLevelName = ((ThriftTopLevelDeclaration)child).getName(); if (topLevelName != null) { for (ThriftDeclaration declaration : ((ThriftTopLevelDeclaration)child).findSubDeclarations()) { String subName = declaration.getName(); if (subName != null) { result.put(subName, topLevelName); } } } } } return result; }
@Nonnull @Override public Map<TodoIndexEntry, Integer> map(FileContent inputData) { Map<TodoIndexEntry, Integer> result = ContainerUtil.newTroveMap(); for (DataIndexer<TodoIndexEntry, Integer, FileContent> indexer : indexers) { for (Map.Entry<TodoIndexEntry, Integer> entry : indexer.map(inputData).entrySet()) { TodoIndexEntry key = entry.getKey(); if (result.containsKey(key)) { result.put(key, result.get(key) + entry.getValue()); } else { result.put(key, entry.getValue()); } } } return result; }
@Override @Nonnull public Map<IdIndexEntry, Integer> map(@Nonnull final FileContent inputData) { final IdDataConsumer consumer = new IdDataConsumer(); final CharSequence chars = inputData.getContentAsText(); IdTableBuilding.scanWords(new IdTableBuilding.ScanWordProcessor() { @Override public void run(final CharSequence chars11, @Nullable char[] charsArray, final int start, final int end) { if (charsArray != null) { consumer.addOccurrence(charsArray, start, end, (int)UsageSearchContext.IN_PLAIN_TEXT); } else { consumer.addOccurrence(chars11, start, end, (int)UsageSearchContext.IN_PLAIN_TEXT); } } }, chars, 0, chars.length()); return consumer.getResult(); }
@NotNull @Override public ElementPattern<FileContent> createSuitableFilePattern() { return FileContentPattern.fileContent().andOr( FileContentPattern.fileContent().withName("mule-app.properties"), FileContentPattern.fileContent().withName("mule-domain-config.xml")); // new FileContent() // return FileContentPattern.fileContent().oneOf(FileContentPattern.fileContent().withName("mule-app.properties")., FileContentPattern.fileContent().withName("mule-domain-config.xml")); //.withName("mule-app.properties")..withName("mule-domain-config.xml"); }
@NotNull @Override public Map<String, Void> map(@NotNull FileContent fileContent) { PsiFile psiFile = fileContent.getPsiFile(); if (NeosProjectComponent.isEnabledForIndex(psiFile.getProject()) && isValidForIndex(fileContent, psiFile)) { return doIndex(fileContent); } return Collections.emptyMap(); }
@NotNull private Map<String, Void> doIndex(FileContent fileContent) { Map<String, Void> result = new HashMap<>(); YAMLUtil.getTopLevelKeys((YAMLFile)fileContent.getPsiFile()) .forEach(yamlKeyValue -> result.put(yamlKeyValue.getKeyText(), null)); return result; }
private static boolean isValidForIndex(FileContent inputData, PsiFile psiFile) { if (inputData.getFile().getLength() > MAX_FILE_BYTE_SIZE) { return false; } return psiFile.getName().startsWith("NodeTypes."); }
@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 ScanContent scanContent(FileContent content, IdAndToDoScannerBasedOnFilterLexer indexer) { ScanContent data = content.getUserData(scanContentKey); if (data != null) { content.putUserData(scanContentKey, null); return data; } final boolean needTodo = content.getFile().isInLocalFileSystem(); // same as TodoIndex.getFilter().isAcceptable final boolean needIdIndex = IdTableBuilding.getFileTypeIndexer(content.getFileType()) instanceof LexerBasedIdIndexer; final IdDataConsumer consumer = needIdIndex? new IdDataConsumer():null; final OccurrenceConsumer todoOccurrenceConsumer = new OccurrenceConsumer(consumer, needTodo); final Lexer filterLexer = indexer.createLexer(todoOccurrenceConsumer); filterLexer.start(content.getContentAsText()); while (filterLexer.getTokenType() != null) filterLexer.advance(); Map<TodoIndexEntry,Integer> todoMap = null; if (needTodo) { for (IndexPattern indexPattern : IndexPatternUtil.getIndexPatterns()) { final int count = todoOccurrenceConsumer.getOccurrenceCount(indexPattern); if (count > 0) { if (todoMap == null) todoMap = new THashMap<TodoIndexEntry, Integer>(); todoMap.put(new TodoIndexEntry(indexPattern.getPatternString(), indexPattern.isCaseSensitive()), count); } } } data = new ScanContent( consumer != null? consumer.getResult():Collections.<IdIndexEntry, Integer>emptyMap(), todoMap != null ? todoMap: Collections.<TodoIndexEntry,Integer>emptyMap() ); if (needIdIndex && needTodo) content.putUserData(scanContentKey, data); return data; }
@Override @NotNull public Map<IdIndexEntry, Integer> map(@NotNull final FileContent inputData) { final CharSequence chars = inputData.getContentAsText(); final char[] charsArray = CharArrayUtil.fromSequenceWithoutCopying(chars); final IdDataConsumer consumer = new IdDataConsumer(); myScanner.processWords(chars, new Processor<WordOccurrence>() { @Override public boolean process(final WordOccurrence t) { if (charsArray != null && t.getBaseText() == chars) { consumer.addOccurrence(charsArray, t.getStart(), t.getEnd(), convertToMask(t.getKind())); } else { consumer.addOccurrence(t.getBaseText(), t.getStart(), t.getEnd(), convertToMask(t.getKind())); } return true; } private int convertToMask(final WordOccurrence.Kind kind) { if (kind == null) { return UsageSearchContext.ANY; } if (kind == WordOccurrence.Kind.CODE) return UsageSearchContext.IN_CODE; if (kind == WordOccurrence.Kind.COMMENTS) return UsageSearchContext.IN_COMMENTS; if (kind == WordOccurrence.Kind.LITERALS) return UsageSearchContext.IN_STRINGS; if (kind == WordOccurrence.Kind.FOREIGN_LANGUAGE) return UsageSearchContext.IN_FOREIGN_LANGUAGES; return 0; } }); return consumer.getResult(); }
public FileContentPattern withName(@NotNull final String name) { return with(new PatternCondition<FileContent>("withName") { @Override public boolean accepts(@NotNull FileContent fileContent, ProcessingContext context) { return name.equals(fileContent.getFileName()); } }); }
public FileContentPattern withName(final StringPattern namePattern) { return with(new PatternCondition<FileContent>("withName") { @Override public boolean accepts(@NotNull FileContent fileContent, ProcessingContext context) { return namePattern.accepts(fileContent.getFileName()); } }); }
public FileContentPattern inDirectory(final @NotNull String name) { return with(new PatternCondition<FileContent>("inDirectory") { @Override public boolean accepts(@NotNull FileContent fileContent, ProcessingContext context) { return name.equals(fileContent.getFile().getParent().getName()); } }); }
public FileContentPattern xmlWithRootTag(@NotNull final String rootTag) { return with(new PatternCondition<FileContent>("withRootTag") { @Override public boolean accepts(@NotNull FileContent fileContent, ProcessingContext context) { try { return rootTag.equals(parseHeaderWithException(fileContent).getRootTagLocalName()); } catch (IOException e) { return false; } } }); }
public FileContentPattern xmlWithRootTagNamespace(final ElementPattern<String> namespacePattern) { return with(new PatternCondition<FileContent>("xmlWithRootTagNamespace") { @Override public boolean accepts(@NotNull final FileContent fileContent, final ProcessingContext context) { try { String rootTagNamespace = parseHeaderWithException(fileContent).getRootTagNamespace(); return rootTagNamespace != null && namespacePattern.accepts(rootTagNamespace, context); } catch (IOException e) { return false; } } }); }
@Override public Stub buildStubTree(FileContent fileContent) { PsiFile psiFile = fileContent.getPsiFile(); if (!(psiFile instanceof XmlFile)) return null; Document document = FileDocumentManager.getInstance().getCachedDocument(fileContent.getFile()); Project project = fileContent.getProject(); if (project == null) { project = psiFile.getProject(); } if (document != null) { PsiFile existingPsi = PsiDocumentManager.getInstance(project).getPsiFile(document); if (existingPsi instanceof XmlFile) { psiFile = existingPsi; } } XmlFile xmlFile = (XmlFile)psiFile; try { XmlUtil.BUILDING_DOM_STUBS.set(Boolean.TRUE); DomFileElement<? extends DomElement> fileElement = DomManager.getDomManager(project).getFileElement(xmlFile); if (fileElement == null || !fileElement.getFileDescription().hasStubs()) return null; XmlFileHeader header = DomService.getInstance().getXmlFileHeader(xmlFile); if (header.getRootTagLocalName() == null) { LOG.error("null root tag for " + fileElement + " for " + fileContent.getFile()); } FileStub fileStub = new FileStub(header); XmlTag rootTag = xmlFile.getRootTag(); if (rootTag != null) { new DomStubBuilderVisitor(DomManagerImpl.getDomManager(project)).visitXmlElement(rootTag, fileStub, 0); } return fileStub; } finally { XmlUtil.BUILDING_DOM_STUBS.set(Boolean.FALSE); SemService.getSemService(project).clearCache(); } }
@NotNull @Override public FileIncludeInfo[] getIncludeInfos(FileContent content) { final ArrayList<FileIncludeInfo> infos; if (content.getFileType() == XmlFileType.INSTANCE) { CharSequence inputDataContentAsText = content.getContentAsText(); if (CharArrayUtil.indexOf(inputDataContentAsText, ApplicationLoader.RNG_NAMESPACE, 0) == -1) return FileIncludeInfo.EMPTY; infos = new ArrayList<FileIncludeInfo>(); NanoXmlUtil.parse(CharArrayUtil.readerFromCharSequence(content.getContentAsText()), new RngBuilderAdapter(infos)); } else if (content.getFileType() == RncFileType.getInstance()) { infos = new ArrayList<FileIncludeInfo>(); content.getPsiFile().acceptChildren(new RncElementVisitor() { @Override public void visitElement(RncElement element) { element.acceptChildren(this); } @Override public void visitInclude(RncInclude include) { final String path = include.getFileReference(); if (path != null) { infos.add(new FileIncludeInfo(path)); } } }); } else { return FileIncludeInfo.EMPTY; } return infos.toArray(new FileIncludeInfo[infos.size()]); }
public static ResourceIndexData indexFile(FileContent content) { ResourceIndexData indexData = content.getUserData(ourDartCachesData); if(indexData != null) { return indexData; } synchronized(content) { indexData = content.getUserData(ourDartCachesData); if(indexData != null) { return indexData; } indexData = indexResources(content.getPsiFile()); } return indexData; }
@NotNull @Override public ElementPattern<FileContent> createSuitableFilePattern() { return FileContentPattern.fileContent() .withName(AppEngineUtil.APP_ENGINE_WEB_XML_NAME) .xmlWithRootTag("appengine-web-app"); }
@Test public void createSuitableFilePattern_withInvalidFilename_doesNotMatch() { // unaccepted file name pattern, although correct content. MockVirtualFile invalidNameFile = new MockVirtualFile("myApp.yaml"); FileContent wrongFile = new FileContentImpl( invalidNameFile, VALID_APP_ENGINE_FLEX_YAML_CONTENT, System.currentTimeMillis()); assertThat(pattern.accepts(wrongFile)).isFalse(); }
@Test public void createSuitableFilePattern_appYml_withInvalidContent_doesNotMatch() { MockVirtualFile invalidYamlFile = new MockVirtualFile("app.yml"); FileContent invalidYamlFileContent = new FileContentImpl( invalidYamlFile, NOT_APP_ENGINE_FLEX_YAML_CONTENT, System.currentTimeMillis()); assertThat(pattern.accepts(invalidYamlFileContent)).isFalse(); }
@Test public void createSuitableFilePattern_appYaml_withInvalidContent_doesNotMatch() { MockVirtualFile invalidYamlFile = new MockVirtualFile("app.yaml"); FileContent invalidYamlFileContent = new FileContentImpl( invalidYamlFile, NOT_APP_ENGINE_FLEX_YAML_CONTENT, System.currentTimeMillis()); assertThat(pattern.accepts(invalidYamlFileContent)).isFalse(); }
@Test public void createSuitableFilePattern_appYml_withValidContent_matches() { MockVirtualFile validAppEngineFlexFile = new MockVirtualFile("app.yml"); FileContent validAppEngineFlexFileContent = new FileContentImpl( validAppEngineFlexFile, VALID_APP_ENGINE_FLEX_YAML_CONTENT, System.currentTimeMillis()); assertThat(pattern.accepts(validAppEngineFlexFileContent)).isTrue(); }
@Test public void createSuitableFilePattern_appYaml_withValidContent_matches() { MockVirtualFile validAppEngineFlexFile = new MockVirtualFile("app.yaml"); FileContent validAppEngineFlexFileContent = new FileContentImpl( validAppEngineFlexFile, VALID_APP_ENGINE_FLEX_YAML_CONTENT, System.currentTimeMillis()); assertThat(pattern.accepts(validAppEngineFlexFileContent)).isTrue(); }
public static ScanContent scanContent(FileContent content, IdAndToDoScannerBasedOnFilterLexer indexer) { ScanContent data = content.getUserData(scanContentKey); if (data != null) { content.putUserData(scanContentKey, null); return data; } final boolean needTodo = content.getFile().getFileSystem().getProtocol().equals(StandardFileSystems.FILE_PROTOCOL); final boolean needIdIndex = IdTableBuilding.getFileTypeIndexer(content.getFileType()) instanceof LexerBasedIdIndexer; final IdDataConsumer consumer = needIdIndex? new IdDataConsumer():null; final OccurrenceConsumer todoOccurrenceConsumer = new OccurrenceConsumer(consumer, needTodo); final Lexer filterLexer = indexer.createLexer(todoOccurrenceConsumer); filterLexer.start(content.getContentAsText()); while (filterLexer.getTokenType() != null) filterLexer.advance(); Map<TodoIndexEntry,Integer> todoMap = null; if (needTodo) { for (IndexPattern indexPattern : IndexPatternUtil.getIndexPatterns()) { final int count = todoOccurrenceConsumer.getOccurrenceCount(indexPattern); if (count > 0) { if (todoMap == null) todoMap = new THashMap<TodoIndexEntry, Integer>(); todoMap.put(new TodoIndexEntry(indexPattern.getPatternString(), indexPattern.isCaseSensitive()), count); } } } data = new ScanContent( consumer != null? consumer.getResult():Collections.<IdIndexEntry, Integer>emptyMap(), todoMap != null ? todoMap: Collections.<TodoIndexEntry,Integer>emptyMap() ); if (needIdIndex && needTodo) content.putUserData(scanContentKey, data); return data; }