Skip to content

Commit f12cb2f

Browse files
committed
refactor: map
1 parent b120133 commit f12cb2f

File tree

63 files changed

+11482
-11170
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+11482
-11170
lines changed

scripts/generateIndex.ts

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,20 @@ export async function run() {
2525
})
2626
const template
2727
= `import { componentsReducer, propsReducer } from '../../utils'
28-
${imports.join('\n')}
28+
import { getComponentsMap, getPropsMap } from './mapping'
2929
// import directives from '../directives.json'
3030
31-
const map: any = [
32-
${entry.map((_url: string) => `${_url.split('.')[0]},`).join('\n ')}
33-
]
3431
export function ${name}() {
35-
return propsReducer('${lib}', map, '${prefix}')
32+
return propsReducer({
33+
uiName: '${lib}',
34+
lib: '${lib}',
35+
map: getPropsMap(),
36+
})
3637
}
3738
38-
const componentsMap = [
39-
${map.join('\n ')}
40-
]
41-
4239
export function ${name}Components() {
4340
return componentsReducer({
44-
map: componentsMap,
41+
map: getComponentsMap(),
4542
isSeperatorByHyphen: ${isHyphen},
4643
prefix: '${prefix}',
4744
isReact: ${isReact},
@@ -50,7 +47,22 @@ export function ${name}Components() {
5047
})
5148
}
5249
`
50+
// 生成 index.ts
5351
fsp.writeFile(path.resolve(root, `${folder}/${name}/index.ts`), template)
52+
// 生成 mapping.ts
53+
fsp.writeFile(path.resolve(root, `${folder}/${name}/mapping.ts`), `${imports.join('\n')}
54+
55+
export function getPropsMap() {
56+
return [
57+
${entry.map((_url: string) => `${_url.split('.')[0]},`).join('\n ')}
58+
]
59+
}
60+
61+
export function getComponentsMap() {
62+
return [
63+
${map.join('\n ')}
64+
]
65+
}`)
5466
}
5567

5668
run()

src/index.ts

