@sunshj/vscode-ab5x-utils 0.0.3 → 0.0.5
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/index.cjs +29 -20
- package/dist/index.d.mts +33 -17
- package/dist/index.mjs +29 -20
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -12,14 +12,18 @@ function createRegexCollector(regex, setupContextOrTransform, transform) {
|
|
|
12
12
|
const text = document.getText();
|
|
13
13
|
const globalRegex = new RegExp(regex.source, regex.flags.includes("g") ? regex.flags : `${regex.flags}g`);
|
|
14
14
|
let match;
|
|
15
|
-
while ((match = globalRegex.exec(text)) !== null)
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
15
|
+
while ((match = globalRegex.exec(text)) !== null) {
|
|
16
|
+
const _item = {
|
|
17
|
+
text: match[0],
|
|
18
|
+
startIndex: match.index,
|
|
19
|
+
endIndex: match.index + match[0].length,
|
|
20
|
+
extra: {}
|
|
21
|
+
};
|
|
22
|
+
if (transformFn) {
|
|
23
|
+
const item = transformFn(match, Object.assign({ _item }, context));
|
|
24
|
+
if (item) items.push(item);
|
|
25
|
+
} else items.push(_item);
|
|
26
|
+
}
|
|
23
27
|
return items;
|
|
24
28
|
};
|
|
25
29
|
}
|
|
@@ -51,7 +55,7 @@ function definePatternProvider(options) {
|
|
|
51
55
|
function createDocumentLinkProvider() {
|
|
52
56
|
return { provideDocumentLinks(document) {
|
|
53
57
|
if (!fileFilter(document)) return;
|
|
54
|
-
return collect(document).map(
|
|
58
|
+
return collect(document).map((item) => {
|
|
55
59
|
const range = createRange(document, item);
|
|
56
60
|
const target = documentLink?.target?.(item, document);
|
|
57
61
|
const link = new vscode.DocumentLink(range, target);
|
|
@@ -84,19 +88,19 @@ function definePatternProvider(options) {
|
|
|
84
88
|
return collect(document).flatMap((item) => codeLens?.lenses(item, document) ?? []);
|
|
85
89
|
} };
|
|
86
90
|
}
|
|
87
|
-
function
|
|
91
|
+
function updateDecorations(editor) {
|
|
92
|
+
if (!editor || !fileFilter(editor.document)) return;
|
|
93
|
+
const decorations = collect(editor.document, true).map((item) => decoration?.render(item, editor.document)).filter((opt) => opt !== void 0);
|
|
94
|
+
if (decorationType) editor.setDecorations(decorationType, decorations);
|
|
95
|
+
}
|
|
96
|
+
function registerAll() {
|
|
88
97
|
const disposables = [];
|
|
89
98
|
if (commandsConfig?.length) for (const cmd of commandsConfig) disposables.push(vscode.commands.registerCommand(cmd.id, cmd.handler));
|
|
90
|
-
if (documentLink?.enabled
|
|
91
|
-
if (hover?.enabled) disposables.push(vscode.languages.registerHoverProvider(selector, createHoverProvider()));
|
|
92
|
-
if (completion?.enabled) disposables.push(vscode.languages.registerCompletionItemProvider(selector, createCompletionProvider(), ...completion.triggerCharacters ?? []));
|
|
93
|
-
if (codeLens?.enabled) disposables.push(vscode.languages.registerCodeLensProvider(selector, createCodeLensProvider()));
|
|
99
|
+
if (documentLink?.enabled) disposables.push(vscode.languages.registerDocumentLinkProvider(documentLink?.selector ?? selector, createDocumentLinkProvider()));
|
|
100
|
+
if (hover?.enabled) disposables.push(vscode.languages.registerHoverProvider(hover?.selector ?? selector, createHoverProvider()));
|
|
101
|
+
if (completion?.enabled) disposables.push(vscode.languages.registerCompletionItemProvider(completion?.selector ?? selector, createCompletionProvider(), ...completion.triggerCharacters ?? []));
|
|
102
|
+
if (codeLens?.enabled) disposables.push(vscode.languages.registerCodeLensProvider(codeLens?.selector ?? selector, createCodeLensProvider()));
|
|
94
103
|
if (decoration?.enabled && decorationType) {
|
|
95
|
-
const updateDecorations = (editor) => {
|
|
96
|
-
if (!editor || !fileFilter(editor.document)) return;
|
|
97
|
-
const decorations = collect(editor.document, true).map((item) => decoration.render(item, editor.document)).filter((opt) => opt !== void 0);
|
|
98
|
-
editor.setDecorations(decorationType, decorations);
|
|
99
|
-
};
|
|
100
104
|
updateDecorations(vscode.window.activeTextEditor);
|
|
101
105
|
disposables.push(vscode.window.onDidChangeActiveTextEditor(updateDecorations), vscode.workspace.onDidChangeTextDocument((e) => {
|
|
102
106
|
if (vscode.window.activeTextEditor?.document === e.document) updateDecorations(vscode.window.activeTextEditor);
|
|
@@ -108,7 +112,12 @@ function definePatternProvider(options) {
|
|
|
108
112
|
cache,
|
|
109
113
|
collect,
|
|
110
114
|
decorationType,
|
|
111
|
-
|
|
115
|
+
updateDecorations,
|
|
116
|
+
registerAll,
|
|
117
|
+
createDocumentLinkProvider,
|
|
118
|
+
createHoverProvider,
|
|
119
|
+
createCompletionProvider,
|
|
120
|
+
createCodeLensProvider
|
|
112
121
|
};
|
|
113
122
|
}
|
|
114
123
|
/** Vue 文件过滤器 */
|
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as vscode0 from "vscode";
|
|
2
|
+
import { CodeLens, CodeLensProvider, CompletionItem, CompletionItemProvider, DecorationOptions, Disposable, DocumentLink, DocumentLinkProvider, DocumentSelector, Hover, HoverProvider, Position, Range, TextDocument, TextEditor, Uri, window } from "vscode";
|
|
2
3
|
|
|
3
4
|
//#region src/index.d.ts
|
|
4
5
|
/** 匹配结果项 */
|
|
@@ -10,7 +11,7 @@ interface MatchItem<TExtra = unknown> {
|
|
|
10
11
|
/** 匹配结束索引 */
|
|
11
12
|
endIndex: number;
|
|
12
13
|
/** 额外数据,用于 Hover/Completion/Decoration */
|
|
13
|
-
extra
|
|
14
|
+
extra: TExtra;
|
|
14
15
|
}
|
|
15
16
|
/** 收集函数类型 */
|
|
16
17
|
type CollectFn<TExtra = unknown> = (document: TextDocument, skipCache?: boolean) => Array<MatchItem<TExtra>>;
|
|
@@ -18,6 +19,8 @@ type CollectFn<TExtra = unknown> = (document: TextDocument, skipCache?: boolean)
|
|
|
18
19
|
interface DocumentLinkConfig<TExtra = unknown> {
|
|
19
20
|
/** 是否启用 */
|
|
20
21
|
enabled?: boolean;
|
|
22
|
+
/** 文档选择器 */
|
|
23
|
+
selector?: DocumentSelector;
|
|
21
24
|
/** 生成链接目标 URI */
|
|
22
25
|
target?: (item: MatchItem<TExtra>, document: TextDocument) => Uri | undefined;
|
|
23
26
|
/** 生成 tooltip */
|
|
@@ -27,6 +30,8 @@ interface DocumentLinkConfig<TExtra = unknown> {
|
|
|
27
30
|
interface HoverConfig<TExtra = unknown> {
|
|
28
31
|
/** 是否启用 */
|
|
29
32
|
enabled?: boolean;
|
|
33
|
+
/** 文档选择器 */
|
|
34
|
+
selector?: DocumentSelector;
|
|
30
35
|
/** 生成 Hover 内容 */
|
|
31
36
|
content: (item: MatchItem<TExtra>, document: TextDocument) => Hover | undefined;
|
|
32
37
|
}
|
|
@@ -34,6 +39,8 @@ interface HoverConfig<TExtra = unknown> {
|
|
|
34
39
|
interface CompletionConfig {
|
|
35
40
|
/** 是否启用 */
|
|
36
41
|
enabled?: boolean;
|
|
42
|
+
/** 文档选择器 */
|
|
43
|
+
selector?: DocumentSelector;
|
|
37
44
|
/** 触发字符 */
|
|
38
45
|
triggerCharacters?: string[];
|
|
39
46
|
/** 判断当前位置是否应该触发补全 */
|
|
@@ -54,6 +61,8 @@ interface DecorationConfig<TExtra = unknown> {
|
|
|
54
61
|
interface CodeLensConfig<TExtra = unknown> {
|
|
55
62
|
/** 是否启用 */
|
|
56
63
|
enabled?: boolean;
|
|
64
|
+
/** 文档选择器 */
|
|
65
|
+
selector?: DocumentSelector;
|
|
57
66
|
/** 生成 CodeLens 列表 */
|
|
58
67
|
lenses: (item: MatchItem<TExtra>, document: TextDocument) => CodeLens[] | undefined;
|
|
59
68
|
}
|
|
@@ -85,21 +94,18 @@ interface PatternProviderOptions<TExtra = unknown> {
|
|
|
85
94
|
/** 注册的命令 */
|
|
86
95
|
commands?: CommandConfig[];
|
|
87
96
|
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
/** 注册所有 Provider,返回 disposables */
|
|
97
|
-
register: () => Disposable[];
|
|
98
|
-
}
|
|
97
|
+
type ContextWithMatchItem<TContext> = TContext & {
|
|
98
|
+
/**
|
|
99
|
+
* @param text match[0]
|
|
100
|
+
* @param startIndex match.index
|
|
101
|
+
* @param endIndex match.index + match[0].length
|
|
102
|
+
*/
|
|
103
|
+
_item: MatchItem;
|
|
104
|
+
};
|
|
99
105
|
/** 创建基于正则的收集函数 - 无 context */
|
|
100
|
-
declare function createRegexCollector<TExtra = unknown>(regex: RegExp, transform?: (match: RegExpExecArray) => MatchItem<TExtra> | undefined): CollectFn<TExtra>;
|
|
106
|
+
declare function createRegexCollector<TExtra = unknown>(regex: RegExp, transform?: (match: RegExpExecArray, context: ContextWithMatchItem<unknown>) => MatchItem<TExtra> | undefined): CollectFn<TExtra>;
|
|
101
107
|
/** 创建基于正则的收集函数 - 带 context */
|
|
102
|
-
declare function createRegexCollector<TContext, TExtra = unknown>(regex: RegExp, setupContext: () => TContext, transform: (match: RegExpExecArray, context: TContext) => MatchItem<TExtra> | undefined): CollectFn<TExtra>;
|
|
108
|
+
declare function createRegexCollector<TContext = object, TExtra = unknown>(regex: RegExp, setupContext: () => TContext, transform: (match: RegExpExecArray, context: ContextWithMatchItem<TContext>) => MatchItem<TExtra> | undefined): CollectFn<TExtra>;
|
|
103
109
|
/** 从匹配项创建 Range */
|
|
104
110
|
declare function createRange(document: TextDocument, item: MatchItem): Range;
|
|
105
111
|
/** 获取匹配项的 extra 数据 */
|
|
@@ -107,9 +113,19 @@ declare function getItemExtra<TExtra>(link: DocumentLink): TExtra | undefined;
|
|
|
107
113
|
/**
|
|
108
114
|
* 定义模式匹配 Provider,统一管理 DocumentLink、Hover、Completion、Decoration、CodeLens 等功能
|
|
109
115
|
*/
|
|
110
|
-
declare function definePatternProvider<TExtra = unknown>(options: PatternProviderOptions<TExtra>):
|
|
116
|
+
declare function definePatternProvider<TExtra = unknown>(options: PatternProviderOptions<TExtra>): {
|
|
117
|
+
cache: WeakMap<Uri, MatchItem<TExtra>[]>;
|
|
118
|
+
collect: (document: TextDocument, skipCache?: boolean) => Array<MatchItem<TExtra>>;
|
|
119
|
+
decorationType: vscode0.TextEditorDecorationType | undefined;
|
|
120
|
+
updateDecorations: (editor: TextEditor | undefined) => void;
|
|
121
|
+
registerAll: () => Disposable[];
|
|
122
|
+
createDocumentLinkProvider: () => DocumentLinkProvider;
|
|
123
|
+
createHoverProvider: () => HoverProvider;
|
|
124
|
+
createCompletionProvider: () => CompletionItemProvider;
|
|
125
|
+
createCodeLensProvider: () => CodeLensProvider;
|
|
126
|
+
};
|
|
111
127
|
/** Vue 文件过滤器 */
|
|
112
128
|
declare const vueFileFilter: (document: TextDocument) => boolean;
|
|
113
129
|
declare function createExtFilter(...exts: string[]): (document: TextDocument) => boolean;
|
|
114
130
|
//#endregion
|
|
115
|
-
export {
|
|
131
|
+
export { CollectFn, MatchItem, PatternProviderOptions, createExtFilter, createRange, createRegexCollector, definePatternProvider, getItemExtra, vueFileFilter };
|
package/dist/index.mjs
CHANGED
|
@@ -12,14 +12,18 @@ function createRegexCollector(regex, setupContextOrTransform, transform) {
|
|
|
12
12
|
const text = document.getText();
|
|
13
13
|
const globalRegex = new RegExp(regex.source, regex.flags.includes("g") ? regex.flags : `${regex.flags}g`);
|
|
14
14
|
let match;
|
|
15
|
-
while ((match = globalRegex.exec(text)) !== null)
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
15
|
+
while ((match = globalRegex.exec(text)) !== null) {
|
|
16
|
+
const _item = {
|
|
17
|
+
text: match[0],
|
|
18
|
+
startIndex: match.index,
|
|
19
|
+
endIndex: match.index + match[0].length,
|
|
20
|
+
extra: {}
|
|
21
|
+
};
|
|
22
|
+
if (transformFn) {
|
|
23
|
+
const item = transformFn(match, Object.assign({ _item }, context));
|
|
24
|
+
if (item) items.push(item);
|
|
25
|
+
} else items.push(_item);
|
|
26
|
+
}
|
|
23
27
|
return items;
|
|
24
28
|
};
|
|
25
29
|
}
|
|
@@ -51,7 +55,7 @@ function definePatternProvider(options) {
|
|
|
51
55
|
function createDocumentLinkProvider() {
|
|
52
56
|
return { provideDocumentLinks(document) {
|
|
53
57
|
if (!fileFilter(document)) return;
|
|
54
|
-
return collect(document).map(
|
|
58
|
+
return collect(document).map((item) => {
|
|
55
59
|
const range = createRange(document, item);
|
|
56
60
|
const target = documentLink?.target?.(item, document);
|
|
57
61
|
const link = new DocumentLink(range, target);
|
|
@@ -84,19 +88,19 @@ function definePatternProvider(options) {
|
|
|
84
88
|
return collect(document).flatMap((item) => codeLens?.lenses(item, document) ?? []);
|
|
85
89
|
} };
|
|
86
90
|
}
|
|
87
|
-
function
|
|
91
|
+
function updateDecorations(editor) {
|
|
92
|
+
if (!editor || !fileFilter(editor.document)) return;
|
|
93
|
+
const decorations = collect(editor.document, true).map((item) => decoration?.render(item, editor.document)).filter((opt) => opt !== void 0);
|
|
94
|
+
if (decorationType) editor.setDecorations(decorationType, decorations);
|
|
95
|
+
}
|
|
96
|
+
function registerAll() {
|
|
88
97
|
const disposables = [];
|
|
89
98
|
if (commandsConfig?.length) for (const cmd of commandsConfig) disposables.push(commands.registerCommand(cmd.id, cmd.handler));
|
|
90
|
-
if (documentLink?.enabled
|
|
91
|
-
if (hover?.enabled) disposables.push(languages.registerHoverProvider(selector, createHoverProvider()));
|
|
92
|
-
if (completion?.enabled) disposables.push(languages.registerCompletionItemProvider(selector, createCompletionProvider(), ...completion.triggerCharacters ?? []));
|
|
93
|
-
if (codeLens?.enabled) disposables.push(languages.registerCodeLensProvider(selector, createCodeLensProvider()));
|
|
99
|
+
if (documentLink?.enabled) disposables.push(languages.registerDocumentLinkProvider(documentLink?.selector ?? selector, createDocumentLinkProvider()));
|
|
100
|
+
if (hover?.enabled) disposables.push(languages.registerHoverProvider(hover?.selector ?? selector, createHoverProvider()));
|
|
101
|
+
if (completion?.enabled) disposables.push(languages.registerCompletionItemProvider(completion?.selector ?? selector, createCompletionProvider(), ...completion.triggerCharacters ?? []));
|
|
102
|
+
if (codeLens?.enabled) disposables.push(languages.registerCodeLensProvider(codeLens?.selector ?? selector, createCodeLensProvider()));
|
|
94
103
|
if (decoration?.enabled && decorationType) {
|
|
95
|
-
const updateDecorations = (editor) => {
|
|
96
|
-
if (!editor || !fileFilter(editor.document)) return;
|
|
97
|
-
const decorations = collect(editor.document, true).map((item) => decoration.render(item, editor.document)).filter((opt) => opt !== void 0);
|
|
98
|
-
editor.setDecorations(decorationType, decorations);
|
|
99
|
-
};
|
|
100
104
|
updateDecorations(window.activeTextEditor);
|
|
101
105
|
disposables.push(window.onDidChangeActiveTextEditor(updateDecorations), workspace.onDidChangeTextDocument((e) => {
|
|
102
106
|
if (window.activeTextEditor?.document === e.document) updateDecorations(window.activeTextEditor);
|
|
@@ -108,7 +112,12 @@ function definePatternProvider(options) {
|
|
|
108
112
|
cache,
|
|
109
113
|
collect,
|
|
110
114
|
decorationType,
|
|
111
|
-
|
|
115
|
+
updateDecorations,
|
|
116
|
+
registerAll,
|
|
117
|
+
createDocumentLinkProvider,
|
|
118
|
+
createHoverProvider,
|
|
119
|
+
createCompletionProvider,
|
|
120
|
+
createCodeLensProvider
|
|
112
121
|
};
|
|
113
122
|
}
|
|
114
123
|
/** Vue 文件过滤器 */
|