Java Code Examples for com.intellij.openapi.editor.Document#getImmutableCharSequence()

The following examples show how to use com.intellij.openapi.editor.Document#getImmutableCharSequence() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: bamboo-soy   File: ClosingTagHandler.java    License: Apache License 2.0 6 votes vote down vote up
private static void insertClosingTag(@NotNull Editor editor, int offset, String tag) {
  Document document = editor.getDocument();
  CharSequence charSequence = document.getImmutableCharSequence();
  int startPosition = offset - 2;
  // Consume second left brace if present.
  if (offset > 0 && charSequence.charAt(startPosition - 1) == '{') {
    startPosition--;
  }
  int endPosition = offset;
  // Consume at most 2 right braces if present.
  while (endPosition < charSequence.length()
      && charSequence.charAt(endPosition) == '}'
      && endPosition < offset + 2) {
    endPosition++;
  }
  editor.getDocument().replaceString(startPosition, endPosition, tag);
  editor.getCaretModel().moveToOffset(startPosition + tag.length());

}
 
Example 2
Source Project: consulo   File: DiffRequestFactoryImpl.java    License: Apache License 2.0 6 votes vote down vote up
@Nonnull
@Override
public MergeRequest createMergeRequest(@Nullable Project project,
                                       @Nullable FileType fileType,
                                       @Nonnull Document outputDocument,
                                       @Nonnull List<String> textContents,
                                       @Nullable String title,
                                       @Nonnull List<String> titles,
                                       @Nullable Consumer<MergeResult> applyCallback) throws InvalidDiffRequestException {
  if (textContents.size() != 3) throw new IllegalArgumentException();
  if (titles.size() != 3) throw new IllegalArgumentException();

  if (!DiffUtil.canMakeWritable(outputDocument)) throw new InvalidDiffRequestException("Output is read only");

  DocumentContent outputContent = myContentFactory.create(project, outputDocument, fileType);
  CharSequence originalContent = outputDocument.getImmutableCharSequence();

  List<DocumentContent> contents = new ArrayList<>(3);
  for (String text : textContents) {
    contents.add(myContentFactory.create(project, text, fileType));
  }

  return new TextMergeRequestImpl(project, outputContent, originalContent, contents, title, titles, applyCallback);
}
 
Example 3
Source Project: consulo   File: DiffRequestFactoryImpl.java    License: Apache License 2.0 6 votes vote down vote up
@Nonnull
@Override
public TextMergeRequest createTextMergeRequest(@Nullable Project project,
                                               @Nonnull VirtualFile output,
                                               @Nonnull List<byte[]> byteContents,
                                               @Nullable String title,
                                               @Nonnull List<String> contentTitles,
                                               @Nullable Consumer<MergeResult> applyCallback) throws InvalidDiffRequestException {
  if (byteContents.size() != 3) throw new IllegalArgumentException();
  if (contentTitles.size() != 3) throw new IllegalArgumentException();

  final Document outputDocument = FileDocumentManager.getInstance().getDocument(output);
  if (outputDocument == null) throw new InvalidDiffRequestException("Can't get output document: " + output.getPresentableUrl());
  if (!DiffUtil.canMakeWritable(outputDocument)) throw new InvalidDiffRequestException("Output is read only: " + output.getPresentableUrl());

  DocumentContent outputContent = myContentFactory.create(project, outputDocument);
  CharSequence originalContent = outputDocument.getImmutableCharSequence();

  List<DocumentContent> contents = new ArrayList<>(3);
  for (byte[] bytes : byteContents) {
    contents.add(myContentFactory.createDocumentFromBytes(project, bytes, output));
  }

  return new TextMergeRequestImpl(project, outputContent, originalContent, contents, title, contentTitles, applyCallback);
}
 
Example 4
Source Project: consulo   File: LogicalPositionCache.java    License: Apache License 2.0 5 votes vote down vote up
private static LineData create(@Nonnull Document document, int line, int tabSize) {
  int start = document.getLineStartOffset(line);
  int end = document.getLineEndOffset(line);
  int cacheSize = (end - start) / CACHE_FREQUENCY;
  int[] cache = ArrayUtil.newIntArray(cacheSize);
  CharSequence text = document.getImmutableCharSequence();
  int column = 0;
  boolean hasTabsOrSurrogates = false;
  for (int i = start; i < end; i++) {
    if (i > start && (i - start) % CACHE_FREQUENCY == 0) {
      cache[(i - start) / CACHE_FREQUENCY - 1] = column;
    }
    char c = text.charAt(i);
    if (c == '\t') {
      column = (column / tabSize + 1) * tabSize;
      hasTabsOrSurrogates = true;
    }
    else {
      if (Character.isHighSurrogate(c)) {
        hasTabsOrSurrogates = true;
        if (i + 1 < text.length() && Character.isLowSurrogate(text.charAt(i + 1))) continue;
      }
      else {
        hasTabsOrSurrogates |= Character.isLowSurrogate(c);
      }
      column++;
    }
  }
  if (cacheSize > 0 && (end - start) % CACHE_FREQUENCY == 0) cache[cacheSize - 1] = column;
  return hasTabsOrSurrogates ? new LineData(cache) : TRIVIAL;
}
 
Example 5
Source Project: consulo   File: DocumentUtil.java    License: Apache License 2.0 5 votes vote down vote up
public static int getFirstNonSpaceCharOffset(@Nonnull Document document, int startOffset, int endOffset) {
  CharSequence text = document.getImmutableCharSequence();
  for (int i = startOffset; i < endOffset; i++) {
    char c = text.charAt(i);
    if (c != ' ' && c != '\t') {
      return i;
    }
  }
  return startOffset;
}
 
