WI-36470 Spellchecking: Quick fix "Change to..." works incorrectly for injected language

This commit is contained in:
Olga Strizhenko
2017-08-21 13:57:23 +03:00
parent e5ee7ed081
commit 9d7509d835
9 changed files with 57 additions and 30 deletions

View File

@@ -26,7 +26,6 @@ import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.PsiElement;
import com.intellij.psi.util.PsiUtilBase;
import com.intellij.spellchecker.util.SpellCheckerBundle;
import org.jetbrains.annotations.NotNull;
@@ -60,11 +59,8 @@ public class ChangeTo extends ShowSuggestions implements SpellCheckerQuickFix {
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
PsiElement element = descriptor.getPsiElement();
if (element == null) return;
Editor editor = PsiUtilBase.findEditor(element);
if (editor == null) {
return;
}
final Editor editor = getEditor(element, project);
if (editor == null) return;
TextRange textRange = ((ProblemDescriptorBase)descriptor).getTextRange();
final int documentLength = editor.getDocument().getTextLength();
@@ -85,8 +81,5 @@ public class ChangeTo extends ShowSuggestions implements SpellCheckerQuickFix {
items = lookupItems.toArray(items);
LookupManager lookupManager = LookupManager.getInstance(project);
lookupManager.showLookup(editor, items);
}
}

View File

@@ -22,13 +22,10 @@ import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.actionSystem.impl.SimpleDataContext;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.impl.EditorComponentImpl;
import com.intellij.openapi.extensions.Extensions;
import com.intellij.openapi.fileEditor.impl.text.TextEditorPsiDataProvider;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil;
import com.intellij.refactoring.actions.RenameElementAction;
import com.intellij.refactoring.rename.NameSuggestionProvider;
import com.intellij.refactoring.rename.RenameHandlerRegistry;
@@ -37,9 +34,6 @@ import com.intellij.util.containers.HashMap;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.awt.*;
public class RenameTo extends ShowSuggestions implements SpellCheckerQuickFix {
public static final String FIX_NAME = "RenameTo";
@@ -83,15 +77,10 @@ public class RenameTo extends ShowSuggestions implements SpellCheckerQuickFix {
provider.setActive(true);
}
Editor editor = getEditorFromFocus();
HashMap<String, Object> map = new HashMap<>();
PsiElement psiElement = descriptor.getPsiElement();
if (psiElement == null) return;
PsiFile containingFile = psiElement.getContainingFile();
if (editor == null) {
editor = InjectedLanguageUtil.openEditorFor(containingFile, project);
}
final Editor editor = getEditor(psiElement, project);
if (editor == null) return;
if (editor instanceof EditorWindow) {
@@ -124,13 +113,4 @@ public class RenameTo extends ShowSuggestions implements SpellCheckerQuickFix {
public boolean startInWriteAction() {
return false;
}
@Nullable
private static Editor getEditorFromFocus() {
final Component c = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
if (c instanceof EditorComponentImpl) {
return ((EditorComponentImpl)c).getEditor();
}
return null;
}
}

View File

@@ -16,13 +16,19 @@
package com.intellij.spellchecker.quickfixes;
import com.intellij.codeInspection.LocalQuickFix;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.impl.EditorComponentImpl;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Iconable;
import com.intellij.psi.PsiElement;
import com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil;
import com.intellij.spellchecker.SpellCheckerManager;
import icons.SpellcheckerIcons;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.awt.*;
import java.util.List;
@@ -49,4 +55,22 @@ public abstract class ShowSuggestions implements LocalQuickFix, Iconable {
public Icon getIcon(int flags) {
return SpellcheckerIcons.Spellcheck;
}
@Nullable
protected Editor getEditor(PsiElement element, @NotNull Project project) {
Editor editor = getEditorFromFocus();
if (editor == null) {
editor = InjectedLanguageUtil.openEditorFor(element.getContainingFile(), project);
}
return editor;
}
@Nullable
private static Editor getEditorFromFocus() {
final Component c = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
if (c instanceof EditorComponentImpl) {
return ((EditorComponentImpl)c).getEditor();
}
return null;
}
}

View File

@@ -0,0 +1,6 @@
import org.intellij.lang.annotations.Language;
class InjectionChangeTo {
@Language("HTML")
String myString = "<html>typo</html>";
}

View File

@@ -0,0 +1,6 @@
import org.intellij.lang.annotations.Language;
class InjectionChangeTo {
@Language("HTML")
String myString = "<html>typppo<caret></html>";
}

View File

@@ -0,0 +1,5 @@
<note>
<script xmlns="http://www.w3.org/1999/xhtml"><![CDATA[
alert('typo');
]]></script>
</note>

View File

@@ -0,0 +1,5 @@
<note>
<script xmlns="http://www.w3.org/1999/xhtml"><![CDATA[
alert('typppo<caret>');
]]></script>
</note>

View File

@@ -23,4 +23,8 @@ public class JavaSpellCheckerFixesTest extends AbstractSpellCheckerFixesTest {
public void testSimpleWordChangeTo() {
doChangeToTest();
}
public void testInjectionChangeTo() {
doChangeToTest();
}
}

View File

@@ -23,4 +23,8 @@ public class XmlSpellCheckerFixesTest extends AbstractSpellCheckerFixesTest {
public void testSimpleWordChangeTo() {
doChangeToTest();
}
public void testInjectionChangeTo() {
doChangeToTest();
}
}