Java Code Examples for com.intellij.openapi.fileEditor.FileDocumentManager#getFile()

The following examples show how to use com.intellij.openapi.fileEditor.FileDocumentManager#getFile() . 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
@Override
public void actionPerformed(@NotNull final AnActionEvent event) {
  Project project = event.getProject();
  final Editor editor = event.getData(CommonDataKeys.EDITOR);
  if (project == null || editor == null) return;
  Document document = editor.getDocument();
  FileDocumentManager fileDocumentManager = FileDocumentManager.getInstance();
  VirtualFile virtualFile = fileDocumentManager.getFile(document);
  ProjectFileIndex projectFileIndex = ProjectRootManager.getInstance(project).getFileIndex();
  if (virtualFile != null) {
    Module module = projectFileIndex.getModuleForFile(virtualFile);
    String moduleName;
    moduleName = module != null ? module.getName() : "No module defined for file";

    VirtualFile moduleContentRoot = projectFileIndex.getContentRootForFile(virtualFile);
    boolean isLibraryFile = projectFileIndex.isLibraryClassFile(virtualFile);
    boolean isInLibraryClasses = projectFileIndex.isInLibraryClasses(virtualFile);
    boolean isInLibrarySource = projectFileIndex.isInLibrarySource(virtualFile);
    Messages.showInfoMessage("Module: " + moduleName + "\n" +
                             "Module content root: " + moduleContentRoot + "\n" +
                             "Is library file: " + isLibraryFile + "\n" +
                             "Is in library classes: " + isInLibraryClasses +
                             ", Is in library source: " + isInLibrarySource,
                             "Main File Info for" + virtualFile.getName());
  }
}
 
Example 2
@Override
public void documentChanged(DocumentEvent event) {
  Document document = event.getDocument();
  FileDocumentManager fileDocumentManager = FileDocumentManager.getInstance();
  VirtualFile file = fileDocumentManager.getFile(document);
  if (file == null) {
    return;
  }

  String path = ExternalSystemApiUtil.getLocalFileSystemPath(file);
  for (MyEntry entry : myAutoImportAware) {
    if (entry.aware.getAffectedExternalProjectPath(path, myProject) != null) {
      // Document save triggers VFS event but FileDocumentManager might be registered after the current listener, that's why
      // call to 'saveDocument()' might not produce the desired effect. That's why we reschedule document save if necessary.
      scheduleDocumentSave(document);
      return;
    }
  } 
}
 
Example 3
Source Project: consulo   File: DesktopEditorImpl.java    License: Apache License 2.0 6 votes vote down vote up
private boolean processKeyTyped(char c) {

    if (ProgressManager.getInstance().hasModalProgressIndicator()) {
      return false;
    }
    FileDocumentManager manager = FileDocumentManager.getInstance();
    final VirtualFile file = manager.getFile(myDocument);
    if (file != null && !file.isValid()) {
      return false;
    }

    DataContext context = getDataContext();

    Graphics graphics = GraphicsUtil.safelyGetGraphics(myEditorComponent);
    if (graphics != null) { // editor component is not showing
      PaintUtil.alignTxToInt((Graphics2D)graphics, PaintUtil.insets2offset(getInsets()), true, false, RoundingMode.CEIL);
      processKeyTypedImmediately(c, graphics, context);
      graphics.dispose();
    }

    ActionManagerEx.getInstanceEx().fireBeforeEditorTyping(c, context);
    EditorUIUtil.hideCursorInEditor(this);
    processKeyTypedNormally(c, context);

    return true;
  }
 
Example 4
@Override
public void visibleAreaChanged(VisibleAreaEvent visibleAreaEvent) {
    final FileDocumentManager instance = FileDocumentManager.getInstance();
    final VirtualFile file = instance.getFile(visibleAreaEvent.getEditor().getDocument());
    if (file != null) {
        GTMRecord.record(file.getPath(), visibleAreaEvent.getEditor().getProject());
    }
}
 
Example 5
@Override
public void mousePressed(EditorMouseEvent editorMouseEvent) {
    final FileDocumentManager instance = FileDocumentManager.getInstance();
    final VirtualFile file = instance.getFile(editorMouseEvent.getEditor().getDocument());
    if (file != null) {
        GTMRecord.record(file.getPath(), editorMouseEvent.getEditor().getProject());
    }
}
 
Example 6
@Override
public void documentChanged(DocumentEvent documentEvent) {
    Document document = documentEvent.getDocument();
    FileDocumentManager instance = FileDocumentManager.getInstance();
    VirtualFile file = instance.getFile(document);
    WakaTime.appendHeartbeat(file, WakaTime.getProject(document), false);
}
 
