-
Notifications
You must be signed in to change notification settings - Fork 3
/
QueryCompletionEngine.java
204 lines (183 loc) · 6.34 KB
/
QueryCompletionEngine.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*******************************************************************************
* Copyright (c) 2015, 2024 Obeo.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.acceleo.query.runtime.impl;
import java.util.Map;
import java.util.Set;
import org.eclipse.acceleo.query.parser.AstCompletor;
import org.eclipse.acceleo.query.runtime.ICompletionResult;
import org.eclipse.acceleo.query.runtime.IQueryCompletionEngine;
import org.eclipse.acceleo.query.runtime.IQueryValidationEngine;
import org.eclipse.acceleo.query.runtime.IReadOnlyQueryEnvironment;
import org.eclipse.acceleo.query.runtime.IValidationResult;
import org.eclipse.acceleo.query.validation.type.IType;
/**
* {@link QueryCompletionEngine} is the default query validation engine.
*
* @author <a href="mailto:yvan.lussaud@obeo.fr">Yvan Lussaud</a>
*/
public class QueryCompletionEngine implements IQueryCompletionEngine {
/**
* A colon.
*/
private static final String COLON = ":";
/**
* The environment containing all necessary information and used to execute query services.
*/
private IReadOnlyQueryEnvironment queryEnvironment;
/**
* Constructor. It takes an {@link IReadOnlyQueryEnvironment} as parameter.
*
* @param queryEnvironment
* The environment containing all necessary information and used to execute query services.
*/
public QueryCompletionEngine(IReadOnlyQueryEnvironment queryEnvironment) {
this.queryEnvironment = queryEnvironment;
}
/**
* {@inheritDoc}
*
* @see org.eclipse.acceleo.query.runtime.IQueryCompletionEngine#getCompletion(java.lang.String, int,
* java.util.Map)
*/
@Override
public ICompletionResult getCompletion(String expression, int offset,
Map<String, Set<IType>> variableTypes) {
final ICompletionResult result;
final AstCompletor completor = new AstCompletor(new CompletionServices(queryEnvironment));
if (offset < 0 || (expression != null && offset > expression.length())) {
throw new IllegalArgumentException("offset (" + offset
+ ") must be in the range of the given expression: \"" + expression + "\"");
}
final String prefix = getPrefix(expression, offset);
final String remaining = getRemaining(expression, offset);
final String toParse = getToParse(expression, offset, prefix);
final IQueryValidationEngine builder = new QueryValidationEngine(queryEnvironment);
final IValidationResult validationResult = builder.validate(toParse, variableTypes);
result = new CompletionResult(completor.getProposals(variableTypes.keySet(), validationResult));
result.setPrefix(prefix);
result.setRemaining(remaining);
if (prefix != null) {
result.setReplacementOffset(offset - prefix.length());
result.setReplacementLength(prefix.length());
}
return result;
}
/**
* Gets the {@link String} containing only identifier part before the cursor if any, an empty
* {@link String} otherwise.
*
* @param expression
* the original expression to parse
* @param offset
* the offset of the cursor
* @return the {@link String} containing only identifier part before the cursor if any, an empty
* {@link String} otherwise
*/
private String getPrefix(String expression, int offset) {
final String result;
if (expression == null) {
result = null;
} else {
int start = offset;
while (start - 1 >= 0) {
char charAt = expression.charAt(start - 1);
if (Character.isLetter(charAt) || Character.isDigit(charAt) || charAt == '_'
|| charAt == ':') {
--start;
} else {
break;
}
}
final String prefix = expression.substring(start, offset);
if (COLON.equals(prefix)) {
result = "";
} else if (!prefix.endsWith(COLON)) {
// special case of valid EClassifier and EEnumLiteral
final String[] splited = prefix.split(COLON + COLON);
if (splited.length == 2) {
// EClassifier
if (!queryEnvironment.getEPackageProvider().getTypes(splited[0], splited[1]).isEmpty()) {
result = "";
} else {
result = prefix;
}
} else if (splited.length == 3) {
// EEnumLiteral
if (!queryEnvironment.getEPackageProvider().getEnumLiterals(splited[0], splited[1],
splited[2]).isEmpty()) {
result = "";
} else {
result = prefix;
}
} else {
result = prefix;
}
} else {
result = prefix;
}
}
return result;
}
/**
* Gets the {@link String} containing only identifier part after the cursor if any, <code>null</code> or
* an empty {@link String} otherwise.
*
* @param expression
* the original expression to parse
* @param offset
* the offset of the cursor
* @return the {@link String} containing only identifier part after the cursor if any, <code>null</code>
* or an empty {@link String} otherwise
*/
private String getRemaining(String expression, int offset) {
final String result;
if (expression == null) {
result = null;
} else {
int length = expression.length();
int end = offset;
while (end < length) {
char charAt = expression.charAt(end);
if (Character.isLetter(charAt) || Character.isDigit(charAt) || charAt == '_') {
++end;
} else {
break;
}
}
result = expression.substring(offset, end);
}
return result;
}
/**
* Gets the portion of the original expression to parse.
*
* @param expression
* the original expression to parse
* @param offset
* the offset of the cursor
* @param prefix
* the {@link String} containing only letter before the cursor if any, an empty {@link String}
* otherwise
* @return the portion of the original expression to parse
*/
private String getToParse(String expression, int offset, String prefix) {
final String result;
if (expression == null) {
result = null;
} else if (prefix.contains(COLON)) {
// special case of EClassifier and EEnumLiteral
result = expression.substring(0, expression.lastIndexOf(COLON) + 1);
} else {
result = expression.substring(0, offset - prefix.length());
}
return result;
}
}