From a43eb4243516f4789e35800e5edae1644fd57c57 Mon Sep 17 00:00:00 2001 From: Alexey Kudravtsev Date: Fri, 24 Oct 2014 13:16:17 +0400 Subject: [PATCH] cleanup --- .../impl/smartPointers/SelfElementInfo.java | 27 ++++++++++--------- .../SmartPsiElementPointerImpl.java | 18 +++++++------ 2 files changed, 25 insertions(+), 20 deletions(-) diff --git a/platform/core-impl/src/com/intellij/psi/impl/smartPointers/SelfElementInfo.java b/platform/core-impl/src/com/intellij/psi/impl/smartPointers/SelfElementInfo.java index 748a98ead7de..7ebf149d7122 100644 --- a/platform/core-impl/src/com/intellij/psi/impl/smartPointers/SelfElementInfo.java +++ b/platform/core-impl/src/com/intellij/psi/impl/smartPointers/SelfElementInfo.java @@ -38,13 +38,13 @@ import java.lang.ref.SoftReference; public class SelfElementInfo implements SmartPointerElementInfo { protected final VirtualFile myVirtualFile; private Reference 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(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; diff --git a/platform/core-impl/src/com/intellij/psi/impl/smartPointers/SmartPsiElementPointerImpl.java b/platform/core-impl/src/com/intellij/psi/impl/smartPointers/SmartPsiElementPointerImpl.java index 9e755ad5a615..ac37bf8f7aed 100644 --- a/platform/core-impl/src/com/intellij/psi/impl/smartPointers/SmartPsiElementPointerImpl.java +++ b/platform/core-impl/src/com/intellij/psi/impl/smartPointers/SmartPsiElementPointerImpl.java @@ -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 implements SmartPointerEx private final Class 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 elementClass) { + SmartPsiElementPointerImpl(@NotNull E element, + @NotNull SmartPointerElementInfo elementInfo, + @NotNull Class 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 implements SmartPointerEx @NotNull static 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 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);