Example 7
@Override
public void visibleAreaChanged(VisibleAreaEvent visibleAreaEvent) {
    FileDocumentManager instance = FileDocumentManager.getInstance();
    VirtualFile file = instance.getFile(visibleAreaEvent.getEditor().getDocument());
    Project project = visibleAreaEvent.getEditor().getProject();
    WakaTime.appendHeartbeat(file, project, false);
}
 
Example 8
@Override
public void mousePressed(EditorMouseEvent editorMouseEvent) {
    FileDocumentManager instance = FileDocumentManager.getInstance();
    VirtualFile file = instance.getFile(editorMouseEvent.getEditor().getDocument());
    Project project = editorMouseEvent.getEditor().getProject();
    WakaTime.appendHeartbeat(file, project, false);
}
 
Example 9
Source Project: consulo   File: DocumentImpl.java    License: Apache License 2.0 5 votes vote down vote up
private void beforeChangedUpdate(DocumentEvent event, DelayedExceptions exceptions) {
  Application app = ApplicationManager.getApplication();
  if (app != null) {
    FileDocumentManager manager = FileDocumentManager.getInstance();
    VirtualFile file = manager.getFile(this);
    if (file != null && !file.isValid()) {
      LOG.error("File of this document has been deleted: " + file);
    }
  }
  assertInsideCommand();

  getLineSet(); // initialize line set to track changed lines

  if (!ShutDownTracker.isShutdownHookRunning()) {
    DocumentListener[] listeners = getListeners();
    for (int i = listeners.length - 1; i >= 0; i--) {
      try {
        listeners[i].beforeDocumentChange(event);
      }
      catch (Throwable e) {
        exceptions.register(e);
      }
    }
  }

  myEventsHandling = true;
}
 
Example 10
@Override
public void beforeDocumentSaving(Document document) {
    FileDocumentManager instance = FileDocumentManager.getInstance();
    VirtualFile file = instance.getFile(document);
    WakaTime.appendHeartbeat(file, WakaTime.getProject(document), true);
}
 
Example 11
Source Project: consulo   File: TrailingSpacesStripper.java    License: Apache License 2.0 4 votes vote down vote up
private void strip(@Nonnull final Document document) {
  if (!document.isWritable()) return;
  FileDocumentManager fileDocumentManager = FileDocumentManager.getInstance();
  VirtualFile file = fileDocumentManager.getFile(document);
  if (file == null || !file.isValid() || Boolean.TRUE.equals(DISABLE_FOR_FILE_KEY.get(file))) return;

  final EditorSettingsExternalizable settings = EditorSettingsExternalizable.getInstance();
  if (settings == null) return;

  final String overrideStripTrailingSpacesData = file.getUserData(OVERRIDE_STRIP_TRAILING_SPACES_KEY);
  final Boolean overrideEnsureNewlineData = file.getUserData(OVERRIDE_ENSURE_NEWLINE_KEY);
  @EditorSettingsExternalizable.StripTrailingSpaces
  String stripTrailingSpaces = overrideStripTrailingSpacesData != null ? overrideStripTrailingSpacesData : settings.getStripTrailingSpaces();
  final boolean doStrip = !stripTrailingSpaces.equals(EditorSettingsExternalizable.STRIP_TRAILING_SPACES_NONE);
  final boolean ensureEOL = overrideEnsureNewlineData != null ? overrideEnsureNewlineData.booleanValue() : settings.isEnsureNewLineAtEOF();

  if (doStrip) {
    final boolean inChangedLinesOnly = !stripTrailingSpaces.equals(EditorSettingsExternalizable.STRIP_TRAILING_SPACES_WHOLE);
    boolean success = strip(document, inChangedLinesOnly, settings.isKeepTrailingSpacesOnCaretLine());
    if (!success) {
      myDocumentsToStripLater.add(document);
    }
  }

  final int lines = document.getLineCount();
  if (ensureEOL && lines > 0) {
    final int start = document.getLineStartOffset(lines - 1);
    final int end = document.getLineEndOffset(lines - 1);
    if (start != end) {
      final CharSequence content = document.getCharsSequence();
      ApplicationManager.getApplication().runWriteAction(new DocumentRunnable(document, null) {
        @Override
        public void run() {
          CommandProcessor.getInstance().runUndoTransparentAction(() -> {
            if (CharArrayUtil.containsOnlyWhiteSpaces(content.subSequence(start, end)) && doStrip) {
              document.deleteString(start, end);
            }
            else {
              document.insertString(end, "\n");
            }
          });
        }
      });
    }
  }
}