@isopodlabs/vscode_utils 0.2.6 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/fs.js CHANGED
@@ -34,6 +34,15 @@ var __importStar = (this && this.__importStar) || (function () {
34
34
  })();
35
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
36
  exports.Change = exports.Glob = exports.SubfileFileSystem = exports.ReadOnlyFilesystem = exports.NormalFile = exports.BaseFileSystem = void 0;
37
+ exports.stat_reject = stat_reject;
38
+ exports.exists = exists;
39
+ exports.check_exists = check_exists;
40
+ exports.getStat = getStat;
41
+ exports.isDirectory = isDirectory;
42
+ exports.loadFile = loadFile;
43
+ exports.writeFile = writeFile;
44
+ exports.deleteFile = deleteFile;
45
+ exports.createDirectory = createDirectory;
37
46
  exports.isFile = isFile;
38
47
  exports.withOffset = withOffset;
39
48
  exports.openFile = openFile;
@@ -43,29 +52,21 @@ exports.directories = directories;
43
52
  exports.files = files;
44
53
  exports.search = search;
45
54
  exports.mapDirs = mapDirs;
46
- exports.stat_reject = stat_reject;
47
- exports.exists = exists;
48
- exports.getStat = getStat;
49
- exports.isDirectory = isDirectory;
50
- exports.loadFile = loadFile;
51
- exports.writeFile = writeFile;
52
- exports.deleteFile = deleteFile;
53
- exports.createDirectory = createDirectory;
55
+ exports.searchPath = searchPath;
54
56
  exports.createNewName = createNewName;
55
57
  exports.copyFile = copyFile;
56
58
  exports.copyFileToDir = copyFileToDir;
57
59
  exports.copyDirectory = copyDirectory;
58
60
  exports.onChange = onChange;
59
- exports.arrayRemove = arrayRemove;
60
61
  exports.removeOnChange = removeOnChange;
61
- const vscode = __importStar(require("vscode"));
62
62
  const nodefs = __importStar(require("fs"));
63
63
  const path = __importStar(require("path"));
64
+ const vscode_1 = require("vscode");
64
65
  function uri(value) {
65
- return value instanceof vscode.Uri ? value : vscode.Uri.file(value);
66
+ return value instanceof vscode_1.Uri ? value : vscode_1.Uri.file(value);
66
67
  }
67
68
  function file(value) {
68
- return value instanceof vscode.Uri ? value.fsPath : value;
69
+ return value instanceof vscode_1.Uri ? value.fsPath : value;
69
70
  }
