use of com.intellij.util.indexing.FileContentImpl in project go-lang-idea-plugin by go-lang-plugin-org.
the class GoCodeInsightFixtureTestCase method buildStubTreeText.
protected static String buildStubTreeText(@NotNull Project project, @NotNull VirtualFile file, @NotNull String fileContent, boolean checkErrors) throws IOException {
String path = file.getPath();
PsiFile psi = PsiFileFactory.getInstance(project).createFileFromText(file.getName(), file.getFileType(), fileContent);
if (checkErrors) {
assertFalse(path + " contains error elements", DebugUtil.psiToString(psi, true).contains("PsiErrorElement"));
}
String full = DebugUtil.stubTreeToString(GoFileElementType.INSTANCE.getBuilder().buildStubTree(psi));
psi.putUserData(IndexingDataKeys.VIRTUAL_FILE, file);
FileContentImpl content = new FileContentImpl(file, fileContent, file.getCharset());
PsiFile psiFile = content.getPsiFile();
String fast = DebugUtil.stubTreeToString(GoFileElementType.INSTANCE.getBuilder().buildStubTree(psiFile));
if (!Comparing.strEqual(full, fast)) {
System.err.println(path);
UsefulTestCase.assertSameLines(full, fast);
}
return fast;
}
use of com.intellij.util.indexing.FileContentImpl in project intellij-community by JetBrains.
the class FrameworkDetectionProcessor method collectSuitableFiles.
private void collectSuitableFiles(@NotNull VirtualFile file) {
try {
VfsUtilCore.visitChildrenRecursively(file, new VirtualFileVisitor() {
@Override
public boolean visitFile(@NotNull VirtualFile file) {
// Since this code is invoked from New Project Wizard it's very possible that VFS isn't loaded to memory yet, so we need to do it
// manually, otherwise refresh will do nothing
myProgressIndicator.checkCanceled();
return true;
}
});
file.refresh(false, true);
VfsUtilCore.visitChildrenRecursively(file, new VirtualFileVisitor() {
@Override
public boolean visitFile(@NotNull VirtualFile file) {
myProgressIndicator.checkCanceled();
if (!myProcessedFiles.add(file)) {
return false;
}
if (!file.isDirectory()) {
final FileType fileType = file.getFileType();
if (myDetectorsByFileType.containsKey(fileType)) {
myProgressIndicator.setText2(file.getPresentableUrl());
try {
final FileContent fileContent = new FileContentImpl(file, file.contentsToByteArray(false));
for (FrameworkDetectorData detector : myDetectorsByFileType.get(fileType)) {
if (detector.myFilePattern.accepts(fileContent)) {
detector.mySuitableFiles.add(file);
}
}
} catch (IOException e) {
LOG.info(e);
}
}
}
return true;
}
});
} catch (ProcessCanceledException ignored) {
}
}
use of com.intellij.util.indexing.FileContentImpl in project intellij-community by JetBrains.
the class StubTreeBuilder method buildStubTree.
@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<>(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;
}
}
use of com.intellij.util.indexing.FileContentImpl in project intellij-community by JetBrains.
the class CoreStubTreeLoader method readOrBuild.
@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;
}
use of com.intellij.util.indexing.FileContentImpl in project intellij-community by JetBrains.
the class GrStubIndexer method indexFile.
@Nullable
@Override
public Unit indexFile(@NotNull FileContent content) {
if (!isNormalGroovyFile(((FileContentImpl) content).getPsiFileForPsiDependentIndex()))
return null;
Stub stubTree = StubTreeBuilder.buildStubTree(content);
if (!(stubTree instanceof GrFileStub))
return null;
GrFileStub grFileStub = (GrFileStub) stubTree;
new StubTree(grFileStub, false);
String pid = "";
ArrayList<ClassDecl> classList = new ArrayList<>();
Set<String> usedNames = new HashSet<>();
for (StubElement<?> el : grFileStub.getChildrenStubs()) {
if (el instanceof GrPackageDefinitionStub) {
GrPackageDefinitionStub packageStub = (GrPackageDefinitionStub) el;
String pkgName = packageStub.getPackageName();
if (pkgName != null) {
pid = id(pkgName, false, null);
}
}
if (el instanceof GrTypeDefinitionStub) {
ClassDecl classDecl = processClassDecl((GrTypeDefinitionStub) el, usedNames, false);
if (classDecl != null) {
classList.add(classDecl);
}
}
}
ArrayList<Import> importList = new ArrayList<>();
for (StubElement<?> el : grFileStub.getChildrenStubs()) {
if (el instanceof GrImportStatementStub) {
processImport((GrImportStatementStub) el, importList, usedNames);
}
}
ClassDecl[] classes = classList.isEmpty() ? ClassDecl.EMPTY_ARRAY : classList.toArray(new ClassDecl[classList.size()]);
Import[] imports = importList.isEmpty() ? Import.EMPTY_ARRAY : importList.toArray(new Import[importList.size()]);
return new Unit(pid, IndexTree.GROOVY, imports, classes);
}
Aggregations