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
.ide
.DataManager
;
23 import com
.intellij
.openapi
.actionSystem
.Anchor
;
24 import com
.intellij
.openapi
.actionSystem
.PlatformDataKeys
;
25 import com
.intellij
.openapi
.editor
.Editor
;
26 import com
.intellij
.openapi
.project
.Project
;
27 import com
.intellij
.openapi
.util
.TextRange
;
28 import com
.intellij
.openapi
.util
.text
.StringUtil
;
29 import com
.intellij
.spellchecker
.SpellCheckerManager
;
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
implements SpellCheckerQuickFix
{
38 private TextRange textRange
;
40 private Project project
;
42 public ChangeTo(@NotNull TextRange textRange
, @NotNull String word
, @NotNull Project project
) {
43 this.textRange
= textRange
;
45 this.project
= project
;
49 public String
getName() {
50 return SpellCheckerBundle
.message("change.to");
54 public String
getFamilyName() {
55 return SpellCheckerBundle
.message("change.to");
59 public Anchor
getPopupActionAnchor() {
64 public void applyFix(@NotNull Project project
, @NotNull ProblemDescriptor descriptor
) {
66 final Editor editor
= PlatformDataKeys
.EDITOR
.getData(DataManager
.getInstance().getDataContext());
71 int psiElementOffset
= descriptor
.getPsiElement().getTextRange().getStartOffset();
72 editor
.offsetToLogicalPosition(psiElementOffset
+ textRange
.getStartOffset());
73 editor
.getSelectionModel().setSelection(psiElementOffset
+ textRange
.getStartOffset(), psiElementOffset
+ textRange
.getEndOffset());
75 String word
= editor
.getSelectionModel().getSelectedText();
77 if (word
== null || StringUtil
.isEmpty(word
)) {
81 SpellCheckerManager manager
= SpellCheckerManager
.getInstance(project
);
82 List
<String
> variants
= manager
.getSuggestions(word
);
84 List
<LookupElement
> lookupItems
= new ArrayList
<LookupElement
>();
85 for (String variant
: variants
) {
86 lookupItems
.add(LookupElementBuilder
.create(variant
));
88 LookupElement
[] items
= new LookupElement
[lookupItems
.size()];
89 items
= lookupItems
.toArray(items
);
90 LookupManager lookupManager
= LookupManager
.getInstance(project
);
91 lookupManager
.showLookup(editor
, items
);