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

The following examples show how to use com.intellij.openapi.util.TextRange#containsOffset() . 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: PsiToDocumentSynchronizer.java    From consulo with Apache License 2.0 6 votes vote down vote up
private int documentToPsiOffset(int offset, boolean greedyRight) {
  int delta = 0;
  for (Map.Entry<TextRange, CharSequence> entry : myAffectedFragments.entrySet()) {
    int lengthAfter = entry.getValue().length();
    TextRange range = entry.getKey();
    // for offsets inside affected fragments, return either start or end of the updated range
    if (range.containsOffset(offset)) {
      return range.getStartOffset() + delta + (greedyRight ? lengthAfter : 0);
    }
    if (range.getStartOffset() > offset) {
      break;
    }
    delta += lengthAfter - range.getLength();
  }
  return offset + delta;
}
 
Example 2
Source File: SharedPsiElementImplUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
private static void addReferences(int offset, PsiElement element, final Collection<PsiReference> outReferences) {
  PsiReference[] references;
  if (element instanceof HintedReferenceHost) {
    references = ((HintedReferenceHost)element).getReferences(new PsiReferenceService.Hints(null, offset));
  } else {
    references = element.getReferences();
  }
  for (final PsiReference reference : references) {
    if (reference == null) {
      LOG.error("Null reference returned from " + element + " of " + element.getClass());
      continue;
    }
    for (TextRange range : ReferenceRange.getRanges(reference)) {
      LOG.assertTrue(range != null, reference);
      if (range.containsOffset(offset)) {
        outReferences.add(reference);
      }
    }
  }
}
 
Example 3
Source File: InplaceRefactoring.java    From consulo with Apache License 2.0 6 votes vote down vote up
private PsiElement getSelectedInEditorElement(@Nullable PsiElement nameIdentifier,
                                              final Collection<PsiReference> refs,
                                              Collection<Pair<PsiElement, TextRange>> stringUsages,
                                              final int offset) {
  //prefer reference in case of self-references
  for (PsiReference ref : refs) {
    final PsiElement element = ref.getElement();
    if (checkRangeContainsOffset(offset, ref.getRangeInElement(), element)) return element;
  }

  if (nameIdentifier != null) {
    final TextRange range = nameIdentifier.getTextRange();
    if (range != null && range.containsOffset(offset)) return nameIdentifier;
  }

  for (Pair<PsiElement, TextRange> stringUsage : stringUsages) {
    if (checkRangeContainsOffset(offset, stringUsage.second, stringUsage.first)) return stringUsage.first;
  }

  LOG.error(nameIdentifier + " by " + this.getClass().getName());
  return null;
}
 
Example 4
Source File: ReferenceRange.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static boolean containsOffsetInElement(PsiReference ref, int offset) {
  if (ref instanceof MultiRangeReference) {
    for (TextRange range : ((MultiRangeReference)ref).getRanges()) {
      if (range.containsOffset(offset)) return true;
    }

    return false;
  }
  TextRange rangeInElement = ref.getRangeInElement();
  return rangeInElement != null && rangeInElement.containsOffset(offset);
}
 
Example 5
Source File: NavigationUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static boolean activatePsiElementIfOpen(@Nonnull PsiElement elt, boolean searchForOpen, boolean requestFocus) {
  if (!elt.isValid()) return false;
  elt = elt.getNavigationElement();
  final PsiFile file = elt.getContainingFile();
  if (file == null || !file.isValid()) return false;

  VirtualFile vFile = file.getVirtualFile();
  if (vFile == null) return false;

  if (!EditorHistoryManager.getInstance(elt.getProject()).hasBeenOpen(vFile)) return false;

  final FileEditorManager fem = FileEditorManager.getInstance(elt.getProject());
  if (!fem.isFileOpen(vFile)) {
    fem.openFile(vFile, requestFocus, searchForOpen);
  }

  final TextRange range = elt.getTextRange();
  if (range == null) return false;

  final FileEditor[] editors = fem.getEditors(vFile);
  for (FileEditor editor : editors) {
    if (editor instanceof TextEditor) {
      final Editor text = ((TextEditor)editor).getEditor();
      final int offset = text.getCaretModel().getOffset();

      if (range.containsOffset(offset)) {
        // select the file
        fem.openFile(vFile, requestFocus, searchForOpen);
        return true;
      }
    }
  }

  return false;
}
 
Example 6
Source File: TemplateLineStartEndHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean isEnabledForCaret(@Nonnull Editor editor, @Nonnull Caret caret, DataContext dataContext) {
  TemplateState templateState = TemplateManagerImpl.getTemplateState(editor);
  if (templateState != null && !templateState.isFinished()) {
    TextRange range = templateState.getCurrentVariableRange();
    int caretOffset = editor.getCaretModel().getOffset();
    if (range != null && range.containsOffset(caretOffset)) return true;
  }
  return myOriginalHandler.isEnabled(editor, caret, dataContext);
}
 
Example 7
Source File: TemplateState.java    From consulo with Apache License 2.0 5 votes vote down vote up
private boolean isCaretInsideNextVariable() {
  if (myEditor != null && myCurrentVariableNumber >= 0) {
    int nextVar = getNextVariableNumber(myCurrentVariableNumber);
    TextRange range = nextVar < 0 ? null : getVariableRange(myTemplate.getVariableNameAt(nextVar));
    return range != null && range.containsOffset(myEditor.getCaretModel().getOffset());
  }
  return false;
}
 
Example 8
Source File: TemplateLineStartEndHandler.java    From consulo with Apache License 2.0 4 votes vote down vote up
private boolean shouldStayInsideVariable(TextRange varRange, int caretOffset) {
  return varRange.containsOffset(caretOffset) &&
         caretOffset != (myIsHomeHandler ? varRange.getStartOffset() : varRange.getEndOffset());
}
 
Example 9
Source File: CodeFormatterFacade.java    From consulo with Apache License 2.0 4 votes vote down vote up
private static boolean canWrapLine(int startOffset, int endOffset, int offsetShift, @Nonnull List<? extends TextRange> enabledRanges) {
  for (TextRange range : enabledRanges) {
    if (range.containsOffset(startOffset - offsetShift) && range.containsOffset(endOffset - offsetShift)) return true;
  }
  return false;
}