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

The following examples show how to use com.intellij.openapi.editor.Document#getUserData() . 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: PerIndexDocumentVersionMap.java    From consulo with Apache License 2.0 6 votes vote down vote up
public long set(@Nonnull Document document, @Nonnull ID<?, ?> indexId, long value) {
  List<IdVersionInfo> list = document.getUserData(KEY);
  if (list == null) {
    list = ((UserDataHolderEx)document).putUserDataIfAbsent(KEY, new ArrayList<>());
  }

  synchronized (list) {
    for (IdVersionInfo info : list) {
      if (info.id == indexId) {
        long old = info.docVersion;
        if (info.mapVersion != mapVersion) {
          old = INVALID_STAMP;
          info.mapVersion = mapVersion;
        }
        info.docVersion = value;
        return old;
      }
    }
    list.add(new IdVersionInfo(indexId, value, mapVersion));
    return INVALID_STAMP;
  }
}
 
Example 2
Source File: EditorHighlighterCache.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nullable
public static EditorHighlighter getEditorHighlighterForCachesBuilding(Document document) {
  if (document == null) {
    return null;
  }
  final WeakReference<EditorHighlighter> editorHighlighterWeakReference = document.getUserData(ourSomeEditorSyntaxHighlighter);
  final EditorHighlighter someEditorHighlighter = SoftReference.dereference(editorHighlighterWeakReference);

  if (someEditorHighlighter instanceof LexerEditorHighlighter &&
      ((LexerEditorHighlighter)someEditorHighlighter).isValid()
          ) {
    return someEditorHighlighter;
  }
  document.putUserData(ourSomeEditorSyntaxHighlighter, null);
  return null;
}
 
Example 3
Source File: UndoRedoStacksHolder.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
private LinkedList<UndoableGroup> doGetStackForDocument(@Nonnull DocumentReference r) {
  // If document is not associated with file, we have to store its stack in document
  // itself to avoid memory leaks caused by holding stacks of all documents, ever created, here.
  // And to know, what documents do exist now, we have to maintain weak reference list of them.

  Document d = r.getDocument();
  LinkedList<UndoableGroup> result = d.getUserData(STACK_IN_DOCUMENT_KEY);
  if (result == null) {
    result = new LinkedList<UndoableGroup>();
    d.putUserData(STACK_IN_DOCUMENT_KEY, result);
    myDocumentsWithStacks.add(d);
  }
  return result;
}
 
Example 4
Source File: PsiDocumentManagerBase.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static List<Runnable> getAndClearActionsAfterCommit(@Nonnull Document document) {
  List<Runnable> list;
  synchronized (ACTION_AFTER_COMMIT) {
    list = document.getUserData(ACTION_AFTER_COMMIT);
    if (list != null) {
      list = new ArrayList<>(list);
      document.putUserData(ACTION_AFTER_COMMIT, null);
    }
  }
  return list;
}
 
Example 5
Source File: PsiDocumentManagerBase.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static void addRunOnCommit(@Nonnull Document document, @Nonnull Runnable action) {
  synchronized (ACTION_AFTER_COMMIT) {
    List<Runnable> list = document.getUserData(ACTION_AFTER_COMMIT);
    if (list == null) {
      document.putUserData(ACTION_AFTER_COMMIT, list = new SmartList<>());
    }
    list.add(action);
  }
}
 
Example 6
Source File: SaveCommittingDocumentsVetoer.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public boolean maySaveDocument(@Nonnull Document document, boolean isSaveExplicit) {
  final Object beingCommitted = document.getUserData(CommitHelper.DOCUMENT_BEING_COMMITTED_KEY);
  if (beingCommitted == VetoSavingCommittingDocumentsAdapter.SAVE_DENIED) {
    return false;
  }
  if (beingCommitted instanceof Project) {
    boolean allowSave = myAdapter.showAllowSaveDialog(Collections.singletonMap(document, (Project)beingCommitted));
    if (!allowSave) {
      return false;
    }
  }
  return true;
}
 
Example 7
Source File: CompletionUtilCoreImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static <T extends PsiElement> T getOriginalElement(@Nonnull T psi, PsiFile containingFile) {
  if (containingFile != null && containingFile != containingFile.getOriginalFile() && psi.getTextRange() != null) {
    TextRange range = psi.getTextRange();
    Integer start = range.getStartOffset();
    Integer end = range.getEndOffset();
    final Document document = containingFile.getViewProvider().getDocument();
    if (document != null) {
      Document hostDocument = document instanceof DocumentWindow ? ((DocumentWindow)document).getDelegate() : document;
      OffsetTranslator translator = hostDocument.getUserData(OffsetTranslator.RANGE_TRANSLATION);
      if (translator != null) {
        if (document instanceof DocumentWindow) {
          TextRange translated = ((DocumentWindow)document).injectedToHost(new TextRange(start, end));
          start = translated.getStartOffset();
          end = translated.getEndOffset();
        }

        start = translator.translateOffset(start);
        end = translator.translateOffset(end);
        if (start == null || end == null) {
          return null;
        }

        if (document instanceof DocumentWindow) {
          start = ((DocumentWindow)document).hostToInjected(start);
          end = ((DocumentWindow)document).hostToInjected(end);
        }
      }
    }
    //noinspection unchecked
    return (T)PsiTreeUtil.findElementOfClassAtRange(containingFile.getOriginalFile(), start, end, psi.getClass());
  }

  return psi;
}
 