Example 6
Source Project: consulo   File: FoldingUpdate.java    License: Apache License 2.0 5 votes vote down vote up
private static void getFoldingsFor(@Nonnull PsiFile file, @Nonnull Document document, @Nonnull List<? super RegionInfo> elementsToFold, boolean quick) {
  final FileViewProvider viewProvider = file.getViewProvider();
  TextRange docRange = TextRange.from(0, document.getTextLength());
  Comparator<Language> preferBaseLanguage = Comparator.comparing((Language l) -> l != viewProvider.getBaseLanguage());
  List<Language> languages = ContainerUtil.sorted(viewProvider.getLanguages(), preferBaseLanguage.thenComparing(Language::getID));

  DocumentEx copyDoc = languages.size() > 1 ? new DocumentImpl(document.getImmutableCharSequence()) : null;
  List<RangeMarker> hardRefToRangeMarkers = new ArrayList<>();

  for (Language language : languages) {
    final PsiFile psi = viewProvider.getPsi(language);
    final FoldingBuilder foldingBuilder = LanguageFolding.INSTANCE.forLanguage(language);
    if (psi != null && foldingBuilder != null) {
      for (FoldingDescriptor descriptor : LanguageFolding.buildFoldingDescriptors(foldingBuilder, psi, document, quick)) {
        PsiElement psiElement = descriptor.getElement().getPsi();
        if (psiElement == null) {
          LOG.error("No PSI for folding descriptor " + descriptor);
          continue;
        }
        TextRange range = descriptor.getRange();
        if (!docRange.contains(range)) {
          diagnoseIncorrectRange(psi, document, language, foldingBuilder, descriptor, psiElement);
          continue;
        }

        if (copyDoc != null && !addNonConflictingRegion(copyDoc, range, hardRefToRangeMarkers)) {
          continue;
        }

        RegionInfo regionInfo = new RegionInfo(descriptor, psiElement, foldingBuilder);
        elementsToFold.add(regionInfo);
      }
    }
  }
}
 
Example 7
Source Project: consulo   File: MockPsiDocumentManager.java    License: Apache License 2.0 4 votes vote down vote up
@Nonnull
@Override
public CharSequence getLastCommittedText(@Nonnull Document document) {
  return document.getImmutableCharSequence();
}
 
Example 8
Source Project: consulo   File: DocumentUtil.java    License: Apache License 2.0 4 votes vote down vote up
public static boolean isSurrogatePair(@Nonnull Document document, int offset) {
  CharSequence text = document.getImmutableCharSequence();
  if (offset < 0 || (offset + 1) >= text.length()) return false;
  return Character.isSurrogatePair(text.charAt(offset), text.charAt(offset + 1));
}
 
Example 9
@Override
protected void doBeforeCharDeleted(char c, PsiFile file, Editor editor) {
  Project project = file.getProject();
  Document document = editor.getDocument();
  CharSequence charSequence = document.getImmutableCharSequence();
  CaretModel caretModel = editor.getCaretModel();
  int caretOffset = caretModel.getOffset();
  LogicalPosition pos = caretModel.getLogicalPosition();
  int lineStartOffset = document.getLineStartOffset(pos.line);
  int beforeWhitespaceOffset = CharArrayUtil.shiftBackward(charSequence, caretOffset - 1, " \t") + 1;
  if (beforeWhitespaceOffset != lineStartOffset) {
    myReplacement = null;
    return;
  }
  PsiDocumentManager.getInstance(project).commitDocument(document);
  CodeStyleFacade codeStyleFacade = CodeStyleFacade.getInstance(project);
  myReplacement = codeStyleFacade.getLineIndent(document, lineStartOffset);
  if (myReplacement == null) {
    return;
  }
  int tabSize = codeStyleFacade.getTabSize(file.getFileType());
  int targetColumn = getWidth(myReplacement, tabSize);
  int endOffset = CharArrayUtil.shiftForward(charSequence, caretOffset, " \t");
  LogicalPosition logicalPosition = caretOffset < endOffset ? editor.offsetToLogicalPosition(endOffset) : pos;
  int currentColumn = logicalPosition.column;
  if (currentColumn > targetColumn) {
    myStartOffset = lineStartOffset;
  }
  else if (logicalPosition.line == 0) {
    myStartOffset = 0;
    myReplacement = "";
  }
  else {
    int prevLineEndOffset = document.getLineEndOffset(logicalPosition.line - 1);
    myStartOffset = CharArrayUtil.shiftBackward(charSequence, prevLineEndOffset - 1, " \t") + 1;
    if (myStartOffset != document.getLineStartOffset(logicalPosition.line - 1)) {
      int spacing = CodeStyleManager.getInstance(project).getSpacing(file, endOffset);
      myReplacement = StringUtil.repeatSymbol(' ', Math.max(0, spacing));
    }
  }
}
 
Example 10
Source Project: consulo   File: LexerEditorHighlighter.java    License: Apache License 2.0 3 votes vote down vote up
@Nonnull
public List<TextAttributes> getAttributesForPreviousAndTypedChars(@Nonnull Document document, int offset, char c) {
  final CharSequence text = document.getImmutableCharSequence();

  CharSequence newText = StringUtil.replaceSubSequence(text, offset, offset, new SingleCharSequence(c));

  final List<IElementType> tokenTypes = getTokenType(newText, offset);

  return Arrays.asList(getAttributes(tokenTypes.get(0)).clone(), getAttributes(tokenTypes.get(1)).clone());
}