use of java.nio.charset.Charset in project intellij-community by JetBrains.
the class EncodingManagerImpl method getFavorites.
@Override
@NotNull
public Collection<Charset> getFavorites() {
Collection<Charset> result = new THashSet<>();
Project[] projects = ProjectManager.getInstance().getOpenProjects();
for (Project project : projects) {
result.addAll(EncodingProjectManager.getInstance(project).getFavorites());
}
result.addAll(EncodingProjectManagerImpl.widelyKnownCharsets());
return result;
}
use of java.nio.charset.Charset in project intellij-community by JetBrains.
the class EncodingProjectManagerImpl method setEncoding.
@Override
public void setEncoding(@Nullable final VirtualFile virtualFileOrDir, @Nullable final Charset charset) {
Charset oldCharset;
if (virtualFileOrDir == null) {
oldCharset = myProjectCharset;
myProjectCharset = charset;
} else {
if (charset == null) {
oldCharset = myMapping.remove(virtualFileOrDir);
} else {
oldCharset = myMapping.put(virtualFileOrDir, charset);
}
}
if (!Comparing.equal(oldCharset, charset)) {
myModificationTracker.incModificationCount();
if (virtualFileOrDir != null) {
virtualFileOrDir.setCharset(virtualFileOrDir.getBOM() == null ? charset : null);
}
reloadAllFilesUnder(virtualFileOrDir);
}
}
use of java.nio.charset.Charset in project intellij-community by JetBrains.
the class EncodingUtil method checkCanReload.
@NotNull
public static // returns pair (existing charset (null means N/A); failReason: null means enabled, notnull means disabled and contains error message)
Pair<Charset, String> checkCanReload(@NotNull VirtualFile virtualFile) {
if (virtualFile.isDirectory()) {
return Pair.create(null, REASON_FILE_IS_A_DIRECTORY);
}
FileDocumentManager documentManager = FileDocumentManager.getInstance();
Document document = documentManager.getDocument(virtualFile);
if (document == null)
return Pair.create(null, REASON_BINARY_FILE);
Charset charsetFromContent = ((EncodingManagerImpl) EncodingManager.getInstance()).computeCharsetFromContent(virtualFile);
Charset existing = virtualFile.getCharset();
String autoDetectedFrom = LoadTextUtil.wasCharsetDetectedFromBytes(virtualFile);
String failReason;
if (autoDetectedFrom != null) {
// no point changing encoding if it was auto-detected
failReason = "the encoding was " + autoDetectedFrom;
} else if (charsetFromContent != null) {
failReason = REASON_HARDCODED_IN_TEXT;
existing = charsetFromContent;
} else {
failReason = fileTypeDescriptionError(virtualFile);
}
return Pair.create(existing, failReason);
}
use of java.nio.charset.Charset in project intellij-community by JetBrains.
the class IncompatibleEncodingDialog method createActions.
@NotNull
@Override
protected Action[] createActions() {
DialogWrapperAction reloadAction = new DialogWrapperAction("Reload") {
@Override
protected void doAction(ActionEvent e) {
if (safeToReload == EncodingUtil.Magic8.NO_WAY) {
Pair<Charset, String> detected = EncodingUtil.checkCanReload(virtualFile);
String failReason = detected.second;
Charset autoDetected = detected.first;
int res;
byte[] bom = virtualFile.getBOM();
if (bom != null) {
Messages.showErrorDialog(XmlStringUtil.wrapInHtml("File '" + virtualFile.getName() + "' can't be reloaded in the '" + charset.displayName() + "' encoding.<br><br>" + (failReason == null ? "" : "Why: " + failReason + "<br>") + (autoDetected == null ? "" : "Detected encoding: '" + autoDetected.displayName() + "'")), "Incompatible Encoding: " + charset.displayName());
res = -1;
} else {
res = Messages.showDialog(XmlStringUtil.wrapInHtml("File '" + virtualFile.getName() + "' most likely isn't stored in the '" + charset.displayName() + "' encoding." + "<br><br>" + (failReason == null ? "" : "Why: " + failReason + "<br>") + (autoDetected == null ? "" : "Detected encoding: '" + autoDetected.displayName() + "'")), "Incompatible Encoding: " + charset.displayName(), new String[] { "Reload anyway", "Cancel" }, 1, AllIcons.General.WarningDialog);
}
if (res != 0) {
doCancelAction();
return;
}
}
close(RELOAD_EXIT_CODE);
}
};
if (!SystemInfo.isMac && safeToReload == EncodingUtil.Magic8.NO_WAY) {
reloadAction.putValue(Action.SMALL_ICON, AllIcons.General.Warning);
}
reloadAction.putValue(Action.MNEMONIC_KEY, (int) 'R');
DialogWrapperAction convertAction = new DialogWrapperAction("Convert") {
@Override
protected void doAction(ActionEvent e) {
if (safeToConvert == EncodingUtil.Magic8.NO_WAY) {
String error = EncodingUtil.checkCanConvert(virtualFile);
int res = Messages.showDialog(XmlStringUtil.wrapInHtml("Please do not convert to '" + charset.displayName() + "'.<br><br>" + (error == null ? "Encoding '" + charset.displayName() + "' does not support some characters from the text." : "Because " + error)), "Incompatible Encoding: " + charset.displayName(), new String[] { "Convert anyway", "Cancel" }, 1, AllIcons.General.WarningDialog);
if (res != 0) {
doCancelAction();
return;
}
}
close(CONVERT_EXIT_CODE);
}
@Override
public boolean isEnabled() {
return !FileUtilRt.isTooLarge(virtualFile.getLength());
}
};
if (!SystemInfo.isMac && safeToConvert == EncodingUtil.Magic8.NO_WAY) {
convertAction.putValue(Action.SMALL_ICON, AllIcons.General.Warning);
}
convertAction.putValue(Action.MNEMONIC_KEY, (int) 'C');
Action cancelAction = getCancelAction();
cancelAction.putValue(DEFAULT_ACTION, Boolean.TRUE);
return new Action[] { reloadAction, convertAction, cancelAction };
}
use of java.nio.charset.Charset in project android by JetBrains.
the class EncodingValidationStrategy method validate.
@Override
void validate(@NotNull Module module, @NotNull AndroidModuleModel androidModel) {
GradleVersion modelVersion = (androidModel.getModelVersion());
if (modelVersion != null) {
boolean isOneDotTwoOrNewer = modelVersion.compareIgnoringQualifiers(myOneDotTwoPluginVersion) >= 0;
// Verify that the encoding in the model is the same as the encoding in the IDE's project settings.
Charset modelEncoding = null;
if (isOneDotTwoOrNewer) {
try {
AndroidProject androidProject = androidModel.getAndroidProject();
modelEncoding = Charset.forName(androidProject.getJavaCompileOptions().getEncoding());
} catch (UnsupportedCharsetException ignore) {
// It's not going to happen.
}
}
if (myMismatchingEncoding == null && modelEncoding != null && myProjectEncoding.compareTo(modelEncoding) != 0) {
myMismatchingEncoding = modelEncoding.displayName();
}
}
}
Aggregations