Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(vscode): configure multiple subdirs #2101

Merged
merged 1 commit into from
Jan 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion packages/vscode/package.json
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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() {}