1 | package com.framsticks.framclipse.editors.codeCompletion; |
---|
2 | |
---|
3 | import java.util.ArrayList; |
---|
4 | import java.util.Collections; |
---|
5 | import java.util.List; |
---|
6 | import java.util.Map; |
---|
7 | import java.util.Set; |
---|
8 | |
---|
9 | import org.eclipse.jface.text.IDocument; |
---|
10 | import org.eclipse.jface.text.ITextViewer; |
---|
11 | import org.eclipse.jface.text.TextPresentation; |
---|
12 | import org.eclipse.jface.text.contentassist.ICompletionProposal; |
---|
13 | import org.eclipse.jface.text.contentassist.IContentAssistProcessor; |
---|
14 | import org.eclipse.jface.text.contentassist.IContextInformation; |
---|
15 | import org.eclipse.jface.text.contentassist.IContextInformationPresenter; |
---|
16 | import org.eclipse.jface.text.contentassist.IContextInformationValidator; |
---|
17 | import org.jdom.Attribute; |
---|
18 | import org.jdom.Document; |
---|
19 | import org.jdom.Element; |
---|
20 | import org.jdom.JDOMException; |
---|
21 | import org.jdom.xpath.XPath; |
---|
22 | |
---|
23 | import com.framsticks.framclipse.Framclipse; |
---|
24 | |
---|
25 | |
---|
26 | /** |
---|
27 | * Framscript completion processor. |
---|
28 | */ |
---|
29 | public class PropertyCompletionProcessor implements IContentAssistProcessor { |
---|
30 | |
---|
31 | private final Set<String> contexts; |
---|
32 | private final Set<String> objectContexts; |
---|
33 | private final Map<String, String> propertyTypes; |
---|
34 | |
---|
35 | public PropertyCompletionProcessor(Set<String> contexts, Set<String> objectContexts, |
---|
36 | Map<String, String> propertyTypes) { |
---|
37 | this.contexts = contexts; |
---|
38 | this.objectContexts = objectContexts; |
---|
39 | this.propertyTypes = propertyTypes; |
---|
40 | } |
---|
41 | |
---|
42 | protected IContextInformationValidator validator = new Validator(); |
---|
43 | |
---|
44 | /* |
---|
45 | * (non-Javadoc) Method declared on IContentAssistProcessor |
---|
46 | */ |
---|
47 | @SuppressWarnings("unchecked") |
---|
48 | public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) { |
---|
49 | // get text before cursor |
---|
50 | IDocument document = viewer.getDocument(); |
---|
51 | String elementPart = SyntaxUtils.getElementBefore(document, documentOffset); |
---|
52 | |
---|
53 | // now if we have parent, ask XPath |
---|
54 | List<OrderableCompletionProposal> proposals = new ArrayList<OrderableCompletionProposal>(); |
---|
55 | |
---|
56 | String query = SyntaxUtils.ROOT + "/" + SyntaxUtils.TYPE_ELEMENT; |
---|
57 | Document syntax = Framclipse.getDefault().getFramscriptSyntax(); |
---|
58 | try { |
---|
59 | XPath xpath = XPath.newInstance(query); |
---|
60 | List<Element> list = xpath.selectNodes(syntax); |
---|
61 | for (Element element : list) { |
---|
62 | String name = element.getAttribute(SyntaxUtils.NAME_ATTRIBUTE).getValue(); |
---|
63 | if ((name != null) && propertyTypes.keySet().contains(name)) { |
---|
64 | Attribute context = element.getAttribute(SyntaxUtils.CONTEXT_ATTRIBUTE); |
---|
65 | if ((context == null) || contexts.contains(context.getValue())) { |
---|
66 | addTypeCompletions(documentOffset, elementPart, element, proposals); |
---|
67 | } |
---|
68 | } |
---|
69 | |
---|
70 | String context = element.getAttribute(SyntaxUtils.CONTEXT_ATTRIBUTE).getValue(); |
---|
71 | if ((context != null) && objectContexts.contains(context)) { |
---|
72 | addObjectCompletions(documentOffset, elementPart, element, proposals); |
---|
73 | } |
---|
74 | } |
---|
75 | } catch (Exception e) { |
---|
76 | e.printStackTrace(); |
---|
77 | } |
---|
78 | |
---|
79 | Collections.sort(proposals); |
---|
80 | ICompletionProposal[] result = new ICompletionProposal[proposals.size()]; |
---|
81 | result = proposals.toArray(result); |
---|
82 | return result; |
---|
83 | } |
---|
84 | |
---|
85 | @SuppressWarnings("unchecked") |
---|
86 | private void addTypeCompletions(int documentOffset, String elementPart, Element typeElement, |
---|
87 | List<OrderableCompletionProposal> proposals) throws JDOMException { |
---|
88 | |
---|
89 | String typeName = typeElement.getAttribute(SyntaxUtils.NAME_ATTRIBUTE).getValue(); |
---|
90 | String objectName = propertyTypes.get(typeName); |
---|
91 | |
---|
92 | List<Element> fieldsList = typeElement.getChildren(SyntaxUtils.ELEMENT_ELEMENT); |
---|
93 | for (Element element : fieldsList) { |
---|
94 | addCompletion(element, documentOffset, elementPart, objectName, proposals); |
---|
95 | } |
---|
96 | } |
---|
97 | |
---|
98 | @SuppressWarnings("unchecked") |
---|
99 | private void addObjectCompletions(int documentOffset, String elementPart, |
---|
100 | Element objectElement, List<OrderableCompletionProposal> proposals) |
---|
101 | throws JDOMException { |
---|
102 | |
---|
103 | String objectName = objectElement.getAttribute(SyntaxUtils.NAME_ATTRIBUTE).getValue(); |
---|
104 | addCompletion(objectElement, documentOffset, elementPart, null, proposals); |
---|
105 | |
---|
106 | List<Element> fieldsList = objectElement.getChildren(SyntaxUtils.ELEMENT_ELEMENT); |
---|
107 | for (Element element : fieldsList) { |
---|
108 | addCompletion(element, documentOffset, elementPart, objectName, proposals); |
---|
109 | } |
---|
110 | } |
---|
111 | |
---|
112 | @SuppressWarnings("unchecked") |
---|
113 | private void addCompletion(Element element, int documentOffset, String elementPart, |
---|
114 | String objectName, List<OrderableCompletionProposal> proposals) { |
---|
115 | String name = element.getAttributeValue(SyntaxUtils.NAME_ATTRIBUTE); |
---|
116 | String completion = name; |
---|
117 | int cursorPosition = name.length(); |
---|
118 | |
---|
119 | // since we are to propose property completions, we do not want function |
---|
120 | // names here |
---|
121 | String functionValue = element.getAttributeValue(SyntaxUtils.FUNCTION_ATTRIBUTE); |
---|
122 | boolean function = (functionValue != null) && functionValue.equals("true"); |
---|
123 | if (function) { |
---|
124 | return; |
---|
125 | } |
---|
126 | |
---|
127 | String displayString = ""; |
---|
128 | if (objectName != null) { |
---|
129 | displayString += objectName + ": "; |
---|
130 | } |
---|
131 | displayString += completion + ":"; |
---|
132 | IContextInformation contextInformation = null; |
---|
133 | |
---|
134 | String type = element.getAttributeValue(SyntaxUtils.TYPE_ATTRIBUTE); |
---|
135 | if ((type != null) && (type.length() > 0)) { |
---|
136 | displayString += " (" + type + ")"; |
---|
137 | } |
---|
138 | |
---|
139 | if (completion.startsWith(elementPart)) { |
---|
140 | int replacementLength = elementPart.length(); |
---|
141 | int replacementOffset = documentOffset - replacementLength; |
---|
142 | OrderableCompletionProposal completionProposal = new OrderableCompletionProposal( |
---|
143 | completion + ": ", replacementOffset, replacementLength, cursorPosition + 2, |
---|
144 | null, displayString, contextInformation, null); |
---|
145 | |
---|
146 | completionProposal.setType(objectName); |
---|
147 | completionProposal.setName(completion); |
---|
148 | proposals.add(completionProposal); |
---|
149 | } |
---|
150 | } |
---|
151 | |
---|
152 | /* |
---|
153 | * (non-Javadoc) Method declared on IContentAssistProcessor |
---|
154 | */ |
---|
155 | public IContextInformation[] computeContextInformation(ITextViewer viewer, int documentOffset) { |
---|
156 | return null; |
---|
157 | } |
---|
158 | |
---|
159 | /* |
---|
160 | * (non-Javadoc) Method declared on IContentAssistProcessor |
---|
161 | */ |
---|
162 | public char[] getCompletionProposalAutoActivationCharacters() { |
---|
163 | return new char[] { '.' }; |
---|
164 | } |
---|
165 | |
---|
166 | /* |
---|
167 | * (non-Javadoc) Method declared on IContentAssistProcessor |
---|
168 | */ |
---|
169 | public char[] getContextInformationAutoActivationCharacters() { |
---|
170 | return new char[] {}; |
---|
171 | } |
---|
172 | |
---|
173 | /* |
---|
174 | * (non-Javadoc) Method declared on IContentAssistProcessor |
---|
175 | */ |
---|
176 | public IContextInformationValidator getContextInformationValidator() { |
---|
177 | return validator; |
---|
178 | } |
---|
179 | |
---|
180 | /* |
---|
181 | * (non-Javadoc) Method declared on IContentAssistProcessor |
---|
182 | */ |
---|
183 | public String getErrorMessage() { |
---|
184 | return null; |
---|
185 | } |
---|
186 | |
---|
187 | /** |
---|
188 | * Simple content assist tip closer. The tip is valid in a range of 5 |
---|
189 | * characters around its popup location. |
---|
190 | */ |
---|
191 | protected static class Validator implements IContextInformationValidator, |
---|
192 | IContextInformationPresenter { |
---|
193 | |
---|
194 | protected int installOffset; |
---|
195 | |
---|
196 | /* |
---|
197 | * @see IContextInformationValidator#isContextInformationValid(int) |
---|
198 | */ |
---|
199 | public boolean isContextInformationValid(int offset) { |
---|
200 | return Math.abs(installOffset - offset) < 5; |
---|
201 | } |
---|
202 | |
---|
203 | /* |
---|
204 | * @see IContextInformationValidator#install(IContextInformation, |
---|
205 | * ITextViewer, int) |
---|
206 | */ |
---|
207 | public void install(IContextInformation info, ITextViewer viewer, int offset) { |
---|
208 | installOffset = offset; |
---|
209 | } |
---|
210 | |
---|
211 | /* |
---|
212 | * @see org.eclipse.jface.text.contentassist.IContextInformationPresenter#updatePresentation(int, |
---|
213 | * TextPresentation) |
---|
214 | */ |
---|
215 | public boolean updatePresentation(int documentPosition, TextPresentation presentation) { |
---|
216 | return false; |
---|
217 | } |
---|
218 | } |
---|
219 | } |
---|