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

The following examples show how to use com.intellij.openapi.editor.Document#putUserData() . These examples are extracted from open source projects. 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 Project: consulo   File: FileBasedIndexImpl.java    License: Apache License 2.0 6 votes vote down vote up
private boolean clearUpToDateStateForPsiIndicesOfUnsavedDocuments(@Nonnull VirtualFile file, Collection<? extends ID<?, ?>> affectedIndices) {
  if (!myUpToDateIndicesForUnsavedOrTransactedDocuments.isEmpty()) {
    myUpToDateIndicesForUnsavedOrTransactedDocuments.clear();
  }

  Document document = myFileDocumentManager.getCachedDocument(file);

  if (document != null && myFileDocumentManager.isDocumentUnsaved(document)) {   // will be reindexed in indexUnsavedDocuments
    myLastIndexedDocStamps.clearForDocument(document); // Q: non psi indices
    document.putUserData(ourFileContentKey, null);

    return true;
  }

  removeTransientFileDataFromIndices(ContainerUtil.intersection(affectedIndices, myPsiDependentIndices), getFileId(file), file);
  return false;
}
 
Example 2
Source Project: consulo   File: EditorHighlighterCache.java    License: 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 Project: consulo   File: DocumentFragmentContent.java    License: Apache License 2.0 5 votes vote down vote up
public DocumentFragmentContent(@Nullable Project project, @Nonnull DocumentContent original, @Nonnull RangeMarker rangeMarker) {
  myOriginal = original;
  myRangeMarker = rangeMarker;

  Document document1 = myOriginal.getDocument();

  Document document2 = EditorFactory.getInstance().createDocument("");
  document2.putUserData(UndoManager.ORIGINAL_DOCUMENT, document1);

  mySynchronizer = new MyDocumentsSynchronizer(project, myRangeMarker, document1, document2);
}
 
Example 4
@Override
public Document getDocument(@Nonnull VirtualFile file) {
  Document document = file.getUserData(MOCK_DOC_KEY);
  if (document == null) {
    if (file.isDirectory() || isBinaryWithoutDecompiler(file)) return null;

    CharSequence text = LoadTextUtil.loadText(file);
    document = myFactory.apply(text);
    document.putUserData(MOCK_VIRTUAL_FILE_KEY, file);
    document = file.putUserDataIfAbsent(MOCK_DOC_KEY, document);
  }
  return document;
}
 
Example 5
Source Project: consulo   File: CodeFoldingManagerImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void dispose() {
  for (Document document : myDocumentsWithFoldingInfo) {
    if (document != null) {
      document.putUserData(myFoldingInfoInDocumentKey, null);
    }
  }
}
 
Example 6
Source Project: consulo   File: PsiDocumentManagerBase.java    License: 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 7
Source Project: consulo   File: CommitHelper.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Marks {@link Document documents} related to the given changes as "being committed".
 *
 * @return documents which were marked that way.
 * @see #unmarkCommittingDocuments(java.util.Collection)
 * @see VetoSavingCommittingDocumentsAdapter
 */
@Nonnull
public static Collection<Document> markCommittingDocuments(@Nonnull Project project, @Nonnull List<Change> changes) {
  Collection<Document> committingDocs = new ArrayList<Document>();
  for (Change change : changes) {
    Document doc = ChangesUtil.getFilePath(change).getDocument();
    if (doc != null) {
      doc.putUserData(DOCUMENT_BEING_COMMITTED_KEY, project);
      committingDocs.add(doc);
    }
  }
  return committingDocs;
}
 
Example 8
Source Project: consulo   File: UndoRedoStacksHolder.java    License: 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 9
@Override
public Document getDocument(@Nonnull VirtualFile file) {
  Document document = file.getUserData(MOCK_DOC_KEY);
  if (document == null) {
    if (file.isDirectory() || isBinaryWithoutDecompiler(file)) return null;

    CharSequence text = LoadTextUtil.loadText(file);
    document = myFactory.fun(text);
    document.putUserData(MOCK_VIRTUAL_FILE_KEY, file);
    document = file.putUserDataIfAbsent(MOCK_DOC_KEY, document);
  }
  return document;
}
 
