-
-
Notifications
You must be signed in to change notification settings - Fork 366
/
nameCasing.ts
218 lines (187 loc) · 6.71 KB
/
nameCasing.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import { hyphenate } from '@vue/shared';
import { ServiceContext, VirtualFile } from '@volar/language-service';
import { checkComponentNames, getTemplateTagsAndAttrs, checkPropsOfTag, checkNativeTags } from '../helpers';
import * as vue from '@vue/language-core';
import type * as vscode from 'vscode-languageserver-protocol';
import { AttrNameCasing, TagNameCasing } from '../types';
export async function convertTagName(
ts: typeof import('typescript/lib/tsserverlibrary'),
context: ServiceContext,
uri: string,
casing: TagNameCasing,
) {
if (!context.typescript)
return;
const rootFile = context.documents.getSourceByUri(uri)?.root;
if (!(rootFile instanceof vue.VueFile))
return;
const desc = rootFile.sfc;
if (!desc.template)
return;
const template = desc.template;
const document = context.documents.getDocumentByFileName(rootFile.snapshot, rootFile.fileName);
const edits: vscode.TextEdit[] = [];
const nativeTags = checkNativeTags(ts, context.typescript.languageService, context.typescript.languageServiceHost);
const components = checkComponentNames(ts, context.typescript.languageService, rootFile, nativeTags);
const tags = getTemplateTagsAndAttrs(rootFile);
for (const [tagName, { offsets }] of tags) {
const componentName = components.find(component => component === tagName || hyphenate(component) === tagName);
if (componentName) {
for (const offset of offsets) {
const start = document.positionAt(template.startTagEnd + offset);
const end = document.positionAt(template.startTagEnd + offset + tagName.length);
const range: vscode.Range = { start, end };
if (casing === TagNameCasing.Kebab && tagName !== hyphenate(componentName)) {
edits.push({ range, newText: hyphenate(componentName) });
}
if (casing === TagNameCasing.Pascal && tagName !== componentName) {
edits.push({ range, newText: componentName });
}
}
}
}
return edits;
}
export async function convertAttrName(
ts: typeof import('typescript/lib/tsserverlibrary'),
context: ServiceContext,
uri: string,
casing: AttrNameCasing,
) {
if (!context.typescript)
return;
const rootFile = context.documents.getSourceByUri(uri)?.root;
if (!(rootFile instanceof vue.VueFile))
return;
const desc = rootFile.sfc;
if (!desc.template)
return;
const template = desc.template;
const document = context.documents.getDocumentByFileName(rootFile.snapshot, rootFile.fileName);
const edits: vscode.TextEdit[] = [];
const nativeTags = checkNativeTags(ts, context.typescript.languageService, context.typescript.languageServiceHost);
const components = checkComponentNames(ts, context.typescript.languageService, rootFile, nativeTags);
const tags = getTemplateTagsAndAttrs(rootFile);
for (const [tagName, { attrs }] of tags) {
const componentName = components.find(component => component === tagName || hyphenate(component) === tagName);
if (componentName) {
const props = checkPropsOfTag(ts, context.typescript.languageService, rootFile, componentName, nativeTags);
for (const [attrName, { offsets }] of attrs) {
const propName = props.find(prop => prop === attrName || hyphenate(prop) === attrName);
if (propName) {
for (const offset of offsets) {
const start = document.positionAt(template.startTagEnd + offset);
const end = document.positionAt(template.startTagEnd + offset + attrName.length);
const range: vscode.Range = { start, end };
if (casing === AttrNameCasing.Kebab && attrName !== hyphenate(propName)) {
edits.push({ range, newText: hyphenate(propName) });
}
if (casing === AttrNameCasing.Camel && attrName !== propName) {
edits.push({ range, newText: propName });
}
}
}
}
}
}
return edits;
}
export async function getNameCasing(
ts: typeof import('typescript/lib/tsserverlibrary'),
context: ServiceContext,
uri: string,
) {
const detected = detect(ts, context, uri);
const [attr, tag] = await Promise.all([
context.env.getConfiguration?.<'autoKebab' | 'autoCamel' | 'kebab' | 'camel'>('vue.complete.casing.props', uri),
context.env.getConfiguration?.<'autoKebab' | 'autoPascal' | 'kebab' | 'pascal'>('vue.complete.casing.tags', uri),
]);
const tagNameCasing = detected.tag.length === 1 && (tag === 'autoPascal' || tag === 'autoKebab') ? detected.tag[0] : (tag === 'autoKebab' || tag === 'kebab') ? TagNameCasing.Kebab : TagNameCasing.Pascal;
const attrNameCasing = detected.attr.length === 1 && (attr === 'autoCamel' || attr === 'autoKebab') ? detected.attr[0] : (attr === 'autoCamel' || attr === 'camel') ? AttrNameCasing.Camel : AttrNameCasing.Kebab;
return {
tag: tagNameCasing,
attr: attrNameCasing,
};
}
export function detect(
ts: typeof import('typescript/lib/tsserverlibrary'),
context: ServiceContext,
uri: string,
): {
tag: TagNameCasing[],
attr: AttrNameCasing[],
} {
if (!context.typescript) {
return {
tag: [],
attr: [],
};
}
const rootFile = context.documents.getSourceByUri(uri)?.root;
if (!(rootFile instanceof vue.VueFile)) {
return {
tag: [],
attr: [],
};
}
return {
tag: getTagNameCase(rootFile),
attr: getAttrNameCase(rootFile),
};
function getAttrNameCase(file: VirtualFile): AttrNameCasing[] {
const tags = getTemplateTagsAndAttrs(file);
const result: AttrNameCasing[] = [];
for (const [_, { attrs }] of tags) {
for (const [tagName] of attrs) {
// attrName
if (tagName !== hyphenate(tagName)) {
result.push(AttrNameCasing.Camel);
break;
}
}
for (const [tagName] of attrs) {
// attr-name
if (tagName.indexOf('-') >= 0) {
result.push(AttrNameCasing.Kebab);
break;
}
}
}
return result;
}
function getTagNameCase(file: VirtualFile): TagNameCasing[] {
if (!context.typescript) {
return [];
}
const nativeTags = checkNativeTags(ts, context.typescript.languageService, context.typescript.languageServiceHost);
const components = checkComponentNames(ts, context.typescript.languageService, file, nativeTags);
const tagNames = getTemplateTagsAndAttrs(file);
const result: TagNameCasing[] = [];
let anyComponentUsed = false;
for (const component of components) {
if (tagNames.has(component) || tagNames.has(hyphenate(component))) {
anyComponentUsed = true;
break;
}
}
if (!anyComponentUsed) {
return []; // not sure component style, because do not have any component using in <template> for check
}
for (const [tagName] of tagNames) {
// TagName
if (tagName !== hyphenate(tagName)) {
result.push(TagNameCasing.Pascal);
break;
}
}
for (const component of components) {
// Tagname -> tagname
// TagName -> tag-name
if (component !== hyphenate(component) && tagNames.has(hyphenate(component))) {
result.push(TagNameCasing.Kebab);
break;
}
}
return result;
}
}