Java Code Examples for com.intellij.openapi.util.TextRange#shiftRight()

The following examples show how to use com.intellij.openapi.util.TextRange#shiftRight() . 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 File: ProblemDescriptorUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
public static String extractHighlightedText(@Nonnull CommonProblemDescriptor descriptor, PsiElement psiElement) {
  if (psiElement == null || !psiElement.isValid()) return "";
  String ref = psiElement.getText();
  if (descriptor instanceof ProblemDescriptorBase) {
    TextRange textRange = ((ProblemDescriptorBase)descriptor).getTextRange();
    final TextRange elementRange = psiElement.getTextRange();
    if (textRange != null && elementRange != null) {
      textRange = textRange.shiftRight(-elementRange.getStartOffset());
      if (textRange.getStartOffset() >= 0 && textRange.getEndOffset() <= elementRange.getLength()) {
        ref = textRange.substring(ref);
      }
    }
  }
  ref = StringUtil.replaceChar(ref, '\n', ' ').trim();
  ref = StringUtil.first(ref, 100, true);
  return ref;
}
 
Example 2
Source File: CodeFormatterFacade.java    From consulo with Apache License 2.0 6 votes vote down vote up
private TextRange preprocessEnabledRanges(@Nonnull final ASTNode node, @Nonnull TextRange range) {
  TextRange result = TextRange.create(range.getStartOffset(), range.getEndOffset());
  List<TextRange> enabledRanges = myTagHandler.getEnabledRanges(node, result);
  int delta = 0;
  for (TextRange enabledRange : enabledRanges) {
    enabledRange = enabledRange.shiftRight(delta);
    for (PreFormatProcessor processor : PreFormatProcessor.EP_NAME.getExtensionList()) {
      if (processor.changesWhitespacesOnly() || !myCanChangeWhitespaceOnly) {
        TextRange processedRange = processor.process(node, enabledRange);
        delta += processedRange.getLength() - enabledRange.getLength();
      }
    }
  }
  result = result.grown(delta);
  return result;
}
 
Example 3
Source File: PsiMultiReference.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public TextRange getRangeInElement(){
  final PsiReference chosenRef = chooseReference();
  TextRange rangeInElement = chosenRef.getRangeInElement();
  PsiElement element = chosenRef.getElement();
  while(element != myElement) {
    rangeInElement = rangeInElement.shiftRight(element.getStartOffsetInParent());
    element = element.getParent();
    if (element instanceof PsiFile) break;
  }
  return rangeInElement;
}
 
Example 4
Source File: NaturalLanguageTextSelectioner.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
private static TextRange expandSelection(PsiElement e, CharSequence editorText, int selStart, int selEnd) {
  TextRange range = e.getTextRange();
  int shift = range.getStartOffset();
  if (selStart <= shift || selEnd >= range.getEndOffset()) {
    return null;
  }

  String elementText = editorText.subSequence(shift, range.getEndOffset()).toString();
  int start = selStart - shift;
  int end = selEnd - shift;

  TextRange best = findSentenceRange(elementText, start, end);
  best = narrowRange(best, findCustomRange(elementText, start, end, '\"', '\"'));
  best = narrowRange(best, findCustomRange(elementText, start, end, '(', ')'));
  best = narrowRange(best, findCustomRange(elementText, start, end, '<', '>'));
  best = narrowRange(best, findCustomRange(elementText, start, end, '[', ']'));

  TextRange natural = findNaturalRange(elementText, start, end);
  if (!natural.contains(best)) {
    return null;
  }

  TextRange paragraph = findParagraphRange(elementText, start, end);
  if (best.getStartOffset() == start && best.getEndOffset() == end || !paragraph.contains(best)) {
    return paragraph.shiftRight(shift);
  }


  return best.shiftRight(shift);
}
 
Example 5
Source File: PsiDynaReference.java    From consulo with Apache License 2.0 5 votes vote down vote up
private TextRange getRange(PsiReference reference) {
  TextRange rangeInElement = reference.getRangeInElement();
  PsiElement element = reference.getElement();
  while(element != myElement) {
    rangeInElement = rangeInElement.shiftRight(element.getStartOffsetInParent());
    element = element.getParent();
    if (element instanceof PsiFile) break;
  }
  return rangeInElement;
}
 
Example 6
Source File: CodeStyleManagerImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static void postProcessEnabledRanges(@Nonnull final PsiFile file, @Nonnull TextRange range, CodeStyleSettings settings) {
  List<TextRange> enabledRanges = new FormatterTagHandler(getSettings(file)).getEnabledRanges(file.getNode(), range);
  int delta = 0;
  for (TextRange enabledRange : enabledRanges) {
    enabledRange = enabledRange.shiftRight(delta);
    for (PostFormatProcessor processor : PostFormatProcessor.EP_NAME.getExtensionList()) {
      TextRange processedRange = processor.processText(file, enabledRange, settings);
      delta += processedRange.getLength() - enabledRange.getLength();
    }
  }
}
 
