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): If uno.root is not set, auto-detection #3069

Merged
merged 14 commits into from
Sep 26, 2023
3 changes: 2 additions & 1 deletion packages/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
"jiti": "^1.19.3",
"prettier": "^2.8.8",
"tsup": "^7.2.0",
"unconfig": "^0.3.10"
"unconfig": "^0.3.10",
"find-up": "^6.3.0"

Check failure on line 115 in packages/vscode/package.json

View workflow job for this annotation

GitHub Actions / lint

Expected object keys to be in ascending order. 'find-up' should be before 'unconfig'
}
}
61 changes: 36 additions & 25 deletions packages/vscode/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import path from 'path'
import type { ExtensionContext, StatusBarItem } from 'vscode'
import { StatusBarAlignment, commands, window, workspace } from 'vscode'
import { findUp } from 'find-up'
import { version } from '../package.json'
import { log } from './log'
import { registerAnnotations } from './annotation'
Expand All @@ -11,7 +12,6 @@ import { isFulfilled, isRejected } from './utils'

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

await contextLoader.ready

const hasConfig = await contextLoader.loadContextInDirectory(cwd)
Expand Down Expand Up @@ -47,7 +47,6 @@ export async function activate(ext: ExtensionContext) {

if (Array.isArray(root) && root.length) {
const cwds = root.map(dir => path.resolve(projectPath, dir))

const contextLoadersResult = await Promise.allSettled(
cwds.map(cwd => registerRoot(ext, status, cwd)),
)
Expand All @@ -70,29 +69,41 @@ export async function activate(ext: ExtensionContext) {
}
return
}

// now if the root is an array, then it is an empty array
const cwd = (root && !Array.isArray(root))
? path.resolve(projectPath, root)
: projectPath

const contextLoader = await registerRoot(ext, status, cwd)
ext.subscriptions.push(
commands.registerCommand('unocss.reload', async () => {
log.appendLine('🔁 Reloading...')
if (contextLoader.contextsMap.get(cwd) === null) {
contextLoader.contextsMap.delete(cwd)
const hasConfig = await contextLoader.loadContextInDirectory(cwd)
if (hasConfig) {
registerAutoComplete(cwd, contextLoader, ext)
registerAnnotations(cwd, contextLoader, status, ext)
registerSelectionStyle(cwd, contextLoader)
}
}
contextLoader.reload()
log.appendLine('✅ Reloaded.')
}),
)

const cacheMap = new Set()
Simon-He95 marked this conversation as resolved.
Show resolved Hide resolved

const registerUnocss = async () => {
const url = window.activeTextEditor?.document.uri.fsPath
if (!url)
return
if (/node_modules/.test(url))
Simon-He95 marked this conversation as resolved.
Show resolved Hide resolved
return
const target = await findUp(['package.json'], { cwd: url })
Simon-He95 marked this conversation as resolved.
Show resolved Hide resolved
if (!target)
return
const cwd = target.slice(0, -13)
Simon-He95 marked this conversation as resolved.
Show resolved Hide resolved
if (cacheMap.has(cwd))
return
cacheMap.add(cwd)
const contextLoader = await registerRoot(ext, status, cwd)

ext.subscriptions.push(
commands.registerCommand('unocss.reload', async () => {
log.appendLine('🔁 Reloading...')
await contextLoader.reload()
log.appendLine('✅ Reloaded.')
}),
)
}

try {
await registerUnocss()
ext.subscriptions.push(window.onDidChangeActiveTextEditor(registerUnocss))
}
catch (e: any) {
log.appendLine(String(e.stack ?? e))
}
}

export function deactivate() {}
export function deactivate() { }
33 changes: 30 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.