Example 8
Source File: DocumentUndoProvider.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static boolean shouldRecordActions(@Nonnull Document document) {
  if (document.getUserData(UndoConstants.DONT_RECORD_UNDO) == Boolean.TRUE) return false;

  VirtualFile vFile = FileDocumentManager.getInstance().getFile(document);
  if (vFile == null) return true;
  return vFile.getUserData(AbstractFileViewProvider.FREE_THREADED) != Boolean.TRUE && vFile.getUserData(UndoConstants.DONT_RECORD_UNDO) != Boolean.TRUE;
}
 
Example 9
Source File: EncodingManagerImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static int addNumberOfRequestedRedetects(@Nonnull Document document, int delta) {
  AtomicInteger oldData = document.getUserData(RUNNING_REDETECTS_KEY);
  if (oldData == null) {
    oldData = ((UserDataHolderEx)document).putUserDataIfAbsent(RUNNING_REDETECTS_KEY, new AtomicInteger());
  }
  return oldData.addAndGet(delta);
}
 
Example 10
Source File: DocumentMarkupModel.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static ConcurrentMap<Project, MarkupModelImpl> getMarkupModelMap(@Nonnull Document document) {
  ConcurrentMap<Project, MarkupModelImpl> markupModelMap = document.getUserData(MARKUP_MODEL_MAP_KEY);
  if (markupModelMap == null) {
    ConcurrentMap<Project, MarkupModelImpl> newMap = ContainerUtil.newConcurrentMap();
    markupModelMap = ((UserDataHolderEx)document).putUserDataIfAbsent(MARKUP_MODEL_MAP_KEY, newMap);
  }
  return markupModelMap;
}
 
Example 11
Source File: UndoManagerImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nonnull
private static Document getOriginal(@Nonnull Document document) {
  Document result = document.getUserData(ORIGINAL_DOCUMENT);
  return result == null ? document : result;
}
 
Example 12
Source File: UndoManagerImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
static boolean isCopy(@Nonnull Document d) {
  return d.getUserData(ORIGINAL_DOCUMENT) != null;
}
 
Example 13
Source File: CppSupportLoader.java    From CppTools with Apache License 2.0 4 votes vote down vote up
private void removeDocumentListener(Document document) {
  if (isAcceptableDocument(document) && document.getUserData(ourListenerKey) != null) {
    document.removeDocumentListener(myDocumentListener);
    document.putUserData(ourListenerKey, null);
  }
}
 
Example 14
Source File: DocumentUndoProvider.java    From consulo with Apache License 2.0 4 votes vote down vote up
private static void handleBeforeDocumentChange(@Nonnull UndoManagerImpl undoManager, @Nonnull Document document) {
  if (undoManager.isActive() && isUndoable(undoManager, document) && undoManager.isUndoOrRedoInProgress() && document.getUserData(UNDOING_EDITOR_CHANGE) != Boolean.TRUE) {
    throw new IllegalStateException("Do not change documents during undo as it will break undo sequence.");
  }
}
 
Example 15
Source File: EncodingManagerImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
@Nullable
public Charset getCachedCharsetFromContent(@Nonnull Document document) {
  return document.getUserData(CACHED_CHARSET_FROM_CONTENT);
}
 
Example 16
Source File: FileDocumentManagerImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public boolean isPartialPreviewOfALargeFile(@Nonnull Document document) {
  return document.getUserData(BIG_FILE_PREVIEW) == Boolean.TRUE;
}
 
Example 17
Source File: FileDocumentManagerImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
@Nullable
public VirtualFile getFile(@Nonnull Document document) {
  if (document instanceof FrozenDocument) return null;
  return document.getUserData(FILE_KEY);
}
 
Example 18
Source File: UpdateHighlightersUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
static boolean isWhitespaceOptimizationAllowed(@Nonnull Document document) {
  return document.getUserData(TYPING_INSIDE_HIGHLIGHTER_OCCURRED) == null;
}
 
Example 19
Source File: DocumentCommitThread.java    From consulo with Apache License 2.0 4 votes vote down vote up
/**
 * @return an internal lock object to prevent read & write phases of commit from running simultaneously for free-threaded PSI
 */
private static Lock getDocumentLock(Document document) {
  Lock lock = document.getUserData(DOCUMENT_LOCK);
  return lock != null ? lock : ((UserDataHolderEx)document).putUserDataIfAbsent(DOCUMENT_LOCK, new ReentrantLock());
}
 
Example 20
Source File: LightFileDocumentManager.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public VirtualFile getFile(@Nonnull Document document) {
  return document.getUserData(MOCK_VIRTUAL_FILE_KEY);
}