Skip to content

Commit

Permalink
feat: add vscode delimiters (#2728)
Browse files Browse the repository at this point in the history
  • Loading branch information
serkodev committed Jun 6, 2023
1 parent 10724f3 commit 3c83ee9
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
13 changes: 8 additions & 5 deletions packages/autocomplete/src/create.ts
Expand Up @@ -3,7 +3,7 @@ import { escapeRegExp, toArray, uniq } from '@unocss/core'
import { LRUCache } from 'lru-cache'
import { parseAutocomplete } from './parse'
import type { ParsedAutocompleteTemplate, UnocssAutocomplete } from './types'
import { searchUsageBoundary } from './utils'
import { searchAttrKey, searchUsageBoundary } from './utils'

export function createAutocomplete(uno: UnoGenerator): UnocssAutocomplete {
const templateCache = new Map<string, ParsedAutocompleteTemplate>()
Expand Down Expand Up @@ -56,8 +56,8 @@ export function createAutocomplete(uno: UnoGenerator): UnocssAutocomplete {
return templateCache.get(template)!.suggest
}

async function suggest(input: string) {
if (input.length < 2)
async function suggest(input: string, allowsEmptyInput = false) {
if (!allowsEmptyInput && input.length < 2)
return []
if (cache.has(input))
return cache.get(input)!
Expand Down Expand Up @@ -89,10 +89,12 @@ export function createAutocomplete(uno: UnoGenerator): UnocssAutocomplete {
}

async function suggestInFile(content: string, cursor: number): Promise<SuggestResult> {
const isInsideAttrValue = searchAttrKey(content, cursor) !== undefined

// try resolve by extractors
const byExtractor = await searchUsageByExtractor(content, cursor)
if (byExtractor) {
const suggestions = await suggest(byExtractor.extracted)
const suggestions = await suggest(byExtractor.extracted, isInsideAttrValue)
const formatted = byExtractor.transformSuggestions ? byExtractor.transformSuggestions(suggestions) : suggestions
return {
suggestions: suggestions.map((v, i) => [v, formatted[i]] as [string, string]),
Expand All @@ -102,7 +104,8 @@ export function createAutocomplete(uno: UnoGenerator): UnocssAutocomplete {

// regular resolve
const regular = searchUsageBoundary(content, cursor)
const suggestions = await suggest(regular.content)
const suggestions = await suggest(regular.content, isInsideAttrValue)

return {
suggestions: suggestions.map(v => [v, v] as [string, string]),
resolveReplacement: suggestion => ({
Expand Down
6 changes: 6 additions & 0 deletions packages/autocomplete/src/utils.ts
Expand Up @@ -12,3 +12,9 @@ export function searchUsageBoundary(line: string, index: number) {
end,
}
}

export function searchAttrKey(content: string, cursor: number) {
const text = content.substring(0, cursor)
if (text.match(/(<\w+\s*)[^>]*$/) !== null)
return text.match(/\S+(?=\s*=\s*["']?[^"']*$)/)?.[0]
}
2 changes: 1 addition & 1 deletion packages/vscode/src/autocomplete.ts
Expand Up @@ -32,7 +32,7 @@ const defaultLanguageIds = [
'astro',
'rust',
]
const delimiters = ['-', ':']
const delimiters = ['-', ':', ' ', '"', '\'']

class UnoCompletionItem extends CompletionItem {
uno: UnoGenerator
Expand Down

0 comments on commit 3c83ee9

Please sign in to comment.