This commit is contained in:
Alexey Kudravtsev
2014-10-24 14:45:31 +04:00
parent ffa9347899
commit a43eb42435
2 changed files with 25 additions and 20 deletions
@@ -38,13 +38,13 @@ import java.lang.ref.SoftReference;
public class SelfElementInfo implements SmartPointerElementInfo {
protected final VirtualFile myVirtualFile;
private Reference<RangeMarker> myMarkerRef; // create marker only in case of live document
private int mySyncStartOffset;
private int mySyncEndOffset;
protected boolean mySyncMarkerIsValid;
private volatile int mySyncStartOffset;
private volatile int mySyncEndOffset;
protected volatile boolean mySyncMarkerIsValid;
private final Class myType;
protected final Project myProject;
@SuppressWarnings({"UnusedDeclaration"})
private RangeMarker myRangeMarker; //maintain hard reference during modification
private volatile RangeMarker myRangeMarker; //maintains hard reference during modification
protected final Language myLanguage;
protected SelfElementInfo(@NotNull Project project, @NotNull PsiElement anchor) {
@@ -90,7 +90,9 @@ public class SelfElementInfo implements SmartPointerElementInfo {
// before change
@Override
public void fastenBelt(int offset, @Nullable RangeMarker[] cachedRangeMarkers) {
if (!mySyncMarkerIsValid) return;
if (!mySyncMarkerIsValid) {
return;
}
RangeMarker marker = getMarker();
int actualEndOffset = marker == null || !marker.isValid() ? getSyncEndOffset() : marker.getEndOffset();
if (offset > actualEndOffset) {
@@ -115,13 +117,16 @@ public class SelfElementInfo implements SmartPointerElementInfo {
}
}
}
else {
if (marker == null) {
marker = document.createRangeMarker(start, end, true);
}
}
setMarker(marker);
}
else if (!marker.isValid()) {
else if (marker.isValid()) {
setRange(marker);
}
else {
mySyncMarkerIsValid = false;
setMarker(null);
marker = null;
@@ -132,7 +137,9 @@ public class SelfElementInfo implements SmartPointerElementInfo {
// after change
@Override
public void unfastenBelt(int offset) {
if (!mySyncMarkerIsValid) return;
if (!mySyncMarkerIsValid) {
return;
}
RangeMarker marker = getMarker();
if (marker != null) {
if (marker.isValid()) {
@@ -210,10 +217,6 @@ public class SelfElementInfo implements SmartPointerElementInfo {
myMarkerRef = marker == null ? null : new SoftReference<RangeMarker>(marker);
}
@Nullable
public static PsiFile restoreFileFromVirtual(final VirtualFile virtualFile, @NotNull final Project project) {
return restoreFileFromVirtual(virtualFile, project, null);
}
@Nullable
public static PsiFile restoreFileFromVirtual(final VirtualFile virtualFile, @NotNull final Project project, @Nullable final Language language) {
if (virtualFile == null) return null;
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2013 JetBrains s.r.o.
* Copyright 2000-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -42,22 +42,24 @@ class SmartPsiElementPointerImpl<E extends PsiElement> implements SmartPointerEx
private final Class<? extends PsiElement> myElementClass;
private byte myReferenceCount;
public SmartPsiElementPointerImpl(@NotNull Project project, @NotNull E element, @Nullable PsiFile containingFile) {
SmartPsiElementPointerImpl(@NotNull Project project, @NotNull E element, @Nullable PsiFile containingFile) {
this(element, createElementInfo(project, element, containingFile), element.getClass());
}
public SmartPsiElementPointerImpl(@NotNull E element,
@NotNull SmartPointerElementInfo elementInfo,
@NotNull Class<? extends PsiElement> elementClass) {
SmartPsiElementPointerImpl(@NotNull E element,
@NotNull SmartPointerElementInfo elementInfo,
@NotNull Class<? extends PsiElement> elementClass) {
ApplicationManager.getApplication().assertReadAccessAllowed();
cacheElement(element);
myElementClass = elementClass;
myElementInfo = elementInfo;
}
@Override
public boolean equals(Object obj) {
return obj instanceof SmartPsiElementPointer && pointsToTheSameElementAs(this, (SmartPsiElementPointer)obj);
}
@Override
public int hashCode() {
return myElementInfo.elementHashCode();
}
@@ -125,6 +127,9 @@ class SmartPsiElementPointerImpl<E extends PsiElement> implements SmartPointerEx
@NotNull
static <E extends PsiElement> SmartPointerElementInfo createElementInfo(@NotNull Project project, @NotNull E element, PsiFile containingFile) {
if (element instanceof PsiDirectory) {
return new DirElementInfo((PsiDirectory)element);
}
if (element instanceof PsiCompiledElement || containingFile == null || !containingFile.isPhysical() || !element.isPhysical()) {
if (element instanceof StubBasedPsiElement && element instanceof PsiCompiledElement) {
if (element instanceof PsiFile) {
@@ -137,9 +142,6 @@ class SmartPsiElementPointerImpl<E extends PsiElement> implements SmartPointerEx
}
return new HardElementInfo(project, element);
}
if (element instanceof PsiDirectory) {
return new DirElementInfo((PsiDirectory)element);
}
for(SmartPointerElementInfoFactory factory: Extensions.getExtensions(SmartPointerElementInfoFactory.EP_NAME)) {
final SmartPointerElementInfo result = factory.createElementInfo(element);