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
.actions
;
19 import com
.intellij
.codeInsight
.lookup
.LookupElement
;
20 import com
.intellij
.codeInsight
.lookup
.LookupElementBuilder
;
21 import com
.intellij
.codeInsight
.lookup
.LookupManager
;
22 import com
.intellij
.openapi
.actionSystem
.*;
23 import com
.intellij
.openapi
.editor
.Editor
;
24 import com
.intellij
.openapi
.project
.Project
;
25 import com
.intellij
.psi
.PsiFile
;
26 import com
.intellij
.psi
.codeStyle
.NameUtil
;
27 import com
.intellij
.spellchecker
.SpellCheckerManager
;
29 import java
.util
.ArrayList
;
30 import java
.util
.List
;
33 * Completion action for spell checker.
35 public final class CompleteWordFromDictionaryAction
extends AnAction
{
36 public void actionPerformed(AnActionEvent e
) {
39 Project project
= e
.getData(PlatformDataKeys
.PROJECT
);
40 Editor editor
= e
.getData(PlatformDataKeys
.EDITOR
);
41 PsiFile psiFile
= e
.getData(LangDataKeys
.PSI_FILE
);
43 if (project
!= null && editor
!= null && psiFile
!= null) {
44 // Get position before caret
45 int caretOffset
= editor
.getCaretModel().getOffset();
47 int documentOffset
= caretOffset
- 1;
49 if (documentOffset
> 0) {
50 StringBuilder prefixBuilder
= new StringBuilder();
51 CharSequence charSequence
= editor
.getDocument().getCharsSequence();
52 for (char c
; documentOffset
>= 0 && Character
.isJavaIdentifierPart(c
= charSequence
.charAt(documentOffset
)); documentOffset
--) {
53 prefixBuilder
.append(c
);
55 documentOffset
= caretOffset
;
56 StringBuilder suffixBuilder
= new StringBuilder();
58 documentOffset
< charSequence
.length() && Character
.isJavaIdentifierPart(c
= charSequence
.charAt(documentOffset
));
60 suffixBuilder
.append(c
);
63 if (prefixBuilder
.length() > 0) {
64 String
[] prefixes
= NameUtil
.nameToWords(prefixBuilder
.reverse().toString());
65 String prefix
= prefixes
.length
> 0 ? prefixes
[prefixes
.length
- 1] : "";
66 if (prefix
.length() > 0) {
67 String
[] suffixes
= NameUtil
.nameToWords(suffixBuilder
.toString());
68 String suffix
= suffixes
.length
> 0 ? suffixes
[0] : "";
69 if (suffix
.length() > 0 && Character
.isLowerCase(suffix
.charAt(0))) {
70 // Select replacement part
71 editor
.getSelectionModel().setSelection(caretOffset
, caretOffset
+ suffix
.length());
73 List
<String
> variants
= SpellCheckerManager
.getInstance(project
).getVariants(prefix
);
75 List
<LookupElement
> lookupItems
= new ArrayList
<LookupElement
>();
76 for (String variant
: variants
) {
77 lookupItems
.add(LookupElementBuilder
.create(variant
));
80 LookupElement
[] items
= lookupItems
.toArray(new LookupElement
[lookupItems
.size()]);
81 LookupManager
.getInstance(project
).showLookup(editor
, items
, prefix
);
88 public void update(AnActionEvent e
) {
90 Project project
= e
.getData(LangDataKeys
.PROJECT
);
91 Editor editor
= e
.getData(LangDataKeys
.EDITOR
);
92 PsiFile psiFile
= e
.getData(LangDataKeys
.PSI_FILE
);
93 boolean available
= project
!= null && editor
!= null && psiFile
!= null;
94 Presentation presentation
= e
.getPresentation();
95 if (presentation
.isVisible()) {
96 presentation
.setVisible(available
);
98 if (presentation
.isEnabled()) {
99 presentation
.setEnabled(available
);
103 /* private static final class LookupItemPreferencePolicyImpl implements LookupItemPreferencePolicy {
104 public void setPrefix(String prefix) {
107 public void itemSelected(LookupItem lookupItem) {
110 public int compare(LookupItem o1, LookupItem o2) {
111 return o1.getLookupString().compareTo(o2.getLookupString());