/platform/platform-impl/src/com/intellij/openapi/editor/impl/softwrap/SoftWrapsStorage.java

https://bitbucket.org/nbargnesi/idea · Java · 206 lines · 112 code · 21 blank · 73 comment · 18 complexity · d22f2d3c60059624008246c8b24157e3 MD5 · raw file

  1. /*
  2. * Copyright 2000-2010 JetBrains s.r.o.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.intellij.openapi.editor.impl.softwrap;
  17. import com.intellij.openapi.editor.SoftWrap;
  18. import com.intellij.openapi.editor.TextChange;
  19. import com.intellij.openapi.editor.ex.SoftWrapChangeListener;
  20. import org.jetbrains.annotations.NotNull;
  21. import org.jetbrains.annotations.Nullable;
  22. import java.util.ArrayList;
  23. import java.util.Collections;
  24. import java.util.List;
  25. /**
  26. * Holds registered soft wraps and provides monitoring and management facilities for them.
  27. * <p/>
  28. * Not thread-safe.
  29. *
  30. * @author Denis Zhdanov
  31. * @since Jun 29, 2010 3:04:20 PM
  32. */
  33. public class SoftWrapsStorage {
  34. private final List<SoftWrapImpl> myWraps = new ArrayList<SoftWrapImpl>();
  35. private final List<SoftWrapImpl> myWrapsView = Collections.unmodifiableList(myWraps);
  36. private final List<SoftWrapChangeListener> myListeners = new ArrayList<SoftWrapChangeListener>();
  37. /**
  38. * @return <code>true</code> if there is at least one soft wrap registered at the current storage; <code>false</code> otherwise
  39. */
  40. public boolean isEmpty() {
  41. return myWraps.isEmpty();
  42. }
  43. @Nullable
  44. public SoftWrap getSoftWrap(int offset) {
  45. int i = getSoftWrapIndex(offset);
  46. return i >= 0 ? myWraps.get(i) : null;
  47. }
  48. /**
  49. * @return view for registered soft wraps sorted by offset in ascending order if any; empty collection otherwise
  50. */
  51. @NotNull
  52. public List<SoftWrapImpl> getSoftWraps() {
  53. return myWrapsView;
  54. }
  55. /**
  56. * Tries to find index of the target soft wrap stored at {@link #myWraps} collection. <code>'Target'</code> soft wrap is the one
  57. * that starts at the given offset.
  58. *
  59. * @param offset target offset
  60. * @return index that conforms to {@link Collections#binarySearch(List, Object)} contract, i.e. non-negative returned
  61. * index points to soft wrap that starts at the given offset; <code>'-(negative value) - 1'</code> points
  62. * to position at {@link #myWraps} collection where soft wrap for the given index should be inserted
  63. */
  64. public int getSoftWrapIndex(int offset) {
  65. int start = 0;
  66. int end = myWraps.size() - 1;
  67. // We use custom inline implementation of binary search here because profiling shows that standard Collections.binarySearch()
  68. // is a bottleneck. The most probable reason is a big number of interface calls.
  69. while (start <= end) {
  70. int i = (start + end) >>> 1;
  71. SoftWrap softWrap = myWraps.get(i);
  72. int softWrapOffset = softWrap.getStart();
  73. if (softWrapOffset > offset) {
  74. end = i - 1;
  75. }
  76. else if (softWrapOffset < offset) {
  77. start = i + 1;
  78. }
  79. else {
  80. return i;
  81. }
  82. }
  83. return -(start + 1);
  84. }
  85. /**
  86. * Allows to answer how many soft wraps which {@link TextChange#getStart() start offsets} belong to given
  87. * <code>[start; end]</code> interval are registered withing the current storage.
  88. *
  89. * @param startOffset target start offset (inclusive)
  90. * @param endOffset target end offset (inclusive)
  91. * @return number of soft wraps which {@link TextChange#getStart() start offsets} belong to the target range
  92. */
  93. public int getNumberOfSoftWrapsInRange(int startOffset, int endOffset) {
  94. int startIndex = getSoftWrapIndex(startOffset);
  95. if (startIndex < 0) {
  96. startIndex = -startIndex - 1;
  97. }
  98. if (startIndex >= myWraps.size()) {
  99. return 0;
  100. }
  101. int result = 0;
  102. int endIndex = startIndex;
  103. for (; endIndex < myWraps.size(); endIndex++) {
  104. SoftWrap softWrap = myWraps.get(endIndex);
  105. if (softWrap.getStart() > endOffset) {
  106. break;
  107. }
  108. result++;
  109. }
  110. return result;
  111. }
  112. /**
  113. * Inserts given soft wrap to {@link #myWraps} collection at the given index.
  114. *
  115. * @param softWrap soft wrap to store
  116. * @param notifyListeners flag that indicates if registered listeners should be notified about soft wrap registration
  117. * @return previous soft wrap object stored for the same offset if any; <code>null</code> otherwise
  118. */
  119. @SuppressWarnings({"ForLoopReplaceableByForEach"})
  120. @Nullable
  121. public SoftWrap storeOrReplace(SoftWrapImpl softWrap, boolean notifyListeners) {
  122. int i = getSoftWrapIndex(softWrap.getStart());
  123. if (i >= 0) {
  124. return myWraps.set(i, softWrap);
  125. }
  126. i = -i - 1;
  127. myWraps.add(i, softWrap);
  128. if (notifyListeners) {
  129. // Use explicit loop as profiling shows that iterator-based processing is quite slow.
  130. for (int j = 0; j < myListeners.size(); j++) {
  131. myListeners.get(j).softWrapAdded(softWrap);
  132. }
  133. }
  134. return null;
  135. }
  136. /**
  137. * Allows to remove all soft wraps registered at the current storage with offsets from <code>[start; end)</code> range if any.
  138. *
  139. * @param startOffset start offset to use (inclusive)
  140. * @param endOffset end offset to use (exclusive)
  141. */
  142. public void removeInRange(int startOffset, int endOffset) {
  143. //CachingSoftWrapDataMapper.log(String.format("xxxxxxxxxx SoftWrapsStorage.removeInRange(%d, %d). Current number: %d", startOffset, endOffset, myWraps.size()));
  144. int startIndex = getSoftWrapIndex(startOffset);
  145. if (startIndex < 0) {
  146. startIndex = -startIndex - 1;
  147. }
  148. if (startIndex >= myWraps.size()) {
  149. return;
  150. }
  151. int endIndex = startIndex;
  152. for (; endIndex < myWraps.size(); endIndex++) {
  153. SoftWrap softWrap = myWraps.get(endIndex);
  154. if (softWrap.getStart() >= endOffset) {
  155. break;
  156. }
  157. }
  158. if (endIndex > startIndex) {
  159. myWraps.subList(startIndex, endIndex).clear();
  160. notifyListenersAboutRemoval();
  161. }
  162. //CachingSoftWrapDataMapper.log(String.format("xxxxxxxxxx SoftWrapsStorage.removeInRange(%d, %d). Remaining: %d", startOffset, endOffset, myWraps.size()));
  163. }
  164. /**
  165. * Removes all soft wraps registered at the current storage.
  166. */
  167. public void removeAll() {
  168. myWraps.clear();
  169. notifyListenersAboutRemoval();
  170. }
  171. /**
  172. * Registers given listener within the current model
  173. *
  174. * @param listener listener to register
  175. * @return <code>true</code> if given listener was not registered before; <code>false</code> otherwise
  176. */
  177. public boolean addSoftWrapChangeListener(@NotNull SoftWrapChangeListener listener) {
  178. return myListeners.add(listener);
  179. }
  180. private void notifyListenersAboutRemoval() {
  181. for (SoftWrapChangeListener listener : myListeners) {
  182. listener.softWrapsRemoved();
  183. }
  184. }
  185. }