Skip to content

Commit ab4cd86

Browse files
committed
refactor(vscode): consolidate component name resolution logic
1 parent 2491af3 commit ab4cd86

2 files changed

Lines changed: 24 additions & 7 deletions

File tree

packages/vscode/src/commands/openDocs.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
import { env, Uri, window } from 'vscode'
2-
import { getComponentAtPosition } from '../utils/component'
2+
import { resolveComponentName } from '../utils/component'
33
import componentMap from '../utils/component-map.json'
44

5-
async function openDocs (type: 'api' | 'component', componentName?: string) {
5+
async function openDocs (type: 'api' | 'component', componentName?: string | Uri) {
66
const editor = window.activeTextEditor
77
if (!editor) {
88
return
99
}
1010

11-
if (!componentName) {
12-
const selection = editor.selection
13-
componentName = getComponentAtPosition(editor.document, selection.active) || undefined
14-
}
11+
componentName = resolveComponentName(componentName, editor)
1512

1613
if (!componentName) {
1714
window.showInformationMessage('No Vuetify component found at cursor position')

packages/vscode/src/utils/component.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import type { Position, TextDocument } from 'vscode'
1+
import type { Position, TextDocument, TextEditor, Uri } from 'vscode'
2+
import componentMap from './component-map.json'
3+
4+
const componentPattern = /^(v-[a-z0-9-]+|V[A-Z][a-zA-Z0-9]+)$/
25

36
export function kebabCase (str: string) {
47
return str.replace(/([a-z0-9])([A-Z])/g, '$1-$2').toLowerCase()
@@ -21,3 +24,20 @@ export function getComponentAtPosition (document: TextDocument, position: Positi
2124

2225
return componentName
2326
}
27+
28+
export function resolveComponentName (componentName: string | Uri | undefined, editor: TextEditor | undefined) {
29+
let resolved: string | undefined
30+
31+
if (typeof componentName === 'string' && componentPattern.test(componentName.trim())) {
32+
resolved = componentName.trim()
33+
} else if (editor) {
34+
const selection = editor.selection
35+
resolved = getComponentAtPosition(editor.document, selection.active) || undefined
36+
}
37+
38+
if (!resolved || !Object.prototype.hasOwnProperty.call(componentMap, resolved)) {
39+
return undefined
40+
}
41+
42+
return resolved
43+
}

0 commit comments

Comments
 (0)