-
Notifications
You must be signed in to change notification settings - Fork 3
/
EclipseAcceleoLanguageServerContext.java
180 lines (156 loc) · 6.16 KB
/
EclipseAcceleoLanguageServerContext.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
/*******************************************************************************
* Copyright (c) 2020, 2023 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.ide.ui;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.Objects;
import org.eclipse.acceleo.aql.ide.AcceleoPlugin;
import org.eclipse.acceleo.aql.ls.IAcceleoLanguageServerContext;
import org.eclipse.acceleo.aql.ls.services.workspace.AcceleoProject;
import org.eclipse.acceleo.aql.ls.services.workspace.AcceleoWorkspace;
import org.eclipse.acceleo.aql.parser.AcceleoParser;
import org.eclipse.acceleo.aql.parser.ModuleLoader;
import org.eclipse.acceleo.query.ide.QueryPlugin;
import org.eclipse.acceleo.query.ide.runtime.impl.namespace.workspace.Synchronizer;
import org.eclipse.acceleo.query.runtime.namespace.IQualifiedNameResolver;
import org.eclipse.acceleo.query.runtime.namespace.workspace.IQueryWorkspace;
import org.eclipse.acceleo.query.runtime.namespace.workspace.IQueryWorkspaceQualifiedNameResolver;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
/**
* Eclipse-specific implementation of {@link IAcceleoLanguageServerContext}.
*
* @author Florent Latombe
*/
public class EclipseAcceleoLanguageServerContext implements IAcceleoLanguageServerContext {
/**
* The {@link Synchronizer} for Acceleo.
*
* @author <a href="mailto:yvan.lussaud@obeo.fr">Yvan Lussaud</a>
*/
private static class AcceleoSynchronizer extends Synchronizer<AcceleoProject> {
AcceleoSynchronizer(IWorkspace eclipseWorkspace, IQueryWorkspace<AcceleoProject> queryWorkspace) {
super(eclipseWorkspace, queryWorkspace);
}
@Override
protected AcceleoProject createProject(IQueryWorkspace<AcceleoProject> acceleoWorkspace,
IProject eclipseProject) {
return new AcceleoProject(eclipseProject.getName(), (AcceleoWorkspace)acceleoWorkspace);
}
public AcceleoWorkspace getAcceleoWorkspace() {
return (AcceleoWorkspace)getQueryWorkspace();
}
}
/**
* The size of the buffer we use to read {@link IFile Acceleo documents}.
*/
private static final int BUFFER_SIZE = 8 * 1024;
/**
* The client {@link IWorkspace}.
*/
private final IWorkspace clientWorkspace;
/**
* The {@link AcceleoSynchronizer}.
*/
private final AcceleoSynchronizer synchronizer;
/**
* The constructor.
*
* @param clientWorkspace
* the (non-{@code null}) contextual {@link IWorkspace}.
*/
public EclipseAcceleoLanguageServerContext(IWorkspace clientWorkspace) {
this.clientWorkspace = Objects.requireNonNull(clientWorkspace);
final AcceleoWorkspace acceleoWorkspace = new AcceleoWorkspace(getAcceleoWorkspaceName(
clientWorkspace), this);
synchronizer = new AcceleoSynchronizer(clientWorkspace, acceleoWorkspace);
synchronizer.synchronize();
}
/**
* Disposes this context.
*/
public void dispose() {
synchronizer.dispose();
}
@Override
public AcceleoWorkspace getWorkspace() {
return synchronizer.getAcceleoWorkspace();
}
@Override
public String getResourceContents(URI resource) {
final IFile file = clientWorkspace.getRoot().getFileForLocation(new Path(resource.getPath()
.toString()));
try {
InputStream inputStream = file.getContents();
ByteArrayOutputStream result = new ByteArrayOutputStream();
byte[] buffer = new byte[BUFFER_SIZE];
int length;
while ((length = inputStream.read(buffer)) != -1) {
result.write(buffer, 0, length);
}
inputStream.close();
return result.toString(file.getCharset());
} catch (IOException | CoreException exception) {
Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
"There was an issue while reading the contents of file " + file.getLocation().toString(),
exception));
}
return null;
}
@Override
public AcceleoProject getProject(AcceleoWorkspace workspace, URI resource) {
final AcceleoProject res;
synchronized(synchronizer) {
final IFile file = clientWorkspace.getRoot().getFileForLocation(new Path(resource.getPath()
.toString()));
final IProject eclipseProject = file.getProject();
final AcceleoProject existingProject = synchronizer.getProject(eclipseProject);
if (existingProject != null) {
res = existingProject;
} else {
synchronizer.add(file.getProject());
res = synchronizer.getProject(eclipseProject);
}
}
return res;
}
@Override
public IQueryWorkspaceQualifiedNameResolver createResolver(AcceleoProject acceleoProject) {
Objects.nonNull(acceleoProject);
final IProject eclipseProject = synchronizer.getProject(acceleoProject);
final IQualifiedNameResolver resolver = QueryPlugin.getPlugin().createQualifiedNameResolver(
AcceleoPlugin.getPlugin().getClass().getClassLoader(), eclipseProject,
AcceleoParser.QUALIFIER_SEPARATOR, true);
resolver.addLoader(new ModuleLoader(new AcceleoParser(), null));
resolver.addLoader(QueryPlugin.getPlugin().createJavaLoader(AcceleoParser.QUALIFIER_SEPARATOR, true));
return QueryPlugin.getPlugin().createWorkspaceQualifiedNameResolver(eclipseProject, resolver,
synchronizer);
}
/**
* Provides the {@link String name} to use for the created {@link AcceleoWorkspace} corresponding to an
* {@link IWorkspace}.
*
* @param clientWorkspace
* the (non-{@code null}) source {@link IWorkspace}.
* @return the {@link String name} intended for the {@link AcceleoWorkspace} corresponding to
* {@code clientWorkspace}.
*/
private static String getAcceleoWorkspaceName(IWorkspace clientWorkspace) {
return "AcceleoWorkspace[" + clientWorkspace.getRoot().getLocationURI().toString() + "]";
}
}