Spellchecker to avoid duplicating info in fixes to save memory
[fedora-idea.git] / plugins / spellchecker / src / com / intellij / spellchecker / quickfixes / ChangeTo.java
blob40d1c55dccffbbbae31cf03c06ee2abab4c5dfe8
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.spellchecker.quickfixes;
18 import com.intellij.codeInsight.lookup.LookupElement;
19 import com.intellij.codeInsight.lookup.LookupElementBuilder;
20 import com.intellij.codeInsight.lookup.LookupManager;
21 import com.intellij.codeInspection.ProblemDescriptor;
22 import com.intellij.codeInspection.ex.ProblemDescriptorImpl;
23 import com.intellij.ide.DataManager;
24 import com.intellij.openapi.actionSystem.Anchor;
25 import com.intellij.openapi.actionSystem.PlatformDataKeys;
26 import com.intellij.openapi.editor.Editor;
27 import com.intellij.openapi.project.Project;
28 import com.intellij.openapi.util.TextRange;
29 import com.intellij.openapi.util.text.StringUtil;
30 import com.intellij.spellchecker.util.SpellCheckerBundle;
31 import org.jetbrains.annotations.NotNull;
33 import java.util.ArrayList;
34 import java.util.List;
36 public class ChangeTo extends ShowSuggestions implements SpellCheckerQuickFix {
38 public ChangeTo() {
39 super();
43 @NotNull
44 public String getName() {
45 return SpellCheckerBundle.message("change.to");
48 @NotNull
49 public String getFamilyName() {
50 return SpellCheckerBundle.message("change.to");
53 @NotNull
54 public Anchor getPopupActionAnchor() {
55 return Anchor.FIRST;
59 public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
61 final Editor editor = PlatformDataKeys.EDITOR.getData(DataManager.getInstance().getDataContext());
62 if (editor == null) {
63 return;
66 int psiElementOffset = descriptor.getPsiElement().getTextRange().getStartOffset();
67 TextRange textRange = ((ProblemDescriptorImpl)descriptor).getTextRange();
68 editor.offsetToLogicalPosition(psiElementOffset + textRange.getStartOffset());
69 editor.getSelectionModel().setSelection(psiElementOffset + textRange.getStartOffset(), psiElementOffset + textRange.getEndOffset());
71 String word = editor.getSelectionModel().getSelectedText();
73 if (word == null || StringUtil.isEmpty(word)) {
74 return;
77 List<LookupElement> lookupItems = new ArrayList<LookupElement>();
78 for (String variant : getSuggestions()) {
79 lookupItems.add(LookupElementBuilder.create(variant));
81 LookupElement[] items = new LookupElement[lookupItems.size()];
82 items = lookupItems.toArray(items);
83 LookupManager lookupManager = LookupManager.getInstance(project);
84 lookupManager.showLookup(editor, items);