Skip to content

Commit

Permalink
fix(vscode): respect unocss.root (#1169)
Browse files Browse the repository at this point in the history
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
  • Loading branch information
holazz and antfu committed Jun 30, 2022
1 parent d27f5af commit 307417e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
11 changes: 7 additions & 4 deletions packages/vscode/src/annotation.ts
Expand Up @@ -3,7 +3,7 @@ import type { DecorationOptions, ExtensionContext, StatusBarItem } from 'vscode'
import { DecorationRangeBehavior, MarkdownString, Range, window, workspace } from 'vscode'
import { INCLUDE_COMMENT_IDE, getMatchedPositions } from './integration'
import { log } from './log'
import { getColorsMap, getPrettiedMarkdown, isCssId, throttle } from './utils'
import { getColorsMap, getPrettiedMarkdown, isCssId, isSubdir, throttle } from './utils'
import type { ContextLoader } from './contextLoader'

export async function registerAnnotations(
Expand Down Expand Up @@ -78,17 +78,20 @@ export async function registerAnnotations(
if (!doc)
return reset()

const code = doc.getText()
const id = doc.uri.fsPath
if (!isSubdir(cwd, id))
return reset()

const code = doc.getText()
if (!code)
return reset()

let ctx = await contextLoader.resolveContext(code, id)
if (!ctx)
ctx = await contextLoader.resolveClosestContext(code, id)
else if (!ctx.filter(code, id) && !code.includes(INCLUDE_COMMENT_IDE) && !isCssId(id))
return null

if (!ctx.filter(code, id) && !code.includes(INCLUDE_COMMENT_IDE) && !isCssId(id))
return reset()

const result = await ctx.uno.generate(code, { id, preflights: false, minify: true })

Expand Down
10 changes: 7 additions & 3 deletions packages/vscode/src/autocomplete.ts
Expand Up @@ -3,7 +3,7 @@ import { createAutocomplete } from '@unocss/autocomplete'
import type { CompletionItemProvider, ExtensionContext } from 'vscode'
import { CompletionItem, CompletionItemKind, CompletionList, MarkdownString, Range, languages } from 'vscode'
import type { UnoGenerator, UnocssPluginContext } from '@unocss/core'
import { getPrettiedMarkdown, isCssId } from './utils'
import { getPrettiedMarkdown, isCssId, isSubdir } from './utils'
import { log } from './log'
import type { ContextLoader } from './contextLoader'

Expand Down Expand Up @@ -36,6 +36,7 @@ class UnoCompletionItem extends CompletionItem {
}

export async function registerAutoComplete(
cwd: string,
contextLoader: ContextLoader,
ext: ExtensionContext,
) {
Expand Down Expand Up @@ -64,16 +65,19 @@ export async function registerAutoComplete(

const provider: CompletionItemProvider<UnoCompletionItem> = {
async provideCompletionItems(doc, position) {
const code = doc.getText()
const id = doc.uri.fsPath
if (!isSubdir(cwd, id))
return null

const code = doc.getText()
if (!code)
return null

let ctx = await contextLoader.resolveContext(code, id)
if (!ctx)
ctx = await contextLoader.resolveClosestContext(code, id)
else if (!ctx.filter(code, id) && !isCssId(id))

if (!ctx.filter(code, id) && !isCssId(id))
return null

try {
Expand Down
7 changes: 6 additions & 1 deletion packages/vscode/src/index.ts
Expand Up @@ -19,12 +19,17 @@ export async function activate(ext: ExtensionContext) {
log.appendLine(`UnoCSS for VS Code v${version} ${process.cwd()}`)

const contextLoader = new ContextLoader(cwd)

const hasConfig = await contextLoader.loadContextInDirectory(cwd)
if (!hasConfig)
return

await contextLoader.ready

const status = window.createStatusBarItem(StatusBarAlignment.Right, 200)
status.text = 'UnoCSS'

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

ext.subscriptions.push(commands.registerCommand('unocss.reload', async () => {
Expand Down

0 comments on commit 307417e

Please sign in to comment.