RUBY-5332 Enable "Lossy encoding" inspection in platform

This commit is contained in:
Dennis Ushakov
2011-03-28 11:13:38 +04:00
parent e391907161
commit 12b0211ef6
5 changed files with 19 additions and 157 deletions
@@ -1,142 +0,0 @@
/*
* Copyright 2000-2009 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.
*/
/*
* Created by IntelliJ IDEA.
* User: cdr
* Date: Aug 6, 2007
* Time: 3:09:55 PM
*/
package com.intellij.codeInspection;
import com.intellij.codeInsight.daemon.GroupNames;
import com.intellij.ide.DataManager;
import com.intellij.lang.properties.charset.Native2AsciiCharset;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.actionSystem.DefaultActionGroup;
import com.intellij.openapi.fileEditor.impl.LoadTextUtil;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.encoding.ChooseFileEncodingAction;
import com.intellij.openapi.vfs.encoding.EncodingManager;
import com.intellij.psi.PsiFile;
import com.intellij.util.ArrayUtil;
import com.intellij.util.SmartList;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.util.List;
public class LossyEncodingInspection extends BaseJavaLocalInspectionTool {
private static final LocalQuickFix CHANGE_ENCODING_FIX = new LocalQuickFix() {
@NotNull
@Override
public String getName() {
return "Change file encoding";
}
@NotNull
@Override
public String getFamilyName() {
return getName();
}
@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);
}
};
@Nls
@NotNull
public String getGroupDisplayName() {
return GroupNames.INTERNATIONALIZATION_GROUP_NAME;
}
@Nls
@NotNull
public String getDisplayName() {
return InspectionsBundle.message("lossy.encoding");
}
@NonNls
@NotNull
public String getShortName() {
return "LossyEncoding";
}
@Nullable
public ProblemDescriptor[] checkFile(@NotNull PsiFile file, @NotNull InspectionManager manager, boolean isOnTheFly) {
if (ArrayUtil.find(file.getPsiRoots(), file) != 0) return null;
VirtualFile virtualFile = file.getVirtualFile();
if (virtualFile == null) 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;
int errorCount = 0;
int start = -1;
List<ProblemDescriptor> descriptors = new SmartList<ProblemDescriptor>();
for (int i = 0; i <= text.length(); i++) {
char c = i == text.length() ? 0 : text.charAt(i);
if (i == text.length() || isRepresentable(c, charset)) {
if (start != -1) {
TextRange range = new TextRange(start, i);
String message = InspectionsBundle.message("unsupported.character.for.the.charset", charset);
ProblemDescriptor descriptor = manager.createProblemDescriptor(file, range, message, ProblemHighlightType.GENERIC_ERROR_OR_WARNING, isOnTheFly,
CHANGE_ENCODING_FIX);
descriptors.add(descriptor);
start = -1;
//do not report too many errors
if (errorCount++ > 200) break;
}
}
else if (start == -1) {
start = i;
}
}
return descriptors.toArray(new ProblemDescriptor[descriptors.size()]);
}
private static boolean isRepresentable(final char c, final Charset charset) {
String str = Character.toString(c);
ByteBuffer out = charset.encode(str);
CharBuffer buffer = charset.decode(out);
return str.equals(buffer.toString());
}
}
@@ -108,7 +108,6 @@ public class StandardInspectionToolsProvider implements InspectionToolProvider {
UncheckedWarningLocalInspection.class,
SuspiciousNameCombinationInspection.class,
DuplicateThrowsInspection.class,
LossyEncodingInspection.class,
FieldAccessNotGuardedInspection.class,
InstanceGuardedByStaticInspection.class,