public void testText() throws Exception { doTest("Text.txt"); Charset ascii = CharsetToolkit.forName("US-ASCII"); VirtualFile myVFile = myFile.getVirtualFile(); FileDocumentManager.getInstance().saveAllDocuments(); EncodingManager.getInstance().setEncoding(myVFile, ascii); UIUtil.dispatchAllInvocationEvents(); // wait for reload requests to bubble up assertEquals(ascii, myVFile.getCharset()); int start = myEditor.getCaretModel().getOffset(); type((char)0x445); type((char)0x438); int end = myEditor.getCaretModel().getOffset(); Collection<HighlightInfo> infos = doHighlighting(); HighlightInfo info = assertOneElement(infos); assertEquals("Unsupported characters for the charset 'US-ASCII'", info.getDescription()); assertEquals(start, info.startOffset); assertEquals(end, info.endOffset); backspace(); backspace(); doDoTest(true, false); }
public void testUsageViewDoesNotHoldPsiFilesOrDocuments() throws Exception { PsiFile psiFile = myFixture.addFileToProject("X.java", "public class X{} //iuggjhfg"); Usage[] usages = new Usage[100]; for (int i = 0; i < usages.length; i++) { usages[i] = createUsage(psiFile,i); } UsageView usageView = UsageViewManager.getInstance(getProject()).createUsageView(UsageTarget.EMPTY_ARRAY, usages, new UsageViewPresentation(), null); Disposer.register(getTestRootDisposable(), usageView); ((EncodingManagerImpl)EncodingManager.getInstance()).clearDocumentQueue(); FileDocumentManager.getInstance().saveAllDocuments(); UIUtil.dispatchAllInvocationEvents(); LeakHunter.checkLeak(usageView, PsiFileImpl.class); LeakHunter.checkLeak(usageView, Document.class); }
protected synchronized void execute(GeneralCommandLine commandLine) throws AuthenticationException { try { commandLine.withParentEnvironmentType(ParentEnvironmentType.CONSOLE); myProcess = commandLine.createProcess(); myErrThread = new ReadProcessThread( new BufferedReader(new InputStreamReader(myProcess.getErrorStream(), EncodingManager.getInstance().getDefaultCharset()))) { protected void textAvailable(String s) { myErrorText.append(s); myErrorRegistry.registerError(s); myContainsError = true; } }; final Application application = ApplicationManager.getApplication(); myStdErrFuture = application.executeOnPooledThread(myErrThread); myInputStream = myProcess.getInputStream(); myOutputStream = myProcess.getOutputStream(); waitForProcess(application); } catch (Exception e) { closeInternal(); throw new AuthenticationException(e.getLocalizedMessage(), e); } }
public static void createMockApplication(Disposable parentDisposable) { final BlazeMockApplication instance = new BlazeMockApplication(parentDisposable); // If there was no previous application, // ApplicationManager leaves the MockApplication in place, which can break future tests. Application oldApplication = ApplicationManager.getApplication(); if (oldApplication == null) { Disposer.register( parentDisposable, () -> { new ApplicationManager() { { ourApplication = null; } }; }); } ApplicationManager.setApplication(instance, FileTypeManager::getInstance, parentDisposable); instance.registerService(EncodingManager.class, EncodingManagerImpl.class); }
/** * For every #createMockApplication there needs to be a corresponding call to * #disposeMockApplication when the test is complete. */ public static Disposable createMockApplication() { Disposable parentDisposable = getParentDisposableForCleanup(); final PluginMockApplication instance = new PluginMockApplication(parentDisposable); ApplicationManager.setApplication( instance, new Getter<FileTypeRegistry>() { @Override public FileTypeRegistry get() { return FileTypeManager.getInstance(); } }, parentDisposable); instance.registerService(EncodingManager.class, EncodingManagerImpl.class); return parentDisposable; }
public void testText() throws Exception { doTest("Text.txt"); Charset ascii = CharsetToolkit.forName("US-ASCII"); EncodingManager.getInstance().setEncoding(myVFile, ascii); assertEquals(ascii, myVFile.getCharset()); int start = myEditor.getCaretModel().getOffset(); type((char)0x445); type((char)0x438); int end = myEditor.getCaretModel().getOffset(); Collection<HighlightInfo> infos = doHighlighting(); HighlightInfo info = assertOneElement(infos); assertEquals("Unsupported characters for the charset 'US-ASCII'", info.getDescription()); assertEquals(start, info.startOffset); assertEquals(end, info.endOffset); backspace(); backspace(); doTestConfiguredFile(true, false, null); }
public void initApplication() { //if (ApplicationManager.getApplication() instanceof MockApplicationEx) return; final MockApplicationEx instance = new MockApplicationEx(getTestRootDisposable()); ApplicationManager.setApplication(instance, new Getter<FileTypeRegistry>() { @Override public FileTypeRegistry get() { return FileTypeManager.getInstance(); } }, new Getter<EncodingRegistry>() { @Override public EncodingRegistry get() { return EncodingManager.getInstance(); } }, getTestRootDisposable()); getApplication().registerService(EncodingManager.class, EncodingManagerImpl.class); }
public EditorChangeAction(DocumentEx document, int offset, CharSequence oldString, CharSequence newString, long oldTimeStamp) { super(document); Charset charset = EncodingManager.getInstance().getEncoding(FileDocumentManager.getInstance().getFile(document), false); myCharset = charset == null ? Charset.defaultCharset() : charset; myOffset = offset; myOldString = oldString == null ? "" : compressCharSequence(oldString, myCharset); myNewString = newString == null ? "" : compressCharSequence(newString, myCharset); myOldTimeStamp = oldTimeStamp; myNewTimeStamp = document.getModificationStamp(); }
public void testUsageViewDoesNotHoldPsiFilesOrDocuments() throws Exception { PsiFile psiFile = createFile("X.java", "public class X{} //iuggjhfg"); Usage[] usages = new Usage[100]; for (int i = 0; i < usages.length; i++) { usages[i] = createUsage(psiFile,i); } UsageView usageView = UsageViewManager.getInstance(getProject()).createUsageView(UsageTarget.EMPTY_ARRAY, usages, new UsageViewPresentation(), null); Disposer.register(getTestRootDisposable(), usageView); ((EncodingManagerImpl)EncodingManager.getInstance()).clearDocumentQueue(); FileDocumentManager.getInstance().saveAllDocuments(); UIUtil.dispatchAllInvocationEvents(); LeakHunter.checkLeak(usageView, PsiFileImpl.class); LeakHunter.checkLeak(usageView, Document.class); }
@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(); }
protected synchronized void execute(GeneralCommandLine commandLine) throws AuthenticationException { try { commandLine.setPassParentEnvironment(true); myProcess = commandLine.createProcess(); myErrThread = new ReadProcessThread( new BufferedReader(new InputStreamReader(myProcess.getErrorStream(), EncodingManager.getInstance().getDefaultCharset()))) { protected void textAvailable(String s) { myErrorText.append(s); myErrorRegistry.registerError(s); myContainsError = true; } }; final Application application = ApplicationManager.getApplication(); myStdErrFuture = application.executeOnPooledThread(myErrThread); myInputStream = myProcess.getInputStream(); myOutputStream = myProcess.getOutputStream(); waitForProcess(application); } catch (Exception e) { closeInternal(); throw new AuthenticationException(e.getLocalizedMessage(), e); } }
@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 testNativeEncoding() throws Exception { EncodingManager.getInstance().setNative2AsciiForPropertiesFiles(null, true); UIUtil.dispatchAllInvocationEvents(); configureByFile(BASE_PATH + "/" + "NativeEncoding.properties"); doDoTest(true, false); }
@Override public void setUp() throws Exception { super.setUp(); initApplication(); setUpProject(); EncodingManager.getInstance(); // adds listeners myEditorListenerTracker = new EditorListenerTracker(); myThreadTracker = new ThreadTracker(); InjectedLanguageManagerImpl.pushInjectors(getProject()); }
public void initApplication() { //if (ApplicationManager.getApplication() instanceof MockApplicationEx) return; final MockApplicationEx instance = new MockApplicationEx(getTestRootDisposable()); ApplicationManager.setApplication(instance, new Getter<FileTypeRegistry>() { @Override public FileTypeRegistry get() { return FileTypeManager.getInstance(); } }, getTestRootDisposable()); getApplication().registerService(EncodingManager.class, EncodingManagerImpl.class); }
@Nullable("returns null if charset set cannot be determined from content") private static Charset cachedCharsetFromContent(final VirtualFile virtualFile) { if (virtualFile == null) return null; final Document document = FileDocumentManager.getInstance().getDocument(virtualFile); if (document == null) return null; return EncodingManager.getInstance().getCachedCharsetFromContent(document); }
@NotNull private static OSProcessHandler doExecute(@NotNull String exePath, @Nullable String workingDirectory, @Nullable VirtualFile scriptFile, String[] parameters, @Nullable Charset charset) throws ExecutionException { GeneralCommandLine commandLine = new GeneralCommandLine(exePath); if (scriptFile != null) { commandLine.addParameter(scriptFile.getPresentableUrl()); } commandLine.addParameters(parameters); if (workingDirectory != null) { commandLine.setWorkDirectory(workingDirectory); } LOG.debug("Command line: ", commandLine.getCommandLineString()); LOG.debug("Command line env: ", commandLine.getEnvironment()); if (charset == null) { charset = EncodingManager.getInstance().getDefaultCharset(); } final OSProcessHandler processHandler = new ColoredProcessHandler(commandLine.createProcess(), commandLine.getCommandLineString(), charset); if (LOG.isDebugEnabled()) { processHandler.addProcessListener(new ProcessAdapter() { @Override public void onTextAvailable(ProcessEvent event, Key outputType) { LOG.debug(outputType + ": " + event.getText()); } }); } return processHandler; }
@Nullable("null means no luck, otherwise it's tuple(guessed encoding, hint about content if was unable to guess, BOM)") public static Trinity<Charset, CharsetToolkit.GuessedEncoding, byte[]> guessFromContent(@NotNull VirtualFile virtualFile, @NotNull byte[] content, int length) { 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) { toolkit.setEnforce8Bit(true); Charset charset = toolkit.guessFromBOM(); if (charset != null) { detectedFromBytes = AUTO_DETECTED_FROM_BOM; byte[] bom = ObjectUtils.notNull(CharsetToolkit.getMandatoryBom(charset), CharsetToolkit.UTF8_BOM); return Trinity.create(charset, null, bom); } CharsetToolkit.GuessedEncoding guessed = toolkit.guessFromContent(length); if (guessed == CharsetToolkit.GuessedEncoding.VALID_UTF8) { detectedFromBytes = "auto-detected from bytes"; return Trinity.create(CharsetToolkit.UTF8_CHARSET, guessed, null); //UTF detected, ignore all directives } if (guessed == CharsetToolkit.GuessedEncoding.SEVEN_BIT) { return Trinity.create(null, guessed, null); } } return null; } finally { setCharsetWasDetectedFromBytes(virtualFile, detectedFromBytes); } }
@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(); }
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()); }
private static OutputStreamWriter createOutputStreamWriter(OutputStream processInput, ProcessHandler processHandler) { Charset charset = null; if (processHandler instanceof OSProcessHandler) { charset = ((OSProcessHandler)processHandler).getCharset(); } if (charset == null) { charset = EncodingManager.getInstance().getDefaultCharset(); } return new OutputStreamWriter(processInput, charset); }
public void testUnnecessaryUnicodeEscape() { myFixture.configureByFile(getTestName(false) + ".java"); final VirtualFile vFile = myFixture.getFile().getVirtualFile(); Charset ascii = CharsetToolkit.forName("US-ASCII"); EncodingManager.getInstance().setEncoding(vFile, ascii); UIUtil.dispatchAllInvocationEvents(); // reload in invokeLater assertEquals(ascii, vFile.getCharset()); myFixture.testHighlighting(true, false, false); }
@Override public void projectOpened() { final PropertyChangeListener myListener = new PropertyChangeListener() { @Override public void propertyChange(final PropertyChangeEvent evt) { String propertyName = evt.getPropertyName(); if (EncodingManager.PROP_NATIVE2ASCII_SWITCH.equals(propertyName) || EncodingManager.PROP_PROPERTIES_FILES_ENCODING.equals(propertyName) ) { DumbService.getInstance(myProject).smartInvokeLater(new Runnable(){ @Override public void run() { ApplicationManager.getApplication().runWriteAction(new Runnable(){ @Override public void run() { Collection<VirtualFile> filesToRefresh = FileBasedIndex.getInstance() .getContainingFiles(FileTypeIndex.NAME, PropertiesFileType.INSTANCE, GlobalSearchScope.allScope(myProject)); VirtualFile[] virtualFiles = VfsUtilCore.toVirtualFileArray(filesToRefresh); FileDocumentManager.getInstance().saveAllDocuments(); //force to re-detect encoding for (VirtualFile virtualFile : virtualFiles) { virtualFile.setCharset(null); } FileDocumentManager.getInstance().reloadFiles(virtualFiles); } }); } }); } } }; EncodingManager.getInstance().addPropertyChangeListener(myListener,myProject); }
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()); } }
private void performUndoableAction(List<String> filesChangedList) { for (String fileName : filesChangedList) try { VirtualFile virtualFile = getInstance().findFileByIoFile(new File(fileName)); if (virtualFile != null) { Document document = FileDocumentManager.getInstance().getDocument(virtualFile); getInstance().refreshAndFindFileByIoFile(new File(fileName)); if (document != null) document.setText(StringUtils.join(FileUtils.readLines(new File(fileName), EncodingManager.getInstance().getEncoding(virtualFile, true)).toArray(), "\n")); } } catch (Exception ignored) { } }
public boolean migrateCharsetSettingsTo(EncodingManager encodingProjectManager) { if (oldCharsetSettingsHaveBeenRead) { encodingProjectManager.setEncoding(null, myCharset); encodingProjectManager.setUseUTFGuessing(null, myUseUTFGuessing); } return oldCharsetSettingsHaveBeenRead; }
public boolean migrateCharsetSettingsTo(EncodingManager encodingManager) { if (myOptions.DEFAULT_PROPERTIES_FILES_CHARSET_NAME != null) { Charset charset = CharsetToolkit.forName(myOptions.DEFAULT_PROPERTIES_FILES_CHARSET_NAME); if (charset != null) { encodingManager.setDefaultCharsetForPropertiesFiles(null, charset); encodingManager.setNative2AsciiForPropertiesFiles(null, myOptions.IS_NATIVE2ASCII_FOR_PROPERTIES_FILES); } return true; } return false; }
@NotNull public static OSProcessHandler execute(@NotNull String exePath, @Nullable String workingDirectory, @Nullable VirtualFile scriptFile, String[] parameters) throws ExecutionException { GeneralCommandLine commandLine = new GeneralCommandLine(); commandLine.setExePath(exePath); commandLine.setPassParentEnvironment(true); if (scriptFile != null) { commandLine.addParameter(scriptFile.getPresentableUrl()); } commandLine.addParameters(parameters); if (workingDirectory != null) { commandLine.setWorkDirectory(workingDirectory); } LOG.debug("Command line: " + commandLine.getCommandLineString()); LOG.debug("Command line env: " + commandLine.getEnvironment()); final OSProcessHandler processHandler = new ColoredProcessHandler(commandLine.createProcess(), commandLine.getCommandLineString(), EncodingManager.getInstance().getDefaultCharset()); if (LOG.isDebugEnabled()) { processHandler.addProcessListener(new ProcessAdapter() { @Override public void onTextAvailable(ProcessEvent event, Key outputType) { LOG.debug(outputType + ": " + event.getText()); } }); } //ProcessTerminatedListener.attach(processHandler, project); return processHandler; }
@Nullable public String convert(final VirtualFilePointer pointer) { if (pointer == null) return null; final String path = pointer.getPresentableUrl(); final ByteContent byteContent = myLabel.getByteContent(FileUtil.toSystemIndependentName(path)); if (byteContent == null || byteContent.isDirectory() || byteContent.getBytes() == null) { return null; } final VirtualFile vf = pointer.getFile(); if (vf == null) { return LoadTextUtil.getTextByBinaryPresentation(byteContent.getBytes(), EncodingManager.getInstance().getDefaultCharset()).toString(); } else { return LoadTextUtil.getTextByBinaryPresentation(byteContent.getBytes(), vf).toString(); } }
private void initEncodingCombo() { final DefaultComboBoxModel encodingsModel = new DefaultComboBoxModel(CharsetToolkit.getAvailableCharsets()); encodingsModel.insertElementAt(SYSTEM_DEFAULT, 0); myEncoding.setModel(encodingsModel); final String name = EncodingManager.getInstance().getDefaultCharsetName(); if (StringUtil.isEmpty(name)) { myEncoding.setSelectedItem(SYSTEM_DEFAULT); } else { myEncoding.setSelectedItem(EncodingManager.getInstance().getDefaultCharset()); } }
@Nullable public Charset getEncoding() { final Object selectedItem = myEncoding.getSelectedItem(); if (SYSTEM_DEFAULT.equals(selectedItem)) { return EncodingManager.getInstance().getDefaultCharset(); } return (Charset)selectedItem; }
private Pair<VcsRevisionNumber, byte[]> loadContent() throws VcsException { final VcsRevisionNumber currentRevision = getCurrentRevisionNumber(); final ContentRevision contentRevision = myDiffProvider.createFileContent(currentRevision, myFile); if (contentRevision == null) { throw new VcsException("Failed to create content for current revision"); } Charset charset = myFile.getCharset(); charset = charset == null ? EncodingManager.getInstance().getDefaultCharset() : charset; return new Pair<VcsRevisionNumber, byte[]>(currentRevision, contentRevision.getContent().getBytes(charset)); }
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()); }