Java Code Examples for com.intellij.openapi.vfs.VfsUtil#saveText()

The following examples show how to use com.intellij.openapi.vfs.VfsUtil#saveText() . 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: AsposeMavenModuleBuilderHelper.java    From Aspose.OCR-for-Java with MIT License 6 votes vote down vote up
private static void updateFileContents(Project project, final VirtualFile vf, final File f) throws Throwable {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    InputStream in = null;
    try {

        in = new FileInputStream(f);

        write(in, bytes);

    } finally {

        if (in != null) {
            in.close();
        }
    }
    VfsUtil.saveText(vf, bytes.toString());

    PsiFile psiFile = PsiManager.getInstance(project).findFile(vf);
    if (psiFile != null) {
        new ReformatCodeProcessor(project, psiFile, null, false).run();
    }
}
 
Example 2
Source File: LightPlatformCodeInsightTestCase.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
private static Document setupFileEditorAndDocument(@Nonnull String fileName, @Nonnull String fileText) throws IOException {
  EncodingProjectManager.getInstance(getProject()).setEncoding(null, CharsetToolkit.UTF8_CHARSET);
  EncodingProjectManager.getInstance(ProjectManager.getInstance().getDefaultProject()).setEncoding(null, CharsetToolkit.UTF8_CHARSET);
  PostprocessReformattingAspect.getInstance(ourProject).doPostponedFormatting();
  deleteVFile();
  myVFile = getSourceRoot().createChildData(null, fileName);
  VfsUtil.saveText(myVFile, fileText);
  final FileDocumentManager manager = FileDocumentManager.getInstance();
  final Document document = manager.getDocument(myVFile);
  assertNotNull("Can't create document for '" + fileName + "'", document);
  manager.reloadFromDisk(document);
  document.insertString(0, " ");
  document.deleteString(0, 1);
  myFile = getPsiManager().findFile(myVFile);
  assertNotNull("Can't create PsiFile for '" + fileName + "'. Unknown file type most probably.", myFile);
  assertTrue(myFile.isPhysical());
  myEditor = createEditor(myVFile);
  myVFile.setCharset(CharsetToolkit.UTF8_CHARSET);

  PsiDocumentManager.getInstance(getProject()).commitAllDocuments();
  return document;
}
 
Example 3
Source File: BookmarkManagerTest.java    From consulo with Apache License 2.0 6 votes vote down vote up
public void testBookmarkManagerDoesNotHardReferenceDocuments() throws IOException {
  @NonNls String text =
    "public class Test {\n" +
    "}";

  myVFile = getSourceRoot().createChildData(null, getTestName(false) + ".txt");
  VfsUtil.saveText(myVFile, text);

  Bookmark bookmark = getManager().addTextBookmark(myVFile, 1, "xxx");
  assertNotNull(bookmark);
  LeakHunter.checkLeak(getManager(), Document.class);

  Document document = FileDocumentManager.getInstance().getDocument(myVFile);
  assertNotNull(document);

  document.insertString(0, "line 0\n");
  assertEquals(2, bookmark.getLine());

  myEditor = createEditor(myVFile);
  checkBookmarkNavigation(bookmark);
}
 
Example 4
Source File: FileContentUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
public static void setFileText(@javax.annotation.Nullable Project project, final VirtualFile virtualFile, final String text) throws IOException {
  if (project == null) {
    project = ProjectUtil.guessProjectForFile(virtualFile);
  }
  if (project != null) {
    final PsiFile psiFile = PsiManager.getInstance(project).findFile(virtualFile);
    final PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(project);
    final Document document = psiFile == null? null : psiDocumentManager.getDocument(psiFile);
    if (document != null) {
      document.setText(text != null ? text : "");
      psiDocumentManager.commitDocument(document);
      FileDocumentManager.getInstance().saveDocument(document);
      return;
    }
  }
  VfsUtil.saveText(virtualFile, text != null ? text : "");
  virtualFile.refresh(false, false);
}
 
Example 5
Source File: ConsoleHistoryController.java    From consulo with Apache License 2.0 6 votes vote down vote up
private void saveHistory() {
  try {
    if (getModel().isEmpty()) return;
    if (myRootType.isHidden()) {
      saveHistoryOld();
      return;
    }
    AccessToken token = ApplicationManager.getApplication().acquireWriteActionLock(getClass());
    try {
      VirtualFile file = HistoryRootType.getInstance().findFile(null, getHistoryName(myRootType, myId), ScratchFileService.Option.create_if_missing);
      VfsUtil.saveText(file, StringUtil.join(getModel().getEntries(), myRootType.getEntrySeparator()));
    }
    finally {
      token.finish();
    }
  }
  catch (Exception ex) {
    LOG.error(ex);
  }
}
 
