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

The following examples show how to use com.intellij.openapi.util.TextRange#contains() . 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: CommentByBlockCommentHandler.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nullable
private PsiElement findCommentAtCaret() {
  int offset = myCaret.getOffset();
  TextRange range = new TextRange(myCaret.getSelectionStart(), myCaret.getSelectionEnd());
  if (offset == range.getEndOffset()) {
    offset--;
  }
  if (offset <= range.getStartOffset()) {
    offset++;
  }
  PsiElement comment = getCommentAtOffset(offset);
  if (comment == null || myCaret.hasSelection() && !range.contains(comment.getTextRange())) {
    return null;
  }

  return comment;
}
 
Example 2
Source File: TypedHandler.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
public static Editor injectedEditorIfCharTypedIsSignificant(final int charTyped, @Nonnull Editor editor, @Nonnull PsiFile oldFile) {
  int offset = editor.getCaretModel().getOffset();
  // even for uncommitted document try to retrieve injected fragment that has been there recently
  // we are assuming here that when user is (even furiously) typing, injected language would not change
  // and thus we can use its lexer to insert closing braces etc
  List<DocumentWindow> injected = InjectedLanguageManager.getInstance(oldFile.getProject()).getCachedInjectedDocumentsInRange(oldFile, ProperTextRange.create(offset, offset));
  for (DocumentWindow documentWindow : injected) {
    if (documentWindow.isValid() && documentWindow.containsRange(offset, offset)) {
      PsiFile injectedFile = PsiDocumentManager.getInstance(oldFile.getProject()).getPsiFile(documentWindow);
      if (injectedFile != null) {
        Editor injectedEditor = InjectedLanguageUtil.getInjectedEditorForInjectedFile(editor, injectedFile);
        // IDEA-52375/WEB-9105 fix: last quote in editable fragment should be handled by outer language quote handler
        TextRange hostRange = documentWindow.getHostRange(offset);
        CharSequence sequence = editor.getDocument().getCharsSequence();
        if (sequence.length() > offset && charTyped != Character.codePointAt(sequence, offset) || hostRange != null && hostRange.contains(offset)) {
          return injectedEditor;
        }
      }
    }
  }

  return editor;
}
 
Example 3
Source File: ParameterInfoController.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public boolean isInnermostContext() {
  PsiElement ourOwner = myComponent.getParameterOwner();
  if (ourOwner == null || !ourOwner.isValid()) return false;
  TextRange ourRange = ourOwner.getTextRange();
  if (ourRange == null) return false;
  List<ParameterInfoController> allControllers = getAllControllers(myEditor);
  for (ParameterInfoController controller : allControllers) {
    if (controller != ParameterInfoController.this) {
      PsiElement parameterOwner = controller.myComponent.getParameterOwner();
      if (parameterOwner != null && parameterOwner.isValid()) {
        TextRange range = parameterOwner.getTextRange();
        if (range != null && range.contains(myOffset) && ourRange.contains(range)) return false;
      }
    }
  }
  return true;
}
 
Example 4
Source File: BlockUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
public static List<DataLanguageBlockWrapper> filterBlocksByRange(@Nonnull List<DataLanguageBlockWrapper> list, @Nonnull TextRange textRange) {
  int i = 0;
  while (i < list.size()) {
    final DataLanguageBlockWrapper wrapper = list.get(i);
    final TextRange range = wrapper.getTextRange();
    if (textRange.contains(range)) {
      i++;
    }
    else if (range.intersectsStrict(textRange)) {
      list.remove(i);
      list.addAll(i, buildChildWrappers(wrapper.getOriginal()));
    }
    else {
      list.remove(i);
    }
  }
  return list;
}
 