Lines changed: 26 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ export function activate(context: vscode.ExtensionContext) {
224224
const endColumn = Math.max(pos.column - 1, 0)
225225
updateText((edit) => {
226226
if (isVine())
227-
edit.insert(getPosition(pos.offset + offset), `\n${empty}<template ${slotName}></template>`)
227+
edit.insert(getPosition(pos.offset + offset).position, `\n${empty}<template ${slotName}></template>`)
228228
else
229229
edit.insert(createPosition(pos.line - 1, endColumn), `\n${empty}<template ${slotName}></template>`)
230230
})
@@ -246,7 +246,7 @@ export function activate(context: vscode.ExtensionContext) {
246246
const pos = getPosition(index)
247247
updateText((edit) => {
248248
if (isVine())
249-
edit.insert(getPosition(pos.offset + offset), `${isNeedLineBlock ? '\n' : ''}${empty} <template ${slotName}></template>\n${isNeedLineBlock ? empty : ''}`)
249+
edit.insert(getPosition(pos.offset + offset).position, `${isNeedLineBlock ? '\n' : ''}${empty} <template ${slotName}></template>\n${isNeedLineBlock ? empty : ''}`)
250250
else
251251
edit.insert(createPosition(pos), `${isNeedLineBlock ? '\n' : ''}${empty} <template ${slotName}></template>\n${isNeedLineBlock ? empty : ''}`)
252252
})
@@ -329,7 +329,7 @@ export function activate(context: vscode.ExtensionContext) {
329329
return UiCompletions.icons
330330

331331
const propName = result.propName
332-
let target = UiCompletions[name] || await findDynamicComponent(name, deps)
332+
let target = await findDynamicComponent(name, deps, uiDeps[name])
333333
const importUiSource = uiDeps[name]
334334
if (importUiSource && target.uiName !== importUiSource) {
335335
for (const p of optionsComponents.prefix.filter(Boolean)) {
@@ -593,6 +593,7 @@ export function activate(context: vscode.ExtensionContext) {
593593
return
594594

595595
const code = document.getText()
596+
const uiDeps = getUiDeps(code)
596597
// word 修正
597598
if (lineText[range.end.character] === '.' || lineText[range.end.character] === '-') {
598599
let index = range.end.character
@@ -601,7 +602,6 @@ export function activate(context: vscode.ExtensionContext) {
601602
index++
602603
}
603604
}
604-
605605
if (lineText[range.start.character - 1] === '.') {
606606
let index = range.start.character - 1
607607
while (!/[<\s/]/.test(lineText[index]) && index >= 0) {
@@ -616,33 +616,16 @@ export function activate(context: vscode.ExtensionContext) {
616616
if (result.type === 'tag') {
617617
const data = optionsComponents.data.map((c: any) => c()).flat()
618618
if (!data?.length || !word)
619-
return new vscode.Hover('')
619+
return createHover('')
620620
const tag = toCamel(result.tag)[0].toUpperCase() + toCamel(result.tag).slice(1)
621-
let target = UiCompletions[tag] || await findDynamicComponent(tag, {})
622-
if (!target)
623-
return
624-
const uiDeps = getUiDeps(code)
625-
const importUiSource = uiDeps[tag]
626-
if (importUiSource && target.uiName !== importUiSource) {
627-
for (const p of optionsComponents.prefix.filter(Boolean)) {
628-
const realName = p[0].toUpperCase() + p.slice(1) + tag
629-
const newTarget = UiCompletions[realName]
630-
if (!newTarget)
631-
continue
632-
if (newTarget.uiName === importUiSource) {
633-
target = newTarget
634-
break
635-
}
636-
}
637-
}
638-
621+
const target = await findDynamicComponent(tag, {}, uiDeps[tag])
639622
if (!target)
640623
return
641624

642625
const tableDocument = target.tableDocument
643626

644627
if (tableDocument)
645-
return new vscode.Hover(tableDocument)
628+
return createHover(tableDocument)
646629
}
647630
else if (!result.propName) {
648631
return
@@ -661,7 +644,7 @@ export function activate(context: vscode.ExtensionContext) {
661644
const detail = getHoverAttribute(completions, propName)
662645
if (!detail)
663646
return
664-
return new vscode.Hover(`## Details \n\n${detail}`)
647+
return createHover(`## Details \n\n${detail}`)
665648
}
666649
// todo: 优化这里的条件,在 react 中, 也可以减少更多的处理步骤
667650
if (isVue()) {
@@ -769,33 +752,16 @@ export function activate(context: vscode.ExtensionContext) {
769752
}
770753
const data = optionsComponents.data.map((c: any) => c()).flat()
771754
if (!data?.length || !word)
772-
return new vscode.Hover('')
755+
return createHover('')
773756
word = toCamel(word)[0].toUpperCase() + toCamel(word).slice(1)
774-
let target = UiCompletions[word] || await findDynamicComponent(word, {})
775-
if (!target)
776-
return
777-
const uiDeps = getUiDeps(code)
778-
const importUiSource = uiDeps[word]
779-
if (importUiSource && target.uiName !== importUiSource) {
780-
for (const p of optionsComponents.prefix.filter(Boolean)) {
781-
const realName = p[0].toUpperCase() + p.slice(1) + word
782-
const newTarget = UiCompletions[realName]
783-
if (!newTarget)
784-
continue
785-
if (newTarget.uiName === importUiSource) {
786-
target = newTarget
787-
break
788-
}
789-
}
790-
}
791-
757+
const target = await findDynamicComponent(word, {}, uiDeps[word])
792758
if (!target)
793759
return
794760

795761
const tableDocument = target.tableDocument
796762

797763
if (tableDocument)
798-
return new vscode.Hover(tableDocument)
764+
return createHover(tableDocument)
799765
},
800766
}))
801767
}
@@ -984,29 +950,38 @@ async function getTemplateParentElementName(url: string) {
984950
return result
985951
}
986952

987-
export async function findDynamicComponent(name: string, deps: Record<string, string>) {
953+
export async function findDynamicComponent(name: string, deps: Record<string, string>, from?: string) {
988954
const prefix = optionsComponents.prefix
989-
let target = findDynamic(name, prefix)
955+
let target = findDynamic(name, prefix, from)
956+
if (target)
957+
return target
990958

991959
let dep
992-
if (!target && (dep = deps[name])) {
960+
if (dep = deps[name]) {
993961
// 只往下找一层
994962
const tag = await getTemplateParentElementName(getAbsoluteUrl(dep))
995963
if (!tag)
996964
return
997-
target = findDynamic(tag, prefix)
965+
target = findDynamic(tag, prefix, from)
998966
}
999967
return target
1000968
}
1001969

1002-
function findDynamic(tag: string, prefix: string[]) {
970+
function findDynamic(tag: string, prefix: string[], from?: string) {
1003971
let target = UiCompletions[tag]
972+
if (target && from && target.lib !== from) {
973+
target = null
974+
}
1004975
if (!target) {
1005976
for (const p of prefix) {
1006977
if (!p)
1007978
continue
1008979
const t = UiCompletions[p[0].toUpperCase() + p.slice(1) + tag]
1009-
if (t) {
980+
if (from && t && t.lib === from) {
981+
target = t
982+
break
983+
}
984+
else if (t) {
1010985
target = t
1011986
break
1012987
}

0 commit comments

Comments
 (0)