use of java.nio.charset.Charset in project intellij-community by JetBrains.
the class DiffContentFactoryImpl method guessCharsetFromContent.
@Nullable
private static Charset guessCharsetFromContent(@NotNull byte[] content) {
// can't use CharsetToolkit.guessEncoding here because of false-positive INVALID_UTF8
CharsetToolkit toolkit = new CharsetToolkit(content);
Charset fromBOM = toolkit.guessFromBOM();
if (fromBOM != null)
return fromBOM;
CharsetToolkit.GuessedEncoding guessedEncoding = toolkit.guessFromContent(content.length);
switch(guessedEncoding) {
case SEVEN_BIT:
return Charset.forName("US-ASCII");
case VALID_UTF8:
return CharsetToolkit.UTF8_CHARSET;
default:
return null;
}
}
use of java.nio.charset.Charset in project intellij-community by JetBrains.
the class ChangeFileEncodingAction method chosen.
// returns true if charset was changed, false if failed
protected boolean chosen(final Document document, final Editor editor, @Nullable final VirtualFile virtualFile, byte[] bytes, @NotNull final Charset charset) {
if (virtualFile == null)
return false;
String text = document.getText();
EncodingUtil.Magic8 isSafeToConvert = EncodingUtil.isSafeToConvertTo(virtualFile, text, bytes, charset);
EncodingUtil.Magic8 isSafeToReload = EncodingUtil.isSafeToReloadIn(virtualFile, text, bytes, charset);
final Project project = ProjectLocator.getInstance().guessProjectForFile(virtualFile);
final Charset oldCharset = virtualFile.getCharset();
final Runnable undo;
final Runnable redo;
if (isSafeToConvert == EncodingUtil.Magic8.ABSOLUTELY && isSafeToReload == EncodingUtil.Magic8.ABSOLUTELY) {
//change and forget
undo = () -> EncodingManager.getInstance().setEncoding(virtualFile, oldCharset);
redo = () -> EncodingManager.getInstance().setEncoding(virtualFile, charset);
} else {
IncompatibleEncodingDialog dialog = new IncompatibleEncodingDialog(virtualFile, charset, isSafeToReload, isSafeToConvert);
dialog.show();
if (dialog.getExitCode() == IncompatibleEncodingDialog.RELOAD_EXIT_CODE) {
undo = () -> EncodingUtil.reloadIn(virtualFile, oldCharset);
redo = () -> EncodingUtil.reloadIn(virtualFile, charset);
} else if (dialog.getExitCode() == IncompatibleEncodingDialog.CONVERT_EXIT_CODE) {
undo = () -> EncodingUtil.saveIn(document, editor, virtualFile, oldCharset);
redo = () -> EncodingUtil.saveIn(document, editor, virtualFile, charset);
} else {
return false;
}
}
final UndoableAction action = new GlobalUndoableAction(virtualFile) {
@Override
public void undo() {
// invoke later because changing document inside undo/redo is not allowed
Application application = ApplicationManager.getApplication();
application.invokeLater(undo, ModalityState.NON_MODAL, (project == null ? application : project).getDisposed());
}
@Override
public void redo() {
// invoke later because changing document inside undo/redo is not allowed
Application application = ApplicationManager.getApplication();
application.invokeLater(redo, ModalityState.NON_MODAL, (project == null ? application : project).getDisposed());
}
};
redo.run();
CommandProcessor.getInstance().executeCommand(project, () -> {
UndoManager undoManager = project == null ? UndoManager.getGlobalInstance() : UndoManager.getInstance(project);
undoManager.undoableActionPerformed(action);
}, "Change encoding for '" + virtualFile.getName() + "'", null, UndoConfirmationPolicy.REQUEST_CONFIRMATION);
return true;
}
use of java.nio.charset.Charset in project intellij-community by JetBrains.
the class ChooseFileEncodingAction method fillCharsetActions.
private void fillCharsetActions(@NotNull DefaultActionGroup group, @Nullable final VirtualFile virtualFile, @NotNull List<Charset> charsets, @NotNull final Function<Charset, String> charsetFilter) {
for (final Charset charset : charsets) {
AnAction action = new DumbAwareAction(charset.displayName(), null, EmptyIcon.ICON_16) {
@Override
public void actionPerformed(AnActionEvent e) {
chosen(virtualFile, charset);
}
@Override
public void update(AnActionEvent e) {
super.update(e);
String description = charsetFilter.fun(charset);
e.getPresentation().setIcon(description == null ? AllIcons.General.Warning : null);
e.getPresentation().setDescription(description);
}
};
group.add(action);
}
}
use of java.nio.charset.Charset in project intellij-community by JetBrains.
the class EncodingManagerImpl method handleDocument.
private void handleDocument(@NotNull final Document document) {
if (document.getUserData(DETECTING_ENCODING_KEY) == null)
return;
try {
VirtualFile virtualFile = FileDocumentManager.getInstance().getFile(document);
if (virtualFile == null)
return;
Project project = guessProject(virtualFile);
if (project != null && project.isDisposed())
return;
Charset charset = LoadTextUtil.charsetFromContentOrNull(project, virtualFile, document.getImmutableCharSequence());
Charset oldCached = getCachedCharsetFromContent(document);
if (!Comparing.equal(charset, oldCached)) {
setCachedCharsetFromContent(charset, oldCached, document);
}
} finally {
document.putUserData(DETECTING_ENCODING_KEY, null);
}
}
use of java.nio.charset.Charset in project intellij-community by JetBrains.
the class EncodingManagerImpl method computeCharsetFromContent.
@Nullable("returns null if charset set cannot be determined from content")
Charset computeCharsetFromContent(@NotNull final VirtualFile virtualFile) {
final Document document = FileDocumentManager.getInstance().getDocument(virtualFile);
if (document == null) {
return null;
}
Charset cached = EncodingManager.getInstance().getCachedCharsetFromContent(document);
if (cached != null) {
return cached;
}
final Project project = ProjectLocator.getInstance().guessProjectForFile(virtualFile);
return ReadAction.compute(() -> {
Charset charsetFromContent = LoadTextUtil.charsetFromContentOrNull(project, virtualFile, document.getImmutableCharSequence());
if (charsetFromContent != null) {
setCachedCharsetFromContent(charsetFromContent, null, document);
}
return charsetFromContent;
});
}
Aggregations