]>
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 | ||
12 | package org.eclipse.jdt.internal.ui.text.spelling; | |
13 | ||
14 | import java.util.ArrayList; | |
15 | import java.util.Collections; | |
16 | import java.util.Iterator; | |
17 | import java.util.List; | |
18 | ||
19 | import org.eclipse.core.runtime.IProgressMonitor; | |
20 | ||
21 | import org.eclipse.jface.text.BadLocationException; | |
22 | import org.eclipse.jface.text.DocumentEvent; | |
23 | import org.eclipse.jface.text.IDocument; | |
24 | import org.eclipse.jface.text.IRegion; | |
25 | import org.eclipse.jface.text.contentassist.ICompletionProposal; | |
26 | import org.eclipse.jface.text.contentassist.IContextInformation; | |
27 | ||
28 | import org.eclipse.jdt.ui.PreferenceConstants; | |
29 | import org.eclipse.jdt.ui.text.java.ContentAssistInvocationContext; | |
30 | import org.eclipse.jdt.ui.text.java.IJavaCompletionProposalComputer; | |
31 | ||
32 | import org.eclipse.jdt.internal.ui.JavaPlugin; | |
33 | import org.eclipse.jdt.internal.ui.JavaPluginImages; | |
34 | import org.eclipse.jdt.internal.ui.text.java.JavaCompletionProposal; | |
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.RankedWordProposal; | |
38 | ||
39 | /** | |
40 | * Content assist processor to complete words. | |
41 | * <strong>Note:</strong> This is currently not supported because the spelling engine | |
42 | * cannot return word proposals but only correction proposals. | |
43 | * <p> | |
44 | * If we enable this again we must register the computer in <code>plugin.xml</code>: | |
45 | * <pre> | |
46 | * </pre> | |
47 | * </p> | |
48 | * | |
49 | * @since 3.0 | |
50 | */ | |
51 | public final class WordCompletionProposalComputer implements IJavaCompletionProposalComputer { | |
52 | ||
53 | /** The prefix rank shift */ | |
54 | public static final int PREFIX_RANK_SHIFT= 500; | |
55 | ||
56 | /* | |
57 | * @see org.eclipse.jface.text.contentassist.ICompletionProposalComputer#computeCompletionProposals(org.eclipse.jface.text.contentassist.TextContentAssistInvocationContext, org.eclipse.core.runtime.IProgressMonitor) | |
58 | */ | |
59 | public List<ICompletionProposal> computeCompletionProposals(ContentAssistInvocationContext context, IProgressMonitor monitor) { | |
60 | if (contributes()) { | |
61 | try { | |
62 | IDocument document= context.getDocument(); | |
63 | final int offset= context.getInvocationOffset(); | |
64 | ||
65 | final IRegion region= document.getLineInformationOfOffset(offset); | |
66 | final String content= document.get(region.getOffset(), region.getLength()); | |
67 | ||
68 | int index= offset - region.getOffset() - 1; | |
69 | while (index >= 0 && Character.isLetter(content.charAt(index))) | |
70 | index--; | |
71 | ||
72 | final int start= region.getOffset() + index + 1; | |
73 | final String candidate= content.substring(index + 1, offset - region.getOffset()); | |
74 | ||
75 | if (candidate.length() > 0) { | |
76 | ||
77 | final ISpellCheckEngine engine= SpellCheckEngine.getInstance(); | |
78 | final ISpellChecker checker= engine.getSpellChecker(); | |
79 | ||
80 | if (checker != null) { | |
81 | ||
82 | final List<RankedWordProposal> proposals= new ArrayList<RankedWordProposal>(checker.getProposals(candidate, Character.isUpperCase(candidate.charAt(0)))); | |
83 | final List<ICompletionProposal> result= new ArrayList<ICompletionProposal>(proposals.size()); | |
84 | ||
85 | for (Iterator<RankedWordProposal> it= proposals.iterator(); it.hasNext();) { | |
86 | RankedWordProposal word= it.next(); | |
87 | String text= word.generated_6472998331556061287(candidate); | |
88 | ||
89 | result.add(new JavaCompletionProposal(text, start, candidate.length(), JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_RENAME), text, word.getRank()) { | |
90 | /* | |
91 | * @see org.eclipse.jdt.internal.ui.text.java.JavaCompletionProposal#validate(org.eclipse.jface.text.IDocument, int, org.eclipse.jface.text.DocumentEvent) | |
92 | */ | |
93 | @Override | |
94 | public boolean validate(IDocument doc, int validate_offset, DocumentEvent event) { | |
95 | return offset == validate_offset; | |
96 | } | |
97 | }); | |
98 | } | |
99 | ||
100 | return result; | |
101 | } | |
102 | } | |
103 | } catch (BadLocationException exception) { | |
104 | // log & ignore | |
105 | JavaPlugin.log(exception); | |
106 | } | |
107 | } | |
108 | return Collections.emptyList(); | |
109 | } | |
110 | ||
111 | private boolean contributes() { | |
112 | return PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.SPELLING_ENABLE_CONTENTASSIST); | |
113 | } | |
114 | ||
115 | /* | |
116 | * @see org.eclipse.jface.text.contentassist.ICompletionProposalComputer#computeContextInformation(org.eclipse.jface.text.contentassist.TextContentAssistInvocationContext, org.eclipse.core.runtime.IProgressMonitor) | |
117 | */ | |
118 | public List<IContextInformation> computeContextInformation(ContentAssistInvocationContext context, IProgressMonitor monitor) { | |
119 | return Collections.emptyList(); | |
120 | } | |
121 | ||
122 | /* | |
123 | * @see org.eclipse.jface.text.contentassist.ICompletionProposalComputer#getErrorMessage() | |
124 | */ | |
125 | public String getErrorMessage() { | |
126 | return null; // no error message available | |
127 | } | |
128 | ||
129 | /* | |
130 | * @see org.eclipse.jdt.ui.text.java.IJavaCompletionProposalComputer#sessionStarted() | |
131 | */ | |
132 | public void sessionStarted() { | |
133 | } | |
134 | ||
135 | /* | |
136 | * @see org.eclipse.jdt.ui.text.java.IJavaCompletionProposalComputer#sessionEnded() | |
137 | */ | |
138 | public void sessionEnded() { | |
139 | } | |
140 | } |