From 40e63d48a017e2508e5c7c8441676b1ea9a65133 Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Fri, 17 Mar 2017 15:23:30 +0100 Subject: [PATCH] IG: don't break code when invoking Fix all problems in file (IDEA-169798) --- .../UnnecessaryUnicodeEscapeInspection.java | 30 +++++++++---------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/internationalization/UnnecessaryUnicodeEscapeInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/internationalization/UnnecessaryUnicodeEscapeInspection.java index 31f72ad71aa2..5d11c31823da 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/internationalization/UnnecessaryUnicodeEscapeInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/internationalization/UnnecessaryUnicodeEscapeInspection.java @@ -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); } } }