|
3 | 3 | import * as path from 'path'; |
4 | 4 | import * as os from 'os'; |
5 | 5 | import * as fs from 'fs'; |
6 | | -import { workspace, extensions, ExtensionContext, window, StatusBarAlignment, commands, ViewColumn, Uri, CancellationToken, TextDocumentContentProvider, TextEditor, WorkspaceConfiguration, languages, IndentAction, ProgressLocation, InputBoxOptions, Selection, Position } from 'vscode'; |
7 | | -import { ExecuteCommandParams, ExecuteCommandRequest, LanguageClient, LanguageClientOptions, RevealOutputChannelOn, ServerOptions, Position as LSPosition, Location as LSLocation, StreamInfo } from 'vscode-languageclient'; |
| 6 | +import { workspace, extensions, ExtensionContext, window, StatusBarAlignment, commands, ViewColumn, Uri, CancellationToken, TextDocumentContentProvider, TextEditor, WorkspaceConfiguration, languages, IndentAction, ProgressLocation, InputBoxOptions, Selection, Position, EventEmitter } from 'vscode'; |
| 7 | +import { ExecuteCommandParams, ExecuteCommandRequest, LanguageClient, LanguageClientOptions, RevealOutputChannelOn, ServerOptions, Position as LSPosition, Location as LSLocation, StreamInfo, VersionedTextDocumentIdentifier } from 'vscode-languageclient'; |
8 | 8 | import { collectionJavaExtensions } from './plugin'; |
9 | 9 | import { prepareExecutable, awaitServerConnection } from './javaServerStarter'; |
10 | 10 | import * as requirements from './requirements'; |
11 | 11 | import { Commands } from './commands'; |
12 | | -import { StatusNotification, ClassFileContentsRequest, ProjectConfigurationUpdateRequest, MessageType, ActionableNotification, FeatureStatus, ActionableMessage, CompileWorkspaceRequest, CompileWorkspaceStatus, ProgressReportNotification, ExecuteClientCommandRequest, SendNotificationRequest } from './protocol'; |
| 12 | +import { StatusNotification, ClassFileContentsRequest, ProjectConfigurationUpdateRequest, MessageType, ActionableNotification, FeatureStatus, ActionableMessage, CompileWorkspaceRequest, CompileWorkspaceStatus, ProgressReportNotification, ExecuteClientCommandRequest, SendNotificationRequest, |
| 13 | +SourceAttachmentRequest, SourceAttachmentResult, SourceAttachmentAttribute } from './protocol'; |
13 | 14 | import { ExtensionAPI } from './extension.api'; |
14 | 15 | import * as net from 'net'; |
15 | 16 |
|
16 | 17 | let oldConfig; |
17 | 18 | let lastStatus; |
18 | 19 | let languageClient: LanguageClient; |
| 20 | +let jdtEventEmitter = new EventEmitter<Uri>(); |
19 | 21 | const cleanWorkspaceFileName = '.cleanWorkspace'; |
20 | 22 |
|
21 | 23 | export function activate(context: ExtensionContext): Promise<ExtensionAPI> { |
@@ -251,12 +253,55 @@ export function activate(context: ExtensionContext): Promise<ExtensionAPI> { |
251 | 253 | }); |
252 | 254 | }); |
253 | 255 |
|
| 256 | + commands.registerCommand(Commands.UPDATE_SOURCE_ATTACHMENT, async (classFileUri: Uri): Promise<boolean> => { |
| 257 | + const resolveRequest: SourceAttachmentRequest = { |
| 258 | + classFileUri: classFileUri.toString(), |
| 259 | + }; |
| 260 | + const resolveResult: SourceAttachmentResult = await <SourceAttachmentResult>commands.executeCommand(Commands.EXECUTE_WORKSPACE_COMMAND, Commands.RESOLVE_SOURCE_ATTACHMENT, JSON.stringify(resolveRequest)); |
| 261 | + if (resolveResult.errorMessage) { |
| 262 | + window.showErrorMessage(resolveResult.errorMessage); |
| 263 | + return false; |
| 264 | + } |
| 265 | + |
| 266 | + const attributes: SourceAttachmentAttribute = resolveResult.attributes || {}; |
| 267 | + const defaultPath = attributes.sourceAttachmentPath || attributes.jarPath; |
| 268 | + const sourceFileUris: Uri[] = await window.showOpenDialog({ |
| 269 | + defaultUri: defaultPath ? Uri.file(defaultPath) : null, |
| 270 | + openLabel: "Select Source File", |
| 271 | + canSelectFiles: true, |
| 272 | + canSelectFolders: false, |
| 273 | + canSelectMany: false, |
| 274 | + filters: { |
| 275 | + "Source files": ["jar", "zip"] |
| 276 | + }, |
| 277 | + }); |
| 278 | + |
| 279 | + if (sourceFileUris && sourceFileUris.length) { |
| 280 | + const updateRequest: SourceAttachmentRequest = { |
| 281 | + classFileUri: classFileUri.toString(), |
| 282 | + attributes: { |
| 283 | + ...attributes, |
| 284 | + sourceAttachmentPath: sourceFileUris[0].fsPath |
| 285 | + }, |
| 286 | + }; |
| 287 | + const updateResult: SourceAttachmentResult = await <SourceAttachmentResult>commands.executeCommand(Commands.EXECUTE_WORKSPACE_COMMAND, Commands.UPDATE_SOURCE_ATTACHMENT, JSON.stringify(updateRequest)); |
| 288 | + if (updateResult.errorMessage) { |
| 289 | + window.showErrorMessage(updateResult.errorMessage); |
| 290 | + return false; |
| 291 | + } |
| 292 | + |
| 293 | + // Notify jdt content provider to rerender the classfile contents. |
| 294 | + jdtEventEmitter.fire(classFileUri); |
| 295 | + return true; |
| 296 | + } |
| 297 | + }); |
| 298 | + |
254 | 299 | window.onDidChangeActiveTextEditor((editor) => { |
255 | 300 | toggleItem(editor, item); |
256 | 301 | }); |
257 | 302 |
|
258 | 303 | let provider: TextDocumentContentProvider = <TextDocumentContentProvider>{ |
259 | | - onDidChange: null, |
| 304 | + onDidChange: jdtEventEmitter.event, |
260 | 305 | provideTextDocumentContent: (uri: Uri, token: CancellationToken): Thenable<string> => { |
261 | 306 | return languageClient.sendRequest(ClassFileContentsRequest.type, { uri: uri.toString() }, token).then((v: string): string => { |
262 | 307 | return v || ''; |
|
0 commit comments