com.intellij.openapi.vfs.encoding.EncodingProjectManager Java Examples

The following examples show how to use com.intellij.openapi.vfs.encoding.EncodingProjectManager. 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: EncodingManager.java    From editorconfig-jetbrains with MIT License 6 votes vote down vote up
private void applySettings(VirtualFile file) {
    if (file == null || !file.isInLocalFileSystem()) return;
    // Prevent "setEncoding" calling "saveAll" from causing an endless loop
    isApplyingSettings = true;
    final String filePath = file.getCanonicalPath();
    final List<OutPair> outPairs = SettingsProviderComponent.getInstance().getOutPairs(filePath);
    final EncodingProjectManager encodingProjectManager = EncodingProjectManager.getInstance(project);
    final String charset = Utils.configValueForKey(outPairs, charsetKey);
    if (!charset.isEmpty()) {
        if (encodingMap.containsKey(charset)) {
            encodingProjectManager.setEncoding(file, encodingMap.get(charset));
            LOG.debug(Utils.appliedConfigMessage(charset, charsetKey, filePath));
        } else {
            LOG.warn(Utils.invalidConfigMessage(charset, charsetKey, filePath));
        }
    }
    isApplyingSettings = false;
}
 
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: BinaryContent.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings({"EmptyCatchBlock"})
@Nullable
public Document getDocument() {
  if (myDocument == null) {
    if (isBinary()) return null;

    String text = null;
    try {
      Charset charset = ObjectUtil
              .notNull(myCharset, EncodingProjectManager.getInstance(myProject).getDefaultCharset());
      text = CharsetToolkit.bytesToString(myBytes, charset);
    }
    catch (IllegalCharsetNameException e) {
    }

    //  Still NULL? only if not supported or an exception was thrown.
    //  Decode a string using the truly default encoding.
    if (text == null) text = new String(myBytes);
    text = LineTokenizer.correctLineSeparators(text);

    myDocument = EditorFactory.getInstance().createDocument(text);
    myDocument.setReadOnly(true);
  }
  return myDocument;
}
 
Example #4
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 #5
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 #6
Source File: CompilerEncodingServiceImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Inject
public CompilerEncodingServiceImpl(@Nonnull Project project) {
  myProject = project;
  myModuleFileEncodings = CachedValuesManager.getManager(project).createCachedValue(new CachedValueProvider<Map<Module, Set<Charset>>>() {
    @Override
    public Result<Map<Module, Set<Charset>>> compute() {
      Map<Module, Set<Charset>> result = computeModuleCharsetMap();
      return Result.create(result, ProjectRootManager.getInstance(myProject),
                           ((EncodingProjectManagerImpl)EncodingProjectManager.getInstance(myProject)).getModificationTracker());
    }
  }, false);
}
 
Example #7
Source File: CompilerEncodingServiceImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
public Collection<Charset> getAllModuleEncodings(@Nonnull Module module) {
  final Set<Charset> encodings = myModuleFileEncodings.getValue().get(module);
  if (encodings != null) {
    return encodings;
  }
  return ContainerUtil.createMaybeSingletonList(EncodingProjectManager.getInstance(myProject).getDefaultCharset());
}
 
Example #8
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 #9
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 #10
Source File: XQueryRunProfileState.java    From intellij-xquery with Apache License 2.0 4 votes vote down vote up
private void setDefaultCharset(SimpleJavaParameters parameters, final Project project) {
    Charset encoding = EncodingProjectManager.getInstance(project).getDefaultCharset();
    parameters.setCharset(encoding);
}
 
Example #11
Source File: EncodingEnvironmentUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
private static Charset getCharset(Project project) {
  return (project != null ? EncodingProjectManager.getInstance(project) : EncodingManager.getInstance()).getDefaultCharset();
}
 
Example #12
Source File: CompilerEncodingServiceImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
@javax.annotation.Nullable
public Charset getPreferredModuleEncoding(@Nonnull Module module) {
  final Set<Charset> encodings = myModuleFileEncodings.getValue().get(module);
  return ContainerUtil.getFirstItem(encodings, EncodingProjectManager.getInstance(myProject).getDefaultCharset());
}
 
Example #13
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();
}
 
Example #14
Source File: CreatePatchConfigurationPanel.java    From consulo with Apache License 2.0 4 votes vote down vote up
private void initEncodingCombo() {
  final DefaultComboBoxModel<Charset> encodingsModel = new DefaultComboBoxModel<>(CharsetToolkit.getAvailableCharsets());
  myEncoding.setModel(encodingsModel);
  Charset projectCharset = EncodingProjectManager.getInstance(myProject).getDefaultCharset();
  myEncoding.setSelectedItem(projectCharset);
}