Example 5
Source File: TodoForRanges.java    From consulo with Apache License 2.0 5 votes vote down vote up
public List<Pair<TextRange, TextAttributes>> execute() {
  final TodoItemData[] todoItems = getTodoItems();
  
  final StepIntersection<TodoItemData, TextRange> stepIntersection =
    new StepIntersection<TodoItemData, TextRange>(new Convertor<TodoItemData, TextRange>() {
      @Override
      public TextRange convert(TodoItemData o) {
        return o.getTextRange();
      }
    }, Convertor.SELF, myRanges, new Getter<String>() {
      @Override
      public String get() {
        return "";
      }
    }
    );
  final List<TodoItemData> filtered = stepIntersection.process(Arrays.asList(todoItems));
  final List<Pair<TextRange, TextAttributes>> result = new ArrayList<Pair<TextRange, TextAttributes>>(filtered.size());
  int offset = 0;
  for (TextRange range : myRanges) {
    Iterator<TodoItemData> iterator = filtered.iterator();
    while (iterator.hasNext()) {
      TodoItemData item = iterator.next();
      if (range.contains(item.getTextRange())) {
        TextRange todoRange = new TextRange(offset - range.getStartOffset() + item.getTextRange().getStartOffset(),
                                            offset - range.getStartOffset() + item.getTextRange().getEndOffset());
        result.add(new Pair<TextRange, TextAttributes>(todoRange, item.getPattern().getAttributes().getTextAttributes()));
        iterator.remove();
      } else {
        break;
      }
    }
    offset += range.getLength() + 1 + myAdditionalOffset;
  }
  return result;
}
 
Example 6
Source File: AnnotationLightCodeInsightFixtureTestCase.java    From idea-php-annotation-plugin with MIT License 5 votes vote down vote up
public void assertLocalInspectionContainsNotContains(String filename, String content, String contains) {
    Pair<List<ProblemDescriptor>, Integer> localInspectionsAtCaret = getLocalInspectionsAtCaret(filename, content);

    for (ProblemDescriptor result : localInspectionsAtCaret.getFirst()) {
        TextRange textRange = result.getPsiElement().getTextRange();
        if (textRange.contains(localInspectionsAtCaret.getSecond()) && result.toString().equals(contains)) {
            fail(String.format("Fail inspection not contains '%s'", contains));
        }
    }
}
 
Example 7
Source File: AnnotationLightCodeInsightFixtureTestCase.java    From idea-php-annotation-plugin with MIT License 5 votes vote down vote up
public void assertLocalInspectionContains(String filename, String content, String contains) {
    Set<String> matches = new HashSet<String>();

    Pair<List<ProblemDescriptor>, Integer> localInspectionsAtCaret = getLocalInspectionsAtCaret(filename, content);
    for (ProblemDescriptor result : localInspectionsAtCaret.getFirst()) {
        TextRange textRange = result.getPsiElement().getTextRange();
        if (textRange.contains(localInspectionsAtCaret.getSecond()) && result.toString().equals(contains)) {
            return;
        }

        matches.add(result.toString());
    }

    fail(String.format("Fail matches '%s' with one of %s", contains, matches));
}
 
Example 8
Source File: SymfonyLightCodeInsightFixtureTestCase.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
public void assertLocalInspectionNotContains(String filename, String content, String contains) {
    Pair<List<ProblemDescriptor>, Integer> localInspectionsAtCaret = getLocalInspectionsAtCaret(filename, content);

    for (ProblemDescriptor result : localInspectionsAtCaret.getFirst()) {
        TextRange textRange = result.getPsiElement().getTextRange();
        if (textRange.contains(localInspectionsAtCaret.getSecond()) && result.toString().contains(contains)) {
            fail(String.format("Fail inspection not contains '%s'", contains));
        }
    }
}
 
Example 9
Source File: SymfonyLightCodeInsightFixtureTestCase.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
public void assertLocalInspectionContains(String filename, String content, String contains) {
    Set<String> matches = new HashSet<String>();

    Pair<List<ProblemDescriptor>, Integer> localInspectionsAtCaret = getLocalInspectionsAtCaret(filename, content);
    for (ProblemDescriptor result : localInspectionsAtCaret.getFirst()) {
        TextRange textRange = result.getPsiElement().getTextRange();
        if (textRange.contains(localInspectionsAtCaret.getSecond()) && result.toString().equals(contains)) {
            return;
        }

        matches.add(result.toString());
    }

    fail(String.format("Fail matches '%s' with one of %s", contains, matches));
}
 
Example 10
Source File: DrupalLightCodeInsightFixtureTestCase.java    From idea-php-drupal-symfony2-bridge with MIT License 5 votes vote down vote up
protected void assertLocalInspectionIsEmpty(String filename, String content) {
    Pair<List<ProblemDescriptor>, Integer> localInspectionsAtCaret = getLocalInspectionsAtCaret(filename, content);

    for (ProblemDescriptor result : localInspectionsAtCaret.getFirst()) {
        TextRange textRange = result.getPsiElement().getTextRange();
        if (textRange.contains(localInspectionsAtCaret.getSecond())) {
            fail("Fail that matches is empty");
        }
    }
}
 
