|
| 1 | +/*--------------------------------------------------------- |
| 2 | + * Copyright (C) Microsoft Corporation. All rights reserved. |
| 3 | + *--------------------------------------------------------*/ |
| 4 | + |
1 | 5 | import vscode = require('vscode'); |
| 6 | +import { IFeature } from '../feature'; |
2 | 7 | import { LanguageClient, RequestType, NotificationType } from 'vscode-languageclient'; |
3 | 8 |
|
4 | 9 | export namespace EvaluateRequest { |
@@ -138,44 +143,66 @@ function onInputEntered(responseText: string): ShowInputPromptResponseBody { |
138 | 143 | } |
139 | 144 | } |
140 | 145 |
|
141 | | -export function registerConsoleCommands(client: LanguageClient): void { |
| 146 | +export class ConsoleFeature implements IFeature { |
| 147 | + private command: vscode.Disposable; |
| 148 | + private languageClient: LanguageClient; |
| 149 | + private consoleChannel: vscode.OutputChannel; |
| 150 | + |
| 151 | + constructor() { |
| 152 | + this.command = |
| 153 | + vscode.commands.registerCommand('PowerShell.RunSelection', () => { |
| 154 | + if (this.languageClient === undefined) { |
| 155 | + // TODO: Log error message |
| 156 | + return; |
| 157 | + } |
| 158 | + |
| 159 | + var editor = vscode.window.activeTextEditor; |
| 160 | + var selectionRange: vscode.Range = undefined; |
| 161 | + |
| 162 | + if (!editor.selection.isEmpty) { |
| 163 | + selectionRange = |
| 164 | + new vscode.Range( |
| 165 | + editor.selection.start, |
| 166 | + editor.selection.end); |
| 167 | + } |
| 168 | + else { |
| 169 | + selectionRange = editor.document.lineAt(editor.selection.start.line).range; |
| 170 | + } |
| 171 | + |
| 172 | + this.languageClient.sendRequest(EvaluateRequest.type, { |
| 173 | + expression: editor.document.getText(selectionRange) |
| 174 | + }); |
| 175 | + }); |
| 176 | + |
| 177 | + this.consoleChannel = vscode.window.createOutputChannel("PowerShell Output"); |
| 178 | + } |
142 | 179 |
|
143 | | - vscode.commands.registerCommand('PowerShell.RunSelection', () => { |
144 | | - var editor = vscode.window.activeTextEditor; |
145 | | - var selectionRange: vscode.Range = undefined; |
| 180 | + public setLanguageClient(languageClient: LanguageClient) { |
| 181 | + this.languageClient = languageClient; |
146 | 182 |
|
147 | | - if (!editor.selection.isEmpty) { |
148 | | - selectionRange = |
149 | | - new vscode.Range( |
150 | | - editor.selection.start, |
151 | | - editor.selection.end); |
152 | | - } |
153 | | - else { |
154 | | - selectionRange = editor.document.lineAt(editor.selection.start.line).range; |
155 | | - } |
| 183 | + this.languageClient.onRequest( |
| 184 | + ShowChoicePromptRequest.type, |
| 185 | + promptDetails => showChoicePrompt(promptDetails, this.languageClient)); |
156 | 186 |
|
157 | | - client.sendRequest(EvaluateRequest.type, { |
158 | | - expression: editor.document.getText(selectionRange) |
159 | | - }); |
160 | | - }); |
| 187 | + this.languageClient.onRequest( |
| 188 | + ShowInputPromptRequest.type, |
| 189 | + promptDetails => showInputPrompt(promptDetails, this.languageClient)); |
161 | 190 |
|
162 | | - var consoleChannel = vscode.window.createOutputChannel("PowerShell Output"); |
163 | | - client.onNotification(OutputNotification.type, (output) => { |
164 | | - var outputEditorExist = vscode.window.visibleTextEditors.some((editor) => { |
165 | | - return editor.document.languageId == 'Log' |
166 | | - }); |
167 | | - if (!outputEditorExist) |
168 | | - consoleChannel.show(vscode.ViewColumn.Three); |
169 | | - consoleChannel.append(output.output); |
170 | | - }); |
| 191 | + this.languageClient.onNotification(OutputNotification.type, (output) => { |
| 192 | + var outputEditorExist = vscode.window.visibleTextEditors.some((editor) => { |
| 193 | + return editor.document.languageId == 'Log' |
| 194 | + }); |
171 | 195 |
|
172 | | - var t: Thenable<ShowChoicePromptResponseBody>; |
| 196 | + if (!outputEditorExist) { |
| 197 | + this.consoleChannel.show(vscode.ViewColumn.Three); |
| 198 | + } |
173 | 199 |
|
174 | | - client.onRequest( |
175 | | - ShowChoicePromptRequest.type, |
176 | | - promptDetails => showChoicePrompt(promptDetails, client)); |
| 200 | + this.consoleChannel.append(output.output); |
| 201 | + }); |
| 202 | + } |
177 | 203 |
|
178 | | - client.onRequest( |
179 | | - ShowInputPromptRequest.type, |
180 | | - promptDetails => showInputPrompt(promptDetails, client)); |
| 204 | + public dispose() { |
| 205 | + this.command.dispose(); |
| 206 | + this.consoleChannel.dispose(); |
| 207 | + } |
181 | 208 | } |
0 commit comments