Example 10
Source Project: CppTools   File: CppSupportLoader.java    License: 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 11
Source Project: consulo   File: UndoUtil.java    License: Apache License 2.0 4 votes vote down vote up
public static void disableUndoFor(@Nonnull Document document) {
  document.putUserData(UndoConstants.DONT_RECORD_UNDO, Boolean.TRUE);
}
 
Example 12
Source Project: consulo   File: DesktopEditorsSplitters.java    License: Apache License 2.0 4 votes vote down vote up
@Override
protected JPanel processFiles(@Nonnull List<Element> fileElements, final JPanel context, Element parent, UIAccess uiAccess) {
  final Ref<DesktopEditorWindow> windowRef = new Ref<>();
  UIUtil.invokeAndWaitIfNeeded((Runnable)() -> {
    DesktopEditorWindow editorWindow = context == null ? createEditorWindow() : findWindowWith(context);
    windowRef.set(editorWindow);
    if (editorWindow != null) {
      updateTabSizeLimit(editorWindow, parent.getAttributeValue(JBTabsImpl.SIDE_TABS_SIZE_LIMIT_KEY.toString()));
    }
  });

  final DesktopEditorWindow window = windowRef.get();
  LOG.assertTrue(window != null);
  VirtualFile focusedFile = null;

  for (int i = 0; i < fileElements.size(); i++) {
    final Element file = fileElements.get(i);
    Element historyElement = file.getChild(HistoryEntry.TAG);
    String fileName = historyElement.getAttributeValue(HistoryEntry.FILE_ATTR);
    Activity activity = StartUpMeasurer.startActivity(PathUtil.getFileName(fileName), ActivityCategory.REOPENING_EDITOR);
    VirtualFile virtualFile = null;
    try {
      final FileEditorManagerImpl fileEditorManager = getManager();
      final HistoryEntry entry = HistoryEntry.createLight(fileEditorManager.getProject(), historyElement);
      virtualFile = entry.getFile();
      if (virtualFile == null) throw new InvalidDataException("No file exists: " + entry.getFilePointer().getUrl());
      virtualFile.putUserData(OPENED_IN_BULK, Boolean.TRUE);
      VirtualFile finalVirtualFile = virtualFile;
      Document document = ReadAction.compute(() -> finalVirtualFile.isValid() ? FileDocumentManager.getInstance().getDocument(finalVirtualFile) : null);

      boolean isCurrentTab = Boolean.valueOf(file.getAttributeValue(CURRENT_IN_TAB)).booleanValue();
      FileEditorOpenOptions openOptions = new FileEditorOpenOptions().withPin(Boolean.valueOf(file.getAttributeValue(PINNED))).withIndex(i).withReopeningEditorsOnStartup();

      fileEditorManager.openFileImpl4(uiAccess, window, virtualFile, entry, openOptions);
      if (isCurrentTab) {
        focusedFile = virtualFile;
      }
      if (document != null) {
        // This is just to make sure document reference is kept on stack till this point
        // so that document is available for folding state deserialization in HistoryEntry constructor
        // and that document will be created only once during file opening
        document.putUserData(DUMMY_KEY, null);
      }
      updateProgress();
    }
    catch (InvalidDataException e) {
      if (ApplicationManager.getApplication().isUnitTestMode()) {
        LOG.error(e);
      }
    }
    finally {
      if (virtualFile != null) virtualFile.putUserData(OPENED_IN_BULK, null);
    }
    activity.end();
  }
  if (focusedFile != null) {
    getManager().addSelectionRecord(focusedFile, window);
    VirtualFile finalFocusedFile = focusedFile;
    uiAccess.giveAndWaitIfNeed(() -> {
      EditorWithProviderComposite editor = window.findFileComposite(finalFocusedFile);
      if (editor != null) {
        window.setEditor(editor, true, true);
      }
    });
  }
  else {
    ToolWindowManager manager = ToolWindowManager.getInstance(getManager().getProject());
    manager.invokeLater(() -> {
      if (null == manager.getActiveToolWindowId()) {
        ToolWindow toolWindow = manager.getToolWindow(ToolWindowId.PROJECT_VIEW);
        if (toolWindow != null) toolWindow.activate(null);
      }
    });
  }
  return window.myPanel;
}
 