Example 6
Source File: AsposeMavenUtil.java    From Aspose.OCR-for-Java with MIT License 5 votes vote down vote up
private static void runOrApplyFileTemplate(Project project,
                                           VirtualFile file,
                                           String templateName,
                                           Properties properties) throws IOException {
    FileTemplateManager manager = FileTemplateManager.getInstance();
    FileTemplate fileTemplate = manager.getJ2eeTemplate(templateName);
    Properties allProperties = manager.getDefaultProperties(project);
    allProperties.putAll(properties);
    String text = fileTemplate.getText(allProperties);
    Pattern pattern = Pattern.compile("\\$\\{(.*)\\}");
    Matcher matcher = pattern.matcher(text);
    StringBuffer builder = new StringBuffer();
    while (matcher.find()) {
        matcher.appendReplacement(builder, "\\$" + matcher.group(1).toUpperCase() + "\\$");
    }
    matcher.appendTail(builder);
    text = builder.toString();

    TemplateImpl template = (TemplateImpl) TemplateManager.getInstance(project).createTemplate("", "", text);
    for (int i = 0; i < template.getSegmentsCount(); i++) {
        if (i == template.getEndSegmentNumber()) continue;
        String name = template.getSegmentName(i);
        String value = "\"" + properties.getProperty(name, "") + "\"";
        template.addVariable(name, value, value, true);
    }

    VfsUtil.saveText(file, template.getTemplateText());

    PsiFile psiFile = PsiManager.getInstance(project).findFile(file);
    if (psiFile != null) {
        new ReformatCodeProcessor(project, psiFile, null, false).run();
    }

}
 
Example 7
Source File: TempFiles.java    From consulo with Apache License 2.0 5 votes vote down vote up
public VirtualFile createVFile(VirtualFile parentDir, String name, String text) {
  try {
    final VirtualFile virtualFile = parentDir.createChildData(this, name);
    VfsUtil.saveText(virtualFile, text + "\n");
    return virtualFile;
  }
  catch (IOException e) {
    throw new RuntimeException(e);
  }
}
 
Example 8
Source File: VfsTestUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static void clearContent(VirtualFile file) {
  Assert.assertNotNull(file);
  try {
    VfsUtil.saveText(file, "");
  }
  catch (IOException e) {
    throw new RuntimeException(e);
  }
}
 
Example 9
Source File: FileTypeManagerTest.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void testAutoDetectTextFileFromContents() throws IOException {
  VirtualFile vFile = myFixture.getTempDirFixture().createFile("test.xxxxxxxx");
  VfsUtil.saveText(vFile, "text");

  FileType type = vFile.getFileType();
  assertEquals(UnknownFileType.INSTANCE, type);

  PsiFile psiFile = ((PsiManagerEx)PsiManager.getInstance(myFixture.getProject())).getFileManager().findFile(vFile); // autodetect text file if needed
  assertNotNull(psiFile);
  assertEquals(PlainTextFileType.INSTANCE, vFile.getFileType());
}
 
Example 10
Source File: ConsoleHistoryController.java    From consulo with Apache License 2.0 5 votes vote down vote up
public boolean loadHistory(String id, VirtualFile consoleFile) {
  try {
    VirtualFile file = myRootType.isHidden() ? null : HistoryRootType.getInstance().findFile(null, getHistoryName(myRootType, id), ScratchFileService.Option.existing_only);
    if (file == null) {
      if (loadHistoryOld(id)) {
        if (!myRootType.isHidden()) {
          // migrate content
          AccessToken token = ApplicationManager.getApplication().acquireWriteActionLock(getClass());
          try {
            VfsUtil.saveText(consoleFile, myContent);
          }
          finally {
            token.finish();
          }
        }
        return true;
      }
      return false;
    }
    String[] split = VfsUtilCore.loadText(file).split(myRootType.getEntrySeparator());
    getModel().resetEntries(Arrays.asList(split));
    return true;
  }
  catch (Exception ignored) {
    return false;
  }
}