@sunshj/vscode-ab5x-utils 0.0.4 → 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 +24 -19
- package/dist/index.d.mts +22 -5
- package/dist/index.mjs +24 -19
- 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
|
}
|
|
@@ -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,8 @@ function definePatternProvider(options) {
|
|
|
108
112
|
cache,
|
|
109
113
|
collect,
|
|
110
114
|
decorationType,
|
|
111
|
-
|
|
115
|
+
updateDecorations,
|
|
116
|
+
registerAll,
|
|
112
117
|
createDocumentLinkProvider,
|
|
113
118
|
createHoverProvider,
|
|
114
119
|
createCompletionProvider,
|
package/dist/index.d.mts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as vscode0 from "vscode";
|
|
2
|
-
import { CodeLens, CodeLensProvider, CompletionItem, CompletionItemProvider, DecorationOptions, Disposable, DocumentLink, DocumentLinkProvider, DocumentSelector, Hover, HoverProvider, Position, Range, TextDocument, Uri, window } from "vscode";
|
|
2
|
+
import { CodeLens, CodeLensProvider, CompletionItem, CompletionItemProvider, DecorationOptions, Disposable, DocumentLink, DocumentLinkProvider, DocumentSelector, Hover, HoverProvider, Position, Range, TextDocument, TextEditor, Uri, window } from "vscode";
|
|
3
3
|
|
|
4
4
|
//#region src/index.d.ts
|
|
5
5
|
/** 匹配结果项 */
|
|
@@ -11,7 +11,7 @@ interface MatchItem<TExtra = unknown> {
|
|
|
11
11
|
/** 匹配结束索引 */
|
|
12
12
|
endIndex: number;
|
|
13
13
|
/** 额外数据,用于 Hover/Completion/Decoration */
|
|
14
|
-
extra
|
|
14
|
+
extra: TExtra;
|
|
15
15
|
}
|
|
16
16
|
/** 收集函数类型 */
|
|
17
17
|
type CollectFn<TExtra = unknown> = (document: TextDocument, skipCache?: boolean) => Array<MatchItem<TExtra>>;
|
|
@@ -19,6 +19,8 @@ type CollectFn<TExtra = unknown> = (document: TextDocument, skipCache?: boolean)
|
|
|
19
19
|
interface DocumentLinkConfig<TExtra = unknown> {
|
|
20
20
|
/** 是否启用 */
|
|
21
21
|
enabled?: boolean;
|
|
22
|
+
/** 文档选择器 */
|
|
23
|
+
selector?: DocumentSelector;
|
|
22
24
|
/** 生成链接目标 URI */
|
|
23
25
|
target?: (item: MatchItem<TExtra>, document: TextDocument) => Uri | undefined;
|
|
24
26
|
/** 生成 tooltip */
|
|
@@ -28,6 +30,8 @@ interface DocumentLinkConfig<TExtra = unknown> {
|
|
|
28
30
|
interface HoverConfig<TExtra = unknown> {
|
|
29
31
|
/** 是否启用 */
|
|
30
32
|
enabled?: boolean;
|
|
33
|
+
/** 文档选择器 */
|
|
34
|
+
selector?: DocumentSelector;
|
|
31
35
|
/** 生成 Hover 内容 */
|
|
32
36
|
content: (item: MatchItem<TExtra>, document: TextDocument) => Hover | undefined;
|
|
33
37
|
}
|
|
@@ -35,6 +39,8 @@ interface HoverConfig<TExtra = unknown> {
|
|
|
35
39
|
interface CompletionConfig {
|
|
36
40
|
/** 是否启用 */
|
|
37
41
|
enabled?: boolean;
|
|
42
|
+
/** 文档选择器 */
|
|
43
|
+
selector?: DocumentSelector;
|
|
38
44
|
/** 触发字符 */
|
|
39
45
|
triggerCharacters?: string[];
|
|
40
46
|
/** 判断当前位置是否应该触发补全 */
|
|
@@ -55,6 +61,8 @@ interface DecorationConfig<TExtra = unknown> {
|
|
|
55
61
|
interface CodeLensConfig<TExtra = unknown> {
|
|
56
62
|
/** 是否启用 */
|
|
57
63
|
enabled?: boolean;
|
|
64
|
+
/** 文档选择器 */
|
|
65
|
+
selector?: DocumentSelector;
|
|
58
66
|
/** 生成 CodeLens 列表 */
|
|
59
67
|
lenses: (item: MatchItem<TExtra>, document: TextDocument) => CodeLens[] | undefined;
|
|
60
68
|
}
|
|
@@ -86,10 +94,18 @@ interface PatternProviderOptions<TExtra = unknown> {
|
|
|
86
94
|
/** 注册的命令 */
|
|
87
95
|
commands?: CommandConfig[];
|
|
88
96
|
}
|
|
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
|
+
};
|
|
89
105
|
/** 创建基于正则的收集函数 - 无 context */
|
|
90
|
-
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>;
|
|
91
107
|
/** 创建基于正则的收集函数 - 带 context */
|
|
92
|
-
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>;
|
|
93
109
|
/** 从匹配项创建 Range */
|
|
94
110
|
declare function createRange(document: TextDocument, item: MatchItem): Range;
|
|
95
111
|
/** 获取匹配项的 extra 数据 */
|
|
@@ -101,7 +117,8 @@ declare function definePatternProvider<TExtra = unknown>(options: PatternProvide
|
|
|
101
117
|
cache: WeakMap<Uri, MatchItem<TExtra>[]>;
|
|
102
118
|
collect: (document: TextDocument, skipCache?: boolean) => Array<MatchItem<TExtra>>;
|
|
103
119
|
decorationType: vscode0.TextEditorDecorationType | undefined;
|
|
104
|
-
|
|
120
|
+
updateDecorations: (editor: TextEditor | undefined) => void;
|
|
121
|
+
registerAll: () => Disposable[];
|
|
105
122
|
createDocumentLinkProvider: () => DocumentLinkProvider;
|
|
106
123
|
createHoverProvider: () => HoverProvider;
|
|
107
124
|
createCompletionProvider: () => CompletionItemProvider;
|
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
|
}
|
|
@@ -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,8 @@ function definePatternProvider(options) {
|
|
|
108
112
|
cache,
|
|
109
113
|
collect,
|
|
110
114
|
decorationType,
|
|
111
|
-
|
|
115
|
+
updateDecorations,
|
|
116
|
+
registerAll,
|
|
112
117
|
createDocumentLinkProvider,
|
|
113
118
|
createHoverProvider,
|
|
114
119
|
createCompletionProvider,
|