summaryrefslogtreecommitdiff
path: root/platform/platform-impl/src/com/intellij/openapi/editor/impl/softwrap/mapping/CacheEntry.java
blob: 27362b52527ae816da272903cba7bbcfbc9c0a5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
 * 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.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.openapi.editor.impl.softwrap.mapping;

import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.FoldRegion;
import com.intellij.openapi.util.Ref;
import gnu.trove.TIntObjectHashMap;
import gnu.trove.TIntObjectProcedure;
import gnu.trove.TObjectProcedure;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * Encapsulates information to cache for the single visual line.
 */
@SuppressWarnings("unchecked")
class CacheEntry implements Comparable<CacheEntry>, Cloneable {

  private static final TIntObjectHashMap<FoldingData> DUMMY = new TIntObjectHashMap<FoldingData>();

  public int visualLine;

  public int startLogicalLine;
  public int startLogicalColumn;
  public int startOffset;
  public int startSoftWrapLinesBefore;
  public int startSoftWrapLinesCurrent;
  public int startSoftWrapColumnDiff;
  public int startFoldedLines;
  public int startFoldingColumnDiff;

  public int endOffset;
  public int endLogicalLine;
  public int endLogicalColumn;
  public int endVisualColumn;
  public int endSoftWrapLinesBefore;
  public int endSoftWrapLinesCurrent;
  public int endSoftWrapColumnDiff;
  public int endFoldedLines;
  public int endFoldingColumnDiff;

  public boolean locked;

  private final Editor myEditor;

  /** Holds positions for the tabulation symbols on a target visual line sorted by offset in ascending order. */
  private List<TabData> myTabPositions = Collections.EMPTY_LIST;

  /** Holds information about single line fold regions representation data. */
  private TIntObjectHashMap<FoldingData> myFoldingData = DUMMY;

  CacheEntry(int visualLine, @NotNull Editor editor) {
    this.visualLine = visualLine;
    myEditor = editor;
  }

  public void setLineStartPosition(@NotNull EditorPosition context) {
    assert context.visualColumn == 0;
    startLogicalLine = context.logicalLine;
    startLogicalColumn = context.logicalColumn;
    visualLine = context.visualLine;
    startOffset = context.offset;
    startSoftWrapLinesBefore = context.softWrapLinesBefore;
    startSoftWrapLinesCurrent = context.softWrapLinesCurrent;
    startSoftWrapColumnDiff = context.softWrapColumnDiff;
    startFoldedLines = context.foldedLines;
    startFoldingColumnDiff = context.foldingColumnDiff;
  }

  public void setLineEndPosition(@NotNull EditorPosition position) {
    endOffset = position.offset;
    endLogicalLine = position.logicalLine;
    endLogicalColumn = position.logicalColumn;
    endVisualColumn = position.visualColumn;
    endSoftWrapLinesBefore = position.softWrapLinesBefore;
    endSoftWrapLinesCurrent = position.softWrapLinesCurrent;
    endSoftWrapColumnDiff = position.softWrapColumnDiff;
    endFoldedLines = position.foldedLines;
    endFoldingColumnDiff = position.foldingColumnDiff;
  }

  public EditorPosition buildStartLinePosition() {
    EditorPosition result = new EditorPosition(myEditor);
    result.logicalLine = startLogicalLine;
    result.logicalColumn = startLogicalColumn;
    result.offset = startOffset;
    result.visualLine = visualLine;
    result.visualColumn = 0;
    result.softWrapLinesBefore = startSoftWrapLinesBefore;
    result.softWrapLinesCurrent = startSoftWrapLinesCurrent;
    result.softWrapColumnDiff = startSoftWrapColumnDiff;
    result.foldedLines = startFoldedLines;
    result.foldingColumnDiff = startFoldingColumnDiff;
    return result;
  }

  public EditorPosition buildEndLinePosition() {
    EditorPosition result = new EditorPosition(myEditor);
    result.logicalLine = endLogicalLine;
    result.logicalColumn = endLogicalColumn;
    result.offset = endOffset;
    result.visualLine = visualLine;
    result.visualColumn = endVisualColumn;
    result.softWrapLinesBefore = endSoftWrapLinesBefore;
    result.softWrapLinesCurrent = endSoftWrapLinesCurrent;
    result.softWrapColumnDiff = endSoftWrapColumnDiff;
    result.foldedLines = endFoldedLines;
    result.foldingColumnDiff = endFoldingColumnDiff;
    return result;
  }

  /**
   * Removes fold data for all fold regions that start at or after the given offset.
   * 
   * @param offset  target offset
   */
  public void removeAllFoldDataAtOrAfter(final int offset) {
    if (myFoldingData == DUMMY || myFoldingData.isEmpty()) {
      return;
    }
    myFoldingData.retainEntries(new TIntObjectProcedure<FoldingData>() {
      @Override
      public boolean execute(int a, FoldingData b) {
        return a < offset;
      }
    });
  }
  
