| 10 | import {LanguageClient} from 'vscode-languageclient/node'; |
| 11 | |
| 12 | export function registerCommands( |
| 13 | context: vscode.ExtensionContext, |
| 14 | client: LanguageClient, |
| 15 | outputChannel: vscode.OutputChannel, |
| 16 | ) { |
| 17 | const TCB_HIGHLIGHT_DECORATION = vscode.window.createTextEditorDecorationType({ |
| 18 | backgroundColor: new vscode.ThemeColor('editor.selectionHighlightBackground'), |
| 19 | }); |
| 20 | |
| 21 | const tcbProvider = new TcbContentProvider(); |
| 22 | context.subscriptions.push( |
| 23 | vscode.workspace.registerTextDocumentContentProvider('ng', tcbProvider), |
| 24 | ); |
| 25 | |
| 26 | context.subscriptions.push( |
| 27 | vscode.commands.registerTextEditorCommand('angular.getTemplateTcb', async (textEditor) => { |
| 28 | tcbProvider.clear(); |
| 29 | try { |
| 30 | outputChannel.appendLine('Requesting TCB from server...'); |
| 31 | const response = (await client.sendRequest('angular/getTcb', { |
| 32 | textDocument: client.code2ProtocolConverter.asTextDocumentIdentifier(textEditor.document), |
| 33 | position: client.code2ProtocolConverter.asPosition(textEditor.selection.active), |
| 34 | })) as {uri: string; content: string; selections: any[]} | null; |
| 35 | |
| 36 | if (!response) { |
| 37 | vscode.window.showInformationMessage('No TCB available for this position.'); |
| 38 | return; |
| 39 | } |
| 40 | |
| 41 | outputChannel.appendLine( |
| 42 | `Received TCB response. Content length: ${response.content.length}`, |
| 43 | ); |
| 44 | |
| 45 | const tcbUri = vscode.Uri.parse(response.uri).with({ |
| 46 | scheme: 'ng', |
| 47 | }); |
| 48 | tcbProvider.update(tcbUri, response.content); |
| 49 | const editor = await vscode.window.showTextDocument(tcbUri, { |
| 50 | viewColumn: vscode.ViewColumn.Beside, |
| 51 | preserveFocus: true, |
| 52 | }); |
| 53 | |
| 54 | const selections = response.selections.map((s: any) => { |
| 55 | return new vscode.Range( |
| 56 | new vscode.Position(s.start.line, s.start.character), |
| 57 | new vscode.Position(s.end.line, s.end.character), |
| 58 | ); |
| 59 | }); |
| 60 | |
| 61 | editor.setDecorations(TCB_HIGHLIGHT_DECORATION, selections); |
| 62 | } catch (err) { |
| 63 | vscode.window.showErrorMessage(`Failed to get TCB: ${err}`); |
| 64 | } |
| 65 | }), |
| 66 | ); |
| 67 | } |
| 68 | |
| 69 | class TcbContentProvider implements vscode.TextDocumentContentProvider { |