Example 13
Source Project: consulo   File: FileDocumentManagerImpl.java    License: Apache License 2.0 4 votes vote down vote up
public static void registerDocument(@Nonnull final Document document, @Nonnull VirtualFile virtualFile) {
  synchronized (lock) {
    document.putUserData(FILE_KEY, virtualFile);
    virtualFile.putUserData(HARD_REF_TO_DOCUMENT_KEY, document);
  }
}
 
Example 14
Source Project: consulo   File: FileDocumentManagerImpl.java    License: Apache License 2.0 4 votes vote down vote up
private void unbindFileFromDocument(@Nonnull VirtualFile file, @Nonnull Document document) {
  removeDocumentFromCache(file);
  file.putUserData(HARD_REF_TO_DOCUMENT_KEY, null);
  document.putUserData(FILE_KEY, null);
}
 
Example 15
Source Project: consulo   File: EncodingManagerImpl.java    License: Apache License 2.0 4 votes vote down vote up
private void setCachedCharsetFromContent(Charset charset, Charset oldCached, @Nonnull Document document) {
  document.putUserData(CACHED_CHARSET_FROM_CONTENT, charset);
  firePropertyChange(document, PROP_CACHED_ENCODING_CHANGED, oldCached, charset);
}
 
Example 16
Source Project: consulo   File: DiffRangeMarker.java    License: Apache License 2.0 4 votes vote down vote up
private void onClientRemoved(@Nonnull Document document, @Nonnull DiffRangeMarker marker, @Nonnull RangeInvalidListener listener) {
  if (myDiffRangeMarkers.remove(marker) == listener && myDiffRangeMarkers.isEmpty()) {
    document.putUserData(KEY, null);
    document.removeDocumentListener(this);
  }
}
 
Example 17
Source Project: consulo   File: DocumentUndoProvider.java    License: Apache License 2.0 4 votes vote down vote up
public static void startDocumentUndo(@Nullable Document doc) {
  if (doc != null) doc.putUserData(UNDOING_EDITOR_CHANGE, Boolean.TRUE);
}
 
Example 18
Source Project: consulo   File: EditorHighlighterCache.java    License: Apache License 2.0 4 votes vote down vote up
public static void rememberEditorHighlighterForCachesOptimization(Document document, @Nonnull final EditorHighlighter highlighter) {
  document.putUserData(ourSomeEditorSyntaxHighlighter, new WeakReference<EditorHighlighter>(highlighter));
}
 
Example 19
Source Project: consulo   File: CommonCodeStyleSettings.java    License: Apache License 2.0 4 votes vote down vote up
public void associateWithDocument(@Nonnull Document document) {
  document.putUserData(INDENT_OPTIONS_KEY, this);
}
 
Example 20
Source Project: consulo   File: CommitHelper.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * Removes the "being committed marker" from the given {@link Document documents}.
 *
 * @see #markCommittingDocuments(com.intellij.openapi.project.Project, java.util.List)
 * @see VetoSavingCommittingDocumentsAdapter
 */
public static void unmarkCommittingDocuments(@Nonnull Collection<Document> committingDocs) {
  for (Document doc : committingDocs) {
    doc.putUserData(DOCUMENT_BEING_COMMITTED_KEY, null);
  }
}