  @Nullable
  public FoldingData getFoldingData(@NotNull final FoldRegion region) {
    FoldingData candidate = myFoldingData.get(region.getStartOffset());
    if (candidate != null) {
      return candidate;
    }
    
    // Folding implementation is known to postpone actual fold region offsets update on document change, i.e. it performs
    // fold data caching with its further replace by up-to-date info. Hence, there is a possible case that soft wraps processing
    // advances fold region offset but folding model still provides old cached values. Hence, we're trying to match exact given
    // fold region against the cached data here.
    final Ref<FoldingData> result = new Ref<FoldingData>();
    myFoldingData.forEachValue(new TObjectProcedure<FoldingData>() {
      @Override
      public boolean execute(FoldingData data) {
        if (data.getFoldRegion().equals(region)) {
          result.set(data);
          return false;
        }
        return true;
      }
    });
    return result.get();
  }
  
  public void store(FoldingData foldData, int offset) {
    if (myFoldingData == DUMMY) {
      myFoldingData = new TIntObjectHashMap<FoldingData>();
    }
    myFoldingData.put(offset, foldData);
  }

  public List<TabData> getTabData() {
    return myTabPositions;
  }

  public void storeTabData(TabData tabData) {
    if (myTabPositions == Collections.EMPTY_LIST) {
      myTabPositions = new ArrayList<TabData>();
    }
    myTabPositions.add(tabData);
  }

  @SuppressWarnings("ForLoopReplaceableByForEach")
  public void advance(final int offsetDiff) {
    startOffset += offsetDiff;
    endOffset += offsetDiff;

    // 'For-each' loop is not used here because this code is called quite often and profile shows the Iterator usage here
    // produces performance drawback. 
    for (int i = 0; i < myTabPositions.size(); i++) {
      myTabPositions.get(i).offset += offsetDiff;
    }

    if (myFoldingData.isEmpty()) {
      return;
    }
    
    final TIntObjectHashMap<FoldingData> newFoldingData = new TIntObjectHashMap<FoldingData>(myFoldingData.size());
    myFoldingData.forEachEntry(new TIntObjectProcedure<FoldingData>() {
      @Override
      public boolean execute(int offset, FoldingData foldingData) {
        newFoldingData.put(offset + offsetDiff, foldingData);
        return true;
      }
    });
    myFoldingData = newFoldingData;
  }

  /**
   * Asks current entry to update its state in a way to set start line data to the end line data.
   */
  public void collapse() {
    endOffset = startOffset;
    endLogicalLine = startLogicalLine;
    endLogicalColumn = startLogicalColumn;
    endVisualColumn = 0;
    endSoftWrapLinesBefore = startSoftWrapLinesBefore;
    endSoftWrapLinesCurrent = startSoftWrapLinesCurrent;
    endSoftWrapColumnDiff = startSoftWrapColumnDiff;
    endFoldedLines = startFoldedLines;
    endFoldingColumnDiff = startFoldingColumnDiff;
  }

  @Override
  public int compareTo(CacheEntry e) {
    return visualLine - e.visualLine;
  }

  @Override
  public String toString() {
    return String.format(
      "%d - visual line: %d, offsets: %d-%d, logical lines: %d-%d, logical columns: %d-%d, end visual column: %d, "
      + "fold regions: %s, tab data: %s",
      System.identityHashCode(this), visualLine, startOffset, endOffset, startLogicalLine, endLogicalLine, startLogicalColumn,
      endLogicalColumn, endVisualColumn, Arrays.toString(myFoldingData.getValues()), myTabPositions
    );
  }

  @Override
  protected CacheEntry clone() {
    final CacheEntry result = new CacheEntry(visualLine, myEditor);

    result.startLogicalLine = startLogicalLine;
    result.startLogicalColumn = startLogicalColumn;
    result.startOffset = startOffset;
    result.startSoftWrapLinesBefore = startSoftWrapLinesBefore;
    result.startSoftWrapLinesCurrent = startSoftWrapLinesCurrent;
    result.startSoftWrapColumnDiff = startSoftWrapColumnDiff;
    result.startFoldedLines = startFoldedLines;
    result.startFoldingColumnDiff = startFoldingColumnDiff;

    result.endOffset = endOffset;
    result.endLogicalLine = endLogicalLine;
    result.endLogicalColumn = endLogicalColumn;
    result.endVisualColumn = endVisualColumn;
    result.endSoftWrapLinesBefore = endSoftWrapLinesBefore;
    result.endSoftWrapLinesCurrent = endSoftWrapLinesCurrent;
    result.endSoftWrapColumnDiff = endSoftWrapColumnDiff;
    result.endFoldedLines = endFoldedLines;
    result.endFoldingColumnDiff = endFoldingColumnDiff;

    myFoldingData.forEachEntry(new TIntObjectProcedure<FoldingData>() {
      @Override
      public boolean execute(int offset, FoldingData foldData) {
        result.store(foldData, offset);
        return true;
      }
    });
    for (TabData tabPosition : myTabPositions) {
      result.storeTabData(tabPosition);
    }

    return result;
  }
}