Java Code Examples for com.intellij.openapi.vfs.encoding.EncodingManager#getInstance()

The following examples show how to use com.intellij.openapi.vfs.encoding.EncodingManager#getInstance() . 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: PersistentFSImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
@Override
public VirtualFile createChildFile(Object requestor, @Nonnull VirtualFile parent, @Nonnull String file) throws IOException {
  getDelegate(parent).createChildFile(requestor, parent, file);
  processEvent(new VFileCreateEvent(requestor, parent, file, false, null, null, false, null));

  final VirtualFile child = parent.findChild(file);
  if (child == null) {
    throw new IOException("Cannot create child file '" + file + "' at " + parent.getPath());
  }
  if (child.getCharset().equals(StandardCharsets.UTF_8)) {
    Project project = ProjectLocator.getInstance().guessProjectForFile(child);
    EncodingManager encodingManager = project == null ? EncodingManager.getInstance() : EncodingProjectManager.getInstance(project);
    if (encodingManager.shouldAddBOMForNewUtf8File()) {
      child.setBOM(CharsetToolkit.UTF8_BOM);
    }
  }
  return child;
}
 
Example 2
Source File: FilePathImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public Charset getCharset(Project project) {
  // try to find existing virtual file
  VirtualFile existing = myVirtualFile != null && myVirtualFile.isValid() ? myVirtualFile : null;
  if (existing == null) {
    LocalFileSystem lfs = LocalFileSystem.getInstance();
    for (File f = myFile; f != null; f = f.getParentFile()) {
      existing = lfs.findFileByIoFile(f);
      if (existing != null && existing.isValid()) {
        break;
      }
    }
  }
  if (existing != null) {
    Charset rc = existing.getCharset();
    if (rc != null) {
      return rc;
    }
  }
  EncodingManager e = project != null ? EncodingProjectManager.getInstance(project) : null;
  if (e == null) {
    e = EncodingManager.getInstance();
  }
  return e.getDefaultCharset();
}
 
Example 3
Source File: HeavyIdeaTestFixtureImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void setUp() throws Exception {
  super.setUp();

  initApplication();
  setUpProject();

  EncodingManager.getInstance(); // adds listeners
  myEditorListenerTracker = new EditorListenerTracker();
  myThreadTracker = new ThreadTracker();
  InjectedLanguageManagerImpl.pushInjectors(getProject());
}
 
Example 4
Source File: LocalFilePath.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
@Nonnull
public Charset getCharset(@Nullable Project project) {
  VirtualFile file = getVirtualFile();
  String path = myPath;
  while ((file == null || !file.isValid()) && !path.isEmpty()) {
    path = PathUtil.getParentPath(path);
    file = LocalFileSystem.getInstance().findFileByPath(path);
  }
  if (file != null) {
    return file.getCharset();
  }
  EncodingManager e = project == null ? EncodingManager.getInstance() : EncodingProjectManager.getInstance(project);
  return e.getDefaultCharset();
}
 
Example 5
Source File: VcsHistoryUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static String loadRevisionContentGuessEncoding(@Nonnull final VcsFileRevision revision, @Nullable final VirtualFile file,
                                                      @javax.annotation.Nullable final Project project) throws VcsException, IOException {
  final byte[] bytes = loadRevisionContent(revision);
  if (file != null) {
    return new String(bytes, file.getCharset());
  }
  EncodingManager e = project != null ? EncodingProjectManager.getInstance(project) : null;
  if (e == null) {
    e = EncodingManager.getInstance();
  }

  return CharsetToolkit.bytesToString(bytes, e.getDefaultCharset());
}
 
Example 6
Source File: EditorListenerTracker.java    From consulo with Apache License 2.0 4 votes vote down vote up
public EditorListenerTracker() {
  EncodingManager.getInstance(); //adds listeners
  EditorEventMulticasterImpl multicaster = (EditorEventMulticasterImpl)EditorFactory.getInstance().getEventMulticaster();
  before = multicaster.getListeners();
  myDefaultProjectInitialized = ((DefaultProjectFactoryImpl)DefaultProjectFactory.getInstance()).isDefaultProjectInitialized();
}
 
Example 7
Source File: RemoteFilePath.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nonnull
@Override
public Charset getCharset(@Nullable Project project) {
  EncodingManager em = project == null ? EncodingManager.getInstance() : EncodingProjectManager.getInstance(project);
  return em.getDefaultCharset();
}