IG: don't break code when invoking Fix all problems in file (IDEA-169798)

This commit is contained in:
Bas Leijdekkers
2017-03-17 17:25:23 +01:00
parent 54f5a31627
commit 40e63d48a0
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
* Copyright 2000-2017 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.
@@ -18,6 +18,7 @@ package com.siyeh.ig.internationalization;
import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.lang.injection.InjectedLanguageManager;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.RangeMarker;
import com.intellij.openapi.fileEditor.impl.LoadTextUtil;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.TextRange;
@@ -59,15 +60,17 @@ public class UnnecessaryUnicodeEscapeInspection extends BaseInspection {
@Nullable
@Override
protected InspectionGadgetsFix buildFix(Object... infos) {
return new UnnecessaryUnicodeEscapeFix(((Character) infos[0]).charValue());
return new UnnecessaryUnicodeEscapeFix(((Character) infos[0]).charValue(), (RangeMarker)infos[1]);
}
private static class UnnecessaryUnicodeEscapeFix extends InspectionGadgetsFix {
private final char c;
private final RangeMarker myRangeMarker;
public UnnecessaryUnicodeEscapeFix(char c) {
public UnnecessaryUnicodeEscapeFix(char c, RangeMarker rangeMarker) {
this.c = c;
myRangeMarker = rangeMarker;
}
@NotNull
@@ -84,17 +87,7 @@ public class UnnecessaryUnicodeEscapeInspection extends BaseInspection {
@Override
protected void doFix(Project project, ProblemDescriptor descriptor) {
final TextRange textRange = descriptor.getTextRangeInElement();
final PsiElement element = descriptor.getPsiElement();
if (!(element instanceof PsiFile)) {
return;
}
final PsiFile file = (PsiFile)element;
final Document document = PsiDocumentManager.getInstance(project).getDocument(file);
if (document == null) {
return;
}
document.replaceString(textRange.getStartOffset(), textRange.getEndOffset(), String.valueOf(c));
myRangeMarker.getDocument().replaceString(myRangeMarker.getStartOffset(), myRangeMarker.getEndOffset(), String.valueOf(c));
}
}
@@ -111,6 +104,10 @@ public class UnnecessaryUnicodeEscapeInspection extends BaseInspection {
if (InjectedLanguageManager.getInstance(file.getProject()).isInjectedFragment(file) || !file.isPhysical()) {
return;
}
final Document document = PsiDocumentManager.getInstance(file.getProject()).getDocument(file);
if (document == null) {
return;
}
final VirtualFile virtualFile = file.getVirtualFile();
final String text = file.getText();
final Charset charset = LoadTextUtil.extractCharsetFromFileContent(file.getProject(), virtualFile, text);
@@ -139,7 +136,7 @@ public class UnnecessaryUnicodeEscapeInspection extends BaseInspection {
break;
}
}
while (text.charAt(nextChar) == 'u'); // \uuuu0061 is a legal unicode escape
while (text.charAt(nextChar) == 'u'); // \\uuuu0061 is a legal unicode escape
if (nextChar == i + 1 || nextChar + 3 >= length) {
continue;
}
@@ -163,7 +160,8 @@ public class UnnecessaryUnicodeEscapeInspection extends BaseInspection {
if (element != null && isSuppressedFor(element)) {
return;
}
registerErrorAtOffset(file, i, escapeEnd - i, Character.valueOf(d));
final RangeMarker rangeMarker = document.createRangeMarker(new TextRange(i, escapeEnd));
registerErrorAtOffset(file, i, escapeEnd - i, Character.valueOf(d), rangeMarker);
}
}
}