1 | package com.framsticks.framclipse.editors.codeCompletion; |
---|
2 | |
---|
3 | import java.util.ArrayList; |
---|
4 | import java.util.List; |
---|
5 | |
---|
6 | import org.eclipse.jface.text.BadLocationException; |
---|
7 | import org.eclipse.jface.text.IDocument; |
---|
8 | import org.jdom.Document; |
---|
9 | import org.jdom.Element; |
---|
10 | import org.jdom.JDOMException; |
---|
11 | import org.jdom.xpath.XPath; |
---|
12 | |
---|
13 | import com.framsticks.framclipse.Framclipse; |
---|
14 | |
---|
15 | |
---|
16 | /** |
---|
17 | * Utils for finding function and field names in document and describing them |
---|
18 | * (e.g. using type). |
---|
19 | * |
---|
20 | * @author Jan Kuniak |
---|
21 | */ |
---|
22 | public class SyntaxUtils { |
---|
23 | |
---|
24 | public static final String ROOT = "/framscript"; |
---|
25 | |
---|
26 | public static final String CONTEXT_ATTRIBUTE = "context"; |
---|
27 | public static final String FUNCTION_ATTRIBUTE = "function"; |
---|
28 | public static final String NAME_ATTRIBUTE = "name"; |
---|
29 | public static final String TYPE_ATTRIBUTE = "type"; |
---|
30 | |
---|
31 | public static final String ARGUMENT_ELEMENT = "argument"; |
---|
32 | public static final String ARGUMENTS_ELEMENT = "arguments"; |
---|
33 | public static final String DESCRIPTION_ELEMENT = "description"; |
---|
34 | public static final String ELEMENT_ELEMENT = "element"; |
---|
35 | public static final String TYPE_ELEMENT = "type"; |
---|
36 | |
---|
37 | // ///////////////////////////////////////////////////////////////////////// |
---|
38 | // Before |
---|
39 | // /////////////////////////////////////////////////////////////////////// |
---|
40 | |
---|
41 | /** |
---|
42 | * Returns identifier before the cursor (if any). |
---|
43 | * |
---|
44 | * @param document |
---|
45 | * document in which element is to be found. |
---|
46 | * @param offset |
---|
47 | * cursor position in the document. |
---|
48 | * @return identifier before the cursor if any, "" if not found. |
---|
49 | */ |
---|
50 | public static String getElementBefore(IDocument document, int offset) { |
---|
51 | String result = ""; |
---|
52 | int i = offset - 1; |
---|
53 | try { |
---|
54 | while ((i > 0)) { |
---|
55 | char ch = document.getChar(i); |
---|
56 | if (!Character.isJavaIdentifierPart(ch)) { |
---|
57 | return result; |
---|
58 | } |
---|
59 | |
---|
60 | result = ch + result; |
---|
61 | i--; |
---|
62 | } |
---|
63 | } catch (BadLocationException e) { |
---|
64 | e.printStackTrace(); |
---|
65 | } |
---|
66 | return result; |
---|
67 | } |
---|
68 | |
---|
69 | /** |
---|
70 | * Returns brace block, starting with opening brace, ending with closing |
---|
71 | * brace and regarding braces inside. |
---|
72 | * |
---|
73 | * @param document |
---|
74 | * document to search brace block in. |
---|
75 | * @param offset |
---|
76 | * cursor position in the document. |
---|
77 | * @return brace block if found, "" otherwise. |
---|
78 | */ |
---|
79 | public static String getBraceBlockBefore(IDocument document, int offset) { |
---|
80 | String result = ""; |
---|
81 | int i = offset - 1; |
---|
82 | int bracketsCount = 0; |
---|
83 | try { |
---|
84 | while ((i > 0)) { |
---|
85 | char ch = document.getChar(i); |
---|
86 | |
---|
87 | switch (ch) { |
---|
88 | case ')': |
---|
89 | bracketsCount++; |
---|
90 | break; |
---|
91 | case '(': |
---|
92 | bracketsCount--; |
---|
93 | break; |
---|
94 | case '\n': |
---|
95 | return result; |
---|
96 | default: |
---|
97 | if (bracketsCount == 0) { |
---|
98 | return result; |
---|
99 | } |
---|
100 | } |
---|
101 | result = ch + result; |
---|
102 | i--; |
---|
103 | } |
---|
104 | } catch (BadLocationException e) { |
---|
105 | e.printStackTrace(); |
---|
106 | } |
---|
107 | return result; |
---|
108 | } |
---|
109 | |
---|
110 | /** |
---|
111 | * Returns list of elements (Classes, methods, fields) connected with dots |
---|
112 | * ending with the element before the cursor position. Functions does not |
---|
113 | * contain braces blocks. |
---|
114 | * |
---|
115 | * @param document |
---|
116 | * document to search elements in. |
---|
117 | * @param offset |
---|
118 | * cursor position in the document. |
---|
119 | * @return list of strings found, may be empty, never null. |
---|
120 | */ |
---|
121 | public static List<String> getElementsBefore(IDocument document, int offset) { |
---|
122 | |
---|
123 | List<String> objects = new ArrayList<String>(); |
---|
124 | String object = ""; |
---|
125 | try { |
---|
126 | char probablyDot = document.getChar(offset); |
---|
127 | |
---|
128 | while (probablyDot == '.') { |
---|
129 | String bracketBlock = SyntaxUtils.getBraceBlockBefore(document, offset); |
---|
130 | offset -= bracketBlock.length(); |
---|
131 | object = SyntaxUtils.getElementBefore(document, offset); |
---|
132 | if (object.length() == 0) { |
---|
133 | return objects; |
---|
134 | } |
---|
135 | objects.add(object); |
---|
136 | |
---|
137 | offset -= object.length() + 1; |
---|
138 | if (offset < 0) { |
---|
139 | return objects; |
---|
140 | } |
---|
141 | |
---|
142 | probablyDot = document.getChar(offset); |
---|
143 | } |
---|
144 | } catch (BadLocationException e) { |
---|
145 | e.printStackTrace(); |
---|
146 | } |
---|
147 | |
---|
148 | return objects; |
---|
149 | } |
---|
150 | |
---|
151 | // ///////////////////////////////////////////////////////////////////////// |
---|
152 | // After |
---|
153 | // /////////////////////////////////////////////////////////////////////// |
---|
154 | |
---|
155 | /** |
---|
156 | * Returns identifier after the cursor position (if any) including character |
---|
157 | * at position. |
---|
158 | * |
---|
159 | * @param document |
---|
160 | * document in which element is to be found. |
---|
161 | * @param offset |
---|
162 | * cursor position in the document. |
---|
163 | * @return identifier after the cursor if any, "" if not found. |
---|
164 | */ |
---|
165 | public static String getElementAfter(IDocument document, int offset) { |
---|
166 | String result = ""; |
---|
167 | int i = offset; |
---|
168 | try { |
---|
169 | while ((i < document.getLength())) { |
---|
170 | char ch = document.getChar(i); |
---|
171 | if (!Character.isJavaIdentifierPart(ch)) { |
---|
172 | return result; |
---|
173 | } |
---|
174 | |
---|
175 | result += ch; |
---|
176 | i++; |
---|
177 | } |
---|
178 | } catch (BadLocationException e) { |
---|
179 | e.printStackTrace(); |
---|
180 | } |
---|
181 | return result; |
---|
182 | } |
---|
183 | |
---|
184 | // ///////////////////////////////////////////////////////////////////////// |
---|
185 | // At |
---|
186 | // /////////////////////////////////////////////////////////////////////// |
---|
187 | |
---|
188 | /** |
---|
189 | * Returns list of elements (Classes, methods, fields) connected with dots |
---|
190 | * ending with the element at the cursor position. Functions does not |
---|
191 | * contain braces blocks. |
---|
192 | * |
---|
193 | * @param document |
---|
194 | * document to search elements in. |
---|
195 | * @param offset |
---|
196 | * cursor position in the document. |
---|
197 | * @return list of strings found, may be empty, never null. |
---|
198 | */ |
---|
199 | public static String getElementAt(IDocument document, int offset) { |
---|
200 | return getElementBefore(document, offset) + getElementAfter(document, offset); |
---|
201 | } |
---|
202 | |
---|
203 | /** |
---|
204 | * Returns identifier at the cursor position (if any). |
---|
205 | * |
---|
206 | * @param document |
---|
207 | * document in which element is to be found. |
---|
208 | * @param offset |
---|
209 | * cursor position in the document. |
---|
210 | * @return identifier at the cursor if any, "" if not found. |
---|
211 | */ |
---|
212 | public static List<String> getElementsAt(IDocument document, int offset) { |
---|
213 | String before = SyntaxUtils.getElementBefore(document, offset); |
---|
214 | int dotPosition = offset - before.length() - 1; |
---|
215 | List<String> elements = SyntaxUtils.getElementsBefore(document, dotPosition); |
---|
216 | String elementAt = SyntaxUtils.getElementAt(document, offset); |
---|
217 | if (elementAt.length() > 0) { |
---|
218 | elements.add(elementAt); |
---|
219 | } |
---|
220 | |
---|
221 | return elements; |
---|
222 | } |
---|
223 | |
---|
224 | // ///////////////////////////////////////////////////////////////////////// |
---|
225 | // Other |
---|
226 | // /////////////////////////////////////////////////////////////////////// |
---|
227 | |
---|
228 | /** |
---|
229 | * Returns the type of the last element in the list, regarding types of it's |
---|
230 | * predecesors, i.e. it distinguishes <code>Vector.new()</code> from |
---|
231 | * <code>Dictionary.new()</code> and so on. |
---|
232 | * |
---|
233 | * @param elements |
---|
234 | * vector of elements (identifiers). |
---|
235 | * @return type of the last element in the list. |
---|
236 | * @throws JDOMException |
---|
237 | * if parsing exception occurs (problem with configuration |
---|
238 | * file). |
---|
239 | */ |
---|
240 | public static String getLastElementType(List<String> elements) throws JDOMException { |
---|
241 | |
---|
242 | if (elements.size() == 0) { |
---|
243 | return ""; |
---|
244 | } |
---|
245 | Document syntax = Framclipse.getDefault().getFramscriptSyntax(); |
---|
246 | |
---|
247 | int i = elements.size() - 1; |
---|
248 | String type = elements.get(i); |
---|
249 | |
---|
250 | while ((--i >= 0) && (type != null) && (type.length() > 0)) { |
---|
251 | String nextElement = elements.get(i); |
---|
252 | String query = SyntaxUtils.ROOT; |
---|
253 | query += "/*[@" + SyntaxUtils.NAME_ATTRIBUTE + "='" + type + "']"; |
---|
254 | query += "/*[@" + SyntaxUtils.NAME_ATTRIBUTE + "='" + nextElement + "']"; |
---|
255 | XPath xpath = XPath.newInstance(query); |
---|
256 | Element element = (Element) xpath.selectSingleNode(syntax); |
---|
257 | type = element.getAttributeValue(SyntaxUtils.TYPE_ATTRIBUTE); |
---|
258 | } |
---|
259 | return type; |
---|
260 | } |
---|
261 | |
---|
262 | public static String typeName(char type) { |
---|
263 | switch (type) { |
---|
264 | case 'd': |
---|
265 | return "integer"; |
---|
266 | case 'f': |
---|
267 | return "float"; |
---|
268 | case 's': |
---|
269 | return "string"; |
---|
270 | case 'p': |
---|
271 | return "Function"; |
---|
272 | case 'o': |
---|
273 | return "object"; |
---|
274 | case 'x': |
---|
275 | return "unknown"; |
---|
276 | } |
---|
277 | return null; |
---|
278 | } |
---|
279 | } |
---|