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

The following examples show how to use com.intellij.openapi.fileEditor.FileDocumentManager#getCachedDocument() . 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: HaskellDocumentationProvider.java    From intellij-haskforce with Apache License 2.0 6 votes vote down vote up
@Nullable
@Override
public String generateDoc(PsiElement element, @Nullable PsiElement originalElement) {
    int startOffset = element.getTextRange().getStartOffset();
    int endOffset = element.getTextRange().getEndOffset();
    Module module = ModuleUtilCore.findModuleForPsiElement(element);
    FileDocumentManager fileDocumentManager= FileDocumentManager.getInstance();
    VirtualFile projectFile = element.getContainingFile().getVirtualFile();
    Document cachedDocument = fileDocumentManager.getCachedDocument(projectFile);
    if (cachedDocument == null) {
        return null;
    }
    int startLineNumber = cachedDocument.getLineNumber(startOffset);
    int endLineNumber = cachedDocument.getLineNumber(endOffset);
    int startColumn = startOffset - cachedDocument.getLineStartOffset(startLineNumber);
    int endColumn = endOffset - cachedDocument.getLineStartOffset(endLineNumber);

    // and also correct them for (0,0) vs (1,1) leftmost coordinate (intellij -> ghc)
    VisualPosition startPosition = TypeInfoUtil.correctFor0BasedVS1Based(new VisualPosition(startLineNumber, startColumn));
    VisualPosition endPosition = TypeInfoUtil.correctFor0BasedVS1Based(new VisualPosition(endLineNumber, endColumn));
    return TypeInfoUtil.getTypeInfo(module,startPosition,endPosition, projectFile);
}
 
Example 2
Source File: PsiFileImplUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
@RequiredUIAccess
public static PsiFile setName(final PsiFile file, String newName) throws IncorrectOperationException {
  VirtualFile vFile = file.getViewProvider().getVirtualFile();
  PsiManagerImpl manager = (PsiManagerImpl)file.getManager();

  try{
    final FileType newFileType = FileTypeRegistry.getInstance().getFileTypeByFileName(newName);
    if (UnknownFileType.INSTANCE.equals(newFileType) || newFileType.isBinary()) {
      // before the file becomes unknown or a binary (thus, not openable in the editor), save it to prevent data loss
      final FileDocumentManager fdm = FileDocumentManager.getInstance();
      final Document doc = fdm.getCachedDocument(vFile);
      if (doc != null) {
        fdm.saveDocumentAsIs(doc);
      }
    }

    vFile.rename(manager, newName);
  }
  catch(IOException e){
    throw new IncorrectOperationException(e);
  }

  return file.getViewProvider().isPhysical() ? manager.findFile(vFile) : file;
}
 
Example 3
Source File: IdeaGateway.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
private Entry doCreateEntry(@Nonnull VirtualFile file, boolean forDeletion) {
  if (!file.isDirectory()) {
    if (!isVersioned(file)) return null;

    Pair<StoredContent, Long> contentAndStamps;
    if (forDeletion) {
      FileDocumentManager m = FileDocumentManager.getInstance();
      Document d = m.isFileModified(file) ? m.getCachedDocument(file) : null; // should not try to load document
      contentAndStamps = acquireAndClearCurrentContent(file, d);
    }
    else {
      contentAndStamps = getActualContentNoAcquire(file);
    }

    return doCreateFileEntry(file, contentAndStamps);
  }

  DirectoryEntry newDir = null;
  if (file instanceof VirtualFileSystemEntry) {
    int nameId = ((VirtualFileSystemEntry)file).getNameId();
    if (nameId > 0) {
      newDir = new DirectoryEntry(nameId);
    }
  }

  if (newDir == null) {
    newDir = new DirectoryEntry(file.getName());
  }

  doCreateChildren(newDir, iterateDBChildren(file), forDeletion);
  if (!isVersioned(file) && newDir.getChildren().isEmpty()) return null;
  return newDir;
}
 
Example 4
Source File: AbstractConvertLineSeparatorsAction.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static void changeLineSeparators(@Nonnull final Project project,
                                        @Nonnull final VirtualFile virtualFile,
                                        @Nonnull final String newSeparator) {
  FileDocumentManager fileDocumentManager = FileDocumentManager.getInstance();
  Document document = fileDocumentManager.getCachedDocument(virtualFile);
  if (document != null) {
    fileDocumentManager.saveDocument(document);
  }

  String currentSeparator = LoadTextUtil.detectLineSeparator(virtualFile, false);
  final String commandText;
  if (StringUtil.isEmpty(currentSeparator)) {
    commandText = "Changed line separators to " + LineSeparator.fromString(newSeparator);
  }
  else {
    commandText = String.format("Changed line separators from %s to %s", LineSeparator.fromString(currentSeparator),
                                LineSeparator.fromString(newSeparator));
  }

  new WriteCommandAction(project, commandText) {
    @Override
    protected void run(@Nonnull Result result) throws Throwable {
      try {
        LoadTextUtil.changeLineSeparators(project, virtualFile, newSeparator, this);
      }
      catch (IOException e) {
        LOG.info(e);
      }
    }
  }.execute();
}