|
| 1 | +import * as requirements from './requirements'; |
| 2 | + |
| 3 | +import { DidChangeConfigurationNotification, LanguageClientOptions } from 'vscode-languageclient'; |
| 4 | +import { LanguageClient } from 'vscode-languageclient/node'; |
| 5 | +import { ExtensionContext, commands, window, workspace, Uri } from 'vscode'; |
| 6 | +import { prepareExecutable } from './javaServerStarter'; |
| 7 | +import { TextEncoder } from 'util'; |
| 8 | + |
| 9 | +export function connectToQuteLS(context: ExtensionContext) { |
| 10 | + registerVSCodeCommands(context); |
| 11 | + |
| 12 | + return requirements.resolveRequirements().then(requirements => { |
| 13 | + const clientOptions: LanguageClientOptions = { |
| 14 | + documentSelector: [ |
| 15 | + { scheme: 'file', language: 'qute-html' }, |
| 16 | + { scheme: 'file', language: 'qute-json' }, |
| 17 | + { scheme: 'file', language: 'qute-yaml' }, |
| 18 | + { scheme: 'file', language: 'qute-txt' }, |
| 19 | + { scheme: 'untitled', language: 'qute-html' }, |
| 20 | + { scheme: 'vscode-notebook-cell', language: 'qute-html' }, |
| 21 | + { scheme: 'file', language: 'java' } |
| 22 | + ], |
| 23 | + // wrap with key 'settings' so it can be handled same a DidChangeConfiguration |
| 24 | + initializationOptions: { |
| 25 | + settings: getQuteSettings() |
| 26 | + }, |
| 27 | + synchronize: { |
| 28 | + // preferences starting with these will trigger didChangeConfiguration |
| 29 | + configurationSection: ['quarkus', '[qute]'] |
| 30 | + }, |
| 31 | + middleware: { |
| 32 | + workspace: { |
| 33 | + didChangeConfiguration: () => { |
| 34 | + quteLanguageClient.sendNotification(DidChangeConfigurationNotification.type, { settings: getQuteSettings() }); |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + }; |
| 39 | + |
| 40 | + function bindQuteRequest(request: string) { |
| 41 | + quteLanguageClient.onRequest(request, async (params: any) => |
| 42 | + <any>await commands.executeCommand("java.execute.workspaceCommand", request, params) |
| 43 | + ); |
| 44 | + } |
| 45 | + |
| 46 | + function bindQuteNotification(notification: string) { |
| 47 | + context.subscriptions.push(commands.registerCommand(notification, (event: any) => { |
| 48 | + quteLanguageClient.sendNotification(notification, event); |
| 49 | + })); |
| 50 | + } |
| 51 | + |
| 52 | + const serverOptions = prepareExecutable(requirements); |
| 53 | + const quteLanguageClient = new LanguageClient('qute', 'Qute Support', serverOptions, clientOptions); |
| 54 | + context.subscriptions.push(quteLanguageClient.start()); |
| 55 | + return quteLanguageClient.onReady().then(() => { |
| 56 | + bindQuteRequest('qute/template/project'); |
| 57 | + bindQuteRequest('qute/template/projectDataModel'); |
| 58 | + bindQuteRequest('qute/template/javaClasses'); |
| 59 | + bindQuteRequest('qute/template/resolvedJavaClass'); |
| 60 | + bindQuteRequest('qute/template/javaDefinition'); |
| 61 | + bindQuteNotification('qute/dataModelChanged'); |
| 62 | + bindQuteRequest('qute/java/codeLens'); |
| 63 | + } |
| 64 | + ) |
| 65 | + }); |
| 66 | +} |
| 67 | + |
| 68 | +/** |
| 69 | + * Returns a json object with key 'quarkus' and a json object value that |
| 70 | + * holds all quarkus. settings. |
| 71 | + * |
| 72 | + * Returns: { |
| 73 | + * 'quarkus': {...} |
| 74 | + * } |
| 75 | + */ |
| 76 | +function getQuteSettings(): JSON { |
| 77 | + const configQuarkus = workspace.getConfiguration().get('quarkus'); |
| 78 | + let quarkus; |
| 79 | + if (!configQuarkus) { // Set default preferences if not provided |
| 80 | + const defaultValue = |
| 81 | + { |
| 82 | + qute: { |
| 83 | + |
| 84 | + } |
| 85 | + }; |
| 86 | + quarkus = defaultValue; |
| 87 | + } else { |
| 88 | + const x = JSON.stringify(configQuarkus); // configQuarkus is not a JSON type |
| 89 | + quarkus = { quarkus: JSON.parse(x) }; |
| 90 | + } |
| 91 | + return quarkus; |
| 92 | +} |
| 93 | + |
| 94 | +function registerVSCodeCommands(context: ExtensionContext) { |
| 95 | + registerOpenUriCommand(context); |
| 96 | + registerGenerateTemplateFileCommand(context); |
| 97 | +} |
| 98 | + |
| 99 | + |
| 100 | +function registerOpenUriCommand(context: ExtensionContext) { |
| 101 | + context.subscriptions.push(commands.registerCommand('qute.command.open.uri', async (uri?: string) => { |
| 102 | + commands.executeCommand('vscode.open', Uri.parse(uri)); |
| 103 | + })); |
| 104 | +} |
| 105 | + |
| 106 | +function registerGenerateTemplateFileCommand(context: ExtensionContext) { |
| 107 | + context.subscriptions.push(commands.registerCommand('qute.command.generate.template.file', async (info?) => { |
| 108 | + const templateContent: string = await commands.executeCommand('qute.command.generate.template.content', info); |
| 109 | + const uri = info.templateFileUri; |
| 110 | + const fileUri = Uri.parse(uri); |
| 111 | + await workspace.fs.writeFile(fileUri, new TextEncoder().encode(templateContent)); |
| 112 | + window.showTextDocument(fileUri, { preview: false }); |
| 113 | + })); |
| 114 | +} |
0 commit comments