-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcleanWorkflow.ts
196 lines (180 loc) · 7.06 KB
/
cleanWorkflow.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
import { ApplyWorkspaceEditParams, Range, TextDocumentEdit, TextEdit } from "vscode-languageserver";
import { TextDocument } from "vscode-languageserver-textdocument";
import { ASTNode, PropertyASTNode, WorkflowDocument } from "../languageTypes";
import { GalaxyWorkflowLanguageServer } from "../server";
import { CustomCommand } from "./common";
import {
CleanWorkflowContentsParams,
CleanWorkflowContentsRequest,
CleanWorkflowContentsResult,
CleanWorkflowDocumentParams,
CleanWorkflowDocumentRequest,
CleanWorkflowDocumentResult,
} from "./requestsDefinitions";
/**
* A set of property names that are unrelated to the workflow logic.
* Usually used by other tools like the workflow editor.
*/
const CLEANABLE_PROPERTY_NAMES = new Set(["position", "uuid", "errors", "version"]);
/**
* Command for handling workflow `cleaning` requests.
* Supports both, direct contents (raw document text), and document uri requests
* for cleaning.
* When requesting with a document uri, the workflow document must be already registered in the server
* as a workflow document.
*/
export class CleanWorkflowCommand extends CustomCommand {
public static register(server: GalaxyWorkflowLanguageServer): CleanWorkflowCommand {
return new CleanWorkflowCommand(server);
}
constructor(server: GalaxyWorkflowLanguageServer) {
super(server);
}
protected listenToRequests(): void {
this.connection.onRequest(CleanWorkflowContentsRequest.type, (params) =>
this.onCleanWorkflowContentsRequest(params)
);
this.connection.onRequest(CleanWorkflowDocumentRequest.type, (params) =>
this.onCleanWorkflowDocumentRequest(params)
);
}
/**
* Processes a `CleanWorkflowContentsRequest` by returning the `clean` contents
* of a workflow document given the raw text contents of the workflow document.
* @param params The request parameters containing the raw text contents of the workflow
* @returns The `clean` contents of the workflow document
*/
private async onCleanWorkflowContentsRequest(
params: CleanWorkflowContentsParams
): Promise<CleanWorkflowContentsResult | undefined> {
const tempDocument = this.createTempWorkflowDocumentWithContents(params.contents);
const workflowDocument = this.languageService.parseWorkflowDocument(tempDocument);
if (workflowDocument) {
return await this.cleanWorkflowContentsResult(workflowDocument);
}
return undefined;
}
/**
* Applies the necessary text edits to the workflow document identified by the given URI to
* remove all the properties in the workflow that are unrelated to the essential workflow logic.
* @param params The request parameters containing the URI of the workflow document.
* @returns An error message if something went wrong
*/
private async onCleanWorkflowDocumentRequest(
params: CleanWorkflowDocumentParams
): Promise<CleanWorkflowDocumentResult> {
try {
const workflowDocument = this.workflowDocuments.get(params.uri);
if (workflowDocument) {
const edits = this.getTextEditsToCleanWorkflow(workflowDocument);
const editParams: ApplyWorkspaceEditParams = {
label: "Clean workflow",
edit: {
documentChanges: [
TextDocumentEdit.create(
{
uri: params.uri,
version: null,
},
edits
),
],
},
};
this.connection.workspace.applyEdit(editParams);
}
return { error: "" };
} catch (error) {
return { error: String(error) };
}
}
private getTextEditsToCleanWorkflow(workflowDocument: WorkflowDocument): TextEdit[] {
const nodesToRemove = this.getNonEssentialNodes(workflowDocument, CLEANABLE_PROPERTY_NAMES);
const changes: TextEdit[] = [];
nodesToRemove.forEach((node) => {
const range = this.getReplaceRange(workflowDocument.textDocument, node);
changes.push(TextEdit.replace(range, ""));
});
return changes;
}
private createTempWorkflowDocumentWithContents(contents: string) {
return TextDocument.create("temp://temp-workflow", "galaxyworkflow", 0, contents);
}
private async cleanWorkflowContentsResult(workflowDocument: WorkflowDocument): Promise<CleanWorkflowContentsResult> {
const nodesToRemove = this.getNonEssentialNodes(workflowDocument, CLEANABLE_PROPERTY_NAMES);
const contents = this.getCleanContents(workflowDocument.textDocument.getText(), nodesToRemove.reverse());
const result: CleanWorkflowContentsResult = {
contents: contents,
};
return result;
}
private getNonEssentialNodes(
workflowDocument: WorkflowDocument,
cleanablePropertyNames: Set<string>
): PropertyASTNode[] {
const root = workflowDocument.jsonDocument.root;
if (!root) {
return [];
}
const result: PropertyASTNode[] = [];
const toVisit: { node: ASTNode }[] = [{ node: root }];
let nextToVisit = 0;
const collectNonEssentialProperties = (node: ASTNode) => {
if (node.type === "array") {
node.items.forEach((node) => {
if (node) {
toVisit.push({ node });
}
});
} else if (node.type === "object") {
node.properties.forEach((property: PropertyASTNode) => {
const key = property.keyNode.value;
if (cleanablePropertyNames.has(key)) {
result.push(property);
}
if (property.valueNode) {
toVisit.push({ node: property.valueNode });
}
});
}
};
while (nextToVisit < toVisit.length) {
const next = toVisit[nextToVisit++];
collectNonEssentialProperties(next.node);
}
return result;
}
private getCleanContents(documentText: string, nodesToRemove: ASTNode[]): string {
const removeChunks: string[] = [];
let result = documentText;
nodesToRemove.forEach((node) => {
const rangeOffsets = this.getFullNodeRangeOffsets(documentText, node);
removeChunks.push(documentText.substring(rangeOffsets.start, rangeOffsets.end));
});
removeChunks.forEach((chunk) => {
result = result.replace(chunk, "");
});
return result;
}
/**
* Gets the range offsets (`start` and `end`) for a given syntax node including
* the blank spaces/indentation before and after the node and possible ending comma.
* @param documentText The full workflow document text
* @param node The syntax node
* @returns The `start` and `end` offsets for the given syntax node
*/
private getFullNodeRangeOffsets(documentText: string, node: ASTNode) {
let startPos = node.offset;
let endPos = node.offset + node.length;
startPos = documentText.lastIndexOf("\n", startPos);
if (documentText.charAt(endPos) === ",") {
endPos++;
}
return { start: startPos, end: endPos };
}
private getReplaceRange(document: TextDocument, node: ASTNode): Range {
const documentText = document.getText();
const rangeOffsets = this.getFullNodeRangeOffsets(documentText, node);
return Range.create(document.positionAt(rangeOffsets.start), document.positionAt(rangeOffsets.end));
}
}