From 123e461459f41650c97c993cbb23480126128e12 Mon Sep 17 00:00:00 2001 From: Alexey Kudravtsev Date: Thu, 27 Nov 2014 14:21:01 +0300 Subject: [PATCH] IDEA-131468 Settings / File Encoding / Project Encoding = System default: new files are created with IDE encoding; use EncodingProjectManager instead of EncodingManager wherever possible --- .../compiler/server/BuildManager.java | 16 +-- .../configurations/JavaParameters.java | 6 +- .../com/intellij/openapi/vfs/VirtualFile.java | 1 + .../openapi/vfs/encoding/EncodingManager.java | 16 +-- .../vfs/encoding/EncodingProjectManager.java | 19 ++++ .../vfs/encoding/EncodingRegistry.java | 2 +- .../testFramework/LightVirtualFile.java | 2 +- .../intellij/core/CoreEncodingRegistry.java | 1 + .../openapi/fileEditor/impl/LoadTextUtil.java | 104 +++++++++--------- .../openapi/projectRoots/JdkUtil.java | 8 +- .../impl/ConsoleViewRunningState.java | 6 +- .../encoding/FileEncodingConfigurable.java | 46 ++++---- .../debugger/extensions/PlaybackDebugger.java | 13 +-- .../EncodingEnvironmentUtil.java | 14 +-- .../com/intellij/ide/diff/DiffElement.java | 3 +- .../intellij/openapi/diff/BinaryContent.java | 15 +-- .../execution/process/ScriptRunnerUtil.java | 6 +- .../vfs/encoding/EncodingManagerImpl.java | 25 +---- .../encoding/EncodingProjectManagerImpl.java | 32 +++--- .../PlatformFileEncodingConfigurable.java | 17 ++- .../newvfs/impl/VirtualFileSystemEntry.java | 3 +- .../src/messages/IdeBundle.properties | 3 +- .../testFramework/PlatformTestCase.java | 4 +- .../src/com/intellij/openapi/util/Pair.java | 14 +++ .../intellij/openapi/vfs/CharsetToolkit.java | 33 ++---- .../openapi/vcs/vfs/VcsVirtualFile.java | 3 +- .../com/intellij/openapi/vcs/FilePath.java | 2 + .../intellij/openapi/vcs/FilePathImpl.java | 16 ++- .../vcs/changes/VcsCurrentRevisionProxy.java | 2 - .../patch/CreatePatchConfigurationPanel.java | 15 +-- .../vcs/update/ShowUpdatedDiffAction.java | 39 ++++--- .../changeBrowser/CvsContentRevision.java | 5 +- .../lang/properties/PropertiesFileType.java | 2 +- .../idea/svn/diff/DiffContentRevision.java | 5 +- .../python/console/PydevConsoleRunner.java | 10 +- .../PyStructuredDocstringFormatter.java | 1 - .../python/run/PythonCommandLineState.java | 2 +- .../run/PythonScriptCommandLineState.java | 4 +- .../python/sdk/flavors/PythonSdkFlavor.java | 13 +-- .../convert/ConvertSchemaSettingsImpl.java | 10 +- .../plugins/relaxNG/convert/IdeaDriver.java | 4 +- 41 files changed, 265 insertions(+), 277 deletions(-) rename platform/{platform-api => core-api}/src/com/intellij/openapi/vfs/encoding/EncodingManager.java (82%) rename platform/{platform-api => core-api}/src/com/intellij/openapi/vfs/encoding/EncodingProjectManager.java (66%) diff --git a/java/compiler/impl/src/com/intellij/compiler/server/BuildManager.java b/java/compiler/impl/src/com/intellij/compiler/server/BuildManager.java index 94238840c443..219678ffadbe 100644 --- a/java/compiler/impl/src/com/intellij/compiler/server/BuildManager.java +++ b/java/compiler/impl/src/com/intellij/compiler/server/BuildManager.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2013 JetBrains s.r.o. + * Copyright 2000-2014 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -104,8 +104,7 @@ import org.jetbrains.jps.cmdline.ClasspathBootstrap; import org.jetbrains.jps.incremental.Utils; import org.jetbrains.jps.model.serialization.JpsGlobalLoader; -import javax.tools.JavaCompiler; -import javax.tools.ToolProvider; +import javax.tools.*; import java.awt.*; import java.io.File; import java.io.IOException; @@ -211,14 +210,13 @@ public class BuildManager implements ApplicationComponent{ private final BuildMessageDispatcher myMessageDispatcher = new BuildMessageDispatcher(); private volatile int myListenPort = -1; - @Nullable - private final Charset mySystemCharset; + @NotNull + private final Charset mySystemCharset = CharsetToolkit.getDefaultSystemCharset(); public BuildManager(final ProjectManager projectManager) { final Application application = ApplicationManager.getApplication(); IS_UNIT_TEST_MODE = application.isUnitTestMode(); myProjectManager = projectManager; - mySystemCharset = CharsetToolkit.getDefaultSystemCharset(); final String systemPath = PathManager.getSystemPath(); File system = new File(systemPath); try { @@ -994,10 +992,8 @@ public class BuildManager implements ApplicationComponent{ } // javac's VM should use the same default locale that IDEA uses in order for javac to print messages in 'correct' language - if (mySystemCharset != null) { - cmdLine.setCharset(mySystemCharset); - cmdLine.addParameter("-D" + CharsetToolkit.FILE_ENCODING_PROPERTY + "=" + mySystemCharset.name()); - } + cmdLine.setCharset(mySystemCharset); + cmdLine.addParameter("-D" + CharsetToolkit.FILE_ENCODING_PROPERTY + "=" + mySystemCharset.name()); cmdLine.addParameter("-D" + JpsGlobalLoader.FILE_TYPES_COMPONENT_NAME_KEY + "=" + FileTypeManagerImpl.getFileTypeComponentName()); for (String name : new String[]{"user.language", "user.country", "user.region", PathManager.PROPERTY_PATHS_SELECTOR}) { final String value = System.getProperty(name); diff --git a/java/execution/openapi/src/com/intellij/execution/configurations/JavaParameters.java b/java/execution/openapi/src/com/intellij/execution/configurations/JavaParameters.java index bf89d82ea5c1..7025dc3188ff 100644 --- a/java/execution/openapi/src/com/intellij/execution/configurations/JavaParameters.java +++ b/java/execution/openapi/src/com/intellij/execution/configurations/JavaParameters.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2013 JetBrains s.r.o. + * Copyright 2000-2014 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -107,9 +107,7 @@ public class JavaParameters extends SimpleJavaParameters { public void setDefaultCharset(final Project project) { Charset encoding = EncodingProjectManager.getInstance(project).getDefaultCharset(); - if (encoding != null) { - setCharset(encoding); - } + setCharset(encoding); } public void configureByModule(final Module module, diff --git a/platform/core-api/src/com/intellij/openapi/vfs/VirtualFile.java b/platform/core-api/src/com/intellij/openapi/vfs/VirtualFile.java index f32b49567c43..86716af5904a 100644 --- a/platform/core-api/src/com/intellij/openapi/vfs/VirtualFile.java +++ b/platform/core-api/src/com/intellij/openapi/vfs/VirtualFile.java @@ -506,6 +506,7 @@ public abstract class VirtualFile extends UserDataHolderBase implements Modifica /** * @return Retrieve the charset file has been loaded with (if loaded) and would be saved with (if would). */ + @NotNull public Charset getCharset() { Charset charset = getStoredCharset(); if (charset == null) { diff --git a/platform/platform-api/src/com/intellij/openapi/vfs/encoding/EncodingManager.java b/platform/core-api/src/com/intellij/openapi/vfs/encoding/EncodingManager.java similarity index 82% rename from platform/platform-api/src/com/intellij/openapi/vfs/encoding/EncodingManager.java rename to platform/core-api/src/com/intellij/openapi/vfs/encoding/EncodingManager.java index a8e353c141ce..c54556f73dd5 100644 --- a/platform/platform-api/src/com/intellij/openapi/vfs/encoding/EncodingManager.java +++ b/platform/core-api/src/com/intellij/openapi/vfs/encoding/EncodingManager.java @@ -42,19 +42,14 @@ public abstract class EncodingManager extends EncodingRegistry { @NotNull public abstract Collection getFavorites(); - @Deprecated // does nothing - public abstract void setUseUTFGuessing(VirtualFile virtualFile, boolean useUTFGuessing); - + @Override public abstract boolean isNative2AsciiForPropertiesFiles(); public abstract void setNative2AsciiForPropertiesFiles(VirtualFile virtualFile, boolean native2Ascii); - @Nullable + @NotNull // returns empty for system default - public String getDefaultCharsetName() { - Charset charset = getDefaultCharset(); - return charset == null ? null : charset.displayName(); - } + public abstract String getDefaultCharsetName(); public void setDefaultCharsetName(@NotNull String name) { throw new UnsupportedOperationException("Not implemented"); @@ -63,14 +58,11 @@ public abstract class EncodingManager extends EncodingRegistry { /** * @return null for system-default */ + @Override @Nullable public abstract Charset getDefaultCharsetForPropertiesFiles(@Nullable VirtualFile virtualFile); public abstract void setDefaultCharsetForPropertiesFiles(@Nullable VirtualFile virtualFile, @Nullable Charset charset); - /** - * @deprecated use {@link EncodingManager#addPropertyChangeListener(java.beans.PropertyChangeListener, com.intellij.openapi.Disposable)} instead - */ - public abstract void addPropertyChangeListener(@NotNull PropertyChangeListener listener); public abstract void addPropertyChangeListener(@NotNull PropertyChangeListener listener, @NotNull Disposable parentDisposable); public abstract void removePropertyChangeListener(@NotNull PropertyChangeListener listener); diff --git a/platform/platform-api/src/com/intellij/openapi/vfs/encoding/EncodingProjectManager.java b/platform/core-api/src/com/intellij/openapi/vfs/encoding/EncodingProjectManager.java similarity index 66% rename from platform/platform-api/src/com/intellij/openapi/vfs/encoding/EncodingProjectManager.java rename to platform/core-api/src/com/intellij/openapi/vfs/encoding/EncodingProjectManager.java index 93fe7293efc5..6798fb82fb27 100644 --- a/platform/platform-api/src/com/intellij/openapi/vfs/encoding/EncodingProjectManager.java +++ b/platform/core-api/src/com/intellij/openapi/vfs/encoding/EncodingProjectManager.java @@ -35,4 +35,23 @@ public abstract class EncodingProjectManager extends EncodingManager { public abstract void setMapping(@NotNull Map result); + /** + * @return Project encoding name (configured in Settings|File Encodings|Project Encoding) or empty string if it's configured to "System Default" + */ + @NotNull + @Override + public abstract String getDefaultCharsetName(); + + /** + * @return Project encoding (configured in Settings|File Encodings|Project Encoding) + */ + @NotNull + @Override + public abstract Charset getDefaultCharset(); + + /** + * Sets Project encoding (configured in Settings|File Encodings|Project Encoding). Use empty string to specify "System Default" + */ + @Override + public abstract void setDefaultCharsetName(@NotNull String name); } diff --git a/platform/core-api/src/com/intellij/openapi/vfs/encoding/EncodingRegistry.java b/platform/core-api/src/com/intellij/openapi/vfs/encoding/EncodingRegistry.java index 48f5c2796b9a..6f1891747511 100644 --- a/platform/core-api/src/com/intellij/openapi/vfs/encoding/EncodingRegistry.java +++ b/platform/core-api/src/com/intellij/openapi/vfs/encoding/EncodingRegistry.java @@ -37,7 +37,7 @@ public abstract class EncodingRegistry { /** * @return charset configured in Settings|File Encodings|IDE encoding */ - @Nullable + @NotNull public abstract Charset getDefaultCharset(); /** diff --git a/platform/core-api/src/com/intellij/testFramework/LightVirtualFile.java b/platform/core-api/src/com/intellij/testFramework/LightVirtualFile.java index 982d921b9e97..3b85a6aa7e42 100644 --- a/platform/core-api/src/com/intellij/testFramework/LightVirtualFile.java +++ b/platform/core-api/src/com/intellij/testFramework/LightVirtualFile.java @@ -254,7 +254,7 @@ public class LightVirtualFile extends VirtualFile { public byte[] contentsToByteArray() throws IOException { final Charset charset = getCharset(); final String s = getContent().toString(); - return charset != null ? s.getBytes(charset.name()) : s.getBytes(); + return s.getBytes(charset.name()); } @Override diff --git a/platform/core-impl/src/com/intellij/core/CoreEncodingRegistry.java b/platform/core-impl/src/com/intellij/core/CoreEncodingRegistry.java index 466a50c3471f..22442df71b1c 100644 --- a/platform/core-impl/src/com/intellij/core/CoreEncodingRegistry.java +++ b/platform/core-impl/src/com/intellij/core/CoreEncodingRegistry.java @@ -32,6 +32,7 @@ public class CoreEncodingRegistry extends EncodingRegistry { return false; } + @NotNull @Override public Charset getDefaultCharset() { return CharsetToolkit.getDefaultSystemCharset(); diff --git a/platform/core-impl/src/com/intellij/openapi/fileEditor/impl/LoadTextUtil.java b/platform/core-impl/src/com/intellij/openapi/fileEditor/impl/LoadTextUtil.java index 6f1d24e4a8c3..bd8ec7c9a1e1 100644 --- a/platform/core-impl/src/com/intellij/openapi/fileEditor/impl/LoadTextUtil.java +++ b/platform/core-impl/src/com/intellij/openapi/fileEditor/impl/LoadTextUtil.java @@ -34,10 +34,12 @@ import com.intellij.openapi.util.Trinity; import com.intellij.openapi.util.text.StringUtil; import com.intellij.openapi.vfs.CharsetToolkit; import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.openapi.vfs.encoding.EncodingManager; import com.intellij.openapi.vfs.encoding.EncodingRegistry; import com.intellij.testFramework.LightVirtualFile; import com.intellij.util.ArrayUtil; import com.intellij.util.ExceptionUtil; +import com.intellij.util.ObjectUtils; import com.intellij.util.text.CharArrayUtil; import org.jetbrains.annotations.Nls; import org.jetbrains.annotations.NotNull; @@ -121,6 +123,7 @@ public final class LoadTextUtil { return Pair.create(result, detectedLineSeparator); } + @NotNull public static Charset detectCharset(@NotNull VirtualFile virtualFile, @NotNull byte[] content, @NotNull FileType fileType) { Charset charset = null; @@ -142,7 +145,9 @@ public final class LoadTextUtil { } } - charset = charset == null ? EncodingRegistry.getInstance().getDefaultCharset() : charset; + if (charset == null) { + charset = EncodingRegistry.getInstance().getDefaultCharset(); + } if (fileType.getName().equals("Properties") && EncodingRegistry.getInstance().isNative2AsciiForPropertiesFiles()) { charset = Native2AsciiCharset.wrap(charset); } @@ -156,15 +161,15 @@ public final class LoadTextUtil { } @NotNull - private static Pair doDetectCharsetAndSetBOM(@NotNull VirtualFile virtualFile, @NotNull byte[] content, boolean saveBOM) { + private static Pair.NonNull doDetectCharsetAndSetBOM(@NotNull VirtualFile virtualFile, @NotNull byte[] content, boolean saveBOM) { return doDetectCharsetAndSetBOM(virtualFile, content, saveBOM, virtualFile.getFileType()); } @NotNull - private static Pair doDetectCharsetAndSetBOM(@NotNull VirtualFile virtualFile, @NotNull byte[] content, boolean saveBOM, @NotNull FileType fileType) { - Charset charset = virtualFile.isCharsetSet() ? virtualFile.getCharset() : detectCharset(virtualFile, content,fileType); - Pair bomAndCharset = getBOMAndCharset(content, charset); + private static Pair.NonNull doDetectCharsetAndSetBOM(@NotNull VirtualFile virtualFile, @NotNull byte[] content, boolean saveBOM, @NotNull FileType fileType) { + @NotNull Charset charset = virtualFile.isCharsetSet() ? virtualFile.getCharset() : detectCharset(virtualFile, content,fileType); + Pair.NonNull bomAndCharset = getCharsetAndBOM(content, charset); final byte[] bom = bomAndCharset.second; - if (saveBOM && bom != null && bom.length != 0) { + if (saveBOM && bom.length != 0) { virtualFile.setBOM(bom); setCharsetWasDetectedFromBytes(virtualFile, AUTO_DETECTED_FROM_BOM); } @@ -175,7 +180,8 @@ public final class LoadTextUtil { @Nullable("null means no luck, otherwise it's tuple(guessed encoding, hint about content if was unable to guess, BOM)") public static Trinity guessFromContent(@NotNull VirtualFile virtualFile, @NotNull byte[] content, int length) { - CharsetToolkit toolkit = GUESS_UTF ? new CharsetToolkit(content, EncodingRegistry.getInstance().getDefaultCharset()) : null; + Charset defaultCharset = ObjectUtils.notNull(EncodingManager.getInstance().getEncoding(virtualFile, true), CharsetToolkit.getDefaultSystemCharset()); + CharsetToolkit toolkit = GUESS_UTF ? new CharsetToolkit(content, defaultCharset) : null; String detectedFromBytes = null; try { if (GUESS_UTF) { @@ -183,8 +189,7 @@ public final class LoadTextUtil { Charset charset = toolkit.guessFromBOM(); if (charset != null) { detectedFromBytes = AUTO_DETECTED_FROM_BOM; - byte[] bom = CharsetToolkit.getMandatoryBom(charset); - if (bom == null) bom = CharsetToolkit.UTF8_BOM; + byte[] bom = ObjectUtils.notNull(CharsetToolkit.getMandatoryBom(charset), CharsetToolkit.UTF8_BOM); return Trinity.create(charset, null, bom); } CharsetToolkit.GuessedEncoding guessed = toolkit.guessFromContent(length); @@ -204,20 +209,20 @@ public final class LoadTextUtil { } @NotNull - private static Pair getBOMAndCharset(@NotNull byte[] content, final Charset charset) { - if (charset != null && charset.name().contains(CharsetToolkit.UTF8) && CharsetToolkit.hasUTF8Bom(content)) { - return Pair.create(charset, CharsetToolkit.UTF8_BOM); + private static Pair.NonNull getCharsetAndBOM(@NotNull byte[] content, @NotNull Charset charset) { + if (charset.name().contains(CharsetToolkit.UTF8) && CharsetToolkit.hasUTF8Bom(content)) { + return Pair.createNonNull(charset, CharsetToolkit.UTF8_BOM); } try { Charset fromBOM = CharsetToolkit.guessFromBOM(content); if (fromBOM != null) { - return Pair.create(fromBOM, CharsetToolkit.getMandatoryBom(fromBOM)); + return Pair.createNonNull(fromBOM, ObjectUtils.notNull(CharsetToolkit.getMandatoryBom(fromBOM), ArrayUtil.EMPTY_BYTE_ARRAY)); } } catch (UnsupportedCharsetException ignore) { } - return Pair.create(charset, ArrayUtil.EMPTY_BYTE_ARRAY); + return Pair.createNonNull(charset, ArrayUtil.EMPTY_BYTE_ARRAY); } public static void changeLineSeparators(@Nullable Project project, @@ -254,13 +259,11 @@ public final class LoadTextUtil { @NotNull String text, long newModificationStamp) throws IOException { Charset existing = virtualFile.getCharset(); - Pair chosen = charsetForWriting(project, virtualFile, text, existing); + Pair.NonNull chosen = charsetForWriting(project, virtualFile, text, existing); Charset charset = chosen.first; byte[] buffer = chosen.second; - if (charset != null) { - if (!charset.equals(existing)) { - virtualFile.setCharset(charset); - } + if (!charset.equals(existing)) { + virtualFile.setCharset(charset); } setDetectedFromBytesFlagBack(virtualFile, buffer); @@ -274,12 +277,12 @@ public final class LoadTextUtil { } @NotNull - private static Pair charsetForWriting(@Nullable Project project, + private static Pair.NonNull charsetForWriting(@Nullable Project project, @NotNull VirtualFile virtualFile, @NotNull String text, - @Nullable Charset existing) { + @NotNull Charset existing) { Charset specified = extractCharsetFromFileContent(project, virtualFile, text); - Pair chosen = chooseMostlyHarmlessCharset(existing, specified, text); + Pair.NonNull chosen = chooseMostlyHarmlessCharset(existing, specified, text); Charset charset = chosen.first; // in case of "UTF-16", OutputStreamWriter sometimes adds BOM on it's own. @@ -287,7 +290,7 @@ public final class LoadTextUtil { byte[] bom = virtualFile.getBOM(); Charset fromBom = bom == null ? null : CharsetToolkit.guessFromBOM(bom); if (fromBom != null && !fromBom.equals(charset)) { - chosen = Pair.create(fromBom, toBytes(text, fromBom)); + chosen = Pair.createNonNull(fromBom, toBytes(text, fromBom)); } return chosen; } @@ -303,19 +306,24 @@ public final class LoadTextUtil { } @NotNull - public static Pair chooseMostlyHarmlessCharset(Charset existing, Charset specified, @NotNull String text) { + public static Pair.NonNull chooseMostlyHarmlessCharset(@NotNull Charset existing, @NotNull Charset specified, @NotNull String text) { try { - if (existing == null) return Pair.create(specified, toBytes(text, specified)); - if (specified == null || specified.equals(existing)) return Pair.create(specified, toBytes(text, existing)); + if (specified.equals(existing)) { + return Pair.createNonNull(specified, toBytes(text, existing)); + } byte[] out = isSupported(specified, text); - if (out != null) return Pair.create(specified, out); //if explicitly specified encoding is safe, return it + if (out != null) { + return Pair.createNonNull(specified, out); //if explicitly specified encoding is safe, return it + } out = isSupported(existing, text); - if (out != null) return Pair.create(existing, out); //otherwise stick to the old encoding if it's ok - return Pair.create(specified, toBytes(text, specified)); //if both are bad there is no difference + if (out != null) { + return Pair.createNonNull(existing, out); //otherwise stick to the old encoding if it's ok + } + return Pair.createNonNull(specified, toBytes(text, specified)); //if both are bad there is no difference } catch (RuntimeException e) { - return Pair.create(Charset.defaultCharset(), toBytes(text, null)); //if both are bad and there is no hope, use the default charset + return Pair.createNonNull(Charset.defaultCharset(), toBytes(text, null)); //if both are bad and there is no hope, use the default charset } } @@ -341,18 +349,9 @@ public final class LoadTextUtil { } } + @NotNull public static Charset extractCharsetFromFileContent(@Nullable Project project, @NotNull VirtualFile virtualFile, @NotNull String text) { - Charset charset = charsetFromContentOrNull(project, virtualFile, text); - if (charset == null) charset = virtualFile.getCharset(); - return charset; - } - - /** - * @deprecated use {@link #charsetFromContentOrNull(com.intellij.openapi.project.Project, com.intellij.openapi.vfs.VirtualFile, CharSequence)} - */ - @Nullable("returns null if cannot determine from content") - public static Charset charsetFromContentOrNull(@Nullable Project project, @NotNull VirtualFile virtualFile, @NotNull String text) { - return CharsetUtil.extractCharsetFromFileContent(project, virtualFile, virtualFile.getFileType(), text); + return ObjectUtils.notNull(charsetFromContentOrNull(project, virtualFile, text), virtualFile.getCharset()); } @Nullable("returns null if cannot determine from content") @@ -436,11 +435,12 @@ public final class LoadTextUtil { public static CharSequence getTextByBinaryPresentation(@NotNull byte[] bytes, @NotNull VirtualFile virtualFile, boolean saveDetectedSeparators, - boolean saveBOM, @NotNull FileType fileType) { - Pair pair = doDetectCharsetAndSetBOM(virtualFile, bytes, saveBOM, fileType); + boolean saveBOM, + @NotNull FileType fileType) { + Pair.NonNull pair = doDetectCharsetAndSetBOM(virtualFile, bytes, saveBOM, fileType); Charset charset = pair.getFirst(); byte[] bom = pair.getSecond(); - int offset = bom == null ? 0 : bom.length; + int offset = bom.length; Pair result = convertBytes(bytes, charset, offset); if (saveDetectedSeparators) { @@ -476,26 +476,20 @@ public final class LoadTextUtil { } @NotNull - public static CharSequence getTextByBinaryPresentation(@NotNull byte[] bytes, Charset charset) { - Pair pair = getBOMAndCharset(bytes, charset); + public static CharSequence getTextByBinaryPresentation(@NotNull byte[] bytes, @NotNull Charset charset) { + Pair.NonNull pair = getCharsetAndBOM(bytes, charset); byte[] bom = pair.getSecond(); - int offset = bom == null ? 0 : bom.length; + int offset = bom.length; - final Pair result = convertBytes(bytes, charset, offset); + final Pair result = convertBytes(bytes, pair.first, offset); return result.getFirst(); } // do not need to think about BOM here. it is processed outside @NotNull - private static Pair convertBytes(@NotNull byte[] bytes, Charset charset, final int startOffset) { + private static Pair convertBytes(@NotNull byte[] bytes, @NotNull Charset charset, final int startOffset) { ByteBuffer byteBuffer = ByteBuffer.wrap(bytes, startOffset, bytes.length - startOffset); - if (charset == null) { - charset = CharsetToolkit.getDefaultSystemCharset(); - } - if (charset == null) { - charset = Charset.forName("ISO-8859-1"); - } CharBuffer charBuffer; try { charBuffer = charset.decode(byteBuffer); diff --git a/platform/lang-api/src/com/intellij/openapi/projectRoots/JdkUtil.java b/platform/lang-api/src/com/intellij/openapi/projectRoots/JdkUtil.java index 5772c4c1e19c..f7691316df2d 100644 --- a/platform/lang-api/src/com/intellij/openapi/projectRoots/JdkUtil.java +++ b/platform/lang-api/src/com/intellij/openapi/projectRoots/JdkUtil.java @@ -27,7 +27,6 @@ import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.util.text.StringUtilRt; -import com.intellij.openapi.vfs.CharsetToolkit; import com.intellij.openapi.vfs.JarFileSystem; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.openapi.vfs.encoding.EncodingManager; @@ -271,11 +270,8 @@ public class JdkUtil { if (encoding == null) { Charset charset = javaParameters.getCharset(); if (charset == null) charset = EncodingManager.getInstance().getDefaultCharset(); - if (charset == null) charset = CharsetToolkit.getDefaultSystemCharset(); - if (charset != null) { - commandLine.addParameter("-Dfile.encoding=" + charset.name()); - commandLine.withCharset(charset); - } + commandLine.addParameter("-Dfile.encoding=" + charset.name()); + commandLine.withCharset(charset); } else { try { diff --git a/platform/lang-impl/src/com/intellij/execution/impl/ConsoleViewRunningState.java b/platform/lang-impl/src/com/intellij/execution/impl/ConsoleViewRunningState.java index e080d165092a..63d0376d2b09 100644 --- a/platform/lang-impl/src/com/intellij/execution/impl/ConsoleViewRunningState.java +++ b/platform/lang-impl/src/com/intellij/execution/impl/ConsoleViewRunningState.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2011 JetBrains s.r.o. + * Copyright 2000-2014 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,9 +22,7 @@ import com.intellij.execution.process.ProcessEvent; import com.intellij.execution.process.ProcessHandler; import com.intellij.execution.ui.ConsoleViewContentType; import com.intellij.openapi.util.Key; -import com.intellij.openapi.vfs.CharsetToolkit; import com.intellij.openapi.vfs.encoding.EncodingManager; -import com.intellij.util.ObjectUtils; import org.jetbrains.annotations.NotNull; import java.io.IOException; @@ -76,7 +74,7 @@ public class ConsoleViewRunningState extends ConsoleState { charset = ((OSProcessHandler)processHandler).getCharset(); } if (charset == null) { - charset = ObjectUtils.notNull(EncodingManager.getInstance().getDefaultCharset(), CharsetToolkit.UTF8_CHARSET); + charset = EncodingManager.getInstance().getDefaultCharset(); } return new OutputStreamWriter(processInput, charset); } diff --git a/platform/lang-impl/src/com/intellij/openapi/vfs/encoding/FileEncodingConfigurable.java b/platform/lang-impl/src/com/intellij/openapi/vfs/encoding/FileEncodingConfigurable.java index e6c1ae903265..e3157d5626c0 100644 --- a/platform/lang-impl/src/com/intellij/openapi/vfs/encoding/FileEncodingConfigurable.java +++ b/platform/lang-impl/src/com/intellij/openapi/vfs/encoding/FileEncodingConfigurable.java @@ -28,7 +28,7 @@ import com.intellij.openapi.options.SearchableConfigurable; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.Comparing; import com.intellij.openapi.util.Ref; -import com.intellij.openapi.util.text.StringUtil; +import com.intellij.openapi.vfs.CharsetToolkit; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.ui.ScrollPaneFactory; import com.intellij.ui.table.JBTable; @@ -52,8 +52,8 @@ public class FileEncodingConfigurable implements SearchableConfigurable, Optiona private JCheckBox myTransparentNativeToAsciiCheckBox; private JPanel myPropertiesFilesEncodingCombo; private final Ref mySelectedCharsetForPropertiesFiles = new Ref(); - private final Ref mySelectedIdeCharset = new Ref(); - private final Ref mySelectedProjectCharset = new Ref(); + private final Ref mySelectedIdeCharset = new Ref(); // IDE encoding or null if "System Default" + private final Ref mySelectedProjectCharset = new Ref(); // Project encoding or null if "System Default" private JLabel myTitleLabel; private JPanel myIdeEncodingsListCombo; private JPanel myProjectEncodingListCombo; @@ -90,13 +90,14 @@ public class FileEncodingConfigurable implements SearchableConfigurable, Optiona return null; } + @NotNull private static ChooseFileEncodingAction installChooseEncodingCombo(@NotNull JPanel parentPanel, @NotNull final Ref selected) { ChooseFileEncodingAction myAction = new ChooseFileEncodingAction(null) { @Override public void update(final AnActionEvent e) { getTemplatePresentation().setEnabled(true); Charset charset = selected.get(); - getTemplatePresentation().setText(charset == null ? IdeBundle.message("encoding.name.system.default") : charset.displayName()); + getTemplatePresentation().setText(charset == null ? IdeBundle.message("encoding.name.system.default", CharsetToolkit.getDefaultSystemCharset().displayName()) : charset.displayName()); } @Override @@ -150,33 +151,30 @@ public class FileEncodingConfigurable implements SearchableConfigurable, Optiona } private boolean isIdeEncodingModified() { - Charset charset = mySelectedIdeCharset.get(); - if (null == charset) { - return !StringUtil.isEmpty(EncodingManager.getInstance().getDefaultCharsetName()); - } - - return !Comparing.equal(charset, EncodingManager.getInstance().getDefaultCharset()); + String charsetName = getSelectedCharsetName(mySelectedIdeCharset); + return !charsetName.equals(EncodingManager.getInstance().getDefaultCharsetName()); } private boolean isProjectEncodingModified() { - Charset charset = mySelectedProjectCharset.get(); - return !Comparing.equal(charset, EncodingProjectManager.getInstance(myProject).getEncoding(null, false)); + String charsetName = getSelectedCharsetName(mySelectedProjectCharset); + return !charsetName.equals(EncodingProjectManager.getInstance(myProject).getDefaultCharsetName()); + } + + @NotNull // charset name or empty for System Default + private static String getSelectedCharsetName(@NotNull Ref selectedCharset) { + Charset charset = selectedCharset.get(); + return charset == null ? "" : charset.name(); } @Override public void apply() throws ConfigurationException { - Charset projectCharset = mySelectedProjectCharset.get(); + String projectCharsetName = getSelectedCharsetName(mySelectedProjectCharset); Map result = myTreeView.getValues(); - if (projectCharset == null) { - result.remove(null); - } - else { - result.put(null, projectCharset); - } - EncodingProjectManager encodingManager = EncodingProjectManager.getInstance(myProject); - encodingManager.setMapping(result); - encodingManager.setDefaultCharsetForPropertiesFiles(null, mySelectedCharsetForPropertiesFiles.get()); - encodingManager.setNative2AsciiForPropertiesFiles(null, myTransparentNativeToAsciiCheckBox.isSelected()); + EncodingProjectManager encodingProjectManager = EncodingProjectManager.getInstance(myProject); + encodingProjectManager.setMapping(result); + encodingProjectManager.setDefaultCharsetName(projectCharsetName); + encodingProjectManager.setDefaultCharsetForPropertiesFiles(null, mySelectedCharsetForPropertiesFiles.get()); + encodingProjectManager.setNative2AsciiForPropertiesFiles(null, myTransparentNativeToAsciiCheckBox.isSelected()); Charset ideCharset = mySelectedIdeCharset.get(); EncodingManager.getInstance().setDefaultCharsetName(ideCharset == null ? "" : ideCharset.name()); @@ -190,7 +188,7 @@ public class FileEncodingConfigurable implements SearchableConfigurable, Optiona mySelectedCharsetForPropertiesFiles.set(encodingManager.getDefaultCharsetForPropertiesFiles(null)); mySelectedIdeCharset.set(EncodingManager.getInstance().getDefaultCharsetName().isEmpty() ? null : EncodingManager.getInstance().getDefaultCharset()); - mySelectedProjectCharset.set(EncodingProjectManager.getInstance(myProject).getEncoding(null, false)); + mySelectedProjectCharset.set(EncodingProjectManager.getInstance(myProject).getDefaultCharsetName().isEmpty() ? null : EncodingProjectManager.getInstance(myProject).getDefaultCharset()); myPropertiesEncodingAction.update(null); myIdeEncodingAction.update(null); myProjectEncodingAction.update(null); diff --git a/platform/lang-impl/src/com/intellij/ui/debugger/extensions/PlaybackDebugger.java b/platform/lang-impl/src/com/intellij/ui/debugger/extensions/PlaybackDebugger.java index 5dc6cca3a1f5..3177196a090b 100644 --- a/platform/lang-impl/src/com/intellij/ui/debugger/extensions/PlaybackDebugger.java +++ b/platform/lang-impl/src/com/intellij/ui/debugger/extensions/PlaybackDebugger.java @@ -25,13 +25,13 @@ import com.intellij.openapi.fileChooser.FileChooser; import com.intellij.openapi.fileChooser.FileChooserDescriptor; import com.intellij.openapi.fileChooser.FileElement; import com.intellij.openapi.fileChooser.ex.FileChooserKeys; +import com.intellij.openapi.fileEditor.impl.LoadTextUtil; import com.intellij.openapi.ui.Messages; import com.intellij.openapi.ui.Splitter; import com.intellij.openapi.ui.playback.PlaybackContext; import com.intellij.openapi.ui.playback.PlaybackRunner; import com.intellij.openapi.util.text.StringUtil; import com.intellij.openapi.vfs.*; -import com.intellij.openapi.vfs.encoding.EncodingRegistry; import com.intellij.openapi.wm.IdeFrame; import com.intellij.openapi.wm.WindowManager; import com.intellij.openapi.wm.ex.WindowManagerEx; @@ -275,14 +275,9 @@ public class PlaybackDebugger implements UiDebuggerExtension, PlaybackRunner.Sta } private void loadFrom(@NotNull VirtualFile file) { - try { - final String text = CharsetToolkit.bytesToString(file.contentsToByteArray(), EncodingRegistry.getInstance().getDefaultCharset()); - fillDocument(text); - myChanged = false; - } - catch (IOException e) { - Messages.showErrorDialog(e.getMessage(), "Cannot load file"); - } + final String text = LoadTextUtil.loadText(file).toString(); + fillDocument(text); + myChanged = false; } private File getScriptsFile() { diff --git a/platform/platform-api/src/com/intellij/execution/configurations/EncodingEnvironmentUtil.java b/platform/platform-api/src/com/intellij/execution/configurations/EncodingEnvironmentUtil.java index e8999b1fabd7..d728cab1b188 100644 --- a/platform/platform-api/src/com/intellij/execution/configurations/EncodingEnvironmentUtil.java +++ b/platform/platform-api/src/com/intellij/execution/configurations/EncodingEnvironmentUtil.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2013 JetBrains s.r.o. + * Copyright 2000-2014 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,7 +18,6 @@ package com.intellij.execution.configurations; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.SystemInfo; -import com.intellij.openapi.vfs.CharsetToolkit; import com.intellij.openapi.vfs.encoding.EncodingManager; import com.intellij.openapi.vfs.encoding.EncodingProjectManager; import com.intellij.util.EnvironmentUtil; @@ -138,15 +137,12 @@ public class EncodingEnvironmentUtil { @NotNull private static Charset getCharset(@Nullable Project project) { - Charset charset = null; - if (project != null) { - charset = EncodingProjectManager.getInstance(project).getDefaultCharset(); - } - if (charset == null) { + Charset charset; + if (project == null) { charset = EncodingManager.getInstance().getDefaultCharset(); } - if (charset == null) { - charset = CharsetToolkit.UTF8_CHARSET; + else { + charset = EncodingProjectManager.getInstance(project).getDefaultCharset(); } return charset; } diff --git a/platform/platform-api/src/com/intellij/ide/diff/DiffElement.java b/platform/platform-api/src/com/intellij/ide/diff/DiffElement.java index 3fccaad7bc78..185c9ebe55a9 100644 --- a/platform/platform-api/src/com/intellij/ide/diff/DiffElement.java +++ b/platform/platform-api/src/com/intellij/ide/diff/DiffElement.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2011 JetBrains s.r.o. + * Copyright 2000-2014 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -80,6 +80,7 @@ public abstract class DiffElement /*implements Disposable */{ @Nullable public abstract byte[] getContent() throws IOException; + @NotNull public Charset getCharset() { return EncodingManager.getInstance().getDefaultCharset(); } diff --git a/platform/platform-api/src/com/intellij/openapi/diff/BinaryContent.java b/platform/platform-api/src/com/intellij/openapi/diff/BinaryContent.java index b73c59e3ab2f..d4f9a388e33c 100644 --- a/platform/platform-api/src/com/intellij/openapi/diff/BinaryContent.java +++ b/platform/platform-api/src/com/intellij/openapi/diff/BinaryContent.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2013 JetBrains s.r.o. + * Copyright 2000-2014 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,8 +25,9 @@ import com.intellij.openapi.project.ProjectManager; import com.intellij.openapi.vfs.CharsetToolkit; import com.intellij.openapi.vfs.LocalFileSystem; import com.intellij.openapi.vfs.VirtualFile; -import com.intellij.openapi.vfs.encoding.EncodingRegistry; +import com.intellij.openapi.vfs.encoding.EncodingProjectManager; import com.intellij.testFramework.LightVirtualFile; +import com.intellij.util.ObjectUtils; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -45,7 +46,7 @@ public class BinaryContent extends DiffContent { private final byte[] myBytes; private final Charset myCharset; private Document myDocument = null; - private String myFilePath; + private final String myFilePath; /** * @param charset use to convert bytes to String. null means bytes can't be converted to text. Has no sense if fileType.isBinary() @@ -88,12 +89,8 @@ public class BinaryContent extends DiffContent { String text = null; try { - if (myCharset == null) { - text = CharsetToolkit.bytesToString(myBytes, EncodingRegistry.getInstance().getDefaultCharset()); - } - else { - text = CharsetToolkit.bytesToString(myBytes, myCharset); - } + Charset charset = ObjectUtils.notNull(myCharset, EncodingProjectManager.getInstance(myProject).getDefaultCharset()); + text = CharsetToolkit.bytesToString(myBytes, charset); } catch (IllegalCharsetNameException e) { } diff --git a/platform/platform-impl/src/com/intellij/execution/process/ScriptRunnerUtil.java b/platform/platform-impl/src/com/intellij/execution/process/ScriptRunnerUtil.java index 9933dd59f46a..4edb5caadc45 100644 --- a/platform/platform-impl/src/com/intellij/execution/process/ScriptRunnerUtil.java +++ b/platform/platform-impl/src/com/intellij/execution/process/ScriptRunnerUtil.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2013 JetBrains s.r.o. + * Copyright 2000-2014 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,10 +25,8 @@ import com.intellij.openapi.util.Condition; import com.intellij.openapi.util.Conditions; import com.intellij.openapi.util.Key; import com.intellij.openapi.util.text.StringUtil; -import com.intellij.openapi.vfs.CharsetToolkit; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.openapi.vfs.encoding.EncodingManager; -import com.intellij.util.ObjectUtils; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -138,7 +136,7 @@ public final class ScriptRunnerUtil { LOG.debug("Command line env: ", commandLine.getEnvironment()); if (charset == null) { - charset = ObjectUtils.notNull(EncodingManager.getInstance().getDefaultCharset(), CharsetToolkit.UTF8_CHARSET); + charset = EncodingManager.getInstance().getDefaultCharset(); } final OSProcessHandler processHandler = new ColoredProcessHandler(commandLine.createProcess(), commandLine.getCommandLineString(), diff --git a/platform/platform-impl/src/com/intellij/openapi/vfs/encoding/EncodingManagerImpl.java b/platform/platform-impl/src/com/intellij/openapi/vfs/encoding/EncodingManagerImpl.java index 01924432c178..294efab550b1 100644 --- a/platform/platform-impl/src/com/intellij/openapi/vfs/encoding/EncodingManagerImpl.java +++ b/platform/platform-impl/src/com/intellij/openapi/vfs/encoding/EncodingManagerImpl.java @@ -47,6 +47,7 @@ import com.intellij.openapi.util.Key; import com.intellij.openapi.vfs.CharsetToolkit; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.util.Alarm; +import com.intellij.util.ObjectUtils; import com.intellij.util.Processor; import com.intellij.util.xmlb.annotations.Attribute; import gnu.trove.Equality; @@ -83,6 +84,7 @@ public class EncodingManagerImpl extends EncodingManager implements PersistentSt private final PropertyChangeSupport myPropertyChangeSupport = new PropertyChangeSupport(this); static class State { + @NotNull private Charset myDefaultEncoding = CharsetToolkit.UTF8_CHARSET; @Attribute("default_encoding") @@ -92,17 +94,9 @@ public class EncodingManagerImpl extends EncodingManager implements PersistentSt } public void setDefaultCharsetName(@NotNull String name) { - if (name.isEmpty()) { - myDefaultEncoding = ChooseFileEncodingAction.NO_ENCODING; - return; - } - myDefaultEncoding = CharsetToolkit.forName(name); - if (myDefaultEncoding == null) { - myDefaultEncoding = CharsetToolkit.getDefaultSystemCharset(); - } - if (myDefaultEncoding == null) { - myDefaultEncoding = CharsetToolkit.UTF8_CHARSET; - } + myDefaultEncoding = name.isEmpty() + ? ChooseFileEncodingAction.NO_ENCODING + : ObjectUtils.notNull(CharsetToolkit.forName(name), CharsetToolkit.getDefaultSystemCharset()); } } @@ -250,10 +244,6 @@ public class EncodingManagerImpl extends EncodingManager implements PersistentSt return true; } - @Override - public void setUseUTFGuessing(final VirtualFile virtualFile, final boolean useUTFGuessing) { - } - @Override public boolean isNative2Ascii(@NotNull final VirtualFile virtualFile) { Project project = guessProject(virtualFile); @@ -305,11 +295,6 @@ public class EncodingManagerImpl extends EncodingManager implements PersistentSt EncodingProjectManager.getInstance(project).setDefaultCharsetForPropertiesFiles(virtualFile, charset); } - @Override - public void addPropertyChangeListener(@NotNull PropertyChangeListener listener){ - myPropertyChangeSupport.addPropertyChangeListener(listener); - } - @Override public void addPropertyChangeListener(@NotNull final PropertyChangeListener listener, @NotNull Disposable parentDisposable) { myPropertyChangeSupport.addPropertyChangeListener(listener); diff --git a/platform/platform-impl/src/com/intellij/openapi/vfs/encoding/EncodingProjectManagerImpl.java b/platform/platform-impl/src/com/intellij/openapi/vfs/encoding/EncodingProjectManagerImpl.java index 81bc32b511af..37c01c3a512c 100644 --- a/platform/platform-impl/src/com/intellij/openapi/vfs/encoding/EncodingProjectManagerImpl.java +++ b/platform/platform-impl/src/com/intellij/openapi/vfs/encoding/EncodingProjectManagerImpl.java @@ -171,7 +171,8 @@ public class EncodingProjectManagerImpl extends EncodingProjectManager implement if (parent == null) break; parent = parent.getParent(); } - return null; + + return getDefaultCharset(); } @NotNull @@ -331,7 +332,7 @@ public class EncodingProjectManagerImpl extends EncodingProjectManager implement }; } - private boolean processSubFiles(@Nullable("null means in the project") VirtualFile file, @NotNull final Processor processor) { + private boolean processSubFiles(@Nullable("null means all in the project") VirtualFile file, @NotNull final Processor processor) { if (file == null) { for (VirtualFile virtualFile : ProjectRootManager.getInstance(myProject).getContentRoots()) { if (!processSubFiles(virtualFile, processor)) return false; @@ -349,10 +350,10 @@ public class EncodingProjectManagerImpl extends EncodingProjectManager implement //retrieves encoding for the Project node @Override - @Nullable + @NotNull public Charset getDefaultCharset() { Charset charset = getEncoding(null, false); - return charset == null ? EncodingManager.getInstance().getDefaultCharset() : charset; + return charset == null ? Charset.defaultCharset() : charset; } @Override @@ -360,10 +361,6 @@ public class EncodingProjectManagerImpl extends EncodingProjectManager implement return true; } - @Override - public void setUseUTFGuessing(final VirtualFile virtualFile, final boolean useUTFGuessing) { - } - private static final ThreadLocal SUPPRESS_RELOAD = new ThreadLocal(); static void suppressReloadDuring(@NotNull Runnable action) { Boolean old = SUPPRESS_RELOAD.get(); @@ -388,7 +385,7 @@ public class EncodingProjectManagerImpl extends EncodingProjectManager implement }, "Reload Files", false, myProject); } - private void reloadAllFilesUnder(final VirtualFile root) { + private void reloadAllFilesUnder(@Nullable final VirtualFile root) { tryStartReloadWithProgress(new Runnable() { @Override public void run() { @@ -435,6 +432,18 @@ public class EncodingProjectManagerImpl extends EncodingProjectManager implement } } + @NotNull // empty means system default + @Override + public String getDefaultCharsetName() { + Charset charset = getEncoding(null, false); + return charset == null ? "" : charset.name(); + } + + @Override + public void setDefaultCharsetName(@NotNull String name) { + setEncoding(null, name.isEmpty() ? null : CharsetToolkit.forName(name)); + } + @Override @Nullable public Charset getDefaultCharsetForPropertiesFiles(@Nullable final VirtualFile virtualFile) { @@ -450,11 +459,6 @@ public class EncodingProjectManagerImpl extends EncodingProjectManager implement } } - @Override - public void addPropertyChangeListener(@NotNull PropertyChangeListener listener){ - EncodingManager.getInstance().addPropertyChangeListener(listener); - } - @Override public void addPropertyChangeListener(@NotNull PropertyChangeListener listener, @NotNull Disposable parentDisposable) { EncodingManager.getInstance().addPropertyChangeListener(listener,parentDisposable); diff --git a/platform/platform-impl/src/com/intellij/openapi/vfs/encoding/PlatformFileEncodingConfigurable.java b/platform/platform-impl/src/com/intellij/openapi/vfs/encoding/PlatformFileEncodingConfigurable.java index 5da2387c2899..8ae9aadb0dd1 100644 --- a/platform/platform-impl/src/com/intellij/openapi/vfs/encoding/PlatformFileEncodingConfigurable.java +++ b/platform/platform-impl/src/com/intellij/openapi/vfs/encoding/PlatformFileEncodingConfigurable.java @@ -1,3 +1,18 @@ +/* + * Copyright 2000-2014 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.intellij.openapi.vfs.encoding; import com.intellij.ide.IdeBundle; @@ -19,7 +34,7 @@ import java.nio.charset.Charset; * @author yole */ public class PlatformFileEncodingConfigurable implements SearchableConfigurable { - private static final String SYSTEM_DEFAULT = IdeBundle.message("encoding.name.system.default"); + private static final String SYSTEM_DEFAULT = IdeBundle.message("encoding.name.system.default", CharsetToolkit.getDefaultSystemCharset().displayName()); private PlatformEncodingOptionsPanel myPanel; @Override diff --git a/platform/platform-impl/src/com/intellij/openapi/vfs/newvfs/impl/VirtualFileSystemEntry.java b/platform/platform-impl/src/com/intellij/openapi/vfs/newvfs/impl/VirtualFileSystemEntry.java index 00fbdc5dd724..6aa95de1668e 100644 --- a/platform/platform-impl/src/com/intellij/openapi/vfs/newvfs/impl/VirtualFileSystemEntry.java +++ b/platform/platform-impl/src/com/intellij/openapi/vfs/newvfs/impl/VirtualFileSystemEntry.java @@ -21,7 +21,6 @@ import com.intellij.openapi.util.Key; import com.intellij.openapi.util.ThrowableComputable; import com.intellij.openapi.util.io.FileTooBigException; import com.intellij.openapi.util.io.FileUtil; -import com.intellij.openapi.util.text.StringUtil; import com.intellij.openapi.vfs.LocalFileSystem; import com.intellij.openapi.vfs.VFileProperty; import com.intellij.openapi.vfs.VfsBundle; @@ -346,11 +345,13 @@ public abstract class VirtualFileSystemEntry extends NewVirtualFile { VfsData.invalidateFile(myId); } + @NotNull @Override public Charset getCharset() { return isCharsetSet() ? super.getCharset() : computeCharset(); } + @NotNull private Charset computeCharset() { Charset charset; if (isDirectory()) { diff --git a/platform/platform-resources-en/src/messages/IdeBundle.properties b/platform/platform-resources-en/src/messages/IdeBundle.properties index 7442f86d09fd..e5edab33cbf6 100644 --- a/platform/platform-resources-en/src/messages/IdeBundle.properties +++ b/platform/platform-resources-en/src/messages/IdeBundle.properties @@ -26,7 +26,6 @@ title.select.path.to.browser=Select Path to Browser title.general=System Settings editbox.default.encoding.for.properties.files=Default encoding for properties files: checkbox.transparent.native.to.ascii.conversion=Transparent native-to-ascii conversion -checkbox.autodetect.utf=Autodetect UTF-encoded files group.http.proxy=HTTP Proxy group.web.browser=Web Browser group.general.options=General Options @@ -1091,7 +1090,7 @@ file.encodings.not.configured=Encodings are not configured encodings.dialog.caption=To change encoding $productName uses for a file or directory, click an item and then select encoding from the Default Encoding list.

\ Built-in file encoding (e.g. JSP, HTML or XML) overrides encoding you specify here.
\ If not specified, files and directories inherit encoding settings from the parent directory or from the Project Encoding. -encoding.name.system.default= +encoding.name.system.default= (now {0}) quick.lists.presentable.name=Quick lists error.saving.statistics=Error saving system information: {0} diff --git a/platform/testFramework/src/com/intellij/testFramework/PlatformTestCase.java b/platform/testFramework/src/com/intellij/testFramework/PlatformTestCase.java index 13b6c70ee97b..5784d76fbac8 100644 --- a/platform/testFramework/src/com/intellij/testFramework/PlatformTestCase.java +++ b/platform/testFramework/src/com/intellij/testFramework/PlatformTestCase.java @@ -783,7 +783,7 @@ public abstract class PlatformTestCase extends UsefulTestCase implements DataPro return file; } - public static void setContentOnDisk(File file, byte[] bom, String content, Charset charset) throws IOException { + public static void setContentOnDisk(@NotNull File file, byte[] bom, @NotNull String content, @NotNull Charset charset) throws IOException { FileOutputStream stream = new FileOutputStream(file); if (bom != null) { stream.write(bom); @@ -797,7 +797,7 @@ public abstract class PlatformTestCase extends UsefulTestCase implements DataPro } } - public static VirtualFile createTempFile(@NonNls String ext, @Nullable byte[] bom, @NonNls String content, Charset charset) throws IOException { + public static VirtualFile createTempFile(@NonNls @NotNull String ext, @Nullable byte[] bom, @NonNls @NotNull String content, @NotNull Charset charset) throws IOException { File temp = FileUtil.createTempFile("copy", "." + ext); setContentOnDisk(temp, bom, content, charset); diff --git a/platform/util-rt/src/com/intellij/openapi/util/Pair.java b/platform/util-rt/src/com/intellij/openapi/util/Pair.java index 9e74659bdb8c..a8b2a7c8f8e1 100644 --- a/platform/util-rt/src/com/intellij/openapi/util/Pair.java +++ b/platform/util-rt/src/com/intellij/openapi/util/Pair.java @@ -27,6 +27,10 @@ public class Pair { //noinspection DontUsePairConstructor return new Pair(first, second); } + @NotNull + public static NonNull createNonNull(@NotNull A first, @NotNull B second) { + return new NonNull(first, second); + } @NotNull @SuppressWarnings("MethodNamesDifferingOnlyByCase") @@ -38,6 +42,7 @@ public class Pair { @NotNull public static Function> createFunction(final B value) { return new Function>() { + @Override public Pair fun(A a) { return create(a, value); } @@ -73,17 +78,26 @@ public class Pair { return second; } + @Override public final boolean equals(Object o) { return o instanceof Pair && Comparing.equal(first, ((Pair)o).first) && Comparing.equal(second, ((Pair)o).second); } + @Override public int hashCode() { int result = first != null ? first.hashCode() : 0; result = 31 * result + (second != null ? second.hashCode() : 0); return result; } + @Override public String toString() { return "<" + first + "," + second + ">"; } + + public static class NonNull extends Pair { + public NonNull(@NotNull A first, @NotNull B second) { + super(first, second); + } + } } diff --git a/platform/util/src/com/intellij/openapi/vfs/CharsetToolkit.java b/platform/util/src/com/intellij/openapi/vfs/CharsetToolkit.java index b7f95c525168..0efb83543847 100644 --- a/platform/util/src/com/intellij/openapi/vfs/CharsetToolkit.java +++ b/platform/util/src/com/intellij/openapi/vfs/CharsetToolkit.java @@ -88,6 +88,7 @@ public class CharsetToolkit { private static final int BINARY_THRESHOLD = 9; // characters with codes below this considered to be binary private final byte[] buffer; + @NotNull private final Charset defaultCharset; private boolean enforce8Bit = false; @@ -122,9 +123,9 @@ public class CharsetToolkit { * @param buffer the byte buffer of which we want to know the encoding. * @param defaultCharset the default Charset to use in case an 8-bit charset is recognized. */ - public CharsetToolkit(@NotNull byte[] buffer, Charset defaultCharset) { + public CharsetToolkit(@NotNull byte[] buffer, @NotNull Charset defaultCharset) { this.buffer = buffer; - this.defaultCharset = defaultCharset == null ? getDefaultSystemCharset() : defaultCharset; + this.defaultCharset = defaultCharset; } @NotNull @@ -240,6 +241,7 @@ public class CharsetToolkit { /** * Retrieves the default Charset */ + @NotNull public Charset getDefaultCharset() { return defaultCharset; } @@ -268,7 +270,7 @@ public class CharsetToolkit { * * @return the Charset recognized. */ - public Charset guessEncoding(int guess_length, Charset defaultCharset) { + public Charset guessEncoding(int guess_length, @NotNull Charset defaultCharset) { // if the file has a Byte Order Marker, we can assume the file is in UTF-xx // otherwise, the file would not be human readable Charset charset = guessFromBOM(); @@ -436,7 +438,7 @@ public class CharsetToolkit { return guessEncoding(guess_length, defaultCharset); } - public static Charset guessEncoding(@NotNull File f, int bufferLength, Charset defaultCharset) throws IOException { + public static Charset guessEncoding(@NotNull File f, int bufferLength, @NotNull Charset defaultCharset) throws IOException { byte[] buffer = new byte[bufferLength]; int read; FileInputStream fis = new FileInputStream(f); @@ -515,16 +517,9 @@ public class CharsetToolkit { * * @return the default Charset. */ - @Nullable + @NotNull public static Charset getDefaultSystemCharset() { - Charset charset = null; - try { - charset = Charset.forName(System.getProperty(FILE_ENCODING_PROPERTY)); - } catch (Exception ignored) { - // Null is OK here. - } - - return charset; + return Charset.defaultCharset(); } /** @@ -587,8 +582,8 @@ public class CharsetToolkit { } } - public static int getBOMLength(@NotNull byte[] content, Charset charset) { - if (charset != null && charset.name().contains(UTF8) && hasUTF8Bom(content)) { + public static int getBOMLength(@NotNull byte[] content, @NotNull Charset charset) { + if (charset.name().contains(UTF8) && hasUTF8Bom(content)) { return UTF8_BOM.length; } if (hasUTF32BEBom(content)) { @@ -606,14 +601,6 @@ public class CharsetToolkit { return 0; } - /** - * @deprecated use {@link CharsetToolkit#getMandatoryBom(java.nio.charset.Charset)} - */ - @Nullable - public static byte[] getBom(@NotNull Charset charset) { - return getMandatoryBom(charset); - } - /** * @return BOM which is associated with this charset and the charset must have this BOM, or null otherwise. * Currently these are UTF-16xx and UTF-32xx families. diff --git a/platform/vcs-api/src/com/intellij/openapi/vcs/vfs/VcsVirtualFile.java b/platform/vcs-api/src/com/intellij/openapi/vcs/vfs/VcsVirtualFile.java index 4a1953f02f4c..0ff553bbc036 100644 --- a/platform/vcs-api/src/com/intellij/openapi/vcs/vfs/VcsVirtualFile.java +++ b/platform/vcs-api/src/com/intellij/openapi/vcs/vfs/VcsVirtualFile.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2014 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -122,6 +122,7 @@ public class VcsVirtualFile extends AbstractVcsVirtualFile { } + @NotNull @Override public Charset getCharset() { if (myCharset != null) return myCharset; diff --git a/platform/vcs-api/vcs-api-core/src/com/intellij/openapi/vcs/FilePath.java b/platform/vcs-api/vcs-api-core/src/com/intellij/openapi/vcs/FilePath.java index 1ca489c0d128..701dd5838af0 100644 --- a/platform/vcs-api/vcs-api-core/src/com/intellij/openapi/vcs/FilePath.java +++ b/platform/vcs-api/vcs-api-core/src/com/intellij/openapi/vcs/FilePath.java @@ -59,6 +59,7 @@ public interface FilePath { @Nullable Document getDocument(); + @NotNull Charset getCharset(); /** @@ -67,6 +68,7 @@ public interface FilePath { * @param project the project which settings will be consulted * @return the character set of the file */ + @NotNull Charset getCharset(Project project); /** diff --git a/platform/vcs-impl/src/com/intellij/openapi/vcs/FilePathImpl.java b/platform/vcs-impl/src/com/intellij/openapi/vcs/FilePathImpl.java index 5c8e0abac81f..1f00a7c77b3b 100644 --- a/platform/vcs-impl/src/com/intellij/openapi/vcs/FilePathImpl.java +++ b/platform/vcs-impl/src/com/intellij/openapi/vcs/FilePathImpl.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2012 JetBrains s.r.o. + * Copyright 2000-2014 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -144,6 +144,7 @@ public class FilePathImpl implements FilePath { } } + @NotNull @Override public String getPath() { final VirtualFile virtualFile = myVirtualFile; @@ -221,6 +222,7 @@ public class FilePathImpl implements FilePath { return myFile; } + @NotNull @Override public String getName() { return myName; @@ -246,11 +248,13 @@ public class FilePathImpl implements FilePath { } @Override + @NotNull public Charset getCharset() { return getCharset(null); } @Override + @NotNull public Charset getCharset(Project project) { // try to find existing virtual file VirtualFile existing = myVirtualFile != null && myVirtualFile.isValid() ? myVirtualFile : null; @@ -264,15 +268,9 @@ public class FilePathImpl implements FilePath { } } 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 existing.getCharset(); } + EncodingManager e = project == null ? EncodingManager.getInstance() : EncodingProjectManager.getInstance(project); return e.getDefaultCharset(); } diff --git a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/VcsCurrentRevisionProxy.java b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/VcsCurrentRevisionProxy.java index 16ef7515a738..92aff9207c9a 100644 --- a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/VcsCurrentRevisionProxy.java +++ b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/VcsCurrentRevisionProxy.java @@ -23,7 +23,6 @@ import com.intellij.openapi.vcs.history.VcsRevisionNumber; import com.intellij.openapi.vcs.impl.ContentRevisionCache; import com.intellij.openapi.vcs.impl.CurrentRevisionProvider; import com.intellij.openapi.vfs.VirtualFile; -import com.intellij.openapi.vfs.encoding.EncodingManager; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -134,7 +133,6 @@ public class VcsCurrentRevisionProxy implements ContentRevision { throw new VcsException("Failed to create content for current revision"); } Charset charset = myFile.getCharset(); - charset = charset == null ? EncodingManager.getInstance().getDefaultCharset() : charset; return Pair.create(currentRevision, contentRevision.getContent().getBytes(charset)); } } diff --git a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/patch/CreatePatchConfigurationPanel.java b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/patch/CreatePatchConfigurationPanel.java index 2fb12e914799..c02b25e80d91 100644 --- a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/patch/CreatePatchConfigurationPanel.java +++ b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/patch/CreatePatchConfigurationPanel.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2012 JetBrains s.r.o. + * Copyright 2000-2014 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -38,10 +38,11 @@ import com.intellij.openapi.vfs.CharsetToolkit; import com.intellij.openapi.vfs.LocalFileSystem; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.openapi.vfs.VirtualFileWrapper; -import com.intellij.openapi.vfs.encoding.EncodingManager; +import com.intellij.openapi.vfs.encoding.EncodingProjectManager; import com.intellij.ui.HideableTitledPanel; import com.intellij.util.Consumer; import com.intellij.util.ui.FormBuilder; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import javax.swing.*; @@ -54,7 +55,7 @@ import java.util.Collection; import java.util.List; public class CreatePatchConfigurationPanel { - private static final String SYSTEM_DEFAULT = IdeBundle.message("encoding.name.system.default"); + private static final String SYSTEM_DEFAULT = IdeBundle.message("encoding.name.system.default", CharsetToolkit.getDefaultSystemCharset().displayName()); public static final String ALL = "(All)"; private JPanel myMainPanel; @@ -73,7 +74,7 @@ public class CreatePatchConfigurationPanel { private JPanel myPanelWithSelectedFiles; private boolean myExecute; - public CreatePatchConfigurationPanel(final Project project) { + public CreatePatchConfigurationPanel(@NotNull final Project project) { myProject = project; initMainPanel(); @@ -130,12 +131,12 @@ public class CreatePatchConfigurationPanel { encodingsModel.insertElementAt(SYSTEM_DEFAULT, 0); myEncoding.setModel(encodingsModel); - final String name = EncodingManager.getInstance().getDefaultCharsetName(); + final String name = EncodingProjectManager.getInstance(myProject).getDefaultCharsetName(); if (StringUtil.isEmpty(name)) { myEncoding.setSelectedItem(SYSTEM_DEFAULT); } else { - myEncoding.setSelectedItem(EncodingManager.getInstance().getDefaultCharset()); + myEncoding.setSelectedItem(EncodingProjectManager.getInstance(myProject).getDefaultCharset()); } } @@ -143,7 +144,7 @@ public class CreatePatchConfigurationPanel { public Charset getEncoding() { final Object selectedItem = myEncoding.getSelectedItem(); if (SYSTEM_DEFAULT.equals(selectedItem)) { - return EncodingManager.getInstance().getDefaultCharset(); + return CharsetToolkit.getDefaultSystemCharset(); } return (Charset)selectedItem; } diff --git a/platform/vcs-impl/src/com/intellij/openapi/vcs/update/ShowUpdatedDiffAction.java b/platform/vcs-impl/src/com/intellij/openapi/vcs/update/ShowUpdatedDiffAction.java index d6158c594e4f..0bdb4efc7c2e 100644 --- a/platform/vcs-impl/src/com/intellij/openapi/vcs/update/ShowUpdatedDiffAction.java +++ b/platform/vcs-impl/src/com/intellij/openapi/vcs/update/ShowUpdatedDiffAction.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2013 JetBrains s.r.o. + * Copyright 2000-2014 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -31,7 +31,7 @@ import com.intellij.openapi.vcs.changes.actions.ShowDiffAction; import com.intellij.openapi.vcs.changes.actions.ShowDiffUIContext; import com.intellij.openapi.vcs.history.VcsRevisionNumber; import com.intellij.openapi.vfs.VirtualFile; -import com.intellij.openapi.vfs.encoding.EncodingManager; +import com.intellij.openapi.vfs.encoding.EncodingProjectManager; import com.intellij.openapi.vfs.pointers.VirtualFilePointer; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -72,7 +72,7 @@ public class ShowUpdatedDiffAction extends AnAction implements DumbAware { final String selectedUrl = VcsDataKeys.UPDATE_VIEW_SELECTED_PATH.getData(dc); - ShowDiffAction.showDiffForChange(new MyIterableWrapper(iterable.iterator(), before, after), new MySelectionMarker(selectedUrl), + ShowDiffAction.showDiffForChange(new MyIterableWrapper(iterable.iterator(), before, after, project), new MySelectionMarker(selectedUrl), project, new ShowDiffUIContext(true)); } @@ -102,15 +102,20 @@ public class ShowUpdatedDiffAction extends AnAction implements DumbAware { private final Iterator> myVfIterator; private final Label myBefore; private final Label myAfter; + @NotNull private final Project myProject; - private MyIterableWrapper(Iterator> vfIterator, final Label before, final Label after) { + private MyIterableWrapper(Iterator> vfIterator, + final Label before, + final Label after, + @NotNull Project project) { myVfIterator = vfIterator; myBefore = before; myAfter = after; + myProject = project; } public Iterator iterator() { - return new MyIteratorWrapper(myVfIterator, myBefore, myAfter); + return new MyIteratorWrapper(myVfIterator, myBefore, myAfter, myProject); } } @@ -122,7 +127,7 @@ public class ShowUpdatedDiffAction extends AnAction implements DumbAware { } @Nullable - public String convert(final VirtualFilePointer pointer) { + public String convert(final VirtualFilePointer pointer, @NotNull Project project) { if (pointer == null) return null; final String path = pointer.getPresentableUrl(); final ByteContent byteContent = myLabel.getByteContent(FileUtil.toSystemIndependentName(path)); @@ -131,8 +136,9 @@ public class ShowUpdatedDiffAction extends AnAction implements DumbAware { } final VirtualFile vf = pointer.getFile(); if (vf == null) { - return LoadTextUtil.getTextByBinaryPresentation(byteContent.getBytes(), EncodingManager.getInstance().getDefaultCharset()).toString(); - } else { + return LoadTextUtil.getTextByBinaryPresentation(byteContent.getBytes(), EncodingProjectManager.getInstance(project).getDefaultCharset()).toString(); + } + else { return LoadTextUtil.getTextByBinaryPresentation(byteContent.getBytes(), vf).toString(); } } @@ -143,11 +149,13 @@ public class ShowUpdatedDiffAction extends AnAction implements DumbAware { private final MyLoader myLoader; private final VirtualFilePointer myPointer; private final boolean myBefore; + @NotNull private final Project myProject; - private MyCheckpointContentRevision(final VirtualFilePointer pointer, final MyLoader loader, final boolean before) { + private MyCheckpointContentRevision(final VirtualFilePointer pointer, final MyLoader loader, final boolean before, @NotNull Project project) { myLoader = loader; myPointer = pointer; myBefore = before; + myProject = project; } public String getContent() throws VcsException { @@ -156,7 +164,7 @@ public class ShowUpdatedDiffAction extends AnAction implements DumbAware { return s; } - final String loaded = myLoader.convert(myPointer); + final String loaded = myLoader.convert(myPointer, myProject); myContent = new SoftReference(loaded); return loaded; @@ -193,9 +201,14 @@ public class ShowUpdatedDiffAction extends AnAction implements DumbAware { private final MyLoader myBeforeLoader; private final MyLoader myAfterLoader; private final Iterator> myVfIterator; + @NotNull private final Project myProject; - public MyIteratorWrapper(final Iterator> vfIterator, final Label before, final Label after) { + public MyIteratorWrapper(final Iterator> vfIterator, + final Label before, + final Label after, + @NotNull Project project) { myVfIterator = vfIterator; + myProject = project; myBeforeLoader = new MyLoader(before); myAfterLoader = new MyLoader(after); } @@ -208,8 +221,8 @@ public class ShowUpdatedDiffAction extends AnAction implements DumbAware { final Pair pair = myVfIterator.next(); final VirtualFilePointer pointer = pair.getFirst(); - MyCheckpointContentRevision before = new MyCheckpointContentRevision(pointer, myBeforeLoader, true); - MyCheckpointContentRevision after = new MyCheckpointContentRevision(pointer, myAfterLoader, false); + MyCheckpointContentRevision before = new MyCheckpointContentRevision(pointer, myBeforeLoader, true, myProject); + MyCheckpointContentRevision after = new MyCheckpointContentRevision(pointer, myAfterLoader, false, myProject); if (FileStatus.ADDED.equals(pair.getSecond())) { before = null; } else if (FileStatus.DELETED.equals(pair.getSecond())) { diff --git a/plugins/cvs/cvs-plugin/src/com/intellij/cvsSupport2/changeBrowser/CvsContentRevision.java b/plugins/cvs/cvs-plugin/src/com/intellij/cvsSupport2/changeBrowser/CvsContentRevision.java index 10788cb563e6..25063be661e2 100644 --- a/plugins/cvs/cvs-plugin/src/com/intellij/cvsSupport2/changeBrowser/CvsContentRevision.java +++ b/plugins/cvs/cvs-plugin/src/com/intellij/cvsSupport2/changeBrowser/CvsContentRevision.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2014 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -38,7 +38,6 @@ import com.intellij.openapi.vcs.actions.VcsContextFactory; import com.intellij.openapi.vcs.changes.ContentRevision; import com.intellij.openapi.vcs.history.VcsRevisionNumber; import com.intellij.openapi.vfs.CharsetToolkit; -import com.intellij.openapi.vfs.encoding.EncodingRegistry; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -73,7 +72,7 @@ public class CvsContentRevision implements ContentRevision { byte[] content = loadContent(); if (content != null) { final Charset charset = myLocalFile.getCharset(); - myContent = charset == null ? CharsetToolkit.bytesToString(content, EncodingRegistry.getInstance().getDefaultCharset()) : CharsetToolkit.bytesToString(content, charset); + myContent = CharsetToolkit.bytesToString(content, charset); } } return myContent; diff --git a/plugins/properties/properties-psi-api/src/com/intellij/lang/properties/PropertiesFileType.java b/plugins/properties/properties-psi-api/src/com/intellij/lang/properties/PropertiesFileType.java index 8173aa4915ca..d9b57434d57c 100644 --- a/plugins/properties/properties-psi-api/src/com/intellij/lang/properties/PropertiesFileType.java +++ b/plugins/properties/properties-psi-api/src/com/intellij/lang/properties/PropertiesFileType.java @@ -67,6 +67,6 @@ public class PropertiesFileType extends LanguageFileType { if (charset == null) { charset = CharsetToolkit.getDefaultSystemCharset(); } - return charset != null ? charset.name() : null; + return charset.name(); } } diff --git a/plugins/svn4idea/src/org/jetbrains/idea/svn/diff/DiffContentRevision.java b/plugins/svn4idea/src/org/jetbrains/idea/svn/diff/DiffContentRevision.java index 6d360fe1c685..24d92183f00e 100644 --- a/plugins/svn4idea/src/org/jetbrains/idea/svn/diff/DiffContentRevision.java +++ b/plugins/svn4idea/src/org/jetbrains/idea/svn/diff/DiffContentRevision.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2014 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,7 +21,6 @@ import com.intellij.openapi.vcs.VcsException; import com.intellij.openapi.vcs.changes.ContentRevision; import com.intellij.openapi.vcs.history.VcsRevisionNumber; import com.intellij.openapi.vfs.CharsetToolkit; -import com.intellij.openapi.vfs.encoding.EncodingRegistry; import com.intellij.vcsUtil.VcsUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -60,7 +59,7 @@ public class DiffContentRevision implements ContentRevision { } final byte[] bytes = bos.toByteArray(); final Charset charset = myFilePath.getCharset(); - myContents = charset == null ? CharsetToolkit.bytesToString(bytes, EncodingRegistry.getInstance().getDefaultCharset()) : CharsetToolkit.bytesToString(bytes, charset); + myContents = CharsetToolkit.bytesToString(bytes, charset); } return myContents; } diff --git a/python/src/com/jetbrains/python/console/PydevConsoleRunner.java b/python/src/com/jetbrains/python/console/PydevConsoleRunner.java index adae12303a0c..aab118a4bbae 100644 --- a/python/src/com/jetbrains/python/console/PydevConsoleRunner.java +++ b/python/src/com/jetbrains/python/console/PydevConsoleRunner.java @@ -64,7 +64,7 @@ import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.util.io.StreamUtil; import com.intellij.openapi.vfs.CharsetToolkit; import com.intellij.openapi.vfs.VirtualFile; -import com.intellij.openapi.vfs.encoding.EncodingManager; +import com.intellij.openapi.vfs.encoding.EncodingProjectManager; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiFile; import com.intellij.psi.impl.source.tree.FileElement; @@ -243,10 +243,10 @@ public class PydevConsoleRunner extends AbstractConsoleRunnerWithHistory addDefaultEnvironments(Sdk sdk, Map envs) { - Charset defaultCharset = EncodingManager.getInstance().getDefaultCharset(); + public static Map addDefaultEnvironments(Sdk sdk, Map envs, @NotNull Project project) { + Charset defaultCharset = EncodingProjectManager.getInstance(project).getDefaultCharset(); - final String encoding = defaultCharset != null ? defaultCharset.name() : "utf-8"; + final String encoding = defaultCharset.name(); setPythonIOEncoding(setPythonUnbuffered(envs), encoding); PythonSdkFlavor.initPythonPath(envs, true, PythonCommandLineState.getAddedPaths(sdk)); @@ -392,7 +392,7 @@ public class PydevConsoleRunner extends AbstractConsoleRunnerWithHistory getAdditionalEnvs() { - return addDefaultEnvironments(sdk, environmentVariables); + return addDefaultEnvironments(sdk, environmentVariables,getProject()); } }; } diff --git a/python/src/com/jetbrains/python/documentation/PyStructuredDocstringFormatter.java b/python/src/com/jetbrains/python/documentation/PyStructuredDocstringFormatter.java index 43786cf3542f..e533f98609e9 100644 --- a/python/src/com/jetbrains/python/documentation/PyStructuredDocstringFormatter.java +++ b/python/src/com/jetbrains/python/documentation/PyStructuredDocstringFormatter.java @@ -100,7 +100,6 @@ public class PyStructuredDocstringFormatter { if (sdkHome == null) return null; final Charset charset = EncodingProjectManager.getInstance(module.getProject()).getDefaultCharset(); - if (charset == null) return null; final ByteBuffer encoded = charset.encode(docstring); final byte[] data = new byte[encoded.limit()]; diff --git a/python/src/com/jetbrains/python/run/PythonCommandLineState.java b/python/src/com/jetbrains/python/run/PythonCommandLineState.java index 476b8646bed9..c6615604b61b 100644 --- a/python/src/com/jetbrains/python/run/PythonCommandLineState.java +++ b/python/src/com/jetbrains/python/run/PythonCommandLineState.java @@ -269,7 +269,7 @@ public abstract class PythonCommandLineState extends CommandLineState { public void addPredefinedEnvironmentVariables(Map envs, boolean passParentEnvs) { final PythonSdkFlavor flavor = PythonSdkFlavor.getFlavor(myConfig.getInterpreterPath()); if (flavor != null) { - flavor.addPredefinedEnvironmentVariables(envs); + flavor.addPredefinedEnvironmentVariables(envs, myConfig.getProject()); } } diff --git a/python/src/com/jetbrains/python/run/PythonScriptCommandLineState.java b/python/src/com/jetbrains/python/run/PythonScriptCommandLineState.java index bc3a19082087..fd3423ebd07b 100644 --- a/python/src/com/jetbrains/python/run/PythonScriptCommandLineState.java +++ b/python/src/com/jetbrains/python/run/PythonScriptCommandLineState.java @@ -24,9 +24,9 @@ import com.intellij.execution.configurations.GeneralCommandLine; import com.intellij.execution.configurations.ParametersList; import com.intellij.execution.configurations.ParamsGroup; import com.intellij.execution.executors.DefaultDebugExecutor; +import com.intellij.execution.process.CommandLineArgumentsProvider; import com.intellij.execution.runners.ExecutionEnvironment; import com.intellij.openapi.actionSystem.AnAction; -import com.intellij.execution.process.CommandLineArgumentsProvider; import com.intellij.openapi.project.Project; import com.intellij.openapi.projectRoots.Sdk; import com.intellij.openapi.util.io.FileUtil; @@ -158,7 +158,7 @@ public class PythonScriptCommandLineState extends PythonCommandLineState { @Override public Map getAdditionalEnvs() { - Map map = addDefaultEnvironments(sdk, environmentVariables); + Map map = addDefaultEnvironments(sdk, environmentVariables,getProject()); map.putAll(cmd.getEnvironment()); return map; } diff --git a/python/src/com/jetbrains/python/sdk/flavors/PythonSdkFlavor.java b/python/src/com/jetbrains/python/sdk/flavors/PythonSdkFlavor.java index 3e61178f1ad6..0f33f555a359 100644 --- a/python/src/com/jetbrains/python/sdk/flavors/PythonSdkFlavor.java +++ b/python/src/com/jetbrains/python/sdk/flavors/PythonSdkFlavor.java @@ -19,13 +19,14 @@ import com.google.common.collect.Lists; import com.intellij.execution.configurations.GeneralCommandLine; import com.intellij.execution.process.ProcessOutput; import com.intellij.openapi.diagnostic.Logger; +import com.intellij.openapi.project.Project; import com.intellij.openapi.projectRoots.Sdk; import com.intellij.openapi.projectRoots.SdkAdditionalData; import com.intellij.openapi.util.SystemInfo; import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.util.text.StringUtil; import com.intellij.openapi.vfs.VirtualFile; -import com.intellij.openapi.vfs.encoding.EncodingManager; +import com.intellij.openapi.vfs.encoding.EncodingProjectManager; import com.intellij.util.PatternUtil; import com.jetbrains.python.psi.LanguageLevel; import com.jetbrains.python.sdk.PySdkUtil; @@ -208,12 +209,10 @@ public abstract class PythonSdkFlavor { } @SuppressWarnings({"MethodMayBeStatic"}) - public void addPredefinedEnvironmentVariables(Map envs) { - Charset defaultCharset = EncodingManager.getInstance().getDefaultCharset(); - if (defaultCharset != null) { - final String encoding = defaultCharset.name(); - PythonEnvUtil.setPythonIOEncoding(envs, encoding); - } + public void addPredefinedEnvironmentVariables(Map envs, @NotNull Project project) { + Charset defaultCharset = EncodingProjectManager.getInstance(project).getDefaultCharset(); + final String encoding = defaultCharset.name(); + PythonEnvUtil.setPythonIOEncoding(envs, encoding); } @NotNull diff --git a/xml/relaxng/src/org/intellij/plugins/relaxNG/convert/ConvertSchemaSettingsImpl.java b/xml/relaxng/src/org/intellij/plugins/relaxNG/convert/ConvertSchemaSettingsImpl.java index f91ecfb8882e..16956e5ff22f 100644 --- a/xml/relaxng/src/org/intellij/plugins/relaxNG/convert/ConvertSchemaSettingsImpl.java +++ b/xml/relaxng/src/org/intellij/plugins/relaxNG/convert/ConvertSchemaSettingsImpl.java @@ -28,7 +28,7 @@ import com.intellij.openapi.ui.ComboBox; import com.intellij.openapi.ui.TextFieldWithBrowseButton; import com.intellij.openapi.vfs.CharsetToolkit; import com.intellij.openapi.vfs.VirtualFile; -import com.intellij.openapi.vfs.encoding.EncodingManager; +import com.intellij.openapi.vfs.encoding.EncodingProjectManager; import com.intellij.psi.codeStyle.CodeStyleSettings; import com.intellij.psi.codeStyle.CodeStyleSettingsManager; import com.intellij.ui.DocumentAdapter; @@ -120,12 +120,8 @@ public class ConvertSchemaSettingsImpl implements ConvertSchemaSettings { } myEncoding.setModel(new DefaultComboBoxModel(suggestions.toArray())); - final Charset charset = EncodingManager.getInstance().getDefaultCharset(); - if (charset == null) { - myEncoding.setSelectedItem(System.getProperty("file.encoding", "UTF-8")); - } else { - myEncoding.setSelectedItem(charset.name()); - } + final Charset charset = EncodingProjectManager.getInstance(project).getDefaultCharset(); + myEncoding.setSelectedItem(charset.name()); final CodeStyleSettings styleSettings = CodeStyleSettingsManager.getSettings(project); final int indent = styleSettings.getIndentSize(type); diff --git a/xml/relaxng/src/org/intellij/plugins/relaxNG/convert/IdeaDriver.java b/xml/relaxng/src/org/intellij/plugins/relaxNG/convert/IdeaDriver.java index a0b8cad3d94f..723ee4bf9302 100644 --- a/xml/relaxng/src/org/intellij/plugins/relaxNG/convert/IdeaDriver.java +++ b/xml/relaxng/src/org/intellij/plugins/relaxNG/convert/IdeaDriver.java @@ -83,9 +83,7 @@ public class IdeaDriver { if (inputType != SchemaType.DTD) { final Charset charset = inputFile.getCharset(); - if (charset != null) { - inputParams.add("encoding=" + charset.name()); - } + inputParams.add("encoding=" + charset.name()); } final ArrayList outputParams = new ArrayList();