mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-01-08 15:09:39 +07:00
remove PsiDocumentManagerBase.HARD_REF_TO_PSI
we already have doc<->file and file<->psi links, requiring also doc<->psi one complicates things and makes it easier to forget some of them
This commit is contained in:
@@ -55,7 +55,6 @@ import java.util.concurrent.ConcurrentMap;
|
||||
public abstract class PsiDocumentManagerBase extends PsiDocumentManager implements DocumentListener, ProjectComponent {
|
||||
static final Logger LOG = Logger.getInstance("#com.intellij.psi.impl.PsiDocumentManagerImpl");
|
||||
private static final Key<Document> HARD_REF_TO_DOCUMENT = Key.create("HARD_REFERENCE_TO_DOCUMENT");
|
||||
private final Key<PsiFile> HARD_REF_TO_PSI = Key.create("HARD_REF_TO_PSI"); // has to be different for each project to avoid mixups
|
||||
private static final Key<List<Runnable>> ACTION_AFTER_COMMIT = Key.create("ACTION_AFTER_COMMIT");
|
||||
|
||||
protected final Project myProject;
|
||||
@@ -100,11 +99,6 @@ public abstract class PsiDocumentManagerBase extends PsiDocumentManager implemen
|
||||
return null;
|
||||
}
|
||||
|
||||
final PsiFile userData = document.getUserData(HARD_REF_TO_PSI);
|
||||
if (userData != null) {
|
||||
return ensureValidFile(userData, "From hard ref");
|
||||
}
|
||||
|
||||
PsiFile psiFile = getCachedPsiFile(document);
|
||||
if (psiFile != null) {
|
||||
return ensureValidFile(psiFile, "Cached PSI");
|
||||
@@ -135,14 +129,11 @@ public abstract class PsiDocumentManagerBase extends PsiDocumentManager implemen
|
||||
}
|
||||
|
||||
public void associatePsi(@NotNull Document document, @Nullable PsiFile file) {
|
||||
document.putUserData(HARD_REF_TO_PSI, file);
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public PsiFile getCachedPsiFile(@NotNull Document document) {
|
||||
final PsiFile userData = document.getUserData(HARD_REF_TO_PSI);
|
||||
if (userData != null) return userData;
|
||||
|
||||
final VirtualFile virtualFile = FileDocumentManager.getInstance().getFile(document);
|
||||
if (virtualFile == null || !virtualFile.isValid()) return null;
|
||||
return getCachedPsiFile(virtualFile);
|
||||
@@ -182,7 +173,7 @@ public abstract class PsiDocumentManagerBase extends PsiDocumentManager implemen
|
||||
|
||||
Document document = getCachedDocument(file);
|
||||
if (document != null) {
|
||||
if (!file.getViewProvider().isPhysical() && document.getUserData(HARD_REF_TO_PSI) == null) {
|
||||
if (!file.getViewProvider().isPhysical()) {
|
||||
PsiUtilCore.ensureValid(file);
|
||||
associatePsi(document, file);
|
||||
}
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
|
||||
package com.intellij.psi.impl.file.impl;
|
||||
|
||||
import com.intellij.injected.editor.DocumentWindow;
|
||||
import com.intellij.injected.editor.VirtualFileWindow;
|
||||
import com.intellij.lang.Language;
|
||||
import com.intellij.lang.LanguageUtil;
|
||||
@@ -36,7 +35,10 @@ import com.intellij.openapi.vfs.VfsUtilCore;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.openapi.vfs.VirtualFileVisitor;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.impl.*;
|
||||
import com.intellij.psi.impl.DebugUtil;
|
||||
import com.intellij.psi.impl.PsiManagerImpl;
|
||||
import com.intellij.psi.impl.PsiModificationTrackerImpl;
|
||||
import com.intellij.psi.impl.PsiTreeChangeEventImpl;
|
||||
import com.intellij.psi.impl.file.PsiDirectoryFactory;
|
||||
import com.intellij.testFramework.LightVirtualFile;
|
||||
import com.intellij.util.ConcurrencyUtil;
|
||||
@@ -179,23 +181,11 @@ public class FileManagerImpl implements FileManager {
|
||||
|
||||
@Override
|
||||
public FileViewProvider findCachedViewProvider(@NotNull final VirtualFile file) {
|
||||
FileViewProvider viewProvider = getFromInjected(file);
|
||||
if (viewProvider == null) viewProvider = myVFileToViewProviderMap.get(file);
|
||||
FileViewProvider viewProvider = myVFileToViewProviderMap.get(file);
|
||||
if (viewProvider == null) viewProvider = file.getUserData(myPsiHardRefKey);
|
||||
return viewProvider;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private FileViewProvider getFromInjected(@NotNull VirtualFile file) {
|
||||
if (file instanceof VirtualFileWindow) {
|
||||
DocumentWindow document = ((VirtualFileWindow)file).getDocumentWindow();
|
||||
PsiFile psiFile = PsiDocumentManager.getInstance(myManager.getProject()).getCachedPsiFile(document);
|
||||
if (psiFile == null) return null;
|
||||
return psiFile.getViewProvider();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setViewProvider(@NotNull final VirtualFile virtualFile, @Nullable final FileViewProvider fileViewProvider) {
|
||||
FileViewProvider prev = findCachedViewProvider(virtualFile);
|
||||
@@ -211,16 +201,14 @@ public class FileManagerImpl implements FileManager {
|
||||
}
|
||||
}
|
||||
|
||||
if (!(virtualFile instanceof VirtualFileWindow)) {
|
||||
if (fileViewProvider == null) {
|
||||
myVFileToViewProviderMap.remove(virtualFile);
|
||||
}
|
||||
else if (virtualFile instanceof LightVirtualFile) {
|
||||
virtualFile.putUserData(myPsiHardRefKey, fileViewProvider);
|
||||
}
|
||||
else {
|
||||
myVFileToViewProviderMap.put(virtualFile, fileViewProvider);
|
||||
}
|
||||
if (fileViewProvider == null) {
|
||||
myVFileToViewProviderMap.remove(virtualFile);
|
||||
}
|
||||
else if (virtualFile instanceof LightVirtualFile) {
|
||||
virtualFile.putUserData(myPsiHardRefKey, fileViewProvider);
|
||||
}
|
||||
else {
|
||||
myVFileToViewProviderMap.put(virtualFile, fileViewProvider);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -437,12 +425,7 @@ public class FileManagerImpl implements FileManager {
|
||||
|
||||
private void markInvalidated(@NotNull FileViewProvider viewProvider) {
|
||||
((AbstractFileViewProvider)viewProvider).markInvalidated();
|
||||
VirtualFile virtualFile = viewProvider.getVirtualFile();
|
||||
Document document = FileDocumentManager.getInstance().getCachedDocument(virtualFile);
|
||||
if (document != null) {
|
||||
((PsiDocumentManagerBase)PsiDocumentManager.getInstance(myManager.getProject())).associatePsi(document, null);
|
||||
}
|
||||
virtualFile.putUserData(myPsiHardRefKey, null);
|
||||
viewProvider.getVirtualFile().putUserData(myPsiHardRefKey, null);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
|
||||
@@ -230,4 +230,17 @@ public class PsiDocumentManagerImpl extends PsiDocumentManagerBase {
|
||||
protected DocumentWindow freezeWindow(@NotNull DocumentWindow document) {
|
||||
return InjectedLanguageManager.getInstance(myProject).freezeWindow(document);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void associatePsi(@NotNull Document document, @Nullable PsiFile file) {
|
||||
if (file != null) {
|
||||
VirtualFile vFile = file.getViewProvider().getVirtualFile();
|
||||
Document cachedDocument = FileDocumentManager.getInstance().getCachedDocument(vFile);
|
||||
if (cachedDocument != null && cachedDocument != document) {
|
||||
throw new IllegalStateException("Can't replace existing document");
|
||||
}
|
||||
|
||||
FileDocumentManagerImpl.registerDocument(document, vFile);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -277,7 +277,7 @@ class InjectionRegistrarImpl extends MultiHostRegistrarImpl implements MultiHost
|
||||
PsiFile psiFile = (PsiFile)parsedNode.getPsi();
|
||||
InjectedFileViewProvider viewProvider = (InjectedFileViewProvider)psiFile.getViewProvider();
|
||||
|
||||
cacheEverything(place, documentWindow, viewProvider, psiFile, documentManager);
|
||||
cacheEverything(place, documentWindow, viewProvider, psiFile);
|
||||
|
||||
PsiFile cachedPsiFile = documentManager.getCachedPsiFile(documentWindow);
|
||||
assert cachedPsiFile == psiFile : "Cached psi :"+ cachedPsiFile +" instead of "+psiFile;
|
||||
@@ -294,7 +294,7 @@ class InjectionRegistrarImpl extends MultiHostRegistrarImpl implements MultiHost
|
||||
psiFile = newFile;
|
||||
viewProvider = (InjectedFileViewProvider)psiFile.getViewProvider();
|
||||
documentWindow = (DocumentWindowImpl)viewProvider.getDocument();
|
||||
boolean shredsReused = !cacheEverything(place, documentWindow, viewProvider, psiFile, documentManager);
|
||||
boolean shredsReused = !cacheEverything(place, documentWindow, viewProvider, psiFile);
|
||||
if (shredsReused) {
|
||||
place.dispose();
|
||||
mergedPlace = documentWindow.getShreds();
|
||||
@@ -366,8 +366,7 @@ class InjectionRegistrarImpl extends MultiHostRegistrarImpl implements MultiHost
|
||||
private static boolean cacheEverything(@NotNull Place place,
|
||||
@NotNull DocumentWindowImpl documentWindow,
|
||||
@NotNull InjectedFileViewProvider viewProvider,
|
||||
@NotNull PsiFile psiFile,
|
||||
@NotNull PsiDocumentManagerBase documentManager) {
|
||||
@NotNull PsiFile psiFile) {
|
||||
FileDocumentManagerImpl.registerDocument(documentWindow, viewProvider.getVirtualFile());
|
||||
|
||||
DebugUtil.startPsiModification("MultiHostRegistrar cacheEverything");
|
||||
@@ -380,7 +379,6 @@ class InjectionRegistrarImpl extends MultiHostRegistrarImpl implements MultiHost
|
||||
|
||||
SmartPsiElementPointer<PsiLanguageInjectionHost> pointer = ((ShredImpl)place.get(0)).getSmartPointer();
|
||||
psiFile.putUserData(FileContextUtil.INJECTED_IN_ELEMENT, pointer);
|
||||
documentManager.associatePsi(documentWindow, psiFile);
|
||||
|
||||
keepTreeFromChameleoningBack(psiFile);
|
||||
|
||||
@@ -630,8 +628,6 @@ class InjectionRegistrarImpl extends MultiHostRegistrarImpl implements MultiHost
|
||||
try {
|
||||
DocumentCommitThread.doActualPsiChange(oldInjectedPsi, diffLog);
|
||||
|
||||
PsiDocumentManagerBase documentManagerBase = (PsiDocumentManagerBase)PsiDocumentManager.getInstance(project);
|
||||
|
||||
// create new shreds after commit is complete because otherwise the range markers will be changed in MarkerCache.updateMarkers
|
||||
Place newPlace = new Place();
|
||||
for (int i = 0; i < oldPlace.size(); i++) {
|
||||
@@ -646,7 +642,7 @@ class InjectionRegistrarImpl extends MultiHostRegistrarImpl implements MultiHost
|
||||
newPlace.add(newShred);
|
||||
}
|
||||
|
||||
cacheEverything(newPlace, oldDocumentWindow, oldInjectedPsiViewProvider, oldInjectedPsi, documentManagerBase);
|
||||
cacheEverything(newPlace, oldDocumentWindow, oldInjectedPsiViewProvider, oldInjectedPsi);
|
||||
String docText = oldDocumentWindow.getText();
|
||||
assert docText.equals(newText) : "=\n" + docText + "\n==\n" + newDocumentText + "\n===\n";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user