Skip to content

Commit

Permalink
refactor: map
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon-He95 committed Aug 21, 2024
1 parent b120133 commit f12cb2f
Show file tree
Hide file tree
Showing 63 changed files with 11,482 additions and 11,170 deletions.
32 changes: 22 additions & 10 deletions scripts/generateIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,20 @@ export async function run() {
})
const template
= `import { componentsReducer, propsReducer } from '../../utils'
${imports.join('\n')}
import { getComponentsMap, getPropsMap } from './mapping'
// import directives from '../directives.json'
const map: any = [
${entry.map((_url: string) => `${_url.split('.')[0]},`).join('\n ')}
]
export function ${name}() {
return propsReducer('${lib}', map, '${prefix}')
return propsReducer({
uiName: '${lib}',
lib: '${lib}',
map: getPropsMap(),
})
}
const componentsMap = [
${map.join('\n ')}
]
export function ${name}Components() {
return componentsReducer({
map: componentsMap,
map: getComponentsMap(),
isSeperatorByHyphen: ${isHyphen},
prefix: '${prefix}',
isReact: ${isReact},
Expand All @@ -50,7 +47,22 @@ export function ${name}Components() {
})
}
`
// 生成 index.ts
fsp.writeFile(path.resolve(root, `${folder}/${name}/index.ts`), template)
// 生成 mapping.ts
fsp.writeFile(path.resolve(root, `${folder}/${name}/mapping.ts`), `${imports.join('\n')}
export function getPropsMap() {
return [
${entry.map((_url: string) => `${_url.split('.')[0]},`).join('\n ')}
]
}
export function getComponentsMap() {
return [
${map.join('\n ')}
]
}`)
}

run()
Expand Down
77 changes: 26 additions & 51 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ export function activate(context: vscode.ExtensionContext) {
const endColumn = Math.max(pos.column - 1, 0)
updateText((edit) => {
if (isVine())
edit.insert(getPosition(pos.offset + offset), `\n${empty}<template ${slotName}></template>`)
edit.insert(getPosition(pos.offset + offset).position, `\n${empty}<template ${slotName}></template>`)
else
edit.insert(createPosition(pos.line - 1, endColumn), `\n${empty}<template ${slotName}></template>`)
})
Expand All @@ -246,7 +246,7 @@ export function activate(context: vscode.ExtensionContext) {
const pos = getPosition(index)
updateText((edit) => {
if (isVine())
edit.insert(getPosition(pos.offset + offset), `${isNeedLineBlock ? '\n' : ''}${empty} <template ${slotName}></template>\n${isNeedLineBlock ? empty : ''}`)
edit.insert(getPosition(pos.offset + offset).position, `${isNeedLineBlock ? '\n' : ''}${empty} <template ${slotName}></template>\n${isNeedLineBlock ? empty : ''}`)
else
edit.insert(createPosition(pos), `${isNeedLineBlock ? '\n' : ''}${empty} <template ${slotName}></template>\n${isNeedLineBlock ? empty : ''}`)
})
Expand Down Expand Up @@ -329,7 +329,7 @@ export function activate(context: vscode.ExtensionContext) {
return UiCompletions.icons

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

const code = document.getText()
const uiDeps = getUiDeps(code)
// word 修正
if (lineText[range.end.character] === '.' || lineText[range.end.character] === '-') {
let index = range.end.character
Expand All @@ -601,7 +602,6 @@ export function activate(context: vscode.ExtensionContext) {
index++
}
}

if (lineText[range.start.character - 1] === '.') {
let index = range.start.character - 1
while (!/[<\s/]/.test(lineText[index]) && index >= 0) {
Expand All @@ -616,33 +616,16 @@ export function activate(context: vscode.ExtensionContext) {
if (result.type === 'tag') {
const data = optionsComponents.data.map((c: any) => c()).flat()
if (!data?.length || !word)
return new vscode.Hover('')
return createHover('')
const tag = toCamel(result.tag)[0].toUpperCase() + toCamel(result.tag).slice(1)
let target = UiCompletions[tag] || await findDynamicComponent(tag, {})
if (!target)
return
const uiDeps = getUiDeps(code)
const importUiSource = uiDeps[tag]
if (importUiSource && target.uiName !== importUiSource) {
for (const p of optionsComponents.prefix.filter(Boolean)) {
const realName = p[0].toUpperCase() + p.slice(1) + tag
const newTarget = UiCompletions[realName]
if (!newTarget)
continue
if (newTarget.uiName === importUiSource) {
target = newTarget
break
}
}
}

const target = await findDynamicComponent(tag, {}, uiDeps[tag])
if (!target)
return

const tableDocument = target.tableDocument

if (tableDocument)
return new vscode.Hover(tableDocument)
return createHover(tableDocument)
}
else if (!result.propName) {
return
Expand All @@ -661,7 +644,7 @@ export function activate(context: vscode.ExtensionContext) {
const detail = getHoverAttribute(completions, propName)
if (!detail)
return
return new vscode.Hover(`## Details \n\n${detail}`)
return createHover(`## Details \n\n${detail}`)
}
// todo: 优化这里的条件,在 react 中, 也可以减少更多的处理步骤
if (isVue()) {
Expand Down Expand Up @@ -769,33 +752,16 @@ export function activate(context: vscode.ExtensionContext) {
}
const data = optionsComponents.data.map((c: any) => c()).flat()
if (!data?.length || !word)
return new vscode.Hover('')
return createHover('')
word = toCamel(word)[0].toUpperCase() + toCamel(word).slice(1)
let target = UiCompletions[word] || await findDynamicComponent(word, {})
if (!target)
return
const uiDeps = getUiDeps(code)
const importUiSource = uiDeps[word]
if (importUiSource && target.uiName !== importUiSource) {
for (const p of optionsComponents.prefix.filter(Boolean)) {
const realName = p[0].toUpperCase() + p.slice(1) + word
const newTarget = UiCompletions[realName]
if (!newTarget)
continue
if (newTarget.uiName === importUiSource) {
target = newTarget
break
}
}
}

const target = await findDynamicComponent(word, {}, uiDeps[word])
if (!target)
return

const tableDocument = target.tableDocument

if (tableDocument)
return new vscode.Hover(tableDocument)
return createHover(tableDocument)
},
}))
}
Expand Down Expand Up @@ -984,29 +950,38 @@ async function getTemplateParentElementName(url: string) {
return result
}

export async function findDynamicComponent(name: string, deps: Record<string, string>) {
export async function findDynamicComponent(name: string, deps: Record<string, string>, from?: string) {
const prefix = optionsComponents.prefix
let target = findDynamic(name, prefix)
let target = findDynamic(name, prefix, from)
if (target)
return target

let dep
if (!target && (dep = deps[name])) {
if (dep = deps[name]) {
// 只往下找一层
const tag = await getTemplateParentElementName(getAbsoluteUrl(dep))
if (!tag)
return
target = findDynamic(tag, prefix)
target = findDynamic(tag, prefix, from)
}
return target
}

function findDynamic(tag: string, prefix: string[]) {
function findDynamic(tag: string, prefix: string[], from?: string) {
let target = UiCompletions[tag]
if (target && from && target.lib !== from) {
target = null
}
if (!target) {
for (const p of prefix) {
if (!p)
continue
const t = UiCompletions[p[0].toUpperCase() + p.slice(1) + tag]
if (t) {
if (from && t && t.lib === from) {
target = t
break
}
else if (t) {
target = t
break
}
Expand Down
Loading

0 comments on commit f12cb2f

Please sign in to comment.