]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2000, 2011 IBM Corporation and others. | |
3 | * All rights reserved. This program and the accompanying materials | |
4 | * are made available under the terms of the Eclipse Public License v1.0 | |
5 | * which accompanies this distribution, and is available at | |
6 | * http://www.eclipse.org/legal/epl-v10.html | |
7 | * | |
8 | * Contributors: | |
9 | * IBM Corporation - initial API and implementation | |
10 | *******************************************************************************/ | |
11 | package org.eclipse.jdt.internal.ui.text.spelling; | |
12 | ||
13 | import java.util.ArrayList; | |
14 | import java.util.Collections; | |
15 | import java.util.List; | |
16 | ||
17 | import org.eclipse.core.runtime.Assert; | |
18 | ||
19 | import org.eclipse.jface.text.BadLocationException; | |
20 | import org.eclipse.jface.text.IDocument; | |
21 | import org.eclipse.jface.text.IRegion; | |
22 | import org.eclipse.jface.text.contentassist.ICompletionProposal; | |
23 | import org.eclipse.jface.text.quickassist.IQuickAssistInvocationContext; | |
24 | import org.eclipse.jface.text.source.TextInvocationContext; | |
25 | ||
26 | import org.eclipse.ui.texteditor.spelling.SpellingProblem; | |
27 | ||
28 | import org.eclipse.jdt.internal.corext.util.Messages; | |
29 | ||
30 | import org.eclipse.jdt.ui.PreferenceConstants; | |
31 | import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal; | |
32 | ||
33 | import org.eclipse.jdt.internal.ui.JavaUIMessages; | |
34 | import org.eclipse.jdt.internal.ui.text.javadoc.IHtmlTagConstants; | |
35 | import org.eclipse.jdt.internal.ui.text.spelling.engine.ISpellCheckEngine; | |
36 | import org.eclipse.jdt.internal.ui.text.spelling.engine.ISpellChecker; | |
37 | import org.eclipse.jdt.internal.ui.text.spelling.engine.ISpellEvent; | |
38 | import org.eclipse.jdt.internal.ui.text.spelling.engine.RankedWordProposal; | |
39 | ||
40 | /** | |
41 | * A {@link SpellingProblem} that adapts a {@link ISpellEvent}. | |
42 | * <p> | |
43 | * TODO: remove {@link ISpellEvent} notification mechanism | |
44 | * </p> | |
45 | */ | |
46 | public class JavaSpellingProblem extends SpellingProblem { | |
47 | ||
48 | /** Spell event */ | |
49 | private ISpellEvent fSpellEvent; | |
50 | ||
51 | /** | |
52 | * The associated document. | |
53 | * | |
54 | * @since 3.3 | |
55 | */ | |
56 | private IDocument fDocument; | |
57 | ||
58 | /** | |
59 | * Initialize with the given spell event. | |
60 | * | |
61 | * @param spellEvent the spell event | |
62 | * @param document the document | |
63 | */ | |
64 | public JavaSpellingProblem(ISpellEvent spellEvent, IDocument document) { | |
65 | Assert.isLegal(document != null); | |
66 | Assert.isLegal(spellEvent != null); | |
67 | fSpellEvent= spellEvent; | |
68 | fDocument= document; | |
69 | } | |
70 | ||
71 | /* | |
72 | * @see org.eclipse.ui.texteditor.spelling.SpellingProblem#getOffset() | |
73 | */ | |
74 | @Override | |
75 | public int getOffset() { | |
76 | return fSpellEvent.getBegin(); | |
77 | } | |
78 | ||
79 | /* | |
80 | * @see org.eclipse.ui.texteditor.spelling.SpellingProblem#getLength() | |
81 | */ | |
82 | @Override | |
83 | public int getLength() { | |
84 | return fSpellEvent.getEnd() - fSpellEvent.getBegin() + 1; | |
85 | } | |
86 | ||
87 | /* | |
88 | * @see org.eclipse.ui.texteditor.spelling.SpellingProblem#getMessage() | |
89 | */ | |
90 | @Override | |
91 | public String getMessage() { | |
92 | if (isSentenceStart() && isDictionaryMatch()) | |
93 | return Messages.format(JavaUIMessages.Spelling_error_case_label, new String[] { fSpellEvent.getWord() }); | |
94 | ||
95 | return Messages.format(JavaUIMessages.Spelling_error_label, new String[] { fSpellEvent.getWord() }); | |
96 | } | |
97 | ||
98 | /* | |
99 | * @see org.eclipse.ui.texteditor.spelling.SpellingProblem#getProposals() | |
100 | */ | |
101 | @Override | |
102 | public ICompletionProposal[] getProposals() { | |
103 | return getProposals(null); | |
104 | } | |
105 | ||
106 | /* | |
107 | * @see org.eclipse.ui.texteditor.spelling.SpellingProblem#getProposals(org.eclipse.jface.text.quickassist.IQuickAssistInvocationContext) | |
108 | * @since 3.4 | |
109 | */ | |
110 | @Override | |
111 | public ICompletionProposal[] getProposals(IQuickAssistInvocationContext context) { | |
112 | String[] arguments= getArguments(); | |
113 | if (arguments == null) | |
114 | return new ICompletionProposal[0]; | |
115 | ||
116 | if (arguments[0].indexOf('&') != -1 && isIgnoringAmpersand()) | |
117 | return new ICompletionProposal[0]; // no proposals for now | |
118 | ||
119 | final int threshold= PreferenceConstants.getPreferenceStore().getInt(PreferenceConstants.SPELLING_PROPOSAL_THRESHOLD); | |
120 | int size= 0; | |
121 | List<RankedWordProposal> proposals= null; | |
122 | ||
123 | RankedWordProposal proposal= null; | |
124 | IJavaCompletionProposal[] result= null; | |
125 | int index= 0; | |
126 | ||
127 | boolean fixed= false; | |
128 | boolean match= false; | |
129 | boolean sentence= false; | |
130 | ||
131 | final ISpellCheckEngine engine= SpellCheckEngine.getInstance(); | |
132 | final ISpellChecker checker= engine.getSpellChecker(); | |
133 | ||
134 | if (checker != null) { | |
135 | ||
136 | if (context == null) | |
137 | context= new TextInvocationContext(null, getOffset(), getLength()); | |
138 | else | |
139 | context= new TextInvocationContext(context.getSourceViewer(), getOffset(), getLength()); | |
140 | ||
141 | // FIXME: this is a pretty ugly hack | |
142 | fixed= arguments[0].charAt(0) == IHtmlTagConstants.HTML_TAG_PREFIX | |
143 | || arguments[0].charAt(0) == IJavaDocTagConstants.JAVADOC_TAG_PREFIX; | |
144 | ||
145 | if ((sentence && match) && !fixed) | |
146 | result= new IJavaCompletionProposal[] { new ChangeCaseProposal( | |
147 | arguments, getOffset(), getLength(), context, engine | |
148 | .getLocale()) }; | |
149 | else { | |
150 | ||
151 | proposals= new ArrayList<RankedWordProposal>(checker.getProposals(arguments[0], | |
152 | sentence)); | |
153 | size= proposals.size(); | |
154 | ||
155 | if (threshold > 0 && size > threshold) { | |
156 | ||
157 | Collections.sort(proposals); | |
158 | proposals= proposals | |
159 | .subList(size - threshold - 1, size - 1); | |
160 | size= proposals.size(); | |
161 | } | |
162 | ||
163 | boolean extendable= !fixed ? (checker.acceptsWords() || AddWordProposal.canAskToConfigure()) : false; | |
164 | result= new IJavaCompletionProposal[size + (extendable ? 3 : 2)]; | |
165 | ||
166 | for (index= 0; index < size; index++) { | |
167 | ||
168 | proposal= proposals.get(index); | |
169 | result[index]= new WordCorrectionProposal(proposal | |
170 | .getText(), arguments, getOffset(), getLength(), | |
171 | context, proposal.getRank()); | |
172 | } | |
173 | ||
174 | if (extendable) | |
175 | result[index++]= new AddWordProposal(arguments[0], context); | |
176 | ||
177 | result[index++]= new WordIgnoreProposal(arguments[0], context); | |
178 | result[index++]= new DisableSpellCheckingProposal(context); | |
179 | } | |
180 | } | |
181 | ||
182 | return result; | |
183 | } | |
184 | ||
185 | private boolean isIgnoringAmpersand() { | |
186 | return PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.SPELLING_IGNORE_AMPERSAND_IN_PROPERTIES); | |
187 | } | |
188 | ||
189 | public String[] getArguments() { | |
190 | ||
191 | String prefix= ""; //$NON-NLS-1$ | |
192 | String postfix= ""; //$NON-NLS-1$ | |
193 | String word; | |
194 | try { | |
195 | word= fDocument.get(getOffset(), getLength()); | |
196 | } catch (BadLocationException e) { | |
197 | return null; | |
198 | } | |
199 | ||
200 | try { | |
201 | ||
202 | IRegion line= fDocument.getLineInformationOfOffset(getOffset()); | |
203 | prefix= fDocument.get(line.getOffset(), getOffset() - line.getOffset()); | |
204 | int postfixStart= getOffset() + getLength(); | |
205 | postfix= fDocument.get(postfixStart, line.getOffset() + line.getLength() - postfixStart); | |
206 | ||
207 | } catch (BadLocationException exception) { | |
208 | // Do nothing | |
209 | } | |
210 | return new String[] { | |
211 | word, | |
212 | prefix, | |
213 | postfix, | |
214 | isSentenceStart() ? Boolean.toString(true) : Boolean | |
215 | .toString(false), | |
216 | isDictionaryMatch() ? Boolean.toString(true) : Boolean | |
217 | .toString(false) }; | |
218 | } | |
219 | ||
220 | /** | |
221 | * Returns <code>true</code> iff the corresponding word was found in the dictionary. | |
222 | * <p> | |
223 | * NOTE: to be removed, see {@link #getProposals()} | |
224 | * </p> | |
225 | * | |
226 | * @return <code>true</code> iff the corresponding word was found in the dictionary | |
227 | */ | |
228 | public boolean isDictionaryMatch() { | |
229 | return fSpellEvent.isMatch(); | |
230 | } | |
231 | ||
232 | /** | |
233 | * Returns <code>true</code> iff the corresponding word starts a sentence. | |
234 | * <p> | |
235 | * NOTE: to be removed, see {@link #getProposals()} | |
236 | * </p> | |
237 | * | |
238 | * @return <code>true</code> iff the corresponding word starts a sentence | |
239 | */ | |
240 | public boolean isSentenceStart() { | |
241 | return fSpellEvent.isStart(); | |
242 | } | |
243 | ||
244 | } |