@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; }
public void testIndex() { final VirtualFile file = myFixture.configureByFile("foo.xml").getVirtualFile(); Map<XmlPropertiesIndex.Key, String> map = new XmlPropertiesIndex().map(FileContentImpl.createByFile(file)); assertEquals(3, map.size()); assertEquals("bar", map.get(new XmlPropertiesIndex.Key("foo"))); assertEquals("baz", map.get(new XmlPropertiesIndex.Key("fu"))); assertTrue(map.containsKey(XmlPropertiesIndex.MARKER_KEY)); }
@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(); }
private static PsiFile getPsiFile(@Nonnull Project project, @Nonnull VirtualFile file) { PsiFile psi = PsiManager.getInstance(project).findFile(file); if (psi == null || !(psi instanceof PsiFileImpl) || ((PsiFileImpl)psi).isContentsLoaded()) { return psi; } FileType fileType = file.getFileType(); if (!(fileType instanceof LanguageFileType)) return null; return FileContentImpl.createFileFromText(project, psi.getViewProvider().getContents(), (LanguageFileType)fileType, file, file.getName()); }
@Nullable public static Stub buildStubTree(final FileContent inputData) { Stub data = inputData.getUserData(stubElementKey); if (data != null) return data; //noinspection SynchronizationOnLocalVariableOrMethodParameter synchronized (inputData) { data = inputData.getUserData(stubElementKey); if (data != null) return data; final FileType fileType = inputData.getFileType(); final BinaryFileStubBuilder builder = BinaryFileStubBuilders.INSTANCE.forFileType(fileType); if (builder != null) { data = builder.buildStubTree(inputData); if (data instanceof PsiFileStubImpl && !((PsiFileStubImpl)data).rootsAreSet()) { ((PsiFileStubImpl)data).setStubRoots(new PsiFileStub[]{(PsiFileStubImpl)data}); } } else { CharSequence contentAsText = inputData.getContentAsText(); FileContentImpl fileContent = (FileContentImpl)inputData; PsiFile psi = fileContent.getPsiFileForPsiDependentIndex(); final FileViewProvider viewProvider = psi.getViewProvider(); psi = viewProvider.getStubBindingRoot(); psi.putUserData(IndexingDataKeys.FILE_TEXT_CONTENT_KEY, contentAsText); // if we load AST, it should be easily gc-able. See PsiFileImpl.createTreeElementPointer() psi.getManager().startBatchFilesProcessingMode(); try { IStubFileElementType stubFileElementType = ((PsiFileImpl)psi).getElementTypeForStubBuilder(); if (stubFileElementType != null) { final StubBuilder stubBuilder = stubFileElementType.getBuilder(); if (stubBuilder instanceof LightStubBuilder) { LightStubBuilder.FORCED_AST.set(fileContent.getLighterASTForPsiDependentIndex()); } data = stubBuilder.buildStubTree(psi); final List<Pair<IStubFileElementType, PsiFile>> stubbedRoots = getStubbedRoots(viewProvider); final List<PsiFileStub> stubs = new ArrayList<PsiFileStub>(stubbedRoots.size()); stubs.add((PsiFileStub)data); for (Pair<IStubFileElementType, PsiFile> stubbedRoot : stubbedRoots) { final PsiFile secondaryPsi = stubbedRoot.second; if (psi == secondaryPsi) continue; final StubBuilder stubbedRootBuilder = stubbedRoot.first.getBuilder(); if (stubbedRootBuilder instanceof LightStubBuilder) { LightStubBuilder.FORCED_AST.set(new TreeBackedLighterAST(secondaryPsi.getNode())); } final StubElement element = stubbedRootBuilder.buildStubTree(secondaryPsi); if (element instanceof PsiFileStub) { stubs.add((PsiFileStub)element); } } final PsiFileStub[] stubsArray = stubs.toArray(new PsiFileStub[stubs.size()]); for (PsiFileStub stub : stubsArray) { if (stub instanceof PsiFileStubImpl) { ((PsiFileStubImpl)stub).setStubRoots(stubsArray); } } } } finally { psi.putUserData(IndexingDataKeys.FILE_TEXT_CONTENT_KEY, null); psi.getManager().finishBatchFilesProcessingMode(); } } inputData.putUserData(stubElementKey, data); return data; } }
public void testSystemId() throws Exception { final VirtualFile file = myFixture.configureByFile("wrong.xml").getVirtualFile(); Map<XmlPropertiesIndex.Key, String> map = new XmlPropertiesIndex().map(FileContentImpl.createByFile(file)); assertEquals(0, map.size()); }
@Nullable @RequiredReadAction public static Stub buildStubTree(final FileContent inputData) { Stub data = inputData.getUserData(stubElementKey); if (data != null) return data; //noinspection SynchronizationOnLocalVariableOrMethodParameter synchronized (inputData) { data = inputData.getUserData(stubElementKey); if (data != null) return data; final FileType fileType = inputData.getFileType(); final BinaryFileStubBuilder builder = BinaryFileStubBuilders.INSTANCE.forFileType(fileType); if (builder != null) { data = builder.buildStubTree(inputData); if (data instanceof PsiFileStubImpl && !((PsiFileStubImpl)data).rootsAreSet()) { ((PsiFileStubImpl)data).setStubRoots(new PsiFileStub[]{(PsiFileStubImpl)data}); } } else { CharSequence contentAsText = inputData.getContentAsText(); FileContentImpl fileContent = (FileContentImpl)inputData; PsiFile psi = fileContent.getPsiFileForPsiDependentIndex(); final FileViewProvider viewProvider = psi.getViewProvider(); psi = viewProvider.getStubBindingRoot(); psi.putUserData(IndexingDataKeys.FILE_TEXT_CONTENT_KEY, contentAsText); // if we load AST, it should be easily gc-able. See PsiFileImpl.createTreeElementPointer() psi.getManager().startBatchFilesProcessingMode(); try { IStubFileElementType stubFileElementType = ((PsiFileImpl)psi).getElementTypeForStubBuilder(); if (stubFileElementType != null) { final StubBuilder stubBuilder = stubFileElementType.getBuilder(); if (stubBuilder instanceof LightStubBuilder) { LightStubBuilder.FORCED_AST.set(fileContent.getLighterASTForPsiDependentIndex()); } data = stubBuilder.buildStubTree(psi); final List<Pair<IStubFileElementType, PsiFile>> stubbedRoots = getStubbedRoots(viewProvider); final List<PsiFileStub> stubs = new ArrayList<PsiFileStub>(stubbedRoots.size()); stubs.add((PsiFileStub)data); for (Pair<IStubFileElementType, PsiFile> stubbedRoot : stubbedRoots) { final PsiFile secondaryPsi = stubbedRoot.second; if (psi == secondaryPsi) continue; final StubBuilder stubbedRootBuilder = stubbedRoot.first.getBuilder(); if (stubbedRootBuilder instanceof LightStubBuilder) { LightStubBuilder.FORCED_AST.set(new TreeBackedLighterAST(secondaryPsi.getNode())); } final StubElement element = stubbedRootBuilder.buildStubTree(secondaryPsi); if (element instanceof PsiFileStub) { stubs.add((PsiFileStub)element); } } final PsiFileStub[] stubsArray = stubs.toArray(new PsiFileStub[stubs.size()]); for (PsiFileStub stub : stubsArray) { if (stub instanceof PsiFileStubImpl) { ((PsiFileStubImpl)stub).setStubRoots(stubsArray); } } } } finally { psi.putUserData(IndexingDataKeys.FILE_TEXT_CONTENT_KEY, null); psi.getManager().finishBatchFilesProcessingMode(); } } inputData.putUserData(stubElementKey, data); return data; } }
@NotNull @Override public DataIndexer<MethodCallData, Void, FileContent> getIndexer() { return inputData -> { if(myOfflineMode) { return Collections.emptyMap(); } int[] nullOffsets = new StringSearcher(PsiKeyword.NULL, true, true).findAllOccurrences(inputData.getContentAsText()); if(nullOffsets.length == 0) { return Collections.emptyMap(); } LighterAST lighterAst = ((FileContentImpl) inputData).getLighterASTForPsiDependentIndex(); Set<LighterASTNode> calls = findCallsWithNulls(lighterAst, nullOffsets); if(calls.isEmpty()) { return Collections.emptyMap(); } Map<MethodCallData, Void> result = new THashMap<>(); for(LighterASTNode element : calls) { final IntArrayList indices = getNullParameterIndices(lighterAst, element); if(indices != null) { final String name = getMethodName(lighterAst, element, element.getTokenType()); if(name != null) { for(int i = 0; i < indices.size(); i++) { result.put(new MethodCallData(name, indices.get(i)), null); } } } } return result; }; }