|
| 1 | +/** |
| 2 | + * copyright (c) 2023, Matthias Behr |
| 3 | + * |
| 4 | + * todo: |
| 5 | + * [] implement watch |
| 6 | + * [] implement frag (aka members) |
| 7 | + * [] fix fba staying old if hidden |
| 8 | + * [] how to provide a file type (json,...) to command.open |
| 9 | + */ |
| 10 | + |
| 11 | +import { TextDecoder, TextEncoder } from 'util' |
| 12 | +import * as vscode from 'vscode' |
| 13 | +import { FBAEditorProvider, FishboneTreeItem } from './fbaEditor' |
| 14 | + |
| 15 | +export class FBAFSProvider implements vscode.FileSystemProvider { |
| 16 | + private onDidChangeEmitter = new vscode.EventEmitter<vscode.FileChangeEvent[]>() |
| 17 | + |
| 18 | + constructor(private editorProvider: FBAEditorProvider) { |
| 19 | + this.onDidChangeFile = this.onDidChangeEmitter.event |
| 20 | + } |
| 21 | + |
| 22 | + private static decodeQueryParams(query: string): Record<string, string> { |
| 23 | + return query.split('&').reduce((accumulator: Record<string, string>, singleQueryParam) => { |
| 24 | + const [key, value] = singleQueryParam.split('=') |
| 25 | + accumulator[key] = decodeURIComponent(value) |
| 26 | + return accumulator |
| 27 | + }, {}) |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * get data for uri from the known documents from provider |
| 32 | + * |
| 33 | + * @param uri |
| 34 | + * @returns a member from the docData.lastPostedObj that can/must be modified directly! |
| 35 | + */ |
| 36 | + private getDataForUri(uri: vscode.Uri): [FishboneTreeItem, any] | undefined { |
| 37 | + console.log(`FBAFSProvider.getDataForUri(${uri.toString()})`) |
| 38 | + try { |
| 39 | + const title = uri.path.slice(1) // without the / |
| 40 | + const queryParams = FBAFSProvider.decodeQueryParams(uri.query) |
| 41 | + |
| 42 | + // do we find a document? |
| 43 | + const treeItems = this.editorProvider._treeRootNodes |
| 44 | + const doc = treeItems.find((v) => v.docData?.lastPostedObj?.title === title) |
| 45 | + if (doc) { |
| 46 | + console.log(`FBAFSProvider.getDataForUri found doc`, Object.keys(doc)) |
| 47 | + const lastFBA = doc.docData!.lastPostedObj |
| 48 | + // now search for the fbUid |
| 49 | + const elem = FBAFSProvider.getElemFromFBA(lastFBA, queryParams) |
| 50 | + console.log(`FBAFSProvider.getDataForUri found elem`, elem) |
| 51 | + return [doc, elem] |
| 52 | + } else { |
| 53 | + console.log(`FBAFSProvider.getDataForUri found no doc for title:'${title}'`) |
| 54 | + } |
| 55 | + } catch (e) { |
| 56 | + console.log(`FBAFSProvider.getDataForUri() got error:'${e}'`) |
| 57 | + } |
| 58 | + return undefined |
| 59 | + } |
| 60 | + |
| 61 | + private static getElemFromFBA(fba: any, queryParams: Record<string, string>): any { |
| 62 | + console.log(`FBAFSProvider.getElemFromFBA(${JSON.stringify(queryParams)})`) |
| 63 | + // search for fbUid in fba.fishbone and fba.attributes |
| 64 | + const fbUid = queryParams.fbUid |
| 65 | + if (fbUid) { |
| 66 | + // todo in fishbone... |
| 67 | + const attributes = fba.attributes |
| 68 | + if (Array.isArray(attributes)) { |
| 69 | + for (const attr of attributes) { |
| 70 | + if (attr.fbUid === fbUid) { |
| 71 | + return attr |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + return undefined |
| 77 | + } |
| 78 | + |
| 79 | + watch(uri: vscode.Uri, options: { readonly recursive: boolean; readonly excludes: readonly string[] }): vscode.Disposable { |
| 80 | + console.log(`FBAFSProvider.watch(${uri.toString()}, ${JSON.stringify(options)})`) |
| 81 | + return { |
| 82 | + dispose() { |
| 83 | + console.log(`FBAFSProvider watch on '${uri.toString()}' disposed.`) |
| 84 | + }, |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + /// if this fires the stat.mtime needs to advance and a correct size needs to be reported! |
| 89 | + onDidChangeFile: vscode.Event<vscode.FileChangeEvent[]> |
| 90 | + |
| 91 | + stat(uri: vscode.Uri): vscode.FileStat | Thenable<vscode.FileStat> { |
| 92 | + console.log(`FBAFSProvider.stat(${uri.toString()})`) |
| 93 | + //console.log(`FBAFSProvider.stat uri.path='${uri.path}'`) |
| 94 | + //console.log(`FBAFSProvider.stat uri.query='${uri.query}'`) |
| 95 | + //console.log(`FBAFSProvider.stat uri.fragment='${uri.fragment}'`) |
| 96 | + |
| 97 | + const queryParams = FBAFSProvider.decodeQueryParams(uri.query) |
| 98 | + console.log(`FBAFSProvider.stat queryParams='${JSON.stringify(queryParams)}'`) |
| 99 | + |
| 100 | + const [doc, data] = this.getDataForUri(uri) || [undefined, undefined] |
| 101 | + console.log(`FBAFSProvider.stat keys(data)=`, Object.keys(data)) |
| 102 | + |
| 103 | + const size = data ? JSON.stringify(data).length : 0 |
| 104 | + |
| 105 | + return { |
| 106 | + ctime: 0, |
| 107 | + mtime: doc ? doc.docData?.lastPostedDocVersion || 0 : 0, |
| 108 | + size, |
| 109 | + type: vscode.FileType.File, |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + readFile(uri: vscode.Uri): Uint8Array | Thenable<Uint8Array> { |
| 114 | + console.log(`FBAFSProvider.readFile(${uri.toString()})`) |
| 115 | + const [doc, data] = this.getDataForUri(uri) || [undefined, undefined] |
| 116 | + if (data) { |
| 117 | + const text = JSON.stringify(data) |
| 118 | + return new TextEncoder().encode(text) |
| 119 | + } |
| 120 | + return new Uint8Array() |
| 121 | + } |
| 122 | + |
| 123 | + writeFile( |
| 124 | + uri: vscode.Uri, |
| 125 | + content: Uint8Array, |
| 126 | + options: { readonly create: boolean; readonly overwrite: boolean }, |
| 127 | + ): void | Thenable<void> { |
| 128 | + console.log(`FBAFSProvider.writeFile(${uri.toString()}, content.length=${content.length}, options=${JSON.stringify(options)}})`) |
| 129 | + const [doc, data] = this.getDataForUri(uri) || [undefined, undefined] |
| 130 | + if (doc && doc.docData && doc._document && data) { |
| 131 | + const newText = new TextDecoder().decode(content) |
| 132 | + const newData = JSON.parse(newText) |
| 133 | + // modify the data object directly |
| 134 | + let didModify = false |
| 135 | + if (typeof newData === 'object') { |
| 136 | + Object.entries(newData).forEach(([key, value]) => { |
| 137 | + console.log(`FBAFSProvider.writeFile setting member '${key}'`) |
| 138 | + if (value !== undefined) { |
| 139 | + const oldValue = data[key] |
| 140 | + if (JSON.stringify(oldValue) !== JSON.stringify(value)) { |
| 141 | + data[key] = value |
| 142 | + didModify = true |
| 143 | + } |
| 144 | + } else { |
| 145 | + if (data[key] !== undefined) { |
| 146 | + data.delete(key) |
| 147 | + didModify = true |
| 148 | + } |
| 149 | + } |
| 150 | + }) |
| 151 | + } |
| 152 | + // updateTextDocument |
| 153 | + FBAEditorProvider.updateTextDocument(doc.docData, doc?._document, doc?.docData?.lastPostedObj) |
| 154 | + } else { |
| 155 | + console.log(`FBAFSProvider.writeFile(${uri.toString()})...ignored due to wrong doc...`) |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + delete(uri: vscode.Uri, options: { readonly recursive: boolean }): void | Thenable<void> { |
| 160 | + console.log(`FBAFSProvider.delete(${uri.toString()}, ${JSON.stringify(options)})`) |
| 161 | + throw vscode.FileSystemError.Unavailable('delete for fba content not possible') |
| 162 | + } |
| 163 | + |
| 164 | + rename(oldUri: vscode.Uri, newUri: vscode.Uri, options: { readonly overwrite: boolean }): void | Thenable<void> { |
| 165 | + console.log(`FBAFSProvider.rename(${oldUri.toString()}->${newUri.toString()}, ${JSON.stringify(options)})`) |
| 166 | + throw vscode.FileSystemError.Unavailable('rename for fba content not possible') |
| 167 | + } |
| 168 | + |
| 169 | + readDirectory(uri: vscode.Uri): [string, vscode.FileType][] | Thenable<[string, vscode.FileType][]> { |
| 170 | + console.log(`FBAFSProvider.readDirectory(${uri.toString()})`) |
| 171 | + return [] |
| 172 | + } |
| 173 | + |
| 174 | + createDirectory(uri: vscode.Uri): void | Thenable<void> { |
| 175 | + console.log(`FBAFSProvider.createDirectory(${uri.toString()})`) |
| 176 | + throw vscode.FileSystemError.Unavailable('create directory for fba content not possible') |
| 177 | + } |
| 178 | +} |
0 commit comments