private static void addReferences(
     int offset, PsiElement element, final Collection<PsiReference> outReferences) {
   for (final PsiReference reference : element.getReferences()) {
     if (reference == null) {
       LOG.error(element);
     }
     for (TextRange range : ReferenceRange.getRanges(reference)) {
       assert range != null : reference;
       if (range.containsOffset(offset)) {
         outReferences.add(reference);
       }
     }
   }
 }
 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;
 }
Ejemplo n.º 3
0
  private static boolean activatePsiElementIfOpen(
      @NotNull 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;
  }