use of com.intellij.psi.impl.source.PsiFileImpl in project intellij-community by JetBrains.
the class MultiHostRegistrarImpl method registerDocument.
// under com.intellij.psi.PsiLock.LOCK
private static PsiFile registerDocument(final DocumentWindowImpl documentWindow, final PsiFile injectedPsi, final Place shreds, final PsiFile hostPsiFile, final PsiDocumentManager documentManager) {
List<DocumentWindow> injected = InjectedLanguageUtil.getCachedInjectedDocuments(hostPsiFile);
for (int i = injected.size() - 1; i >= 0; i--) {
DocumentWindowImpl oldDocument = (DocumentWindowImpl) injected.get(i);
final PsiFileImpl oldFile = (PsiFileImpl) documentManager.getCachedPsiFile(oldDocument);
FileViewProvider viewProvider;
if (oldFile == null || !oldFile.isValid() || !((viewProvider = oldFile.getViewProvider()) instanceof InjectedFileViewProvider) || ((InjectedFileViewProvider) viewProvider).isDisposed()) {
injected.remove(i);
Disposer.dispose(oldDocument);
continue;
}
InjectedFileViewProvider oldViewProvider = (InjectedFileViewProvider) viewProvider;
final ASTNode injectedNode = injectedPsi.getNode();
final ASTNode oldFileNode = oldFile.getNode();
assert injectedNode != null : "New node is null";
if (oldDocument.areRangesEqual(documentWindow)) {
if (oldFile.getFileType() != injectedPsi.getFileType() || oldFile.getLanguage() != injectedPsi.getLanguage()) {
injected.remove(i);
Disposer.dispose(oldDocument);
continue;
}
oldFile.putUserData(FileContextUtil.INJECTED_IN_ELEMENT, injectedPsi.getUserData(FileContextUtil.INJECTED_IN_ELEMENT));
assert shreds.isValid();
if (!oldFile.textMatches(injectedPsi)) {
oldViewProvider.performNonPhysically(() -> {
DebugUtil.startPsiModification("injected tree diff");
try {
final DiffLog diffLog = BlockSupportImpl.mergeTrees(oldFile, oldFileNode, injectedNode, new DaemonProgressIndicator(), oldFileNode.getText());
DocumentCommitThread.doActualPsiChange(oldFile, diffLog);
} finally {
DebugUtil.finishPsiModification();
}
});
}
assert shreds.isValid();
return oldFile;
}
}
injected.add(documentWindow);
return injectedPsi;
}
use of com.intellij.psi.impl.source.PsiFileImpl in project intellij-community by JetBrains.
the class TemplateDataElementType method parseContents.
@Override
public ASTNode parseContents(ASTNode chameleon) {
final CharTable charTable = SharedImplUtil.findCharTableByTree(chameleon);
final FileElement fileElement = TreeUtil.getFileElement((TreeElement) chameleon);
final PsiFile psiFile = (PsiFile) fileElement.getPsi();
PsiFile originalPsiFile = psiFile.getOriginalFile();
final TemplateLanguageFileViewProvider viewProvider = (TemplateLanguageFileViewProvider) originalPsiFile.getViewProvider();
final Language templateLanguage = getTemplateFileLanguage(viewProvider);
final CharSequence sourceCode = chameleon.getChars();
RangesCollector collector = new RangesCollector();
final PsiFile templatePsiFile = createTemplateFile(psiFile, templateLanguage, sourceCode, viewProvider, collector);
final FileElement templateFileElement = ((PsiFileImpl) templatePsiFile).calcTreeElement();
DebugUtil.startPsiModification("template language parsing");
try {
prepareParsedTemplateFile(templateFileElement);
insertOuters(templateFileElement, sourceCode, collector.myRanges, charTable);
TreeElement childNode = templateFileElement.getFirstChildNode();
DebugUtil.checkTreeStructure(templateFileElement);
DebugUtil.checkTreeStructure(chameleon);
if (fileElement != chameleon) {
DebugUtil.checkTreeStructure(psiFile.getNode());
DebugUtil.checkTreeStructure(originalPsiFile.getNode());
}
return childNode;
} finally {
DebugUtil.finishPsiModification();
}
}
use of com.intellij.psi.impl.source.PsiFileImpl in project intellij-community by JetBrains.
the class PomModelImpl method startTransaction.
private void startTransaction(@NotNull PomTransaction transaction) {
final ProgressIndicator progressIndicator = ProgressIndicatorProvider.getGlobalProgressIndicator();
if (progressIndicator != null)
progressIndicator.startNonCancelableSection();
final PsiDocumentManagerBase manager = (PsiDocumentManagerBase) PsiDocumentManager.getInstance(myProject);
final PsiToDocumentSynchronizer synchronizer = manager.getSynchronizer();
final PsiElement changeScope = transaction.getChangeScope();
final PsiFile containingFileByTree = getContainingFileByTree(changeScope);
if (containingFileByTree != null && !(containingFileByTree instanceof DummyHolder) && !manager.isCommitInProgress()) {
PsiUtilCore.ensureValid(containingFileByTree);
}
boolean physical = changeScope.isPhysical();
if (physical && synchronizer.toProcessPsiEvent()) {
// so it's important to throw something outside event processing
if (isDocumentUncommitted(containingFileByTree)) {
throw new IllegalStateException("Attempt to modify PSI for non-committed Document!");
}
CommandProcessor commandProcessor = CommandProcessor.getInstance();
if (!commandProcessor.isUndoTransparentActionInProgress() && commandProcessor.getCurrentCommand() == null) {
throw new IncorrectOperationException("Must not change PSI outside command or undo-transparent action. See com.intellij.openapi.command.WriteCommandAction or com.intellij.openapi.command.CommandProcessor");
}
}
if (containingFileByTree != null) {
((SmartPointerManagerImpl) SmartPointerManager.getInstance(myProject)).fastenBelts(containingFileByTree.getViewProvider().getVirtualFile());
if (containingFileByTree instanceof PsiFileImpl) {
((PsiFileImpl) containingFileByTree).beforeAstChange();
}
}
BlockSupportImpl.sendBeforeChildrenChangeEvent((PsiManagerImpl) PsiManager.getInstance(myProject), changeScope, true);
Document document = containingFileByTree == null ? null : physical ? manager.getDocument(containingFileByTree) : manager.getCachedDocument(containingFileByTree);
if (document != null) {
synchronizer.startTransaction(myProject, document, changeScope);
}
}
use of com.intellij.psi.impl.source.PsiFileImpl in project intellij-community by JetBrains.
the class StubBasedPsiElementBase method getNode.
/**
* Ensures this element is AST-based. This is an expensive operation that might take significant time and allocate lots of objects,
* so it should be to be avoided if possible.
*
* @return an AST node corresponding to this element. If the element is currently operating via stubs,
* this causes AST to be loaded for the whole file and all stub-based PSI elements in this file (including the current one)
* to be switched from stub to AST. So, after this call {@link #getStub()} will return null.
*/
@Override
@NotNull
public ASTNode getNode() {
if (mySubstrateRef instanceof SubstrateRef.StubRef) {
ApplicationManager.getApplication().assertReadAccessAllowed();
PsiFileImpl file = (PsiFileImpl) getContainingFile();
if (!file.isValid())
throw new PsiInvalidElementAccessException(this);
FileElement treeElement = file.getTreeElement();
if (treeElement != null && mySubstrateRef instanceof SubstrateRef.StubRef) {
return notBoundInExistingAst(file, treeElement);
}
treeElement = file.calcTreeElement();
if (mySubstrateRef instanceof SubstrateRef.StubRef) {
return failedToBindStubToAst(file, treeElement);
}
}
return mySubstrateRef.getNode();
}
use of com.intellij.psi.impl.source.PsiFileImpl in project intellij-plugins by JetBrains.
the class HbFileViewProvider method createFile.
@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;
}
}
Aggregations