-
Notifications
You must be signed in to change notification settings - Fork 29
/
validate.ts
275 lines (229 loc) · 9.58 KB
/
validate.ts
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/********************************************************************************
* Copyright (c) 2019-2020 EclipseSource and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/
import { inject, injectable, optional } from "inversify";
import {
Action,
Command,
CommandExecutionContext,
CommandReturn,
IActionDispatcher,
SIssue,
SIssueMarker,
SModelElement,
SModelRoot,
SParentElement,
TYPES
} from "sprotty";
import { EditorContextService } from "../../base/editor-context";
import { GLSP_TYPES } from "../../base/types";
import { Marker } from "../../utils/marker";
import { removeCssClasses } from "../../utils/smodel-util";
import { getSeverity } from "../hover/hover";
import { IFeedbackActionDispatcher, IFeedbackEmitter } from "../tool-feedback/feedback-action-dispatcher";
import { FeedbackCommand } from "../tool-feedback/model";
import { createSIssue, getOrCreateSIssueMarker, getSIssueMarker } from "./issue-marker";
/**
* Feedback emitter sending actions for visualizing model validation feedback and
* re-establishing this feedback visualization after the model has been updated.
*/
@injectable()
export class ValidationFeedbackEmitter implements IFeedbackEmitter {
@inject(GLSP_TYPES.IFeedbackActionDispatcher) protected feedbackActionDispatcher: IFeedbackActionDispatcher;
@inject(TYPES.IActionDispatcherProvider) protected actionDispatcher: () => Promise<IActionDispatcher>;
private registeredAction: MarkersAction;
private constructor() { }
/**
* Register the action that should be emitted for visualizing validation feedback.
* @param action the action that should be emitted when the model is updated and that will visualize the model validation feedback.
*/
registerValidationFeedbackAction(action: MarkersAction) {
// De-register old action responsible for applying markers and re-applying them when the model is updated
this.feedbackActionDispatcher.deregisterFeedback(this, []);
// Clear existing markers
if (this.registeredAction !== undefined) {
const deleteMarkersAction = new DeleteMarkersAction(this.registeredAction.markers);
this.actionDispatcher().then(dispatcher => dispatcher.dispatch(deleteMarkersAction));
}
// Register new action responsible for applying markers and re-applying them when the model is updated
this.feedbackActionDispatcher.registerFeedback(this, [action]);
this.registeredAction = action;
}
}
/**
* Action to set markers for a model
*/
export class SetMarkersAction implements MarkersAction {
constructor(public readonly markers: Marker[], public readonly kind = SetMarkersCommand.KIND) { }
}
export function isSetMarkersAction(action: Action): action is SetMarkersAction {
return SetMarkersCommand.KIND === action.kind && ('markers' in action);
}
/**
* Manages current markers for the outside of the GLSP.
*
* Typically this is rebound by the surrounding tool, e.g. Theia, to be aware of
* and propagate current markers.
*/
@injectable()
export abstract class ExternalMarkerManager {
languageLabel: string;
protected actionDispatcher?: IActionDispatcher;
connect(actionDispatcher: IActionDispatcher) {
this.actionDispatcher = actionDispatcher;
}
removeMarkers(markers: Marker[]) {
if (this.actionDispatcher) {
this.actionDispatcher.dispatch(new DeleteMarkersAction(markers));
}
}
abstract setMarkers(markers: Marker[], sourceUri?: string): void;
}
/**
* Command for handling `SetMarkersAction`
*/
@injectable()
export class SetMarkersCommand extends Command {
@inject(ValidationFeedbackEmitter) protected validationFeedbackEmitter: ValidationFeedbackEmitter;
@inject(ExternalMarkerManager) @optional() protected externalMarkerManager?: ExternalMarkerManager;
@inject(EditorContextService) protected editorContextService: EditorContextService;
static readonly KIND = 'setMarkers';
constructor(@inject(TYPES.Action) public action: SetMarkersAction) {
super();
}
async execute(context: CommandExecutionContext): Promise<SModelRoot> {
const markers: Marker[] = this.action.markers;
const uri = await this.editorContextService.getSourceUri();
if (this.externalMarkerManager) this.externalMarkerManager.setMarkers(markers, uri);
const applyMarkersAction: ApplyMarkersAction = new ApplyMarkersAction(markers);
this.validationFeedbackEmitter.registerValidationFeedbackAction(applyMarkersAction);
return context.root;
}
undo(context: CommandExecutionContext): CommandReturn {
return context.root;
}
redo(context: CommandExecutionContext): CommandReturn {
return this.execute(context);
}
}
/**
* Action to retrieve markers for a model
*/
export class RequestMarkersAction implements Action {
static readonly KIND = "requestMarkers";
constructor(public readonly elementsIDs: string[] = [], public readonly kind = RequestMarkersAction.KIND) { }
}
/**
* Interface for actions processing markers
*/
export interface MarkersAction extends Action {
readonly markers: Marker[];
}
/**
* Action for applying makers to a model
*/
@injectable()
export class ApplyMarkersAction implements MarkersAction {
constructor(public readonly markers: Marker[], public readonly kind = ApplyMarkersCommand.KIND) { }
}
/**
* Command for handling `ApplyMarkersAction`
*/
@injectable()
export class ApplyMarkersCommand extends FeedbackCommand {
static KIND = "applyMarkers";
readonly priority = 0;
constructor(@inject(TYPES.Action) protected action: ApplyMarkersAction) {
super();
}
execute(context: CommandExecutionContext): CommandReturn {
const markers: Marker[] = this.action.markers;
for (const marker of markers) {
const modelElement: SModelElement | undefined = context.root.index.getById(marker.elementId);
if (modelElement instanceof SParentElement) {
const issueMarker: SIssueMarker = getOrCreateSIssueMarker(modelElement);
const issue: SIssue = createSIssue(marker);
issueMarker.issues.push(issue);
addMaxSeverityCSSClassToIssueParent(modelElement, issueMarker);
}
}
return context.root;
}
undo(context: CommandExecutionContext): CommandReturn {
return context.root;
}
redo(context: CommandExecutionContext): CommandReturn {
return this.execute(context);
}
}
function addMaxSeverityCSSClassToIssueParent(modelElement: SParentElement, issueMarker: SIssueMarker) {
const maxSeverityCSSClass = getSeverity(issueMarker);
if (!modelElement.cssClasses) {
modelElement.cssClasses = [maxSeverityCSSClass];
} else {
modelElement.cssClasses = modelElement.cssClasses.filter((value: string) => !value.match('info|warning|error'));
modelElement.cssClasses.push(maxSeverityCSSClass);
}
}
function removeCSSClassFromIssueParent(modelElement: SParentElement, issueMarker: SIssueMarker) {
removeCssClasses(modelElement, [getSeverity(issueMarker)]);
}
/**
* Action for clearing makers of a model
*/
@injectable()
export class DeleteMarkersAction implements MarkersAction {
constructor(public readonly markers: Marker[], public readonly kind = DeleteMarkersCommand.KIND) { }
}
/**
* Command for handling `DeleteMarkersAction`
*/
@injectable()
export class DeleteMarkersCommand extends Command {
static KIND = "deleteMarkers";
constructor(@inject(TYPES.Action) protected action: DeleteMarkersAction) {
super();
}
execute(context: CommandExecutionContext): CommandReturn {
const markers: Marker[] = this.action.markers;
for (const marker of markers) {
const modelElement: SModelElement | undefined = context.root.index.getById(marker.elementId);
if (modelElement instanceof SParentElement) {
const issueMarker: SIssueMarker | undefined = getSIssueMarker(modelElement);
if (issueMarker !== undefined) {
removeCSSClassFromIssueParent(modelElement, issueMarker);
for (let index = 0; index < issueMarker.issues.length; ++index) {
const issue: SIssue = issueMarker.issues[index];
if (issue.message === marker.description) {
issueMarker.issues.splice(index--, 1);
}
}
if (issueMarker.issues.length === 0) {
modelElement.remove(issueMarker);
} else {
addMaxSeverityCSSClassToIssueParent(modelElement, issueMarker);
}
}
}
}
return context.root;
}
undo(context: CommandExecutionContext): CommandReturn {
return context.root;
}
redo(context: CommandExecutionContext): CommandReturn {
return this.execute(context);
}
}