Example 7
Source File: FormatProcessorUtils.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static int replaceWhiteSpace(final FormattingModel model,
                                    @Nonnull final LeafBlockWrapper block,
                                    int shift,
                                    final CharSequence _newWhiteSpace,
                                    final CommonCodeStyleSettings.IndentOptions options
) {
  final WhiteSpace whiteSpace = block.getWhiteSpace();
  final TextRange textRange = whiteSpace.getTextRange();
  final TextRange wsRange = textRange.shiftRight(shift);
  final String newWhiteSpace = _newWhiteSpace.toString();
  TextRange newWhiteSpaceRange = model instanceof FormattingModelEx
                                 ? ((FormattingModelEx) model).replaceWhiteSpace(wsRange, block.getNode(), newWhiteSpace)
                                 : model.replaceWhiteSpace(wsRange, newWhiteSpace);

  shift += newWhiteSpaceRange.getLength() - textRange.getLength();

  if (block.isLeaf() && whiteSpace.containsLineFeeds() && block.containsLineFeeds()) {
    final TextRange currentBlockRange = block.getTextRange().shiftRight(shift);

    IndentInside oldBlockIndent = whiteSpace.getInitialLastLineIndent();
    IndentInside whiteSpaceIndent = IndentInside.createIndentOn(IndentInside.getLastLine(newWhiteSpace));
    final int shiftInside = calcShift(oldBlockIndent, whiteSpaceIndent, options);

    if (shiftInside != 0 || !oldBlockIndent.equals(whiteSpaceIndent)) {
      final TextRange newBlockRange = model.shiftIndentInsideRange(block.getNode(), currentBlockRange, shiftInside);
      shift += newBlockRange.getLength() - block.getLength();
    }
  }
  return shift;
}
 
Example 8
Source File: PsiUtils.java    From intellij with Apache License 2.0 4 votes vote down vote up
public static TextRange childRangeInParent(TextRange parentRange, TextRange childRange) {
  return childRange.shiftRight(-parentRange.getStartOffset());
}
 
Example 9
Source File: ParameterInfoComponent.java    From consulo with Apache License 2.0 4 votes vote down vote up
private String setup(String text,
                     Function<? super String, String> escapeFunction,
                     int highlightStartOffset,
                     int highlightEndOffset,
                     boolean isDisabled,
                     boolean strikeout,
                     boolean isDisabledBeforeHighlight,
                     Color background) {
  StringBuilder buf = new StringBuilder(text.length());
  setBackground(background);

  String[] lines = UIUtil.splitText(text, getFontMetrics(BOLD_FONT),
                                    // disable splitting by width, to avoid depending on platform's font in tests
                                    ApplicationManager.getApplication().isUnitTestMode() ? Integer.MAX_VALUE : myWidthLimit, ',');

  int lineOffset = 0;

  boolean hasHighlighting = highlightStartOffset >= 0 && highlightEndOffset > highlightStartOffset;
  TextRange highlightingRange = hasHighlighting ? new TextRange(highlightStartOffset, highlightEndOffset) : null;

  for (int i = 0; i < lines.length; i++) {
    String line = lines[i];
    OneLineComponent component = getOneLineComponent(i);

    TextRange lRange = new TextRange(lineOffset, lineOffset + line.length());
    TextRange hr = highlightingRange == null ? null : lRange.intersection(highlightingRange);
    hr = hr == null ? null : hr.shiftRight(-lineOffset);

    String before = escapeString(hr == null ? line : line.substring(0, hr.getStartOffset()), escapeFunction);
    String in = hr == null ? "" : escapeString(hr.substring(line), escapeFunction);
    String after = hr == null ? "" : escapeString(line.substring(hr.getEndOffset()), escapeFunction);

    TextRange escapedHighlightingRange = in.isEmpty() ? null : TextRange.create(before.length(), before.length() + in.length());
    buf.append(component.setup(before + in + after, isDisabled, strikeout, background, escapedHighlightingRange,
                               isDisabledBeforeHighlight && (highlightStartOffset < 0 || highlightEndOffset > lineOffset)));

    lineOffset += line.length();
  }
  trimComponents(lines.length);
  return buf.toString();
}
 
Example 10
Source File: WebReference.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public TextRange getTextRange() {
  final TextRange rangeInElement = getRangeInElement();
  final TextRange elementRange = myElement.getTextRange();
  return elementRange != null ? rangeInElement.shiftRight(elementRange.getStartOffset()) : rangeInElement;
}