-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathextension.ts
252 lines (228 loc) · 7.23 KB
/
extension.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
import { resolve } from "path";
import { readFileSync } from "fs";
import {
workspace,
ExtensionContext,
StatusBarItem,
window,
StatusBarAlignment,
commands,
env,
Uri,
languages,
TextDocument,
TextEdit,
} from "vscode";
import {
LanguageClient,
LanguageClientOptions,
ServerOptions,
TransportKind,
} from "vscode-languageclient/node";
import { LogLevel } from "@vscode-logging/types";
import {
SERVER_PATH,
ServerInitializationOptions,
} from "@ui5-language-assistant/language-server";
import {
COMMAND_OPEN_DEMOKIT,
LOGGING_LEVEL_CONFIG_PROP,
MANIFEST_SCHEMA,
} from "./constants";
import { getManifestSchemaProvider } from "./manifest-schema-provider";
import {
getLocalUrl,
tryFetch,
isXMLView,
} from "@ui5-language-assistant/logic-utils";
import { formatDocument, formatRange } from "./formatter";
import {
bindingLegend,
bindingSemanticTokensProvider,
} from "./binding-semantic-token-provider";
import { OPEN_FRAMEWORK } from "@ui5-language-assistant/constant";
type UI5Model = {
url: string;
framework: string;
version: string;
isFallback: boolean;
isIncorrectVersion?: boolean;
};
let client: LanguageClient;
let statusBarItem: StatusBarItem;
let currentModel: UI5Model | undefined;
function init(context: ExtensionContext): void {
// create the StatusBarItem which displays the used UI5 version
statusBarItem = createStatusBarItem(context);
// create the LanguageClient (+Server)
client = createLanguageClient(context);
client.start().then(() => {
// show/hide and update the status bar
client.onNotification(
"UI5LanguageAssistant/ui5Model",
async (model: UI5Model): Promise<void> => await updateCurrentModel(model)
);
client.onNotification(
"UI5LanguageAssistant/context-error",
(error: Error) => handleContextError(error)
);
});
}
export async function activate(context: ExtensionContext): Promise<void> {
// complete initialization task asynchronously
init(context);
// register semantic token provider
context.subscriptions.push(
languages.registerDocumentSemanticTokensProvider(
{ language: "xml" },
bindingSemanticTokensProvider,
bindingLegend
)
);
window.onDidChangeActiveTextEditor(async () => {
await updateCurrentModel(undefined);
});
context.subscriptions.push(
languages.registerDocumentFormattingEditProvider("xml", {
provideDocumentFormattingEdits(document: TextDocument): TextEdit[] {
if (isXMLView(document.uri.fsPath)) {
return formatDocument(document);
}
return [];
},
})
);
context.subscriptions.push(
languages.registerDocumentRangeFormattingEditProvider("xml", {
provideDocumentRangeFormattingEdits(
document,
range,
options
): TextEdit[] {
if (isXMLView(document.uri.fsPath)) {
return formatRange(document, range, options);
}
return [];
},
})
);
context.subscriptions.push(
workspace.registerTextDocumentContentProvider(
MANIFEST_SCHEMA,
await getManifestSchemaProvider(context)
)
);
}
function createLanguageClient(context: ExtensionContext): LanguageClient {
const debugOptions = { execArgv: ["--nolazy", "--inspect=6009"] };
const serverOptions: ServerOptions = {
run: { module: SERVER_PATH, transport: TransportKind.ipc },
debug: {
module: SERVER_PATH,
transport: TransportKind.ipc,
options: debugOptions,
},
};
const meta = JSON.parse(
readFileSync(resolve(context.extensionPath, "package.json"), "utf8")
);
const logLevel = workspace.getConfiguration().get(LOGGING_LEVEL_CONFIG_PROP);
const initializationOptions: ServerInitializationOptions = {
modelCachePath: context.globalStoragePath,
publisher: meta.publisher,
name: meta.name,
// validation of the logLevel value is done on the language server process.
logLevel: logLevel as LogLevel,
};
const clientOptions: LanguageClientOptions = {
documentSelector: [{ scheme: "file", pattern: "**/*.{view,fragment}.xml" }],
synchronize: {
fileEvents: [
workspace.createFileSystemWatcher("**/manifest.json"),
workspace.createFileSystemWatcher("**/ui5.yaml"),
workspace.createFileSystemWatcher("**/*.xml"),
workspace.createFileSystemWatcher("**/*.cds"),
workspace.createFileSystemWatcher("**/package.json"),
],
},
outputChannelName: meta.displayName,
initializationOptions: initializationOptions,
};
return new LanguageClient(
"UI5LanguageAssistant",
"UI5 Language Assistant",
serverOptions,
clientOptions
);
}
function createStatusBarItem(context: ExtensionContext): StatusBarItem {
// create and register the command to open the SAPUI5 demokit
context.subscriptions.push(
commands.registerCommand(COMMAND_OPEN_DEMOKIT, () => {
env.openExternal(Uri.parse(`${currentModel?.url}`));
})
);
// create a statusbar item to display the currently used UI5 version
statusBarItem = window.createStatusBarItem(StatusBarAlignment.Right, 100);
statusBarItem.command = COMMAND_OPEN_DEMOKIT;
return statusBarItem;
}
async function updateCurrentModel(model: UI5Model | undefined): Promise<void> {
currentModel = model;
if (statusBarItem) {
if (currentModel) {
let tooltipText = `${currentModel.framework} Version (XMLView Editor)`;
let version = currentModel.version;
if (currentModel.isFallback) {
tooltipText += ` minUI5 version not found in manifest.json. Fallback to UI5 ${currentModel.version}`;
version = `Fallback: ${currentModel.version}`;
}
if (currentModel.isIncorrectVersion) {
tooltipText += ` minUI5 version found in manifest.json is out of maintenance or not supported by UI5 Language Assistant. Using fallback to UI5 ${currentModel.version}.`;
version = `Fallback: ${currentModel.version}`;
}
const localUrl = getLocalUrl(
version,
workspace.getConfiguration().get("UI5LanguageAssistant")
);
if (localUrl) {
const response = await tryFetch(localUrl);
if (response) {
version = `${version} (local)`;
tooltipText =
"Alternative (local) SAPUI5 web server is defined in user or workspace settings. Using SAPUI5 version fetched from the local server";
}
}
statusBarItem.tooltip = tooltipText;
statusBarItem.text = `$(notebook-mimetype) ${version}${
currentModel.framework === OPEN_FRAMEWORK ? "'" : ""
}`;
statusBarItem.show();
} else {
statusBarItem.hide();
}
}
}
let showedOnce = false;
function handleContextError(error: Error & { code?: string }) {
if (showedOnce) {
return;
}
showedOnce = true;
if (error.code) {
window.showErrorMessage(
"[SAPUI5 SDK](https://tools.hana.ondemand.com/#sapui5) is not accessible. Connect to the internet or setup local web server for offline work."
);
} else {
window.showErrorMessage(
"An error has occurred building context. Please open an [issue](https://github.com/SAP/ui5-language-assistant/issues)"
);
}
}
export async function deactivate(): Promise<Thenable<void>> {
if (!client) {
return undefined;
}
await updateCurrentModel(undefined);
return client.stop();
}