@celljs/ta-workspace 4.7.0 → 4.9.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/README.md +6 -6
- package/README.zh-cn.md +6 -6
- package/package.json +13 -4
- package/web/index.js +2695 -799
- package/web-src/browser.tsx +0 -275
- package/web-src/common.tsx +0 -296
- package/web-src/editors.tsx +0 -471
- package/web-src/explorer.tsx +0 -479
- package/web-src/file-icon.tsx +0 -21
- package/web-src/host.ts +0 -121
- package/web-src/i18n.ts +0 -320
- package/web-src/index.tsx +0 -65
- package/web-src/open-files.tsx +0 -80
- package/web-src/preview.tsx +0 -132
- package/web-src/react.ts +0 -8
- package/web-src/scm.tsx +0 -388
- package/web-src/search.tsx +0 -485
- package/web-src/style.ts +0 -225
- package/web-src/terminal.tsx +0 -350
- package/web-src/util/ansi.spec.ts +0 -66
- package/web-src/util/ansi.ts +0 -438
- package/web-src/util/file-icon.spec.ts +0 -35
- package/web-src/util/file-icon.ts +0 -23
- package/web-src/util/git.ts +0 -203
- package/web-src/util/keys.ts +0 -56
- package/web-src/util/preview.spec.ts +0 -48
- package/web-src/util/preview.ts +0 -90
- package/web-src/util/search.spec.ts +0 -164
- package/web-src/util/search.ts +0 -274
- package/web-src/util/seti-font.ts +0 -6
- package/web-src/util/seti.ts +0 -761
- package/web-src/util/workspace.spec.ts +0 -201
- package/web-src/util/workspace.ts +0 -354
- package/web-src/workspace-client.ts +0 -119
package/web-src/util/keys.ts
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
/** What a key press sends to a terminal; undefined for keys the text input handles (letters, IME). */
|
|
2
|
-
export interface KeyPress {
|
|
3
|
-
key: string;
|
|
4
|
-
ctrlKey?: boolean;
|
|
5
|
-
altKey?: boolean;
|
|
6
|
-
metaKey?: boolean;
|
|
7
|
-
shiftKey?: boolean;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
const KEYS: Record<string, string> = {
|
|
11
|
-
Enter: '\r',
|
|
12
|
-
Backspace: '\x7f',
|
|
13
|
-
Tab: '\t',
|
|
14
|
-
Escape: '\x1b',
|
|
15
|
-
ArrowUp: '\x1b[A',
|
|
16
|
-
ArrowDown: '\x1b[B',
|
|
17
|
-
ArrowRight: '\x1b[C',
|
|
18
|
-
ArrowLeft: '\x1b[D',
|
|
19
|
-
Home: '\x1b[H',
|
|
20
|
-
End: '\x1b[F',
|
|
21
|
-
Delete: '\x1b[3~',
|
|
22
|
-
Insert: '\x1b[2~',
|
|
23
|
-
PageUp: '\x1b[5~',
|
|
24
|
-
PageDown: '\x1b[6~',
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
export namespace TerminalKeys {
|
|
28
|
-
export function sequence(event: KeyPress): string | undefined {
|
|
29
|
-
if (event.metaKey) {
|
|
30
|
-
// The browser's: copy, paste, close the tab.
|
|
31
|
-
return undefined;
|
|
32
|
-
}
|
|
33
|
-
if (event.key === 'Tab' && event.shiftKey) {
|
|
34
|
-
return '\x1b[Z';
|
|
35
|
-
}
|
|
36
|
-
const special = KEYS[event.key];
|
|
37
|
-
if (special) {
|
|
38
|
-
return event.altKey ? `\x1b${special}` : special;
|
|
39
|
-
}
|
|
40
|
-
if (event.ctrlKey && event.key.length === 1) {
|
|
41
|
-
const code = event.key.toUpperCase().charCodeAt(0);
|
|
42
|
-
if (code >= 64 && code <= 95) {
|
|
43
|
-
return String.fromCharCode(code - 64);
|
|
44
|
-
}
|
|
45
|
-
if (event.key === ' ') {
|
|
46
|
-
return '\x00';
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
return undefined;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/** Pasted text as the shell takes it: lines end with `\r`. */
|
|
53
|
-
export function paste(text: string): string {
|
|
54
|
-
return text.replace(/\r?\n/g, '\r');
|
|
55
|
-
}
|
|
56
|
-
}
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'bun:test';
|
|
2
|
-
import { PreviewUtil } from './preview';
|
|
3
|
-
|
|
4
|
-
describe('PreviewUtil', () => {
|
|
5
|
-
it('knows what a file is shown as', () => {
|
|
6
|
-
expect(PreviewUtil.kind('docs/README.md')).toBe('markdown');
|
|
7
|
-
expect(PreviewUtil.kind('site/index.HTML')).toBe('html');
|
|
8
|
-
expect(PreviewUtil.kind('logo.svg')).toBe('image');
|
|
9
|
-
expect(PreviewUtil.kind('a/clip.mov')).toBe('video');
|
|
10
|
-
expect(PreviewUtil.kind('sound.flac')).toBe('audio');
|
|
11
|
-
expect(PreviewUtil.kind('paper.pdf')).toBe('pdf');
|
|
12
|
-
expect(PreviewUtil.kind('src/app.ts')).toBeUndefined();
|
|
13
|
-
expect(PreviewUtil.kind('.md')).toBeUndefined();
|
|
14
|
-
expect(PreviewUtil.kind('md.dir/Makefile')).toBeUndefined();
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
it('edits only the previews that have a text', () => {
|
|
18
|
-
expect(PreviewUtil.hasText('README.md')).toBe(true);
|
|
19
|
-
expect(PreviewUtil.hasText('logo.svg')).toBe(true);
|
|
20
|
-
expect(PreviewUtil.hasText('photo.png')).toBe(false);
|
|
21
|
-
expect(PreviewUtil.hasText('clip.mp4')).toBe(false);
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
it('builds URLs beside a file and again after a change', () => {
|
|
25
|
-
expect(PreviewUtil.directoryUrl('/api/files/id/docs/README.md')).toBe('/api/files/id/docs/');
|
|
26
|
-
expect(PreviewUtil.versioned('/api/files/id/a.html', undefined)).toBe('/api/files/id/a.html');
|
|
27
|
-
expect(PreviewUtil.versioned('/api/files/id/a.html', '"1-2"')).toBe('/api/files/id/a.html?v=%221-2%22');
|
|
28
|
-
const base = 'http://127.0.0.1:1/api/files/id/docs/';
|
|
29
|
-
expect(PreviewUtil.resolve('img/a b.png', base)).toBe('http://127.0.0.1:1/api/files/id/docs/img/a%20b.png');
|
|
30
|
-
expect(PreviewUtil.resolve('../README.md', base)).toBe('http://127.0.0.1:1/api/files/id/README.md');
|
|
31
|
-
expect(PreviewUtil.resolve('#usage', base)).toBe('#usage');
|
|
32
|
-
expect(PreviewUtil.resolve('https://example.com/x', base)).toBe('https://example.com/x');
|
|
33
|
-
expect(PreviewUtil.resolve('mailto:a@b.c', base)).toBe('mailto:a@b.c');
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
it('gives headings the ids GitHub gives them', () => {
|
|
37
|
-
const taken = new Map<string, number>();
|
|
38
|
-
expect(PreviewUtil.slug('Getting Started!', taken)).toBe('getting-started');
|
|
39
|
-
expect(PreviewUtil.slug('Getting Started', taken)).toBe('getting-started-1');
|
|
40
|
-
expect(PreviewUtil.slug('快速 开始', taken)).toBe('快速-开始');
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
it('puts rendered Markdown in a document of its own', () => {
|
|
44
|
-
const page = PreviewUtil.markdownDocument('<h1>Hi</h1>', 'body{}');
|
|
45
|
-
expect(page).toContain('<style>body{}</style>');
|
|
46
|
-
expect(page).toContain('<article class="markdown"><h1>Hi</h1></article>');
|
|
47
|
-
});
|
|
48
|
-
});
|
package/web-src/util/preview.ts
DELETED
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
/** How a file is shown as it is, rather than as text. */
|
|
2
|
-
export type PreviewKind = 'markdown' | 'html' | 'image' | 'video' | 'audio' | 'pdf';
|
|
3
|
-
|
|
4
|
-
const KINDS: Record<string, PreviewKind> = {
|
|
5
|
-
md: 'markdown',
|
|
6
|
-
markdown: 'markdown',
|
|
7
|
-
html: 'html',
|
|
8
|
-
htm: 'html',
|
|
9
|
-
svg: 'image',
|
|
10
|
-
png: 'image',
|
|
11
|
-
jpg: 'image',
|
|
12
|
-
jpeg: 'image',
|
|
13
|
-
gif: 'image',
|
|
14
|
-
webp: 'image',
|
|
15
|
-
avif: 'image',
|
|
16
|
-
bmp: 'image',
|
|
17
|
-
ico: 'image',
|
|
18
|
-
mp4: 'video',
|
|
19
|
-
m4v: 'video',
|
|
20
|
-
webm: 'video',
|
|
21
|
-
ogv: 'video',
|
|
22
|
-
mov: 'video',
|
|
23
|
-
mp3: 'audio',
|
|
24
|
-
m4a: 'audio',
|
|
25
|
-
aac: 'audio',
|
|
26
|
-
wav: 'audio',
|
|
27
|
-
ogg: 'audio',
|
|
28
|
-
oga: 'audio',
|
|
29
|
-
flac: 'audio',
|
|
30
|
-
pdf: 'pdf',
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
/** Files with text behind their preview: they open as a preview and switch to the editor. */
|
|
34
|
-
const TEXT_KINDS = new Set(['md', 'markdown', 'html', 'htm', 'svg']);
|
|
35
|
-
|
|
36
|
-
export namespace PreviewUtil {
|
|
37
|
-
function extension(path: string): string {
|
|
38
|
-
const name = path.slice(path.lastIndexOf('/') + 1);
|
|
39
|
-
const dot = name.lastIndexOf('.');
|
|
40
|
-
return dot > 0 ? name.slice(dot + 1).toLowerCase() : '';
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export function kind(path: string): PreviewKind | undefined {
|
|
44
|
-
return KINDS[extension(path)];
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/** The preview has a text to edit, as Markdown, HTML and SVG; the others are only shown. */
|
|
48
|
-
export function hasText(path: string): boolean {
|
|
49
|
-
return TEXT_KINDS.has(extension(path));
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/** The URL of the directory of a file's URL, for the files a page or a document loads beside it. */
|
|
53
|
-
export function directoryUrl(fileUrl: string): string {
|
|
54
|
-
return fileUrl.slice(0, fileUrl.lastIndexOf('/') + 1);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/** A URL that loads again when the file changed (`version`, its ETag); the server ignores the query. */
|
|
58
|
-
export function versioned(url: string, version: string | undefined): string {
|
|
59
|
-
return version ? `${url}${url.includes('?') ? '&' : '?'}v=${encodeURIComponent(version)}` : url;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/** A URL of a document beside the file at `base` (absolute); fragments and absolute URLs stay as they are. */
|
|
63
|
-
export function resolve(url: string, base: string): string {
|
|
64
|
-
if (!url || url.startsWith('#')) {
|
|
65
|
-
return url;
|
|
66
|
-
}
|
|
67
|
-
try {
|
|
68
|
-
return new URL(url, base).href;
|
|
69
|
-
} catch {
|
|
70
|
-
return url;
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/** The id GitHub gives a heading, for links to `#it`; `taken` makes repeated ones unique. */
|
|
75
|
-
export function slug(text: string, taken: Map<string, number>): string {
|
|
76
|
-
const base = text
|
|
77
|
-
.trim()
|
|
78
|
-
.toLowerCase()
|
|
79
|
-
.replace(/[^\p{L}\p{N}\s_-]/gu, '')
|
|
80
|
-
.replace(/\s/g, '-');
|
|
81
|
-
const count = taken.get(base) ?? 0;
|
|
82
|
-
taken.set(base, count + 1);
|
|
83
|
-
return count ? `${base}-${count}` : base;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/** A document of rendered Markdown, with `style` for the look of the page; it is shown in a frame that runs no scripts. */
|
|
87
|
-
export function markdownDocument(body: string, style: string): string {
|
|
88
|
-
return `<!doctype html><html><head><meta charset="utf-8"><style>${style}</style></head><body><article class="markdown">${body}</article></body></html>`;
|
|
89
|
-
}
|
|
90
|
-
}
|
|
@@ -1,164 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'bun:test';
|
|
2
|
-
import { GitUtil } from './git';
|
|
3
|
-
import { SearchUtil } from './search';
|
|
4
|
-
|
|
5
|
-
describe('SearchUtil', () => {
|
|
6
|
-
it('builds the command of each searcher', () => {
|
|
7
|
-
expect(SearchUtil.command('rg', { pattern: 'a.b', include: '*.ts', exclude: 'dist, *.test.ts' }).args).toEqual([
|
|
8
|
-
'-n',
|
|
9
|
-
'--no-heading',
|
|
10
|
-
'--color',
|
|
11
|
-
'never',
|
|
12
|
-
'--max-columns',
|
|
13
|
-
'1000',
|
|
14
|
-
'-F',
|
|
15
|
-
'-i',
|
|
16
|
-
'-g',
|
|
17
|
-
'*.ts',
|
|
18
|
-
'-g',
|
|
19
|
-
'!dist',
|
|
20
|
-
'-g',
|
|
21
|
-
'!*.test.ts',
|
|
22
|
-
'-e',
|
|
23
|
-
'a.b',
|
|
24
|
-
'.',
|
|
25
|
-
]);
|
|
26
|
-
expect(SearchUtil.command('git', { pattern: '\\d+', regex: true, caseSensitive: true }).args).toContain('-E');
|
|
27
|
-
expect(SearchUtil.command('git', { pattern: '\\d+', regex: true, caseSensitive: true }).args).toContain('[0-9]+');
|
|
28
|
-
expect(SearchUtil.command('grep', { pattern: 'x' })).toMatchObject({
|
|
29
|
-
command: 'grep',
|
|
30
|
-
args: ['-rnI', '-F', '-i', '--exclude-dir=.git', '--exclude-dir=node_modules', '-e', 'x', '.'],
|
|
31
|
-
});
|
|
32
|
-
expect(SearchUtil.command('grep', { pattern: 'x', wholeWord: true }).args).toContain('-w');
|
|
33
|
-
expect(SearchUtil.unavailable('rg', 127, false)).toBe(true);
|
|
34
|
-
expect(SearchUtil.unavailable('git', 128, false)).toBe(true);
|
|
35
|
-
expect(SearchUtil.unavailable('grep', 1, false)).toBe(false);
|
|
36
|
-
expect(SearchUtil.unavailable('rg', null, true)).toBe(false);
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
it('turns globs into expressions', () => {
|
|
40
|
-
const ts = SearchUtil.globToRegExp('src/**/*.ts');
|
|
41
|
-
expect(ts.test('src/a.ts')).toBe(true);
|
|
42
|
-
expect(ts.test('src/a/b/c.ts')).toBe(true);
|
|
43
|
-
expect(ts.test('lib/a.ts')).toBe(false);
|
|
44
|
-
expect(ts.test('src/a.tsx')).toBe(false);
|
|
45
|
-
const name = SearchUtil.globToRegExp('*.{ts,tsx}');
|
|
46
|
-
expect(name.test('a/b.tsx')).toBe(true);
|
|
47
|
-
expect(name.test('a/b.js')).toBe(false);
|
|
48
|
-
expect(SearchUtil.globToRegExp('a?.md').test('ab.md')).toBe(true);
|
|
49
|
-
// A folder takes all inside it, as the fields of VS Code do.
|
|
50
|
-
expect(SearchUtil.globToRegExp('dist').test('dist/a/b.js')).toBe(true);
|
|
51
|
-
expect(SearchUtil.globToRegExp('src/lib/').test('src/lib/a.ts')).toBe(true);
|
|
52
|
-
expect(SearchUtil.globs(' src/**, *.{ts,tsx} ,, ./docs ')).toEqual(['src/**', '*.{ts,tsx}', 'docs']);
|
|
53
|
-
const filter = SearchUtil.pathFilter({ include: 'src, docs/*.md', exclude: '*.test.ts' });
|
|
54
|
-
expect(['src/a.ts', 'src/a.test.ts', 'docs/x.md', 'lib/a.ts'].filter(filter)).toEqual(['src/a.ts', 'docs/x.md']);
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
it('reads the matches of grep, git grep and ripgrep', () => {
|
|
58
|
-
const output = ['./src/a.ts:3: const needle = 1;', 'src/b.md:10:Needle here', 'garbage', 'src/c.ts:1:needle, needle'].join('\n');
|
|
59
|
-
const expression = SearchUtil.queryRegExp({ pattern: 'needle' });
|
|
60
|
-
const all = SearchUtil.parseMatches(output, expression, 10);
|
|
61
|
-
expect(all.truncated).toBe(false);
|
|
62
|
-
expect(all.matches[0]).toEqual({ path: 'src/a.ts', line: 3, column: 11, length: 6, preview: 'const needle = 1;', previewColumn: 6 });
|
|
63
|
-
expect(all.matches[1]).toMatchObject({ path: 'src/b.md', line: 10, column: 1 });
|
|
64
|
-
// Each match of a line is a result of its own.
|
|
65
|
-
expect(all.matches.slice(2).map(match => match.column)).toEqual([1, 9]);
|
|
66
|
-
expect(SearchUtil.parseMatches(output, expression, 2)).toMatchObject({ truncated: true, matches: [{}, {}] });
|
|
67
|
-
expect(SearchUtil.parseMatches(output, expression, 10, SearchUtil.pathFilter({ include: '*.md' })).matches.map(m => m.path)).toEqual(['src/b.md']);
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
it('escapes plain text, and writes the shorthand classes for grep', () => {
|
|
71
|
-
expect(SearchUtil.find('xa.b(y', SearchUtil.queryRegExp({ pattern: 'a.b(' }))).toEqual([[1, 4]]);
|
|
72
|
-
expect(SearchUtil.find('a', SearchUtil.queryRegExp({ pattern: 'A', caseSensitive: true }))).toEqual([]);
|
|
73
|
-
expect(() => SearchUtil.queryRegExp({ pattern: '(', regex: true })).toThrow();
|
|
74
|
-
expect(SearchUtil.patternError({ pattern: '(', regex: true })).toBeString();
|
|
75
|
-
expect(SearchUtil.patternError({ pattern: '(' })).toBeUndefined();
|
|
76
|
-
// Whole words, in any script.
|
|
77
|
-
const word = SearchUtil.queryRegExp({ pattern: 'cat', wholeWord: true });
|
|
78
|
-
expect(SearchUtil.find('cat cats concat 猫cat cat_', word)).toEqual([[0, 3]]);
|
|
79
|
-
expect(SearchUtil.toExtendedRegExp('a\\d+\\s\\w\\\\d')).toBe('a[0-9]+[[:space:]][[:alnum:]_]\\\\d');
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
it('cuts long previews around the match', () => {
|
|
83
|
-
const line = `${'x'.repeat(500)}needle${'y'.repeat(500)}`;
|
|
84
|
-
const shown = SearchUtil.preview(line, 500, 6);
|
|
85
|
-
expect(shown.preview.slice(shown.previewColumn, shown.previewColumn + 6)).toBe('needle');
|
|
86
|
-
expect(shown.preview.startsWith('…')).toBe(true);
|
|
87
|
-
expect(shown.preview.length).toBeLessThan(210);
|
|
88
|
-
expect(SearchUtil.preview(' a b', 6, 1)).toEqual({ preview: 'a b', previewColumn: 2 });
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
it('replaces all the matches of a file, or one', () => {
|
|
92
|
-
const text = 'let a = 1;\nlet b = a + a;\n';
|
|
93
|
-
expect(SearchUtil.replace(text, { pattern: 'a', wholeWord: true }, 'x')).toEqual({ text: 'let x = 1;\nlet b = x + x;\n', count: 3 });
|
|
94
|
-
expect(SearchUtil.replace(text, { pattern: 'a', wholeWord: true }, 'x', { line: 2, column: 13 })).toEqual({
|
|
95
|
-
text: 'let a = 1;\nlet b = a + x;\n',
|
|
96
|
-
count: 1,
|
|
97
|
-
});
|
|
98
|
-
expect(SearchUtil.replace(text, { pattern: 'a', wholeWord: true }, 'x', { line: 2, column: 2 })).toEqual({ text, count: 0 });
|
|
99
|
-
// Groups in a regular expression; a $ in plain text stays.
|
|
100
|
-
expect(SearchUtil.replace('a1 b2', { pattern: '([a-z])(\\d)', regex: true }, '$2$1$$').text).toBe('1a$ 2b$');
|
|
101
|
-
expect(SearchUtil.replace('price', { pattern: 'price' }, '$1 & $&').text).toBe('$1 & $&');
|
|
102
|
-
});
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
describe('GitUtil', () => {
|
|
106
|
-
it('reads git status, under the directory asked for', () => {
|
|
107
|
-
const output = [
|
|
108
|
-
'## main...origin/main [ahead 2, behind 1]',
|
|
109
|
-
'M pkg/a.ts',
|
|
110
|
-
' M pkg/b.ts',
|
|
111
|
-
'MM pkg/c.ts',
|
|
112
|
-
'?? pkg/new.ts',
|
|
113
|
-
'R pkg/to.ts',
|
|
114
|
-
'pkg/from.ts',
|
|
115
|
-
' D other/x.ts',
|
|
116
|
-
'',
|
|
117
|
-
].join('\0');
|
|
118
|
-
expect(GitUtil.parseStatus(output, 'pkg/')).toEqual({
|
|
119
|
-
repository: true,
|
|
120
|
-
branch: 'main',
|
|
121
|
-
ahead: 2,
|
|
122
|
-
behind: 1,
|
|
123
|
-
files: [
|
|
124
|
-
{ path: 'a.ts', staged: 'M' },
|
|
125
|
-
{ path: 'b.ts', unstaged: 'M' },
|
|
126
|
-
{ path: 'c.ts', staged: 'M', unstaged: 'M' },
|
|
127
|
-
{ path: 'new.ts', unstaged: '?' },
|
|
128
|
-
{ path: 'to.ts', staged: 'R', from: 'from.ts' },
|
|
129
|
-
],
|
|
130
|
-
});
|
|
131
|
-
expect(GitUtil.parseBranch('No commits yet on main')).toEqual({ branch: 'main', initial: true });
|
|
132
|
-
expect(GitUtil.parseBranch('HEAD (no branch)')).toEqual({ branch: undefined });
|
|
133
|
-
expect(GitUtil.parseBranch('feature')).toEqual({ branch: 'feature' });
|
|
134
|
-
expect(GitUtil.show('', 'a b.ts')).toEqual(['show', ':./a b.ts']);
|
|
135
|
-
});
|
|
136
|
-
|
|
137
|
-
it('builds the commands of source control, paths taken as they are', () => {
|
|
138
|
-
expect(GitUtil.stage(['a[1].ts', '.'])).toEqual(['add', '-A', '--', ':(literal)a[1].ts', '.']);
|
|
139
|
-
expect(GitUtil.unstage(['a.ts'], false)).toEqual(['reset', '-q', '--', ':(literal)a.ts']);
|
|
140
|
-
expect(GitUtil.unstage(['a.ts'], true)).toEqual(['rm', '--cached', '-r', '-q', '--', ':(literal)a.ts']);
|
|
141
|
-
expect(GitUtil.discard(['a.ts'])).toEqual(['checkout', '-q', '--', ':(literal)a.ts']);
|
|
142
|
-
expect(GitUtil.clean(['n.ts'])).toEqual(['clean', '-f', '-q', '--', ':(literal)n.ts']);
|
|
143
|
-
expect(GitUtil.status(true)).toContain('--ignored=matching');
|
|
144
|
-
expect(GitUtil.status()).not.toContain('--ignored=matching');
|
|
145
|
-
});
|
|
146
|
-
|
|
147
|
-
it('marks the files and folders of the explorer', () => {
|
|
148
|
-
const status = GitUtil.parseStatus(
|
|
149
|
-
['## main', ' M src/a.ts', '?? src/lib/n.ts', 'UU src/lib/c.ts', '!! dist/', '!! debug.log', 'A docs/x.md', ''].join('\0'),
|
|
150
|
-
'',
|
|
151
|
-
);
|
|
152
|
-
expect(status.ignored).toEqual(['dist/', 'debug.log']);
|
|
153
|
-
const marks = GitUtil.decorations(status);
|
|
154
|
-
expect(Object.fromEntries(marks.files)).toEqual({ 'src/a.ts': 'M', 'src/lib/n.ts': 'U', 'src/lib/c.ts': '!', 'docs/x.md': 'A' });
|
|
155
|
-
// A folder shows the change that matters most inside it: a conflict over a change over a new file.
|
|
156
|
-
expect(Object.fromEntries(marks.folders)).toEqual({ src: '!', 'src/lib': '!', docs: 'A' });
|
|
157
|
-
expect(GitUtil.ignored(marks, 'dist')).toBe(true);
|
|
158
|
-
expect(GitUtil.ignored(marks, 'dist/app.js')).toBe(true);
|
|
159
|
-
expect(GitUtil.ignored(marks, 'distant.ts')).toBe(false);
|
|
160
|
-
expect(GitUtil.ignored(marks, 'debug.log')).toBe(true);
|
|
161
|
-
expect(GitUtil.letter({ path: 'a', staged: 'A', unstaged: 'M' })).toBe('M');
|
|
162
|
-
expect(GitUtil.letter({ path: 'a', staged: 'A' })).toBe('A');
|
|
163
|
-
});
|
|
164
|
-
});
|
package/web-src/util/search.ts
DELETED
|
@@ -1,274 +0,0 @@
|
|
|
1
|
-
export interface TextQuery {
|
|
2
|
-
pattern: string;
|
|
3
|
-
regex?: boolean;
|
|
4
|
-
caseSensitive?: boolean;
|
|
5
|
-
/** Only where the match is a whole word. */
|
|
6
|
-
wholeWord?: boolean;
|
|
7
|
-
/** Globs of the files to search, separated by commas: `src/**\/*.ts, *.md`. */
|
|
8
|
-
include?: string;
|
|
9
|
-
/** Globs of the files to leave out, separated by commas. */
|
|
10
|
-
exclude?: string;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export interface TextMatch {
|
|
14
|
-
path: string;
|
|
15
|
-
/** From 1. */
|
|
16
|
-
line: number;
|
|
17
|
-
/** From 1, in characters. */
|
|
18
|
-
column: number;
|
|
19
|
-
/** In characters. */
|
|
20
|
-
length: number;
|
|
21
|
-
/** The line, cut to about PREVIEW_CHARS characters around the match, without its indentation. */
|
|
22
|
-
preview: string;
|
|
23
|
-
/** Where the match starts in `preview`, from 0. */
|
|
24
|
-
previewColumn: number;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export interface TextResult {
|
|
28
|
-
matches: TextMatch[];
|
|
29
|
-
/** More matches than the limit. */
|
|
30
|
-
truncated: boolean;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/** A program that searches, with its arguments. */
|
|
34
|
-
export interface SearchCommand {
|
|
35
|
-
command: string;
|
|
36
|
-
args: string[];
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
export type Searcher = 'rg' | 'git' | 'grep';
|
|
40
|
-
|
|
41
|
-
const PREVIEW_CHARS = 200;
|
|
42
|
-
/** Directories grep skips: it reads no .gitignore. */
|
|
43
|
-
const SKIPPED_DIRECTORIES = ['.git', 'node_modules'];
|
|
44
|
-
|
|
45
|
-
/** Text search with what the machine has (ripgrep, git grep or grep), and their output read back. */
|
|
46
|
-
export namespace SearchUtil {
|
|
47
|
-
export function command(searcher: Searcher, query: TextQuery): SearchCommand {
|
|
48
|
-
const match = [
|
|
49
|
-
query.regex ? (searcher === 'rg' ? undefined : '-E') : '-F',
|
|
50
|
-
query.caseSensitive ? undefined : '-i',
|
|
51
|
-
query.wholeWord ? '-w' : undefined,
|
|
52
|
-
].filter((flag): flag is string => !!flag);
|
|
53
|
-
const pattern = query.regex && searcher !== 'rg' ? toExtendedRegExp(query.pattern) : query.pattern;
|
|
54
|
-
if (searcher === 'rg') {
|
|
55
|
-
const filters = [...globs(query.include).flatMap(glob => ['-g', glob]), ...globs(query.exclude).flatMap(glob => ['-g', `!${glob}`])];
|
|
56
|
-
return { command: 'rg', args: ['-n', '--no-heading', '--color', 'never', '--max-columns', '1000', ...match, ...filters, '-e', pattern, '.'] };
|
|
57
|
-
}
|
|
58
|
-
if (searcher === 'git') {
|
|
59
|
-
return {
|
|
60
|
-
command: 'git',
|
|
61
|
-
args: ['-c', 'core.quotepath=off', 'grep', '-n', '-I', '--untracked', '--exclude-standard', ...match, '-e', pattern, '--', '.'],
|
|
62
|
-
};
|
|
63
|
-
}
|
|
64
|
-
return { command: 'grep', args: ['-rnI', ...match, ...SKIPPED_DIRECTORIES.map(dir => `--exclude-dir=${dir}`), '-e', pattern, '.'] };
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Whether the searcher could not run here, so that the next one should: the program is missing
|
|
69
|
-
* (the shell answers 127), or git is outside a repository (128).
|
|
70
|
-
*/
|
|
71
|
-
export function unavailable(searcher: Searcher, exitCode: number | null, timedOut: boolean): boolean {
|
|
72
|
-
if (timedOut) {
|
|
73
|
-
return false;
|
|
74
|
-
}
|
|
75
|
-
return exitCode === 127 || exitCode === null || (searcher === 'git' && exitCode === 128);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
/** The globs of a field, as VS Code reads it: separated by commas outside braces, trimmed. */
|
|
79
|
-
export function globs(field: string | undefined): string[] {
|
|
80
|
-
const out: string[] = [];
|
|
81
|
-
let current = '';
|
|
82
|
-
let braces = 0;
|
|
83
|
-
for (const char of field ?? '') {
|
|
84
|
-
if (char === ',' && !braces) {
|
|
85
|
-
out.push(current);
|
|
86
|
-
current = '';
|
|
87
|
-
continue;
|
|
88
|
-
}
|
|
89
|
-
braces += char === '{' ? 1 : char === '}' && braces ? -1 : 0;
|
|
90
|
-
current += char;
|
|
91
|
-
}
|
|
92
|
-
out.push(current);
|
|
93
|
-
return out.map(glob => glob.trim().replace(/^\.\//, '')).filter(Boolean);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/** A glob of paths (`*`, `**`, `?`, `{a,b}`) as a regular expression; one without `/` matches the name anywhere, a folder all inside it. */
|
|
97
|
-
export function globToRegExp(glob: string): RegExp {
|
|
98
|
-
let source = '';
|
|
99
|
-
let braces = 0;
|
|
100
|
-
const trimmed = glob.replace(/\/+$/, '');
|
|
101
|
-
for (let i = 0; i < trimmed.length; i++) {
|
|
102
|
-
const char = trimmed[i];
|
|
103
|
-
if (char === '*') {
|
|
104
|
-
if (trimmed[i + 1] === '*') {
|
|
105
|
-
const slash = trimmed[i + 2] === '/';
|
|
106
|
-
source += slash ? '(?:.*/)?' : '.*';
|
|
107
|
-
i += slash ? 2 : 1;
|
|
108
|
-
} else {
|
|
109
|
-
source += '[^/]*';
|
|
110
|
-
}
|
|
111
|
-
} else if (char === '?') {
|
|
112
|
-
source += '[^/]';
|
|
113
|
-
} else if (char === '{') {
|
|
114
|
-
braces++;
|
|
115
|
-
source += '(?:';
|
|
116
|
-
} else if (char === '}' && braces) {
|
|
117
|
-
braces--;
|
|
118
|
-
source += ')';
|
|
119
|
-
} else if (char === ',' && braces) {
|
|
120
|
-
source += '|';
|
|
121
|
-
} else {
|
|
122
|
-
source += char.replace(/[.+^$()|[\]\\]/g, '\\$&');
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
return new RegExp(trimmed.includes('/') ? `^${source}(?:/.*)?$` : `(?:^|/)${source}(?:/.*)?$`);
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
/** Whether a path passes the globs to include (any, when there are some) and to exclude (none). */
|
|
129
|
-
export function pathFilter(query: Pick<TextQuery, 'include' | 'exclude'>): (path: string) => boolean {
|
|
130
|
-
const include = globs(query.include).map(globToRegExp);
|
|
131
|
-
const exclude = globs(query.exclude).map(globToRegExp);
|
|
132
|
-
return path => (include.length === 0 || include.some(glob => glob.test(path))) && !exclude.some(glob => glob.test(path));
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
/** The expression of a query, global; throws a readable error for a bad one. */
|
|
136
|
-
export function queryRegExp(query: Pick<TextQuery, 'pattern' | 'regex' | 'caseSensitive' | 'wholeWord'>): RegExp {
|
|
137
|
-
const source = query.regex ? query.pattern : escapeText(query.pattern);
|
|
138
|
-
return new RegExp(query.wholeWord ? `(?<![\\p{L}\\p{N}_])(?:${source})(?![\\p{L}\\p{N}_])` : source, query.caseSensitive ? 'gu' : 'giu');
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
/** The error of a bad expression, as the user reads it; undefined for a good one. */
|
|
142
|
-
export function patternError(query: TextQuery): string | undefined {
|
|
143
|
-
try {
|
|
144
|
-
queryRegExp(query);
|
|
145
|
-
return undefined;
|
|
146
|
-
} catch (error) {
|
|
147
|
-
return error instanceof Error ? error.message.replace(/^Invalid regular expression: /, '') : String(error);
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
export function escapeText(text: string): string {
|
|
152
|
-
return text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/**
|
|
156
|
-
* A JavaScript expression for grep's POSIX extended ones: the shorthand classes become bracket
|
|
157
|
-
* expressions; the matches are checked again with the JavaScript one.
|
|
158
|
-
*/
|
|
159
|
-
export function toExtendedRegExp(source: string): string {
|
|
160
|
-
const classes: Record<string, string> = { d: '[0-9]', D: '[^0-9]', w: '[[:alnum:]_]', W: '[^[:alnum:]_]', s: '[[:space:]]', S: '[^[:space:]]' };
|
|
161
|
-
return source.replace(/\\\\|\\([dDwWsS])/g, (all, name: string | undefined) => (name ? classes[name] : all));
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
/** Every match of a global expression in a text, as [index, length]; an empty match counts as none. */
|
|
165
|
-
export function find(text: string, expression: RegExp): [number, number][] {
|
|
166
|
-
const out: [number, number][] = [];
|
|
167
|
-
expression.lastIndex = 0;
|
|
168
|
-
for (let found = expression.exec(text); found; found = expression.exec(text)) {
|
|
169
|
-
if (!found[0]) {
|
|
170
|
-
expression.lastIndex++;
|
|
171
|
-
continue;
|
|
172
|
-
}
|
|
173
|
-
out.push([found.index, found[0].length]);
|
|
174
|
-
}
|
|
175
|
-
return out;
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
/**
|
|
179
|
-
* The matches in lines of `path:line:text`, as grep, git grep and ripgrep print them, one for each
|
|
180
|
-
* match in a line; columns and previews come from `expression`, so that they count characters, not bytes.
|
|
181
|
-
*/
|
|
182
|
-
export function parseMatches(output: string, expression: RegExp, limit: number, filter?: (path: string) => boolean): TextResult {
|
|
183
|
-
const matches: TextMatch[] = [];
|
|
184
|
-
for (const line of output.split('\n')) {
|
|
185
|
-
const found = /^(.+?):(\d+):(.*)$/.exec(line);
|
|
186
|
-
if (!found) {
|
|
187
|
-
continue;
|
|
188
|
-
}
|
|
189
|
-
const path = found[1].replace(/^\.\//, '');
|
|
190
|
-
if (filter && !filter(path)) {
|
|
191
|
-
continue;
|
|
192
|
-
}
|
|
193
|
-
const text = found[3].replace(/\r$/, '');
|
|
194
|
-
for (const [index, length] of find(text, expression)) {
|
|
195
|
-
if (matches.length === limit) {
|
|
196
|
-
return { matches, truncated: true };
|
|
197
|
-
}
|
|
198
|
-
matches.push({ path, line: Number(found[2]), column: index + 1, length, ...preview(text, index, length) });
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
return { matches, truncated: false };
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
/** About PREVIEW_CHARS characters of a line around a match, without the indentation, and where the match is in it. */
|
|
205
|
-
export function preview(text: string, index: number, length = 0): { preview: string; previewColumn: number } {
|
|
206
|
-
const trimmed = text.trimStart();
|
|
207
|
-
const at = Math.max(0, index - (text.length - trimmed.length));
|
|
208
|
-
if (trimmed.length <= PREVIEW_CHARS) {
|
|
209
|
-
return { preview: trimmed, previewColumn: at };
|
|
210
|
-
}
|
|
211
|
-
const start = Math.max(0, Math.min(at - PREVIEW_CHARS / 4, trimmed.length - Math.max(PREVIEW_CHARS, length)));
|
|
212
|
-
const end = Math.max(start + PREVIEW_CHARS, at + length);
|
|
213
|
-
const head = start > 0 ? '…' : '';
|
|
214
|
-
return { preview: head + trimmed.slice(start, end) + (end < trimmed.length ? '…' : ''), previewColumn: at - start + head.length };
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
/**
|
|
218
|
-
* `text` with the matches of the query replaced (`$1` takes a group of a regular expression), and how
|
|
219
|
-
* many: all of them, or only the one at `at` (its line and column, from 1).
|
|
220
|
-
*/
|
|
221
|
-
export function replace(text: string, query: TextQuery, replacement: string, at?: { line: number; column: number }): { text: string; count: number } {
|
|
222
|
-
const expression = queryRegExp(query);
|
|
223
|
-
// Plain text goes in as it is: a `$` in it is not a group.
|
|
224
|
-
const expand = (match: string, groups: (string | undefined)[]) => (query.regex ? substitute(replacement, match, groups) : replacement);
|
|
225
|
-
if (!at) {
|
|
226
|
-
let count = 0;
|
|
227
|
-
const replaced = text.replace(expression, (match: string, ...rest: unknown[]) => {
|
|
228
|
-
count++;
|
|
229
|
-
return expand(match, groupsOf(rest));
|
|
230
|
-
});
|
|
231
|
-
return { text: replaced, count };
|
|
232
|
-
}
|
|
233
|
-
let start = 0;
|
|
234
|
-
for (let line = 1; line < at.line; line++) {
|
|
235
|
-
const next = text.indexOf('\n', start);
|
|
236
|
-
if (next < 0) {
|
|
237
|
-
return { text, count: 0 };
|
|
238
|
-
}
|
|
239
|
-
start = next + 1;
|
|
240
|
-
}
|
|
241
|
-
const end = text.indexOf('\n', start);
|
|
242
|
-
const lineText = text.slice(start, end < 0 ? text.length : end);
|
|
243
|
-
const sticky = new RegExp(expression.source, expression.flags.replace('g', 'y'));
|
|
244
|
-
sticky.lastIndex = at.column - 1;
|
|
245
|
-
const found = sticky.exec(lineText);
|
|
246
|
-
if (!found?.[0]) {
|
|
247
|
-
return { text, count: 0 };
|
|
248
|
-
}
|
|
249
|
-
const offset = start + found.index;
|
|
250
|
-
return { text: text.slice(0, offset) + expand(found[0], found.slice(1)) + text.slice(offset + found[0].length), count: 1 };
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
/** The groups among the arguments of a callback of String.replace: those before the offset. */
|
|
254
|
-
function groupsOf(rest: unknown[]): (string | undefined)[] {
|
|
255
|
-
return rest.slice(
|
|
256
|
-
0,
|
|
257
|
-
rest.findIndex(value => typeof value === 'number'),
|
|
258
|
-
) as (string | undefined)[];
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
/** `$&`, `$1`…`$99` and `$$` of a replacement, as String.replace reads them. */
|
|
262
|
-
export function substitute(replacement: string, match: string, groups: (string | undefined)[]): string {
|
|
263
|
-
return replacement.replace(/\$(\$|&|\d{1,2})/g, (all, name: string) => {
|
|
264
|
-
if (name === '$') {
|
|
265
|
-
return '$';
|
|
266
|
-
}
|
|
267
|
-
if (name === '&') {
|
|
268
|
-
return match;
|
|
269
|
-
}
|
|
270
|
-
const index = Number(name);
|
|
271
|
-
return index >= 1 && index <= groups.length ? (groups[index - 1] ?? '') : all;
|
|
272
|
-
});
|
|
273
|
-
}
|
|
274
|
-
}
|