summaryrefslogtreecommitdiff
path: root/platform/lang-impl/src/com/intellij/execution/impl/ConsoleTokenUtil.java
blob: 0c798cd18c26d08bbb406ff0ffe9de51187afd42 (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
// Copyright 2000-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.execution.impl;

import com.intellij.execution.filters.HyperlinkInfo;
import com.intellij.execution.ui.ConsoleViewContentType;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.RangeMarker;
import com.intellij.openapi.editor.ex.MarkupModelEx;
import com.intellij.openapi.editor.ex.RangeHighlighterEx;
import com.intellij.openapi.editor.impl.DocumentMarkupModel;
import com.intellij.openapi.editor.impl.RangeMarkerImpl;
import com.intellij.openapi.editor.markup.HighlighterLayer;
import com.intellij.openapi.editor.markup.HighlighterTargetArea;
import com.intellij.openapi.editor.markup.MarkupModel;
import com.intellij.openapi.editor.markup.RangeHighlighter;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.util.text.StringUtil;
import org.jetbrains.annotations.NotNull;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

class ConsoleTokenUtil {
  private static final char BACKSPACE = '\b';
  private static final Key<ConsoleViewContentType> CONTENT_TYPE = Key.create("ConsoleViewContentType");
  private static final Key<Boolean> USER_INPUT_SENT = Key.create("USER_INPUT_SENT");
  static final Key<Boolean> MANUAL_HYPERLINK = Key.create("MANUAL_HYPERLINK");

  // convert all "a\bc" sequences to "c", not crossing the line boundaries in the process
  private static void normalizeBackspaceCharacters(@NotNull StringBuilder text) {
    int ind = StringUtil.indexOf(text, BACKSPACE);
    if (ind < 0) {
      return;
    }
    int guardLength = 0;
    int newLength = 0;
    for (int i = 0; i < text.length(); i++) {
      char ch = text.charAt(i);
      boolean append;
      if (ch == BACKSPACE) {
        assert guardLength <= newLength;
        if (guardLength == newLength) {
          // Backspace is the first char in a new line:
          // Keep backspace at the first line (guardLength == 0) as it might be in the middle of the actual line,
          // handle it later (see getBackspacePrefixLength).
          // Otherwise (for non-first lines), skip backspace as it can't be interpreted if located right after line ending.
          append = guardLength == 0;
        }
        else {
          append = text.charAt(newLength - 1) == BACKSPACE;
          if (!append) {
            newLength--; // interpret \b: delete prev char
          }
        }
      }
      else {
        append = true;
      }
      if (append) {
        text.setCharAt(newLength, ch);
        newLength++;
        if (ch == '\r' || ch == '\n') guardLength = newLength;
      }
    }
    text.setLength(newLength);
  }

  static int evaluateBackspacesInTokens(@NotNull List<? extends TokenBuffer.TokenInfo> source,
                                        int sourceStartIndex,
                                        @NotNull List<? super TokenBuffer.TokenInfo> dest) {
    int backspacesFromNextToken = 0;
    for (int i = source.size() - 1; i >= sourceStartIndex; i--) {
      TokenBuffer.TokenInfo token = source.get(i);
      TokenBuffer.TokenInfo newToken;
      if (StringUtil.containsChar(token.getText(), BACKSPACE) || backspacesFromNextToken > 0) {
        StringBuilder tokenTextBuilder = new StringBuilder(token.getText().length() + backspacesFromNextToken);
        tokenTextBuilder.append(token.getText());
        StringUtil.repeatSymbol(tokenTextBuilder, BACKSPACE, backspacesFromNextToken);
        normalizeBackspaceCharacters(tokenTextBuilder);
        backspacesFromNextToken = getBackspacePrefixLength(tokenTextBuilder);
        String newText = tokenTextBuilder.substring(backspacesFromNextToken);
        newToken = new TokenBuffer.TokenInfo(token.contentType, newText, token.getHyperlinkInfo());
      }
      else {
        newToken = token;
      }
      dest.add(newToken);
    }
    Collections.reverse(dest);
    return backspacesFromNextToken;
  }

  private static int getBackspacePrefixLength(@NotNull CharSequence text) {
    return StringUtil.countChars(text, BACKSPACE, 0, true);
  }

  static ConsoleViewContentType getTokenType(@NotNull RangeMarker m) {
    return m.getUserData(CONTENT_TYPE);
  }

  private static void saveTokenType(@NotNull RangeMarker m, @NotNull ConsoleViewContentType contentType) {
    m.putUserData(CONTENT_TYPE, contentType);
  }

  // finds range marker the [offset..offset+1) belongs to
  static RangeMarker findTokenMarker(@NotNull Editor editor, @NotNull Project project, int offset) {
    ApplicationManager.getApplication().assertIsDispatchThread();
    RangeMarker[] marker = new RangeMarker[1];
    MarkupModelEx model = (MarkupModelEx)DocumentMarkupModel.forDocument(editor.getDocument(), project, true);
    model.processRangeHighlightersOverlappingWith(offset, offset, m->{
      if (getTokenType(m) == null || m.getStartOffset() > offset || offset + 1 > m.getEndOffset()) return true;
      marker[0] = m;
      return false;
    });

    return marker[0];
  }

  static void createTokenRangeHighlighter(@NotNull Editor editor,
                                          @NotNull Project project,
                                          @NotNull ConsoleViewContentType contentType,
                                          int startOffset,
                                          int endOffset,
                                          boolean mergeWithThePreviousSameTypeToken) {
    ApplicationManager.getApplication().assertIsDispatchThread();
    MarkupModelEx model = (MarkupModelEx)DocumentMarkupModel.forDocument(editor.getDocument(), project, true);
    int layer = HighlighterLayer.SYNTAX + 1; // make custom filters able to draw their text attributes over the default ones
    if (mergeWithThePreviousSameTypeToken && startOffset > 0) {
      RangeMarker prevMarker = findTokenMarker(editor, project, startOffset - 1);
      ConsoleViewContentType prevMarkerType = prevMarker == null ? null : getTokenType(prevMarker);
      int prevMarkerEndOffset = prevMarkerType == null ? -1 : prevMarker.getEndOffset();
      if (contentType.equals(prevMarkerType) &&
          prevMarkerEndOffset >= 0 &&
          prevMarkerEndOffset < editor.getDocument().getTextLength() &&
          // must not merge tokens with end line because user input should be separated by new lines
          editor.getDocument().getCharsSequence().charAt(prevMarkerEndOffset - 1) != '\n') {
        startOffset = prevMarker.getStartOffset();
        prevMarker.dispose();
      }
    }
    model.addRangeHighlighterAndChangeAttributes(
      contentType.getAttributesKey(), startOffset, endOffset, layer, HighlighterTargetArea.EXACT_RANGE, false,
      rm -> {
        // fallback for contentTypes which provide only attributes
        if (rm.getTextAttributesKey() == null) {
          rm.setTextAttributes(contentType.getAttributes());
        }
        saveTokenType(rm, contentType);
      });
  }

  static void updateAllTokenTextAttributes(@NotNull Editor editor, @NotNull Project project) {
    MarkupModel model = DocumentMarkupModel.forDocument(editor.getDocument(), project, false);
    for (RangeHighlighter tokenMarker : model.getAllHighlighters()) {
      ConsoleViewContentType contentType = getTokenType(tokenMarker);
      if (contentType != null && contentType.getAttributesKey() == null && tokenMarker instanceof RangeHighlighterEx) {
        ((RangeHighlighterEx)tokenMarker).setTextAttributes(contentType.getAttributes());
      }
    }
  }

  @NotNull
  static CharSequence computeTextToSend(@NotNull Editor editor, @NotNull Project project) {
    StringBuilder textToSend = new StringBuilder();
    // compute text input from the console contents:
    // all range markers beginning from the caret offset backwards, marked as user input and not marked as already sent
    for (RangeMarker marker = findTokenMarker(editor, project, editor.getCaretModel().getOffset() - 1);
         marker != null;
         marker = ((RangeMarkerImpl)marker).findRangeMarkerBefore()) {
      ConsoleViewContentType tokenType = getTokenType(marker);
      if (tokenType != null) {
        if (tokenType != ConsoleViewContentType.USER_INPUT || marker.getUserData(USER_INPUT_SENT) == Boolean.TRUE) {
          break;
        }
        marker.putUserData(USER_INPUT_SENT, true);
        textToSend.insert(0, marker.getDocument().getText(TextRange.create(marker)));
      }
    }
    return textToSend;
  }

  static void highlightTokenTextAttributes(@NotNull Editor editor,
                                           @NotNull Project project,
                                           @NotNull List<? extends TokenBuffer.TokenInfo> tokens,
                                           @NotNull EditorHyperlinkSupport hyperlinks,
                                           @NotNull Collection<? super ConsoleViewContentType> contentTypes,
                                           @NotNull List<? super Pair<String, ConsoleViewContentType>> contents) {
    // add token information as range markers
    // start from the end because portion of the text can be stripped from the document beginning because of a cycle buffer
    int offset = editor.getDocument().getTextLength();
    int tokenLength = 0;
    for (int i = tokens.size() - 1; i >= 0; i--) {
      TokenBuffer.TokenInfo token = tokens.get(i);
      contentTypes.add(token.contentType);
      contents.add(new Pair<>(token.getText(), token.contentType));
      tokenLength += token.length();
      TokenBuffer.TokenInfo prevToken = i == 0 ? null : tokens.get(i - 1);
      if (prevToken != null && token.contentType == prevToken.contentType && token.getHyperlinkInfo() == prevToken.getHyperlinkInfo()) {
        // do not create highlighter yet because can merge previous token with the current
        continue;
      }
      int start = Math.max(0, offset - tokenLength);
      if (start == offset) {
        continue;
      }
      HyperlinkInfo info = token.getHyperlinkInfo();
      if (info != null) {
        hyperlinks.createHyperlink(start, offset, null, info).putUserData(MANUAL_HYPERLINK, true);
      }
      createTokenRangeHighlighter(editor, project, token.contentType, start, offset, false);
      offset = start;
      tokenLength = 0;
    }
  }
}