@Override @Nullable public ProblemDescriptor[] checkFile(@NotNull PsiFile file, @NotNull InspectionManager manager, boolean isOnTheFly) { if (InjectedLanguageManager.getInstance(file.getProject()).isInjectedFragment(file)) return null; if (!file.isPhysical()) return null; FileViewProvider viewProvider = file.getViewProvider(); if (viewProvider.getBaseLanguage() != file.getLanguage()) return null; VirtualFile virtualFile = file.getVirtualFile(); if (virtualFile == null) return null; if (!virtualFile.isInLocalFileSystem()) return null; CharSequence text = viewProvider.getContents(); Charset charset = LoadTextUtil.extractCharsetFromFileContent(file.getProject(), virtualFile, text); // no sense in checking transparently decoded file: all characters there are already safely encoded if (charset instanceof Native2AsciiCharset) return null; List<ProblemDescriptor> descriptors = new SmartList<ProblemDescriptor>(); boolean ok = checkFileLoadedInWrongEncoding(file, manager, isOnTheFly, virtualFile, charset, descriptors); if (ok) { checkIfCharactersWillBeLostAfterSave(file, manager, isOnTheFly, text, charset, descriptors); } return descriptors.toArray(new ProblemDescriptor[descriptors.size()]); }
@Override @Nullable public ProblemDescriptor[] checkFile(@NotNull PsiFile file, @NotNull InspectionManager manager, boolean isOnTheFly) { if (InjectedLanguageManager.getInstance(file.getProject()).isInjectedFragment(file)) return null; if (!file.isPhysical()) return null; if (file.getViewProvider().getBaseLanguage() != file.getLanguage()) return null; VirtualFile virtualFile = file.getVirtualFile(); if (virtualFile == null) return null; if (virtualFile.getFileSystem() != LocalFileSystem.getInstance() // tests && virtualFile.getFileSystem() != TempFileSystem.getInstance()) return null; String text = file.getText(); Charset charset = LoadTextUtil.extractCharsetFromFileContent(file.getProject(), virtualFile, text); // no sense in checking transparently decoded file: all characters there are already safely encoded if (charset instanceof Native2AsciiCharset) return null; List<ProblemDescriptor> descriptors = new SmartList<ProblemDescriptor>(); boolean ok = checkFileLoadedInWrongEncoding(file, manager, isOnTheFly, text, virtualFile, charset, descriptors); if (ok) { checkIfCharactersWillBeLostAfterSave(file, manager, isOnTheFly, text, charset, descriptors); } return descriptors.toArray(new ProblemDescriptor[descriptors.size()]); }
@Override @Nullable public ProblemDescriptor[] checkFile(@Nonnull PsiFile file, @Nonnull InspectionManager manager, boolean isOnTheFly) { if (InjectedLanguageManager.getInstance(file.getProject()).isInjectedFragment(file)) return null; if (!file.isPhysical()) return null; FileViewProvider viewProvider = file.getViewProvider(); if (viewProvider.getBaseLanguage() != file.getLanguage()) return null; VirtualFile virtualFile = file.getVirtualFile(); if (virtualFile == null) return null; if (!virtualFile.isInLocalFileSystem()) return null; CharSequence text = viewProvider.getContents(); Charset charset = LoadTextUtil.extractCharsetFromFileContent(file.getProject(), virtualFile, text); // no sense in checking transparently decoded file: all characters there are already safely encoded if (charset instanceof Native2AsciiCharset) return null; List<ProblemDescriptor> descriptors = new SmartList<ProblemDescriptor>(); boolean ok = checkFileLoadedInWrongEncoding(file, manager, isOnTheFly, virtualFile, charset, descriptors); if (ok) { checkIfCharactersWillBeLostAfterSave(file, manager, isOnTheFly, text, charset, descriptors); } return descriptors.toArray(new ProblemDescriptor[descriptors.size()]); }
@NotNull public static Charset detectCharset(@NotNull VirtualFile virtualFile, @NotNull byte[] content, @NotNull FileType fileType) { Charset charset = null; Trinity<Charset,CharsetToolkit.GuessedEncoding, byte[]> guessed = guessFromContent(virtualFile, content, content.length); if (guessed != null && guessed.first != null) { charset = guessed.first; } else { String charsetName = fileType.getCharset(virtualFile, content); if (charsetName == null) { Charset specifiedExplicitly = EncodingRegistry.getInstance().getEncoding(virtualFile, true); if (specifiedExplicitly != null) { charset = specifiedExplicitly; } } else { charset = CharsetToolkit.forName(charsetName); } } if (charset == null) { charset = EncodingRegistry.getInstance().getDefaultCharset(); } if (fileType.getName().equals("Properties") && EncodingRegistry.getInstance().isNative2Ascii(virtualFile)) { charset = Native2AsciiCharset.wrap(charset); } virtualFile.setCharset(charset); return charset; }
private static Charset detectCharset(@NotNull VirtualFile virtualFile, @NotNull byte[] content) { Charset charset = null; Trinity<Charset,CharsetToolkit.GuessedEncoding, byte[]> guessed = guessFromContent(virtualFile, content, content.length); if (guessed != null && guessed.first != null) { charset = guessed.first; } else { FileType fileType = virtualFile.getFileType(); String charsetName = fileType.getCharset(virtualFile, content); if (charsetName == null) { Charset specifiedExplicitly = EncodingRegistry.getInstance().getEncoding(virtualFile, true); if (specifiedExplicitly != null) { charset = specifiedExplicitly; } } else { charset = CharsetToolkit.forName(charsetName); } } charset = charset == null ? EncodingRegistry.getInstance().getDefaultCharset() : charset; if (EncodingRegistry.getInstance().isNative2Ascii(virtualFile)) { charset = Native2AsciiCharset.wrap(charset); } virtualFile.setCharset(charset); return charset; }