-
Notifications
You must be signed in to change notification settings - Fork 3
/
AcceleoCompletor.java
190 lines (169 loc) · 7.35 KB
/
AcceleoCompletor.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
/*******************************************************************************
* Copyright (c) 2017, 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.aql.completion;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import org.eclipse.acceleo.Error;
import org.eclipse.acceleo.Module;
import org.eclipse.acceleo.aql.completion.proposals.AcceleoCompletionProposal;
import org.eclipse.acceleo.aql.parser.AcceleoAstResult;
import org.eclipse.acceleo.aql.parser.AcceleoParser;
import org.eclipse.acceleo.aql.validation.AcceleoValidator;
import org.eclipse.acceleo.aql.validation.IAcceleoValidationResult;
import org.eclipse.acceleo.query.runtime.namespace.IQualifiedNameQueryEnvironment;
import org.eclipse.emf.ecore.EObject;
/**
* Acceleo service for content assist / auto-completion.
*
* @author <a href="mailto:yvan.lussaud@obeo.fr">Yvan Lussaud</a>
*/
public class AcceleoCompletor {
/**
* The new line {@link String}.
*/
private String newLine;
/**
* Constructor.
*
* @param newLine
* the new line {@link String}
*/
public AcceleoCompletor(String newLine) {
this.newLine = newLine;
}
/**
* The name space for completion.
*/
private static final String TO_COMPLETION_NAMESPACE = "_reserved_::to::completion";
/**
* Provides the {@link List} of {@link AcceleoCompletionProposal completion proposals} available for the
* given Acceleo source at the given position in the given environment.
*
* @param queryEnvironment
* the {@link IQualifiedNameQueryEnvironment}
* @param moduleFileName
* the (non-{@code null}) name of the file containing the module (without extension).
* @param source
* the (non-{@code null}) Acceleo text source contents.
* @param position
* the caret position in {@code source}.
* @return the {@link List} of {@link AcceleoCompletionProposal} for the given source at the given
* position
*/
public List<AcceleoCompletionProposal> getProposals(IQualifiedNameQueryEnvironment queryEnvironment,
String moduleFileName, String source, int position) {
String moduleQualifiedNameForCompletion = TO_COMPLETION_NAMESPACE + AcceleoParser.QUALIFIER_SEPARATOR
+ moduleFileName;
// First, parse the source contents up to the position.
final AcceleoParser acceleoParser = new AcceleoParser();
final String partialAcceleoSource = source.substring(0, position);
final AcceleoAstResult partialAcceleoAstResult = acceleoParser.parse(partialAcceleoSource,
moduleQualifiedNameForCompletion);
// Second, validate the AST - this is required further on for the AQL completion.
final AcceleoAstResult acceleoAstResult = acceleoParser.parse(source,
moduleQualifiedNameForCompletion);
queryEnvironment.getLookupEngine().getResolver().register(moduleQualifiedNameForCompletion,
acceleoAstResult.getModule());
final List<AcceleoCompletionProposal> proposals;
try {
final AcceleoValidator acceleoValidator = new AcceleoValidator(queryEnvironment);
IAcceleoValidationResult acceleoValidationResult = acceleoValidator.validate(
partialAcceleoAstResult, moduleQualifiedNameForCompletion);
// Find which element of the AST we are completing.
EObject acceleoElementToComplete = getElementToComplete(partialAcceleoAstResult);
queryEnvironment.getLookupEngine().pushImportsContext(moduleQualifiedNameForCompletion,
moduleQualifiedNameForCompletion);
try {
proposals = this.getProposals(queryEnvironment, moduleFileName, partialAcceleoSource,
acceleoValidationResult, acceleoElementToComplete);
} finally {
queryEnvironment.getLookupEngine().popContext(moduleQualifiedNameForCompletion);
}
} finally {
queryEnvironment.getLookupEngine().getResolver().clear(Collections.singleton(
moduleQualifiedNameForCompletion));
queryEnvironment.getLookupEngine().clearContext(moduleQualifiedNameForCompletion);
}
return proposals;
}
/**
* Provides the {@link List} of {@link AcceleoCompletionProposal completion proposals} for the given
* {@link EObject} of an Acceleo AST in the given environment.
*
* @param queryEnvironment
* the (non-{@code null}) contextual {@link IQualifiedNameQueryEnvironment}.
* @param computedModuleName
* the module computed name
* @param sourceFragment
* the module source fragment
* @param acceleoValidationResult
* the (non-{@code null}) contextual {@link IAcceleoValidationResult}.
* @param acceleoElementToComplete
* the {@link EObject Acceleo AST element} to complete.
* @return the {@link List} of {@link AcceleoCompletionProposal}.
*/
protected List<AcceleoCompletionProposal> getProposals(IQualifiedNameQueryEnvironment queryEnvironment,
String computedModuleName, String sourceFragment,
IAcceleoValidationResult acceleoValidationResult, EObject acceleoElementToComplete) {
final List<AcceleoCompletionProposal> completionProposals = new ArrayList<>();
AcceleoAstCompletor acceleoSyntaxCompletor = new AcceleoAstCompletor(queryEnvironment,
acceleoValidationResult, newLine);
completionProposals.addAll(acceleoSyntaxCompletor.getCompletion(computedModuleName, sourceFragment,
acceleoElementToComplete));
return completionProposals;
}
/**
* Provides the Acceleo AST element to complete.
*
* @param acceleoAstResult
* the (non-{@code null}) {@link AcceleoAstResult} to complete.
* @return the Acceleo AST element to complete.
*/
private EObject getElementToComplete(AcceleoAstResult acceleoAstResult) {
final Error errorToComplete = getErrorToComplete(acceleoAstResult);
if (errorToComplete == null) {
final Module moduleToComplete = acceleoAstResult.getModule();
return moduleToComplete;
} else {
return errorToComplete;
}
}
/**
* Gets the {@link Error} to use for completion starting point. It's the first error whose
* {@link org.eclipse.acceleo.ASTNode#getEndPosition() end} is at the end of the {@link Module}.
*
* @param acceleoAstResultToComplete
* the (non-{@code null}) {@link AcceleoAstResult}.
* @return the {@link Error} to use as the completion starting point. {@code null} if there is
* {@link AcceleoAstResult#getErrors() no errors} in {@code acceleoAstResultToComplete}.
*/
private Error getErrorToComplete(AcceleoAstResult acceleoAstResultToComplete) {
Objects.requireNonNull(acceleoAstResultToComplete);
List<Error> errors = acceleoAstResultToComplete.getErrors();
if (errors.isEmpty()) {
return null;
} else {
Error currentError = errors.get(0);
int currentErrorEndPosition = acceleoAstResultToComplete.getEndPosition(currentError);
for (int i = 1; i < errors.size(); i++) {
final Error candidateError = errors.get(i);
int candidateErrorEnd = acceleoAstResultToComplete.getEndPosition(candidateError);
if (candidateErrorEnd > currentErrorEndPosition) {
currentError = candidateError;
currentErrorEndPosition = candidateErrorEnd;
}
}
return currentError;
}
}
}