use of com.intellij.psi.impl.source.PsiFileImpl 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.psi.impl.source.PsiFileImpl in project intellij-community by JetBrains.
the class BlockSupportImpl method makeFullParse.
@NotNull
private static DiffLog makeFullParse(@NotNull PsiFileImpl fileImpl, @NotNull FileASTNode oldFileNode, @NotNull CharSequence newFileText, @NotNull ProgressIndicator indicator, @NotNull CharSequence lastCommittedText) {
if (fileImpl instanceof PsiCodeFragment) {
FileElement parent = fileImpl.getTreeElement();
final FileElement holderElement = new DummyHolder(fileImpl.getManager(), fileImpl.getContext()).getTreeElement();
holderElement.rawAddChildren(fileImpl.createContentLeafElement(holderElement.getCharTable().intern(newFileText, 0, newFileText.length())));
DiffLog diffLog = new DiffLog();
diffLog.appendReplaceFileElement(parent, (FileElement) holderElement.getFirstChildNode());
return diffLog;
} else {
FileViewProvider viewProvider = fileImpl.getViewProvider();
viewProvider.getLanguages();
FileType fileType = viewProvider.getVirtualFile().getFileType();
String fileName = fileImpl.getName();
final LightVirtualFile lightFile = new LightVirtualFile(fileName, fileType, newFileText, viewProvider.getVirtualFile().getCharset(), fileImpl.getViewProvider().getModificationStamp());
lightFile.setOriginalFile(viewProvider.getVirtualFile());
FileViewProvider copy = viewProvider.createCopy(lightFile);
if (copy.isEventSystemEnabled()) {
throw new AssertionError("Copied view provider must be non-physical for reparse to deliver correct events: " + viewProvider);
}
copy.getLanguages();
// optimization: do not convert file contents to bytes to determine if we should codeinsight it
SingleRootFileViewProvider.doNotCheckFileSizeLimit(lightFile);
PsiFileImpl newFile = getFileCopy(fileImpl, copy);
newFile.setOriginalFile(fileImpl);
final FileElement newFileElement = (FileElement) newFile.getNode();
final FileElement oldFileElement = (FileElement) oldFileNode;
if (!lastCommittedText.toString().equals(oldFileElement.getText())) {
throw new IncorrectOperationException(viewProvider.toString());
}
DiffLog diffLog = mergeTrees(fileImpl, oldFileElement, newFileElement, indicator, lastCommittedText);
((PsiManagerEx) fileImpl.getManager()).getFileManager().setViewProvider(lightFile, null);
return diffLog;
}
}
use of com.intellij.psi.impl.source.PsiFileImpl in project intellij-community by JetBrains.
the class BlockSupportImpl method getFileCopy.
@NotNull
public static PsiFileImpl getFileCopy(@NotNull PsiFileImpl originalFile, @NotNull FileViewProvider providerCopy) {
FileViewProvider viewProvider = originalFile.getViewProvider();
Language language = originalFile.getLanguage();
PsiFile file = providerCopy.getPsi(language);
if (file != null && !(file instanceof PsiFileImpl)) {
throw new RuntimeException("View provider " + viewProvider + " refused to provide PsiFileImpl for " + language + details(providerCopy, viewProvider));
}
PsiFileImpl newFile = (PsiFileImpl) file;
if (newFile == null && language == PlainTextLanguage.INSTANCE && originalFile == viewProvider.getPsi(viewProvider.getBaseLanguage())) {
newFile = (PsiFileImpl) providerCopy.getPsi(providerCopy.getBaseLanguage());
}
if (newFile == null) {
throw new RuntimeException("View provider " + viewProvider + " refused to parse text with " + language + details(providerCopy, viewProvider));
}
return newFile;
}
use of com.intellij.psi.impl.source.PsiFileImpl in project intellij-community by JetBrains.
the class AstPath method getNodePath.
@Nullable
public static AstPath getNodePath(@NotNull CompositeElement node) {
if (node instanceof FileElement) {
PsiElement psi = node.getCachedPsi();
if (!(psi instanceof PsiFileImpl))
return null;
PsiFileImpl file = (PsiFileImpl) psi;
if (!(file.getVirtualFile() instanceof VirtualFileWithId) || file.getElementTypeForStubBuilder() == null) {
return null;
}
return new RootPath(file);
}
return node.getUserData(NODE_PATH);
}
use of com.intellij.psi.impl.source.PsiFileImpl in project intellij-community by JetBrains.
the class FileElement method copyElement.
@Override
public ASTNode copyElement() {
PsiFileImpl psiElement = (PsiFileImpl) getPsi();
PsiFileImpl psiElementCopy = (PsiFileImpl) psiElement.copy();
return psiElementCopy.getTreeElement();
}
Aggregations