Skip to content

Commit

Permalink
feat(document-links): Namespaces in schema and mock references (Fix #115
Browse files Browse the repository at this point in the history
)
  • Loading branch information
mvsde committed May 3, 2024
1 parent 1564a8e commit 07d0c0b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
9 changes: 6 additions & 3 deletions src/lib/document-links-mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import path from 'node:path'
import vscode from 'vscode'

import { MOCKS_GLOB } from '../constants'
import { resolveNamespace } from '../utils/resolve-namespace'
import { getProject } from './projects'

type TYPES = 'ref' | 'tpl'
Expand Down Expand Up @@ -55,20 +56,22 @@ const provideDocumentLinks: ProvideDocumentLinks = function (document, token) {

const contentStart = start + match[0].indexOf(reference)
const contentEnd = contentStart + reference.length
const [componentPath] = reference.split('#')

let [id] = reference.split('#')
id = resolveNamespace({ project, id })

const range = new vscode.Range(
document.positionAt(contentStart),
document.positionAt(contentEnd)
)

const filename = filenames[type].replace('<component>', path.basename(componentPath))
const filename = filenames[type].replace('<component>', path.basename(id))
const extension = extensions[type]

const target = vscode.Uri.joinPath(
project.uri,
project.config.components.folder,
componentPath,
id,
`${filename}.${extension}`
)

Expand Down
5 changes: 4 additions & 1 deletion src/lib/document-links-schema.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import vscode from 'vscode'

import { SCHEMA_GLOB } from '../constants'
import { resolveNamespace } from '../utils/resolve-namespace'
import { getProject } from './projects'

interface DocumentLink extends vscode.DocumentLink {
Expand Down Expand Up @@ -46,7 +47,9 @@ const provideDocumentLinks: ProvideDocumentLinks = function (document, token) {

const contentStart = start + match[0].indexOf(reference)
const contentEnd = contentStart + reference.length
const [id] = reference.split('#')

let [id] = reference.split('#')
id = resolveNamespace({ project, id })

const range = new vscode.Range(
document.positionAt(contentStart),
Expand Down
29 changes: 29 additions & 0 deletions src/utils/resolve-namespace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Project } from '../types'

type ResolveNamespaceOptions = {
project: Project
id: string
}

export function resolveNamespace ({ project, id }: ResolveNamespaceOptions): string {
const namespaces = project.config.engine?.options?.namespaces

if (!namespaces) {
return id
}

for (const [name, path] of Object.entries(namespaces)) {
if (!path) {
continue
}

const namespaceReference = `@${name}`
const namespacePath = path.replace(project.config.components.folder, '')

if (id.startsWith(namespaceReference)) {
return id.replace(namespaceReference, namespacePath)
}
}

return id
}

0 comments on commit 07d0c0b

Please sign in to comment.