summaryrefslogtreecommitdiff
path: root/platform/platform-impl/src/com/intellij/openapi/editor/impl/softwrap/mapping/LogicalToVisualMappingStrategy.java
blob: 773d4fdc4d827ed439aac87cefc1624f888ce54f (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
/*
 * 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.*;
import com.intellij.openapi.editor.impl.EditorTextRepresentationHelper;
import com.intellij.openapi.editor.impl.softwrap.SoftWrapsStorage;
import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
* @author Denis Zhdanov
* @since Sep 9, 2010 10:08:08 AM
*/
@SuppressWarnings({"UnusedDeclaration"})
class LogicalToVisualMappingStrategy extends AbstractMappingStrategy<VisualPosition> {

  private LogicalPosition myTargetLogical;

  LogicalToVisualMappingStrategy(@NotNull LogicalPosition logical, @NotNull Editor editor, @NotNull SoftWrapsStorage storage,
                                 @NotNull EditorTextRepresentationHelper representationHelper, @NotNull List<CacheEntry> cache)
    throws IllegalStateException
  {
    super(editor, storage, cache, representationHelper);
    myTargetLogical = logical;
  }

  public void init(@NotNull final LogicalPosition logical, final @NotNull List<CacheEntry> cache) {
    reset();

    myTargetLogical = logical;
    int start = 0;
    int end = cache.size() - 1;

    // We inline binary search here because profiling indicates that it becomes bottleneck to use Collections.binarySearch().
    while (start <= end) {
      int i = (end + start) >>> 1;
      CacheEntry cacheEntry = cache.get(i);

      // There is a possible case that single logical line is represented on multiple visual lines due to soft wraps processing.
      // Hence, we check for bot logical line and logical columns during searching 'anchor' cache entry.

      if (cacheEntry.endLogicalLine < logical.line
          || (cacheEntry.endLogicalLine == logical.line && myStorage.getSoftWrap(cacheEntry.endOffset) != null
              && cacheEntry.endLogicalColumn <= logical.column)) {
        start = i + 1;
        continue;
      }
      if (cacheEntry.startLogicalLine > logical.line
          || (cacheEntry.startLogicalLine == logical.line
              && cacheEntry.startLogicalColumn > logical.column)) {
        end = i - 1;
        continue;
      }

      // There is a possible case that currently found cache entry corresponds to soft-wrapped line and soft wrap occurred
      // at target logical column. We need to return cache entry for the next visual line then (because single logical column
      // is shared for 'before soft wrap' and 'after soft wrap' positions and we want to use the one that points to
      // 'after soft wrap' position).
      if (cacheEntry.endLogicalLine == logical.line && cacheEntry.endLogicalColumn == logical.column && i < cache.size() - 1) {
        CacheEntry nextLineCacheEntry = cache.get(i + 1);
        if (nextLineCacheEntry.startLogicalLine == logical.line
            && nextLineCacheEntry.startLogicalColumn == logical.column) {
          setInitialPosition(nextLineCacheEntry.buildStartLinePosition());
          return;
        }
      }
      setInitialPosition(cacheEntry.buildStartLinePosition());
      return;
    }

    throw new IllegalStateException(String.format(
      "Can't map logical position (%s) to visual position. Reason: no cached information information about target visual "
      + "line is found. Registered entries: %s", logical, cache
    ));
  }

  @Override
  protected VisualPosition buildIfExceeds(EditorPosition position, int offset) {
    if (position.logicalLine < myTargetLogical.line) {
       return null;
    }

    int diff = myTargetLogical.column - position.logicalColumn;
    if (offset - position.offset < diff) {
      return null;
    }

    position.visualColumn += diff;
    // Don't update other dimensions like logical position and offset because we need only visual position here.
    return position.buildVisualPosition();
  }

  @Override
  protected VisualPosition buildIfExceeds(@NotNull EditorPosition context, @NotNull FoldRegion foldRegion) {
    int foldEndLine = myEditor.getDocument().getLineNumber(foldRegion.getEndOffset());
    if (myTargetLogical.line > foldEndLine) {
      return null;
    }

    if (myTargetLogical.line < foldEndLine) {
      // Map all logical position that point inside collapsed fold region to visual position of its start.
      return context.buildVisualPosition();
    }

    FoldingData data = getFoldRegionData(foldRegion);
    int foldEndColumn;
    if (data == null) {
      int xStart = myEditor.getDocument().getLineNumber(foldRegion.getStartOffset()) == foldEndLine ? context.x : 0;
      foldEndColumn = myRepresentationHelper.toVisualColumnSymbolsNumber(
        myEditor.getDocument().getCharsSequence(), foldRegion.getStartOffset(), foldRegion.getEndOffset(), xStart
      );
    } else {
      foldEndColumn = data.getCollapsedSymbolsWidthInColumns();
    }
    
    if (foldEndLine == context.logicalLine) {
      // Single-line fold region.
      foldEndColumn += context.logicalColumn;
    }

    if (foldEndColumn <= myTargetLogical.column) {
      return null;
    }

    // Map all logical position that point inside collapsed fold region to visual position of its start.
    return context.buildVisualPosition();
  }

  @Override
  protected VisualPosition buildIfExceeds(EditorPosition context, TabData tabData) {
    if (context.logicalLine < myTargetLogical.line) {
      return null;
    }

    int diff = myTargetLogical.column - context.logicalColumn;
    if (diff >= tabData.widthInColumns) {
      return null;
    }

    context.logicalColumn += diff;
    context.visualColumn += diff;

    return context.buildVisualPosition();
  }

  @Override
  public VisualPosition processSoftWrap(@NotNull EditorPosition position, SoftWrap softWrap) {
    position.visualColumn = softWrap.getIndentInColumns();
    position.softWrapColumnDiff += softWrap.getIndentInColumns();

    if (position.logicalLine < myTargetLogical.line || position.logicalColumn != myTargetLogical.column) {
      return null;
    }
    return position.buildVisualPosition();
  }

  @NotNull
  @Override
  public VisualPosition build(@NotNull EditorPosition position) {
    int diff = myTargetLogical.column - position.logicalColumn;
    position.logicalColumn += diff;
    position.visualColumn += diff;
    position.offset += diff;
    return position.buildVisualPosition();
  }
}