Skip to content

Commit

Permalink
feat(vscode): configure multiple subdirs (#2101)
Browse files Browse the repository at this point in the history
  • Loading branch information
MellKam committed Jan 25, 2023
1 parent 3b414da commit 3b5fad4
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 22 deletions.
8 changes: 7 additions & 1 deletion packages/vscode/package.json
Expand Up @@ -41,7 +41,13 @@
"description": "Disable the UnoCSS extension"
},
"unocss.root": {
"type": "string",
"type": [
"array",
"string"
],
"items": {
"type": "string"
},
"description": "Project root that contains the UnoCSS configuration file"
},
"unocss.underline": {
Expand Down
75 changes: 54 additions & 21 deletions packages/vscode/src/index.ts
@@ -1,5 +1,5 @@
import path from 'path'
import type { ExtensionContext } from 'vscode'
import type { ExtensionContext, StatusBarItem } from 'vscode'
import { StatusBarAlignment, commands, window, workspace } from 'vscode'
import { version } from '../package.json'
import { log } from './log'
Expand All @@ -8,6 +8,23 @@ import { registerAutoComplete } from './autocomplete'
import { ContextLoader } from './contextLoader'
import { registerSelectionStyle } from './selectionStyle'

const registerRoot = async (ext: ExtensionContext, status: StatusBarItem, cwd: string) => {
const contextLoader = new ContextLoader(cwd)

await contextLoader.ready

const hasConfig = await contextLoader.loadContextInDirectory(cwd)
// TODO: improve this to re-enable after configuration created
if (!hasConfig)
throw new Error(`βž– No config found in ${cwd}`)

registerAutoComplete(cwd, contextLoader, ext)
registerAnnotations(cwd, contextLoader, status, ext)
registerSelectionStyle(cwd, contextLoader)

return contextLoader
}

export async function activate(ext: ExtensionContext) {
log.appendLine(`βšͺ️ UnoCSS for VS Code v${version}\n`)

Expand All @@ -24,34 +41,50 @@ export async function activate(ext: ExtensionContext) {
return
}

const root = config.get<string>('root')
const cwd = root ? path.resolve(projectPath, root) : projectPath
const status = window.createStatusBarItem(StatusBarAlignment.Right, 200)
status.text = 'UnoCSS'

const contextLoader = new ContextLoader(cwd)
const root = config.get<string | string[]>('root')

ext.subscriptions.push(
commands.registerCommand('unocss.reload', async () => {
log.appendLine('πŸ” Reloading...')
await contextLoader.reload()
log.appendLine('βœ… Reloaded.')
}),
)
if (Array.isArray(root) && root.length) {
const cwds = root.map(dir => path.resolve(projectPath, dir))

await contextLoader.ready
try {
const contextLoaders = await Promise.all(cwds.map(cwd => registerRoot(ext, status, cwd)))

const hasConfig = await contextLoader.loadContextInDirectory(cwd)
// TODO: improve this to re-enable after configuration created
if (!hasConfig) {
log.appendLine('βž– No config found, UnoCSS is disabled')
ext.subscriptions.push(
commands.registerCommand('unocss.reload', async () => {
log.appendLine('πŸ” Reloading...')
await Promise.all(contextLoaders.map(ctxLoader => ctxLoader.reload))
log.appendLine('βœ… Reloaded.')
}),
)
}
catch (error) {
log.appendLine((error as Error).message)
}
return
}

const status = window.createStatusBarItem(StatusBarAlignment.Right, 200)
status.text = 'UnoCSS'
// now if the root is an array, then it is an empty array
const cwd = root && !Array.isArray(root)
? path.resolve(projectPath, root)
: projectPath

registerAutoComplete(cwd, contextLoader, ext)
registerAnnotations(cwd, contextLoader, status, ext)
registerSelectionStyle(cwd, contextLoader)
try {
const contextLoader = await registerRoot(ext, status, cwd)

ext.subscriptions.push(
commands.registerCommand('unocss.reload', async () => {
log.appendLine('πŸ” Reloading...')
await contextLoader.reload()
log.appendLine('βœ… Reloaded.')
}),
)
}
catch (error) {
log.appendLine((error as Error).message)
}
}

export function deactivate() {}

0 comments on commit 3b5fad4

Please sign in to comment.