Example 11
Source File: ReferenceRange.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static boolean containsRangeInElement(PsiReference ref, TextRange rangeInElement) {
  if (ref instanceof MultiRangeReference) {
    for (TextRange range : ((MultiRangeReference)ref).getRanges()) {
      if (range.contains(rangeInElement)) return true;
    }

    return false;
  }
  TextRange rangeInElement1 = ref.getRangeInElement();
  return rangeInElement1 != null && rangeInElement1.contains(rangeInElement);
}
 
Example 12
Source File: DrupalLightCodeInsightFixtureTestCase.java    From idea-php-drupal-symfony2-bridge with MIT License 5 votes vote down vote up
public void assertLocalInspectionContains(String filename, String content, String contains) {
    Set<String> matches = new HashSet<String>();

    Pair<List<ProblemDescriptor>, Integer> localInspectionsAtCaret = getLocalInspectionsAtCaret(filename, content);
    for (ProblemDescriptor result : localInspectionsAtCaret.getFirst()) {
        TextRange textRange = result.getPsiElement().getTextRange();
        if (textRange.contains(localInspectionsAtCaret.getSecond()) && result.toString().equals(contains)) {
            return;
        }

        matches.add(result.toString());
    }

    fail(String.format("Fail matches '%s' with one of %s", contains, matches));
}
 
Example 13
Source File: ParameterInfoController.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
private Pair<Point, Short> getBestPointPosition(LightweightHint hint, final PsiElement list, int offset, VisualPosition pos, short preferredPosition) {
  if (list != null) {
    TextRange range = list.getTextRange();
    TextRange rangeWithoutParens = TextRange.from(range.getStartOffset() + 1, Math.max(range.getLength() - 2, 0));
    if (!rangeWithoutParens.contains(offset)) {
      offset = offset < rangeWithoutParens.getStartOffset() ? rangeWithoutParens.getStartOffset() : rangeWithoutParens.getEndOffset();
      pos = null;
    }
  }
  if (previousOffset == offset) return Pair.create(previousBestPoint, previousBestPosition);

  final boolean isMultiline = list != null && StringUtil.containsAnyChar(list.getText(), "\n\r");
  if (pos == null) pos = EditorUtil.inlayAwareOffsetToVisualPosition(myEditor, offset);
  Pair<Point, Short> position;

  if (!isMultiline) {
    position = chooseBestHintPosition(myEditor, pos, hint, preferredPosition, false);
  }
  else {
    Point p = HintManagerImpl.getHintPosition(hint, myEditor, pos, HintManager.ABOVE);
    position = new Pair<>(p, HintManager.ABOVE);
  }
  previousBestPoint = position.getFirst();
  previousBestPosition = position.getSecond();
  previousOffset = offset;
  return position;
}
 
Example 14
Source File: InitialInfoBuilder.java    From consulo with Apache License 2.0 5 votes vote down vote up
private LeafBlockWrapper doProcessSimpleBlock(final Block rootBlock, @Nullable final CompositeBlockWrapper parent, final boolean readOnly, final int index, @Nullable Block parentBlock) {
  if (!INLINE_TABS_ENABLED && !myCurrentWhiteSpace.containsLineFeeds()) {
    myCurrentWhiteSpace.setForceSkipTabulationsUsage(true);
  }
  TextRange textRange = rootBlock.getTextRange();
  LeafBlockWrapper info = new LeafBlockWrapper(rootBlock, parent, myCurrentWhiteSpace, myModel, myOptions, myPreviousBlock, readOnly, textRange);
  if (index == 0) {
    info.arrangeParentTextRange();
  }

  if (myPreviousBlock != null) {
    myPreviousBlock.setNextBlock(info);
  }
  if (myFirstTokenBlock == null) {
    myFirstTokenBlock = info;
  }
  myLastTokenBlock = info;
  if (currentWhiteSpaceIsReadOnly()) {
    myCurrentWhiteSpace.setReadOnly(true);
  }
  if (myCurrentSpaceProperty != null) {
    myCurrentWhiteSpace.setIsSafe(myCurrentSpaceProperty.isSafe());
    myCurrentWhiteSpace.setKeepFirstColumn(myCurrentSpaceProperty.shouldKeepFirstColumn());
  }

  if (info.isEndOfCodeBlock()) {
    myCurrentWhiteSpace.setBeforeCodeBlockEnd(true);
  }

  info.setSpaceProperty(myCurrentSpaceProperty);
  myCurrentWhiteSpace = new WhiteSpace(textRange.getEndOffset(), false);
  if (isDisabled(myCurrentWhiteSpace.getTextRange())) myCurrentWhiteSpace.setReadOnly(true);
  myPreviousBlock = info;

  if (myPositionOfInterest != -1 && (textRange.contains(myPositionOfInterest) || textRange.getEndOffset() == myPositionOfInterest)) {
    myResult.put(info, rootBlock);
    if (parent != null) myResult.put(parent, parentBlock);
  }
  return info;
}
 
