@Override @Nullable public Document getDocument() { if (myDocument == null) { if (isBinary()) return null; String text = null; try { Charset charset = ObjectUtils.notNull(myCharset, EncodingProjectManager.getInstance(myProject).getDefaultCharset()); text = CharsetToolkit.bytesToString(myBytes, charset); } catch (IllegalCharsetNameException ignored) { } // Still NULL? only if not supported or an exception was thrown. // Decode a string using the truly default encoding. if (text == null) text = new String(myBytes); text = LineTokenizer.correctLineSeparators(text); myDocument = EditorFactory.getInstance().createDocument(text); myDocument.setReadOnly(true); } return myDocument; }
@NotNull private static Document setupFileEditorAndDocument(@NotNull String fileName, @NotNull String fileText) throws IOException { EncodingProjectManager.getInstance(getProject()).setEncoding(null, CharsetToolkit.UTF8_CHARSET); EncodingProjectManager.getInstance(ProjectManager.getInstance().getDefaultProject()).setEncoding(null, CharsetToolkit.UTF8_CHARSET); PostprocessReformattingAspect.getInstance(ourProject).doPostponedFormatting(); deleteVFile(); myVFile = getSourceRoot().createChildData(null, fileName); VfsUtil.saveText(myVFile, fileText); final FileDocumentManager manager = FileDocumentManager.getInstance(); final Document document = manager.getDocument(myVFile); assertNotNull("Can't create document for '" + fileName + "'", document); manager.reloadFromDisk(document); document.insertString(0, " "); document.deleteString(0, 1); myFile = getPsiManager().findFile(myVFile); assertNotNull("Can't create PsiFile for '" + fileName + "'. Unknown file type most probably.", myFile); assertTrue(myFile.isPhysical()); myEditor = createEditor(myVFile); myVFile.setCharset(CharsetToolkit.UTF8_CHARSET); PsiDocumentManager.getInstance(getProject()).commitAllDocuments(); return document; }
@Test @IdeGuiTest public void testMismatchingEncodings() throws IOException { IdeFrameFixture projectFrame = importSimpleApplication(); final Project project = projectFrame.getProject(); execute(new GuiTask() { @Override protected void executeInEDT() throws Throwable { EncodingProjectManager encodings = EncodingProjectManager.getInstance(project); encodings.setDefaultCharsetName("ISO-8859-1"); } }); projectFrame.requestProjectSync().waitForGradleProjectSyncToFinish(); String expectedMessage = "The project encoding (ISO-8859-1) has been reset to the encoding specified in the Gradle build files (UTF-8)."; ContentFixture syncMessages = projectFrame.getMessagesToolWindow().getGradleSyncContent(); syncMessages.findMessage(INFO, firstLineStartingWith(expectedMessage)); assertEquals("UTF-8", EncodingProjectManager.getInstance(project).getDefaultCharsetName()); }
private void applySettings(VirtualFile file) { if (file == null) return; if (!Utils.isEnabled(CodeStyleSettingsManager.getInstance(myProject).getCurrentSettings())) return; // Prevent "setEncoding" calling "saveAll" from causing an endless loop isApplyingSettings = true; try { final String filePath = Utils.getFilePath(myProject, file); final List<OutPair> outPairs = SettingsProviderComponent.getInstance().getOutPairs(myProject, filePath); final EncodingProjectManager encodingProjectManager = EncodingProjectManager.getInstance(myProject); final String charset = Utils.configValueForKey(outPairs, charsetKey); if (!charset.isEmpty()) { final Charset newCharset = encodingMap.get(charset); if (newCharset != null) { if (Comparing.equal(newCharset, file.getCharset())) return; encodingProjectManager.setEncoding(file, newCharset); } else { Utils.invalidConfigMessage(myProject, charset, charsetKey, filePath); } } } finally { isApplyingSettings = false; } }
@Override public Charset getCharset(Project project) { // try to find existing virtual file VirtualFile virtualFile = getVirtualFile(); VirtualFile existing = virtualFile != null && virtualFile.isValid() ? virtualFile : null; if (existing == null) { LocalFileSystem lfs = LocalFileSystem.getInstance(); for (File f = myFile; f != null; f = f.getParentFile()) { existing = lfs.findFileByIoFile(f); if (existing != null && existing.isValid()) { break; } } } if (existing != null) { Charset rc = existing.getCharset(); if (rc != null) { return rc; } } EncodingManager e = project != null ? EncodingProjectManager.getInstance(project) : null; if (e == null) { e = EncodingManager.getInstance(); } return e.getDefaultCharset(); }
private void applySettings(VirtualFile file) { if (file == null || !file.isInLocalFileSystem()) return; // Prevent "setEncoding" calling "saveAll" from causing an endless loop isApplyingSettings = true; final String filePath = file.getCanonicalPath(); final List<OutPair> outPairs = SettingsProviderComponent.getInstance().getOutPairs(filePath); final EncodingProjectManager encodingProjectManager = EncodingProjectManager.getInstance(project); final String charset = Utils.configValueForKey(outPairs, charsetKey); if (!charset.isEmpty()) { if (encodingMap.containsKey(charset)) { encodingProjectManager.setEncoding(file, encodingMap.get(charset)); LOG.debug(Utils.appliedConfigMessage(charset, charsetKey, filePath)); } else { LOG.warn(Utils.invalidConfigMessage(charset, charsetKey, filePath)); } } isApplyingSettings = false; }
@Nonnull private static Document setupFileEditorAndDocument(@Nonnull String fileName, @Nonnull String fileText) throws IOException { EncodingProjectManager.getInstance(getProject()).setEncoding(null, CharsetToolkit.UTF8_CHARSET); EncodingProjectManager.getInstance(ProjectManager.getInstance().getDefaultProject()).setEncoding(null, CharsetToolkit.UTF8_CHARSET); PostprocessReformattingAspect.getInstance(ourProject).doPostponedFormatting(); deleteVFile(); myVFile = getSourceRoot().createChildData(null, fileName); VfsUtil.saveText(myVFile, fileText); final FileDocumentManager manager = FileDocumentManager.getInstance(); final Document document = manager.getDocument(myVFile); assertNotNull("Can't create document for '" + fileName + "'", document); manager.reloadFromDisk(document); document.insertString(0, " "); document.deleteString(0, 1); myFile = getPsiManager().findFile(myVFile); assertNotNull("Can't create PsiFile for '" + fileName + "'. Unknown file type most probably.", myFile); assertTrue(myFile.isPhysical()); myEditor = createEditor(myVFile); myVFile.setCharset(CharsetToolkit.UTF8_CHARSET); PsiDocumentManager.getInstance(getProject()).commitAllDocuments(); return document; }
@Override @SuppressWarnings({"EmptyCatchBlock"}) @Nullable public Document getDocument() { if (myDocument == null) { if (isBinary()) return null; String text = null; try { Charset charset = ObjectUtil .notNull(myCharset, EncodingProjectManager.getInstance(myProject).getDefaultCharset()); text = CharsetToolkit.bytesToString(myBytes, charset); } catch (IllegalCharsetNameException e) { } // Still NULL? only if not supported or an exception was thrown. // Decode a string using the truly default encoding. if (text == null) text = new String(myBytes); text = LineTokenizer.correctLineSeparators(text); myDocument = EditorFactory.getInstance().createDocument(text); myDocument.setReadOnly(true); } return myDocument; }
@Override public Charset getCharset(Project project) { // try to find existing virtual file VirtualFile existing = myVirtualFile != null && myVirtualFile.isValid() ? myVirtualFile : null; if (existing == null) { LocalFileSystem lfs = LocalFileSystem.getInstance(); for (File f = myFile; f != null; f = f.getParentFile()) { existing = lfs.findFileByIoFile(f); if (existing != null && existing.isValid()) { break; } } } if (existing != null) { Charset rc = existing.getCharset(); if (rc != null) { return rc; } } EncodingManager e = project != null ? EncodingProjectManager.getInstance(project) : null; if (e == null) { e = EncodingManager.getInstance(); } return e.getDefaultCharset(); }
public void testNativeConversion() throws Exception { configureByText(StdFileTypes.PROPERTIES, "a=<caret>v"); EncodingProjectManager.getInstance(getProject()).setNative2AsciiForPropertiesFiles(null, true); UIUtil.dispatchAllInvocationEvents(); //reload files type('\\'); type('\\'); Collection<HighlightInfo> infos = doHighlighting(); assertEquals(0, infos.size()); }
public CompilerEncodingServiceImpl(@NotNull Project project) { myProject = project; myModuleFileEncodings = CachedValuesManager.getManager(project).createCachedValue(new CachedValueProvider<Map<Module, Set<Charset>>>() { @Override public Result<Map<Module, Set<Charset>>> compute() { Map<Module, Set<Charset>> result = computeModuleCharsetMap(); return Result.create(result, ProjectRootManager.getInstance(myProject), ((EncodingProjectManagerImpl)EncodingProjectManager.getInstance(myProject)).getModificationTracker()); } }, false); }
@NotNull @Override public Collection<Charset> getAllModuleEncodings(@NotNull Module module) { final Set<Charset> encodings = myModuleFileEncodings.getValue().get(module); if (encodings != null) { return encodings; } return ContainerUtil.createMaybeSingletonList(EncodingProjectManager.getInstance(myProject).getDefaultCharset()); }
@Override @NotNull public Charset getCharset(@Nullable Project project) { VirtualFile file = getVirtualFile(); String path = myPath; while ((file == null || !file.isValid()) && !path.isEmpty()) { path = PathUtil.getParentPath(path); file = LocalFileSystem.getInstance().findFileByPath(path); } if (file != null) { return file.getCharset(); } EncodingManager e = project == null ? EncodingManager.getInstance() : EncodingProjectManager.getInstance(project); return e.getDefaultCharset(); }
private void initEncodingCombo() { final DefaultComboBoxModel encodingsModel = new DefaultComboBoxModel(CharsetToolkit.getAvailableCharsets()); encodingsModel.insertElementAt(SYSTEM_DEFAULT, 0); myEncoding.setModel(encodingsModel); final String name = EncodingProjectManager.getInstance(myProject).getDefaultCharsetName(); if (StringUtil.isEmpty(name)) { myEncoding.setSelectedItem(SYSTEM_DEFAULT); } else { myEncoding.setSelectedItem(EncodingProjectManager.getInstance(myProject).getDefaultCharset()); } }
public static String loadRevisionContentGuessEncoding(@NotNull final VcsFileRevision revision, @Nullable final VirtualFile file, @Nullable final Project project) throws VcsException, IOException { final byte[] bytes = loadRevisionContent(revision); if (file != null) { return new String(bytes, file.getCharset()); } EncodingManager e = project != null ? EncodingProjectManager.getInstance(project) : null; if (e == null) { e = EncodingManager.getInstance(); } return CharsetToolkit.bytesToString(bytes, e.getDefaultCharset()); }
@Nullable private static String runExternalTool(@NotNull final Module module, @NotNull final HelperPackage formatter, @NotNull final String docstring) { final Sdk sdk = PythonSdkType.findPython2Sdk(module); if (sdk == null) return null; final String sdkHome = sdk.getHomePath(); if (sdkHome == null) return null; final Charset charset = EncodingProjectManager.getInstance(module.getProject()).getDefaultCharset(); final ByteBuffer encoded = charset.encode(docstring); final byte[] data = new byte[encoded.limit()]; encoded.get(data); final Map<String, String> env = new HashMap<String, String>(); PythonEnvUtil.setPythonDontWriteBytecode(env); final GeneralCommandLine commandLine = formatter.newCommandLine(sdkHome, Lists.<String>newArrayList()); LOG.debug("Command for launching docstring formatter: " + commandLine.getCommandLineString()); final ProcessOutput output = PySdkUtil.getProcessOutput(commandLine, new File(sdkHome).getParent(), env, 5000, data, false); if (!output.checkSuccess(LOG)) { return null; } return output.getStdout(); }
private static void setIdeEncodingAndAddEncodingMismatchMessage(@NotNull String newEncoding, @NotNull Project project) { EncodingProjectManager encodings = EncodingProjectManager.getInstance(project); String[] text = { String.format("The project encoding (%1$s) has been reset to the encoding specified in the Gradle build files (%2$s).", encodings.getDefaultCharset().displayName(), newEncoding), "Mismatching encodings can lead to serious bugs." }; encodings.setDefaultCharsetName(newEncoding); NotificationHyperlink openDocHyperlink = new OpenUrlHyperlink("http://tools.android.com/knownissues/encoding", "More Info..."); ProjectSyncMessages.getInstance(project).add(new Message(UNHANDLED_SYNC_ISSUE_TYPE, INFO, text), openDocHyperlink); }
@NotNull public static Charset getDefaultCharset(@NotNull Project project) { if (HGENCODING != null && HGENCODING.length() > 0 && Charset.isSupported(HGENCODING)) { return Charset.forName(HGENCODING); } Charset defaultCharset = null; if (!project.isDisposed()) { defaultCharset = EncodingProjectManager.getInstance(project).getDefaultCharset(); } return defaultCharset != null ? defaultCharset : Charset.defaultCharset(); }
private static void addScriptEncodingSettings(final JavaParameters params, final VirtualFile scriptFile, Module module) { Charset charset = EncodingProjectManager.getInstance(module.getProject()).getEncoding(scriptFile, true); if (charset == null) { charset = EncodingManager.getInstance().getDefaultCharset(); if (!Comparing.equal(CharsetToolkit.getDefaultSystemCharset(), charset)) { params.getProgramParametersList().add("--encoding=" + charset.displayName()); } } else { params.getProgramParametersList().add("--encoding=" + charset.displayName()); } }
public void testNativeConversion() throws Exception { configureFromFileText("x.properties","a=<caret>v"); EncodingProjectManager.getInstance(getProject()).setNative2AsciiForPropertiesFiles(null, true); UIUtil.dispatchAllInvocationEvents(); //reload files type('\\'); type('\\'); Collection<HighlightInfo> infos = doHighlighting(); assertEquals(0, infos.size()); }
public static String loadRevisionContentGuessEncoding(final VcsFileRevision revision, @Nullable final Project project) throws VcsException, IOException { final byte[] bytes = loadRevisionContent(revision); EncodingManager e = project != null ? EncodingProjectManager.getInstance(project) : null; if (e == null) { e = EncodingManager.getInstance(); } return CharsetToolkit.bytesToString(bytes, e.getDefaultCharset()); }
@NotNull public static Charset getDefaultCharset(@NotNull Project project) { if (HGENCODING != null && HGENCODING.length() > 0 && Charset.isSupported(HGENCODING)) { return Charset.forName(HGENCODING); } else { Charset defaultCharset = EncodingProjectManager.getInstance(project).getDefaultCharset(); if (defaultCharset != null) { return defaultCharset; } } return Charset.defaultCharset(); }
public CompilerEncodingServiceImpl(@Nonnull Project project) { myProject = project; myModuleFileEncodings = CachedValuesManager.getManager(project).createCachedValue(new CachedValueProvider<Map<Module, Set<Charset>>>() { @Override public Result<Map<Module, Set<Charset>>> compute() { Map<Module, Set<Charset>> result = computeModuleCharsetMap(); return Result.create(result, ProjectRootManager.getInstance(myProject), ((EncodingProjectManagerImpl)EncodingProjectManager.getInstance(myProject)).getModificationTracker()); } }, false); }
@Nonnull @Override public Collection<Charset> getAllModuleEncodings(@Nonnull Module module) { final Set<Charset> encodings = myModuleFileEncodings.getValue().get(module); if (encodings != null) { return encodings; } return ContainerUtil.createMaybeSingletonList(EncodingProjectManager.getInstance(myProject).getDefaultCharset()); }
@Override @Nonnull public Charset getCharset(@Nullable Project project) { VirtualFile file = getVirtualFile(); String path = myPath; while ((file == null || !file.isValid()) && !path.isEmpty()) { path = PathUtil.getParentPath(path); file = LocalFileSystem.getInstance().findFileByPath(path); } if (file != null) { return file.getCharset(); } EncodingManager e = project == null ? EncodingManager.getInstance() : EncodingProjectManager.getInstance(project); return e.getDefaultCharset(); }
public static String loadRevisionContentGuessEncoding(@Nonnull final VcsFileRevision revision, @Nullable final VirtualFile file, @Nullable final Project project) throws VcsException, IOException { final byte[] bytes = loadRevisionContent(revision); if (file != null) { return new String(bytes, file.getCharset()); } EncodingManager e = project != null ? EncodingProjectManager.getInstance(project) : null; if (e == null) { e = EncodingManager.getInstance(); } return CharsetToolkit.bytesToString(bytes, e.getDefaultCharset()); }
public void setDefaultCharset(final Project project) { Charset encoding = EncodingProjectManager.getInstance(project).getDefaultCharset(); setCharset(encoding); }
@Override @Nullable public Charset getPreferredModuleEncoding(@NotNull Module module) { final Set<Charset> encodings = myModuleFileEncodings.getValue().get(module); return ContainerUtil.getFirstItem(encodings, EncodingProjectManager.getInstance(myProject).getDefaultCharset()); }
private static Charset getCharset(Project project) { return (project != null ? EncodingProjectManager.getInstance(project) : EncodingManager.getInstance()).getDefaultCharset(); }
@NotNull @Override public Charset getCharset(@Nullable Project project) { EncodingManager em = project == null ? EncodingManager.getInstance() : EncodingProjectManager.getInstance(project); return em.getDefaultCharset(); }
@NotNull private static Charset getProjectDefaultCharset(@NotNull Project project) { return EncodingProjectManager.getInstance(project).getDefaultCharset(); }
@Override protected void setUp() throws Exception { super.setUp(); //noinspection StatementWithEmptyBody if (getName().equals("testNotAndroid")) { // Don't add an Android facet here; we're testing that we're a no-op outside of Android projects // since the inspection is registered at the .java source type level return; } // Module must have Android facet or resource type inspection will become a no-op if (AndroidFacet.getInstance(myModule) == null) { String sdkPath = AndroidTestBase.getDefaultTestSdkPath(); String platform = AndroidTestBase.getDefaultPlatformDir(); AndroidTestCase.addAndroidFacet(myModule, sdkPath, platform, true); Sdk sdk = ModuleRootManager.getInstance(myModule).getSdk(); assertNotNull(sdk); @SuppressWarnings("SpellCheckingInspection") SdkModificator sdkModificator = sdk.getSdkModificator(); ExternalAnnotationsSupport.attachJdkAnnotations(sdkModificator); sdkModificator.commitChanges(); } // Required by testLibraryRevocablePermissions (but placing it there leads to // test ordering issues) myFixture.addFileToProject(SdkConstants.FN_ANDROID_MANIFEST_XML, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<manifest xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" + " package=\"test.pkg.permissiontest\">\n" + "\n" + " <uses-sdk android:minSdkVersion=\"17\" android:targetSdkVersion=\"23\" />" + "\n" + " <permission\n" + " android:name=\"my.normal.P1\"\n" + " android:protectionLevel=\"normal\" />\n" + "\n" + " <permission\n" + " android:name=\"my.dangerous.P2\"\n" + " android:protectionLevel=\"dangerous\" />\n" + "\n" + " <uses-permission android:name=\"my.normal.P1\" />\n" + " <uses-permission android:name=\"my.dangerous.P2\" />\n" + "\n" + "</manifest>\n"); myOldCharset = EncodingProjectManager.getInstance(getProject()).getDefaultCharsetName(); EncodingProjectManager.getInstance(getProject()).setDefaultCharsetName("UTF-8"); }
@Override public void tearDown() throws Exception { EncodingProjectManager.getInstance(getProject()).setDefaultCharsetName(myOldCharset); super.tearDown(); }