70
71
  function file_id(value) {
71
72
  return typeof (value) === 'string' ? value : value.scheme === 'file' ? value.fsPath : value.toString();
@@ -79,25 +80,58 @@ function ext(value) {
79
80
  function dirname(value) {
80
81
  return path.dirname(file(value));
81
82
  }
83
+ function isAbsolute(value) {
84
+ return path.isAbsolute(file(value));
85
+ }
82
86
  function pathComponents(value) {
83
87
  return path.parse(file(value));
84
88
  }
85
89
  function withPathComponents(value, ...comp) {
86
90
  const p = path.join(...comp);
87
- return value instanceof vscode.Uri ? value.with({ path: p }) : p;
91
+ return value instanceof vscode_1.Uri ? value.with({ path: p }) : path.join(p, value);
88
92
  }
89
93
  function join(directory, ...comp) {
90
94
  return withPathComponents(directory, file(directory), ...comp);
91
95
  }
96
+ //-----------------------------------------------------------------------------
97
+ // helpers
98
+ //-----------------------------------------------------------------------------
99
+ function stat_reject(value) {
100
+ return vscode_1.workspace.fs.stat(uri(value));
101
+ }
102
+ function exists(value) {
103
+ return vscode_1.workspace.fs.stat(uri(value)).then(() => true, () => false);
104
+ }
105
+ function check_exists(value) {
106
+ return vscode_1.workspace.fs.stat(uri(value)).then(() => value, () => undefined);
107
+ }
108
+ function getStat(value) {
109
+ return vscode_1.workspace.fs.stat(uri(value)).then(stat => stat, () => undefined);
110
+ }
111
+ function isDirectory(value) {
112
+ return vscode_1.workspace.fs.stat(uri(value)).then(stat => stat.type == vscode_1.FileType.Directory, () => ext(value) === "");
113
+ }
114
+ async function loadFile(file) {
115
+ return vscode_1.workspace.fs.readFile(uri(file)).then(bytes => bytes, error => console.log(`Failed to load ${file} : ${error}`));
116
+ }
117
+ function writeFile(file, bytes) {
118
+ return vscode_1.workspace.fs.writeFile(uri(file), bytes).then(() => true, error => (console.log(`Failed to save ${file} : ${error}`), false));
119
+ }
120
+ function deleteFile(file) {
121
+ return vscode_1.workspace.fs.delete(uri(file)).then(() => true, error => (console.log(`Failed to delete ${file} : ${error}`), false));
122
+ }
123
+ function createDirectory(path) {
124
+ return vscode_1.workspace.fs.createDirectory(uri(path)).then(() => true, error => (console.log(`Failed to create ${path} : ${error}`), false));
125
+ }
92
126
  function isFile(obj) {
93
127
  return obj && typeof obj.dispose === 'function' && typeof obj.read === 'function' && typeof obj.write === 'function';
94
128
  }
95
129
  const filesystems = {};
96
130
  class BaseFileSystem {
97
- _onDidChangeFile = new vscode.EventEmitter();
131
+ _onDidChangeFile = new vscode_1.EventEmitter();
98
132
  constructor(context, scheme) {
99
133
  filesystems[scheme] = this;
100
- context.subscriptions.push(vscode.workspace.registerFileSystemProvider(scheme, this, { isCaseSensitive: true }));
134
+ context.subscriptions.push(vscode_1.workspace.registerFileSystemProvider(scheme, this, { isCaseSensitive: true }));
101
135
  }
102
136
  get onDidChangeFile() { return this._onDidChangeFile.event; }
103
137
  //stubs
@@ -111,7 +145,7 @@ class BaseFileSystem {
111
145
  }
112
146
  exports.BaseFileSystem = BaseFileSystem;
113
147
  function withOffset(file, offset) {
114
- if (file instanceof vscode.Uri)
148
+ if (file instanceof vscode_1.Uri)
115
149
  return SubfileFileSystem.makeUri(file, offset);
116
150
  return new class {
117
151
  length = offset.toOffset - offset.fromOffset;
@@ -182,7 +216,7 @@ function getEncapsulatedUri(uri) {
182
216
  scheme: uri.authority,
183
217
  authority: '',
184
218
  });
185
- // return vscode.Uri.parse(uri.fsPath);
219
+ // return Uri.parse(uri.fsPath);
186
220
  }
187
221
  class ReadOnlyFilesystem extends BaseFileSystem {
188
222
  static SCHEME = 'readonly';
@@ -190,13 +224,13 @@ class ReadOnlyFilesystem extends BaseFileSystem {
190
224
  super(context, ReadOnlyFilesystem.SCHEME);
191
225
  }
192
226
  async stat(uri) {
193
- return { ...await vscode.workspace.fs.stat(getEncapsulatedUri(uri)), permissions: vscode.FilePermission.Readonly };
227
+ return { ...await vscode_1.workspace.fs.stat(getEncapsulatedUri(uri)), permissions: vscode_1.FilePermission.Readonly };
194
228
  }
195
229
  openFile(uri) {
196
230
  return openFile(getEncapsulatedUri(uri));
197
231
  }
198
232
  readFile(uri) {
199
- return vscode.workspace.fs.readFile(getEncapsulatedUri(uri));
233
+ return vscode_1.workspace.fs.readFile(getEncapsulatedUri(uri));
200
234
  }
201
235
  }
202
236
  exports.ReadOnlyFilesystem = ReadOnlyFilesystem;
@@ -221,7 +255,7 @@ class SubfileFileSystem extends BaseFileSystem {
221
255
  }
222
256
  async stat(uri) {
223
257
  const { uri: uri2, offset } = SubfileFileSystem.parseUri(uri);
224
- return { ...await vscode.workspace.fs.stat(uri2), size: offset.toOffset - offset.fromOffset };
258
+ return { ...await vscode_1.workspace.fs.stat(uri2), size: offset.toOffset - offset.fromOffset };
225
259
  }
226
260
  async readFile(uri) {
227
261
  const { uri: uri2, offset } = SubfileFileSystem.parseUri(uri);
@@ -235,7 +269,7 @@ class SubfileFileSystem extends BaseFileSystem {
235
269
  }
236
270
  }
237
271
  else {
238
- const data = await vscode.workspace.fs.readFile(uri2);
272
+ const data = await vscode_1.workspace.fs.readFile(uri2);
239
273
  return data.subarray(offset.fromOffset, offset.toOffset);
240
274
  }
241
275
  }
@@ -327,9 +361,9 @@ function toRegExp(pattern) {
327
361
  return re;
328
362
  }
329
363
  function readDirectory(dir) {
330
- return vscode.workspace.fs.stat(uri(dir)).then(stat => {
331
- if (stat.type == vscode.FileType.Directory) {
332
- return vscode.workspace.fs.readDirectory(uri(dir)).then(items => items, error => {
364
+ return vscode_1.workspace.fs.stat(uri(dir)).then(stat => {
365
+ if (stat.type == vscode_1.FileType.Directory) {
366
+ return vscode_1.workspace.fs.readDirectory(uri(dir)).then(items => items, error => {
333
367
  console.log(`readDirectory failed with ${error}`);
334
368
  return [];
335
369
  });
@@ -344,18 +378,18 @@ function readDirectory(dir) {
344
378
  });
345
379
  }
346
380
  function directories(entries) {
347
- return entries.filter(e => e[1] == vscode.FileType.Directory).map(e => e[0]);
381
+ return entries.filter(e => e[1] == vscode_1.FileType.Directory).map(e => e[0]);
348
382
  }
349
383
  function files(entries, glob) {
350
384
  if (glob) {
351
385
  const include = typeof glob === 'string' ? new Glob(glob) : glob;
352
- return entries.filter(e => e[1] == vscode.FileType.File && include.test(e[0])).map(e => e[0]);
386
+ return entries.filter(e => e[1] == vscode_1.FileType.File && include.test(e[0])).map(e => e[0]);
353
387
  }
354
388
  else {
355
- return entries.filter(e => e[1] == vscode.FileType.File).map(e => e[0]);
389
+ return entries.filter(e => e[1] == vscode_1.FileType.File).map(e => e[0]);
356
390
  }
357
391
  }
358
- async function search(pattern, _exclude, want = vscode.FileType.Unknown) {
392
+ async function search(pattern, _exclude, want = vscode_1.FileType.Unknown) {
359
393
  const m = /[*?[{}]/.exec(pattern);
360
394
  if (!m)
361
395
  return [pattern];
@@ -363,7 +397,7 @@ async function search(pattern, _exclude, want = vscode.FileType.Unknown) {
363
397
  const basePath = pattern.substring(0, sep);
364
398
  const include = new Glob(pattern.substring(sep + 1));
365
399
  const exclude = _exclude ? new Glob(_exclude) : undefined;
366
- const keep = want || vscode.FileType.File;
400
+ const keep = want || vscode_1.FileType.File;
367
401
  const recurse = async (basePath) => {
368
402
  const items = await readDirectory(basePath);
369
403
  const result = [];
@@ -375,7 +409,7 @@ async function search(pattern, _exclude, want = vscode.FileType.Unknown) {
375
409
  continue;
376
410
  if (i[1] === keep && include.test(filename))
377
411
  result.push(filename);
378
- if (i[1] == vscode.FileType.Directory)
412
+ if (i[1] == vscode_1.FileType.Directory)
379
413
  result.push(...await recurse(filename));
380
414
  }
381
415
  return result;
@@ -386,32 +420,16 @@ async function mapDirs(root, glob, onFile, combine) {
386
420
  const glob2 = typeof glob === 'string' ? new Glob(glob) : glob;
387
421
  return readDirectory(root).then(async (dir) => combine(...await Promise.all(files(dir, glob).map(i => onFile(path.join(root, i)))), ...await Promise.all(directories(dir).map(async (i) => mapDirs(path.join(root, i), glob2, onFile, combine)))));
388
422
  }
389
- //-----------------------------------------------------------------------------
390
- // helpers
391
- //-----------------------------------------------------------------------------
392
- function stat_reject(value) {
393
- return vscode.workspace.fs.stat(uri(value));
394
- }
395
- function exists(value) {
396
- return vscode.workspace.fs.stat(uri(value)).then(() => true, () => false);
397
- }
398
- function getStat(value) {
399
- return vscode.workspace.fs.stat(uri(value)).then(stat => stat, () => undefined);
400
- }
401
- function isDirectory(value) {
402
- return vscode.workspace.fs.stat(uri(value)).then(stat => stat.type == vscode.FileType.Directory, () => ext(value) === "");
403
- }
404
- async function loadFile(file) {
405
- return vscode.workspace.fs.readFile(uri(file)).then(bytes => bytes, error => console.log(`Failed to load ${file} : ${error}`));
406
- }
407
- function writeFile(file, bytes) {
408
- return vscode.workspace.fs.writeFile(uri(file), bytes).then(() => true, error => (console.log(`Failed to save ${file} : ${error}`), false));
409
- }
410
- function deleteFile(file) {
411
- return vscode.workspace.fs.delete(uri(file)).then(() => true, error => (console.log(`Failed to delete ${file} : ${error}`), false));
412
- }
413
- function createDirectory(path) {
414
- return vscode.workspace.fs.createDirectory(uri(path)).then(() => true, error => (console.log(`Failed to create ${path} : ${error}`), false));
423
+ async function searchPath(target, paths) {
424
+ if (isAbsolute(target))
425
+ return check_exists(target);
426
+ const promises = paths.map(async (i) => check_exists(withPathComponents(target, i)));
427
+ for (const p of promises) {
428
+ const result = await p;
429
+ if (result)
430
+ return result;
431
+ }
432
+ return undefined;
415
433
  }
416
434
  async function createNewName(filepath) {
417
435
  const parsed = pathComponents(filepath);
@@ -435,13 +453,13 @@ async function createCopyName(filepath) {
435
453
  return filepath;
436
454
  }
437
455
  async function copyFile(sourcepath, destpath) {
438
- return vscode.workspace.fs.readFile(uri(sourcepath)).then(async (bytes) => vscode.workspace.fs.writeFile(uri(destpath), bytes));
456
+ return vscode_1.workspace.fs.readFile(uri(sourcepath)).then(async (bytes) => vscode_1.workspace.fs.writeFile(uri(destpath), bytes));
439
457
  }
440
458
  async function copyFileToDir(sourcepath, destdir) {
441
459
  const dest = createCopyName(join(destdir, basename(sourcepath)));
442
- const bytes = await vscode.workspace.fs.readFile(uri(sourcepath));
460
+ const bytes = await vscode_1.workspace.fs.readFile(uri(sourcepath));
443
461
  const destpath = await dest;
444
- vscode.workspace.fs.writeFile(uri(destpath), bytes);
462
+ vscode_1.workspace.fs.writeFile(uri(destpath), bytes);
445
463
  return destpath;
446
464
  }
447
465
  async function copyDirectory(sourcepath, targetpath) {
@@ -450,7 +468,7 @@ async function copyDirectory(sourcepath, targetpath) {
450
468
  let result = [];
451
469
  for (const i of dir) {
452
470
  const sourcepath2 = join(sourcepath, i[0]);
453
- if (i[1] === vscode.FileType.Directory)
471
+ if (i[1] === vscode_1.FileType.Directory)
454
472
  result = [...result, ...await copyDirectory(sourcepath2, await dest)];
455
473
  else
456
474
  result.push(await copyFileToDir(sourcepath2, await dest));
@@ -542,14 +560,14 @@ function onChange(filename, func) {
542
560
  delete dirWatchers[i];
543
561
  }
544
562
  }
545
- watcher = vscode.workspace.createFileSystemWatcher(new vscode.RelativePattern(withPathComponents(fulluri, dir), "**/*.*"));
563
+ watcher = vscode_1.workspace.createFileSystemWatcher(new vscode_1.RelativePattern(withPathComponents(fulluri, dir), "**/*.*"));
546
564
  watcher.onDidChange((uri) => recCallback(uri, exports.Change.changed));
547
565
  watcher.onDidCreate((uri) => recCallback(uri, exports.Change.created));
548
566
  watcher.onDidDelete((uri) => recCallback(uri, exports.Change.deleted));
549
567
  recWatchers[dir] = watcher;
550
568
  }
551
569
  else {
552
- watcher = vscode.workspace.createFileSystemWatcher(new vscode.RelativePattern(withPathComponents(fulluri, dir), "*.*"));
570
+ watcher = vscode_1.workspace.createFileSystemWatcher(new vscode_1.RelativePattern(withPathComponents(fulluri, dir), "*.*"));
553
571
  watcher.onDidChange((uri) => dirCallback(uri, exports.Change.changed));
554
572
  watcher.onDidCreate((uri) => dirCallback(uri, exports.Change.created));
555
573
  watcher.onDidDelete((uri) => dirCallback(uri, exports.Change.deleted));
@@ -570,7 +588,7 @@ function onChange(filename, func) {
570
588
  (fileCallbacks[filename.toString()] ??= []).push(func);
571
589
  }
572
590
  }
573
- function arrayRemove(array, item) {
591
+ function array_remove(array, item) {
574
592
  const index = array.indexOf(item);
575
593
  if (index === -1)
576
594
  return false;
@@ -583,7 +601,7 @@ function removeOnChange(filename, func) {
583
601
  //recursive
584
602
  const callbacks = recCallbacks[dir];
585
603
  if (callbacks) {
586
- arrayRemove(callbacks, func);
604
+ array_remove(callbacks, func);
587
605
  if (callbacks.length === 0) {
588
606
  delete recCallbacks[dir];
589
607
  const watcher = recWatchers[dir];
@@ -599,7 +617,7 @@ function removeOnChange(filename, func) {
599
617
  //file
600
618
  const callbacks = fileCallbacks[filename.toString()];
601
619
  if (callbacks) {
602
- arrayRemove(callbacks, func);
620
+ array_remove(callbacks, func);
603
621
  if (callbacks.length)
604
622
  return;
605
623
  delete fileCallbacks[filename.toString()];
@@ -66,16 +66,16 @@ class IconTheme {
66
66
  return webview.asWebviewUri(vscode.Uri.file(path.join(this.themeFolder.fsPath, file)));
67
67
  }
68
68
  style(webview) {
69
- return this.theme.fonts ? this.theme.fonts.map(f => `@font-face {
70
- font-family: '${f.id}';
71
- src: ${f.src.map(s => `url('${this.getUri(webview, s.path)}') format('${s.format}')`).join(', ')};
72
- font-weight: ${f.weight};
73
- font-style: ${f.style};
74
- }
75
- [font=${f.id}]::before {
76
- font-family: ${f.id};
77
- font-size: ${f.size};
78
- }
69
+ return this.theme.fonts ? this.theme.fonts.map(f => `@font-face {
70
+ font-family: '${f.id}';
71
+ src: ${f.src.map(s => `url('${this.getUri(webview, s.path)}') format('${s.format}')`).join(', ')};
72
+ font-weight: ${f.weight};
73
+ font-style: ${f.style};
74
+ }
75
+ [font=${f.id}]::before {
76
+ font-family: ${f.id};
77
+ font-size: ${f.size};
78
+ }
79
79
  `).join('\n') : '';
80
80
  }
81
81
  csp(webview) {
@@ -1,5 +1,5 @@
1
1
  /** @jsxImportSource . */
2
- import * as vscode from 'vscode';
2
+ import { Uri, Webview } from 'vscode';
3
3
  export declare function jsx(type: any, props: any): any;
4
4
  export declare function jsxs(type: any, props: any): any;
5
5
  export declare function jsxFrag(props: any): {
@@ -16,23 +16,10 @@ export declare namespace JSX {
16
16
  function render(element: any): string;
17
17
  }
18
18
  export declare function id_selector(id: string | number): string;
19
- export declare const codicons: Record<string, string>;
20
- type IconType0 = string | vscode.Uri;
21
- export type IconType = IconType0 | vscode.ThemeIcon | {
22
- light: IconType0;
23
- dark: IconType0;
24
- };
25
- export declare function iconAttribute(icon: IconType0): string;
26
- export declare function iconAttributes(icon?: IconType): {
27
- icon: string;
28
- color?: undefined;
29
- } | {
30
- icon: string;
31
- color: string;
32
- } | undefined;
33
- export declare function Label({ id, display }: {
19
+ export declare function Label({ id, display, title }: {
34
20
  id: string;
35
21
  display: string;
22
+ title?: string;
36
23
  }): JSX.Element;
37
24
  export declare class Hash {
38
25
  algorithm: string;
@@ -51,16 +38,23 @@ declare const CSPkeywords: {
51
38
  readonly inline_speculation_rules: "'inline-speculation-rules'";
52
39
  readonly strict_dynamic: "'strict-dynamic'";
53
40
  };
54
- type CSPSource = Hash | vscode.Uri | (typeof CSPkeywords)[keyof typeof CSPkeywords] | CSPSource[];
55
- export declare function CSPdefault(extension: vscode.Uri): CSPSource;
41
+ type CSPSource1 = Hash | Uri | (typeof CSPkeywords)[keyof typeof CSPkeywords] | CSPSource1[];
42
+ interface plus<T> {
43
+ source: T;
44
+ plus: boolean;
45
+ }
46
+ declare function plus(source: CSPSource1): plus<CSPSource1>;
47
+ type CSPSource = CSPSource1 | plus<CSPSource1>;
48
+ interface CSPSources {
49
+ main: CSPSource;
50
+ attr?: CSPSource;
51
+ elem?: CSPSource;
52
+ }
53
+ export declare function CSPdefault(extension: Uri): CSPSource1;
56
54
  declare function CSPFunction({ csp, ...others }: {
57
- csp: string;
58
- script?: CSPSource;
59
- script_elem?: CSPSource;
60
- script_attr?: CSPSource;
61
- style?: CSPSource;
62
- stype_elem?: CSPSource;
63
- style_attr?: CSPSource;
55
+ csp: CSPSource1;
56
+ script?: CSPSource | CSPSources;
57
+ style?: CSPSource | CSPSources;
64
58
  font?: CSPSource;
65
59
  img?: CSPSource;
66
60
  media?: CSPSource;
@@ -75,8 +69,8 @@ export declare const CSP: typeof CSPFunction & {
75
69
  readonly strict_dynamic: "'strict-dynamic'";
76
70
  };
77
71
  export declare function ImportMap(props: {
78
- map: Record<string, vscode.Uri>;
79
- webview: vscode.Webview;
72
+ map: Record<string, Uri>;
73
+ webview: Webview;
80
74
  nonce?: Hash;
81
75
  }): JSX.Element;
82
76
  export {};