/
attrNameCase.ts
133 lines (121 loc) · 4.42 KB
/
attrNameCase.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
import * as vscode from 'vscode';
import { userPick } from './splitEditors';
import { CommonLanguageClient, State } from 'vscode-languageclient';
import * as shared from '@volar/shared';
export async function activate(context: vscode.ExtensionContext, languageClient: CommonLanguageClient) {
await languageClient.onReady();
await languageClient.sendRequest(shared.InitDoneRequest.type);
const attrCases = shared.createPathMap<'kebabCase' | 'camelCase'>();
const statusBar = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right);
statusBar.command = 'volar.action.attrNameCase';
onChangeDocument(vscode.window.activeTextEditor?.document);
const d_1 = vscode.window.onDidChangeActiveTextEditor(e => {
onChangeDocument(e?.document);
});
const d_2 = vscode.workspace.onDidCloseTextDocument((doc) => {
attrCases.uriDelete(doc.uri.toString());
});
const d_3 = vscode.commands.registerCommand('volar.action.attrNameCase', async () => {
const crtDoc = vscode.window.activeTextEditor?.document;
if (!crtDoc) return;
const attrCase = attrCases.uriGet(crtDoc.uri.toString());
const options: Record<string, vscode.QuickPickItem> = {};
options[4] = { label: (attrCase === 'kebabCase' ? '• ' : '') + 'Prop Using kebab-case' };
options[5] = { label: (attrCase === 'camelCase' ? '• ' : '') + 'Prop Using camelCase' };
options[6] = { label: 'Detect Prop name from Content' };
const select = await userPick(options);
if (select === undefined)
return; // cancle
if (select === '4') {
attrCases.uriSet(crtDoc.uri.toString(), 'kebabCase');
updateStatusBarText('kebabCase');
}
if (select === '5') {
attrCases.uriSet(crtDoc.uri.toString(), 'camelCase');
updateStatusBarText('camelCase');
}
if (select === '6') {
const detects = await languageClient.sendRequest(shared.DetectDocumentNameCasesRequest.type, languageClient.code2ProtocolConverter.asTextDocumentIdentifier(crtDoc));
if (detects) {
attrCases.uriSet(crtDoc.uri.toString(), getValidAttrCase(detects.attr));
updateStatusBarText(getValidAttrCase(detects.attr));
}
}
});
languageClient.onDidChangeState(e => {
if (e.newState === State.Stopped) {
d_1.dispose();
d_2.dispose();
d_3.dispose();
statusBar.dispose();
}
});
return (uri: string) => {
let attrCase = attrCases.uriGet(uri);
if (uri.toLowerCase() === vscode.window.activeTextEditor?.document.uri.toString().toLowerCase()) {
updateStatusBarText(attrCase);
}
return attrCase ?? 'kebabCase';
};
async function onChangeDocument(newDoc: vscode.TextDocument | undefined) {
if (newDoc?.languageId === 'vue') {
let attrCase = attrCases.uriGet(newDoc.uri.toString());
if (!attrCase) {
const attrMode = vscode.workspace.getConfiguration('volar').get<'auto-kebab' | 'auto-camel' | 'kebab' | 'camel'>('completion.preferredAttrNameCase');
if (attrMode === 'kebab') {
attrCase = 'kebabCase';
}
else if (attrMode === 'camel') {
attrCase = 'camelCase';
}
else {
const templateCases = await languageClient.sendRequest(shared.DetectDocumentNameCasesRequest.type, languageClient.code2ProtocolConverter.asTextDocumentIdentifier(newDoc));
if (templateCases) {
attrCase = getValidAttrCase(templateCases.attr);
if (templateCases.attr === 'both') {
if (attrMode === 'auto-kebab') {
attrCase = 'kebabCase';
}
else if (attrMode === 'auto-camel') {
attrCase = 'camelCase';
}
}
}
}
}
if (attrCase) {
attrCases.uriSet(newDoc.uri.toString(), attrCase ?? 'unsure');
}
updateStatusBarText(attrCase);
statusBar.show();
}
else {
statusBar.hide();
}
}
function getValidAttrCase(attrCase: 'both' | 'kebabCase' | 'camelCase' | 'unsure' | undefined): 'kebabCase' | 'camelCase' {
if (attrCase === 'both' || attrCase === 'unsure') {
const attrMode = vscode.workspace.getConfiguration('volar').get<'auto-kebab' | 'auto-camel' | 'kebab' | 'camel'>('completion.preferredAttrNameCase');
if (attrMode === 'auto-kebab') {
return 'kebabCase';
}
else if (attrMode === 'auto-camel') {
return 'camelCase';
}
return 'kebabCase';
}
return attrCase ?? 'kebabCase';
}
function updateStatusBarText(
attrCase: 'kebabCase' | 'camelCase' | undefined,
) {
let text = `Attr: `;
if (attrCase === 'kebabCase' || attrCase === undefined) {
text += `kebab-case`;
}
else if (attrCase === 'camelCase') {
text += `camelCase`;
}
statusBar.text = text;
}
}