-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.ts
140 lines (127 loc) · 3.74 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import * as vscode from 'vscode';
interface ConfigItem {
trigger: string;
description: string;
hideName: boolean;
format: string;
prefix: string;
suffix: string;
}
class GoCompletionItemProvider implements vscode.CompletionItemProvider {
position?: vscode.Position;
config: ConfigItem[];
constructor(config: ConfigItem[]) {
this.config = config;
}
public provideCompletionItems(
_: vscode.TextDocument,
position: vscode.Position
) {
this.position = position;
const completions = this.config.map((item) => {
const snippetCompletion = new vscode.CompletionItem(
item.trigger,
vscode.CompletionItemKind.Operator
);
snippetCompletion.documentation = new vscode.MarkdownString(
item.description
);
return snippetCompletion;
});
return completions;
}
public resolveCompletionItem(item: vscode.CompletionItem) {
const label = item.label;
if (this.position && this.config && typeof label === 'string') {
const config = this.config.find((config) => config.trigger === label);
item.command = {
command: 'dot-log-replace',
title: 'refactor',
arguments: [this.position.translate(0, label.length + 1), config],
};
}
return item;
}
}
export function activate(context: vscode.ExtensionContext) {
const dotLogConfig: vscode.WorkspaceConfiguration =
vscode.workspace.getConfiguration('dotLog');
const configList: ConfigItem[] | undefined = dotLogConfig.get('config');
if (!configList) {
return;
}
const options = vscode.languages.registerCompletionItemProvider(
[
'html',
'javascript',
'javascriptreact',
'typescript',
'typescriptreact',
'vue',
],
new GoCompletionItemProvider(configList),
'.'
);
const command = 'dot-log-replace';
const commandHandler = (
editor: vscode.TextEditor,
edit: vscode.TextEditorEdit,
position: vscode.Position,
config: ConfigItem
) => {
const lineText = editor.document.lineAt(position.line).text;
// match case name.log etc.
const matchVarReg = new RegExp(
`\(\[^\\s\]*\[^\'\"\`\]\).${config.trigger}$`
);
// match case 'name'.log etc. /(['"`])([^'"])\1.log/
const matchStrReg = new RegExp(
`\(\[\'\"\`\]\)\(\[^\'\"\`\]*\)\\1\.${config.trigger}$`
);
let matchFlag: 'var' | 'str' = 'var';
let text,
key,
quote = "'",
insertVal = '';
[text, key] = lineText.match(matchVarReg) || [];
if (!key) {
[text, quote, key] = lineText.match(matchStrReg) || [];
matchFlag = 'str';
}
// if matched
if (key) {
const index = lineText.indexOf(text);
edit.delete(
new vscode.Range(
position.with(undefined, index),
position.with(undefined, index + text.length)
)
);
const prefix = config.prefix || '';
const suffix = config.suffix || '';
if (matchFlag === 'var' && key.includes("'")) {
quote = '"';
}
// format like console.log("xxx", xxx)
if (matchFlag === 'var') {
// only console.log(xxx)
if (config.hideName === true) {
insertVal = `${config.format}(${key})`;
} else {
insertVal = `${config.format}(${quote}${prefix}${key}${suffix}${quote},${key})`;
}
}
// if key is string format like console.log("xxx")
if (matchFlag === 'str') {
insertVal = `${config.format}(${quote}${key}${quote})`;
}
edit.insert(position.with(undefined, index), insertVal);
}
return Promise.resolve([]);
};
context.subscriptions.push(
vscode.commands.registerTextEditorCommand(command, commandHandler)
);
context.subscriptions.push(options);
}
export function deactivate() {}