|
| 1 | +/* -------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + * ------------------------------------------------------------------------------------------ */ |
| 5 | + |
| 6 | +import * as vscode from 'vscode'; |
| 7 | + |
| 8 | +import { |
| 9 | + ClientCapabilities, StatRequest, ReadFileRequest, ReadDirectoryRequest |
| 10 | +} from 'vscode-languageserver-protocol'; |
| 11 | + |
| 12 | +import { StaticFeature, FeatureClient, FeatureState } from './features'; |
| 13 | + |
| 14 | +export interface FileSystemReadFileSignature { |
| 15 | + (this: void, uri: vscode.Uri, encoding?: string): Promise<string | null>; |
| 16 | +} |
| 17 | + |
| 18 | +export interface FileSystemStatSignature { |
| 19 | + (this: void, uri: vscode.Uri): Promise<vscode.FileStat | null>; |
| 20 | +} |
| 21 | + |
| 22 | +export interface FileSystemReadDirectorySignature { |
| 23 | + (this: void, uri: vscode.Uri): Promise<[string, vscode.FileType][] | null>; |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * File system middleware. |
| 28 | + * |
| 29 | + * @since 3.19.0 |
| 30 | + */ |
| 31 | +export interface FileSystemMiddleware { |
| 32 | + fs?: { |
| 33 | + stat?: (this: void, uri: vscode.Uri, next: FileSystemStatSignature) => vscode.ProviderResult<vscode.FileStat | null>; |
| 34 | + readFile?: (this: void, uri: vscode.Uri, encoding: string | undefined, next: FileSystemReadFileSignature) => vscode.ProviderResult<string | null>; |
| 35 | + readDirectory?: (this: void, uri: vscode.Uri, next: FileSystemReadDirectorySignature) => vscode.ProviderResult<[string, vscode.FileType][] | null>; |
| 36 | + }; |
| 37 | +} |
| 38 | + |
| 39 | +interface WorkspaceFileSystemMiddleware { |
| 40 | + workspace?: FileSystemMiddleware; |
| 41 | +} |
| 42 | + |
| 43 | +// TextDecoder is available in all supported environments, but we can't use it directly |
| 44 | +// because that would require us to use the dom or webworker lib, which we don't want to do. |
| 45 | +// So we declare it here to make TypeScript happy. |
| 46 | +declare class TextDecoder { |
| 47 | + constructor(label?: string, options?: { fatal?: boolean; ignoreBOM?: boolean }); |
| 48 | + decode(input?: Uint8Array): string; |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * file system feature. From server to client. |
| 53 | + */ |
| 54 | +export class FileSystemFeature implements StaticFeature { |
| 55 | + |
| 56 | + private readonly _client: FeatureClient<WorkspaceFileSystemMiddleware>; |
| 57 | + |
| 58 | + constructor(client: FeatureClient<WorkspaceFileSystemMiddleware>) { |
| 59 | + this._client = client; |
| 60 | + } |
| 61 | + |
| 62 | + getState(): FeatureState { |
| 63 | + return { kind: 'static' }; |
| 64 | + } |
| 65 | + |
| 66 | + public fillClientCapabilities(capabilities: ClientCapabilities): void { |
| 67 | + capabilities.workspace ??= {}; |
| 68 | + capabilities.workspace.fileSystem ??= {}; |
| 69 | + capabilities.workspace.fileSystem.stat = true; |
| 70 | + capabilities.workspace.fileSystem.readFile = true; |
| 71 | + capabilities.workspace.fileSystem.readDirectory = true; |
| 72 | + } |
| 73 | + |
| 74 | + public initialize(): void { |
| 75 | + const client = this._client; |
| 76 | + client.onRequest(StatRequest.type, async (params) => { |
| 77 | + const paramsUri = this._client.protocol2CodeConverter.asUri(params.uri); |
| 78 | + const fileStat: FileSystemStatSignature = async (uri) => { |
| 79 | + try { |
| 80 | + const vstat = await vscode.workspace.fs.stat(uri); |
| 81 | + return vstat; |
| 82 | + } catch { |
| 83 | + return null; |
| 84 | + } |
| 85 | + }; |
| 86 | + const middleware = client.middleware.workspace; |
| 87 | + const result = await (middleware?.fs?.stat |
| 88 | + ? middleware.fs.stat(paramsUri, fileStat) |
| 89 | + : fileStat(paramsUri)); |
| 90 | + return result |
| 91 | + ? this._client.code2ProtocolConverter.asFileStat(result) |
| 92 | + : null; |
| 93 | + }); |
| 94 | + client.onRequest(ReadFileRequest.type, async (params) => { |
| 95 | + const paramsUri = this._client.protocol2CodeConverter.asUri(params.uri); |
| 96 | + const fileRead: FileSystemReadFileSignature = async (uri, encoding) => { |
| 97 | + try { |
| 98 | + const bytes = await vscode.workspace.fs.readFile(uri); |
| 99 | + const decoder = new TextDecoder(encoding || 'utf-8'); |
| 100 | + return decoder.decode(bytes); |
| 101 | + } catch { |
| 102 | + return null; |
| 103 | + } |
| 104 | + }; |
| 105 | + const middleware = client.middleware.workspace; |
| 106 | + const result = await (middleware?.fs?.readFile |
| 107 | + ? middleware.fs.readFile(paramsUri, params.encoding, fileRead) |
| 108 | + : fileRead(paramsUri, params.encoding)); |
| 109 | + if (result === undefined || result === null) { |
| 110 | + return null; |
| 111 | + } |
| 112 | + return { text: result }; |
| 113 | + }); |
| 114 | + client.onRequest(ReadDirectoryRequest.type, async (params) => { |
| 115 | + const paramsUri = this._client.protocol2CodeConverter.asUri(params.uri); |
| 116 | + const directoryRead: FileSystemReadDirectorySignature = async (uri) => { |
| 117 | + try { |
| 118 | + const entries = await vscode.workspace.fs.readDirectory(uri); |
| 119 | + return entries; |
| 120 | + } catch { |
| 121 | + return null; |
| 122 | + } |
| 123 | + }; |
| 124 | + const middleware = client.middleware.workspace; |
| 125 | + const result = await (middleware?.fs?.readDirectory |
| 126 | + ? middleware.fs.readDirectory(paramsUri, directoryRead) |
| 127 | + : directoryRead(paramsUri)); |
| 128 | + if (result === undefined || result === null) { |
| 129 | + return null; |
| 130 | + } |
| 131 | + const entries = result.map(this._client.code2ProtocolConverter.asDirectoryEntry); |
| 132 | + return entries; |
| 133 | + }); |
| 134 | + } |
| 135 | + |
| 136 | + public clear(): void { |
| 137 | + } |
| 138 | +} |
0 commit comments