Example 15
Source File: ShopwareLightCodeInsightFixtureTestCase.java    From idea-php-shopware-plugin with MIT License 5 votes vote down vote up
public void assertLocalInspectionNotContains(String filename, String content, String contains) {
    Pair<List<ProblemDescriptor>, Integer> localInspectionsAtCaret = getLocalInspectionsAtCaret(filename, content);

    for (ProblemDescriptor result : localInspectionsAtCaret.getFirst()) {
        TextRange textRange = result.getPsiElement().getTextRange();
        if (textRange.contains(localInspectionsAtCaret.getSecond()) && result.toString().contains(contains)) {
            fail(String.format("Fail inspection not contains '%s'", contains));
        }
    }
}
 
Example 16
Source File: ShopwareLightCodeInsightFixtureTestCase.java    From idea-php-shopware-plugin with MIT License 5 votes vote down vote up
public void assertLocalInspectionContains(String filename, String content, String contains) {
    Set<String> matches = new HashSet<String>();

    Pair<List<ProblemDescriptor>, Integer> localInspectionsAtCaret = getLocalInspectionsAtCaret(filename, content);
    for (ProblemDescriptor result : localInspectionsAtCaret.getFirst()) {
        TextRange textRange = result.getPsiElement().getTextRange();
        if (textRange.contains(localInspectionsAtCaret.getSecond()) && result.toString().equals(contains)) {
            return;
        }

        matches.add(result.toString());
    }

    fail(String.format("Fail matches '%s' with one of %s", contains, matches));
}
 
Example 17
Source File: LaravelLightCodeInsightFixtureTestCase.java    From idea-php-laravel-plugin with MIT License 5 votes vote down vote up
public void assertLocalInspectionContainsNotContains(String filename, String content, String contains) {
    Pair<List<ProblemDescriptor>, Integer> localInspectionsAtCaret = getLocalInspectionsAtCaret(filename, content);

    for (ProblemDescriptor result : localInspectionsAtCaret.getFirst()) {
        TextRange textRange = result.getPsiElement().getTextRange();
        if (textRange.contains(localInspectionsAtCaret.getSecond())) {
            fail(String.format("Fail inspection not contains '%s'", contains));
        }
    }
}
 
Example 18
Source File: LaravelLightCodeInsightFixtureTestCase.java    From idea-php-laravel-plugin with MIT License 5 votes vote down vote up
public void assertLocalInspectionContains(String filename, String content, String contains) {
    Set<String> matches = new HashSet<String>();

    Pair<List<ProblemDescriptor>, Integer> localInspectionsAtCaret = getLocalInspectionsAtCaret(filename, content);
    for (ProblemDescriptor result : localInspectionsAtCaret.getFirst()) {
        TextRange textRange = result.getPsiElement().getTextRange();
        if (textRange.contains(localInspectionsAtCaret.getSecond()) && result.toString().equals(contains)) {
            return;
        }

        matches.add(result.toString());
    }

    fail(String.format("Fail matches '%s' with one of %s", contains, matches));
}
 
Example 19
Source File: AnnotationLightCodeInsightFixtureTestCase.java    From idea-php-generics-plugin with MIT License 5 votes vote down vote up
public void assertLocalInspectionContains(String filename, String content, String contains) {
    Set<String> matches = new HashSet<String>();

    Pair<List<ProblemDescriptor>, Integer> localInspectionsAtCaret = getLocalInspectionsAtCaret(filename, content);
    for (ProblemDescriptor result : localInspectionsAtCaret.getFirst()) {
        TextRange textRange = result.getPsiElement().getTextRange();
        if (textRange.contains(localInspectionsAtCaret.getSecond()) && result.toString().equals(contains)) {
            return;
        }

        matches.add(result.toString());
    }

    fail(String.format("Fail matches '%s' with one of %s", contains, matches));
}
 
Example 20
Source File: FoldingUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
static boolean caretInsideRange(final Editor editor, final TextRange range) {
  final int offset = editor.getCaretModel().getOffset();
  return range.contains(offset) && range.getStartOffset() != offset;
}