@Override public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { PsiFile psiFile = descriptor.getPsiElement().getContainingFile(); VirtualFile virtualFile = psiFile.getVirtualFile(); ChooseFileEncodingAction action = new ChooseFileEncodingAction(virtualFile) { @Override protected void chosen(VirtualFile virtualFile, Charset charset) { if (virtualFile != null) { EncodingManager.getInstance().setEncoding(virtualFile, charset); } } }; DefaultActionGroup group = action.createGroup(false); DataContext dataContext = DataManager.getInstance().getDataContext(); JBPopupFactory.getInstance() .createActionGroupPopup(null, group, dataContext, false, false, false, null, 30, null) .showInBestPositionFor(dataContext); }
private static void checkFileLoadedInWrongEncoding( PsiFile file, InspectionManager manager, boolean isOnTheFly, VirtualFile virtualFile, Charset charset, List<ProblemDescriptor> descriptors) { if (FileDocumentManager.getInstance() .isFileModified(virtualFile) // when file is modified, it's too late to reload it || ChooseFileEncodingAction.isEnabledAndWhyNot(virtualFile) != null // can't reload in another encoding, no point trying ) { return; } // check if file was loaded in correct encoding byte[] bytes; try { bytes = virtualFile.contentsToByteArray(); } catch (IOException e) { return; } String separator = FileDocumentManager.getInstance().getLineSeparator(virtualFile, file.getProject()); String toSave = StringUtil.convertLineSeparators(file.getText(), separator); byte[] bom = virtualFile.getBOM(); byte[] bytesToSave = ArrayUtil.mergeArrays( bom == null ? ArrayUtil.EMPTY_BYTE_ARRAY : bom, toSave.getBytes(charset)); if (!Arrays.equals(bytesToSave, bytes)) { descriptors.add( manager.createProblemDescriptor( file, "File was loaded in a wrong encoding: '" + charset + "'", RELOAD_ENCODING_FIX, ProblemHighlightType.GENERIC_ERROR, isOnTheFly)); } }