Skip to content

Commit

Permalink
refactor(bezier-vscode): use TokenMap type, separate functions
Browse files Browse the repository at this point in the history
  • Loading branch information
yangwooseong committed Nov 12, 2024
1 parent 6b72eeb commit f422800
Showing 1 changed file with 33 additions and 41 deletions.
74 changes: 33 additions & 41 deletions packages/bezier-vscode/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,20 @@ import { TextDocument } from 'vscode-languageserver-textdocument'

import { deepMerge, hexToRGBA } from './utils'

type TokenMap = Record<
string,
Record<string, string | number | Record<string, string | number>>
>
type TokenValue = string | number | Record<string, string | number>
type TokenMap = Record<string, Record<string, TokenValue>>

const assignToTokenMap = (
target: TokenMap,
source: TokenMap,
fn: Function = (v: Record<string, string> | string) => v
fn: Function = (v: TokenMap) => v
) => {
Object.entries(source).forEach(([key, value]) => {
if (target[key] === undefined) {
target[key] = {}
Object.entries(source).forEach(([category, tokenObject]) => {
if (target[category] === undefined) {
target[category] = {}
}
Object.entries(value).forEach(([token, tokenValue]) => {
target[key][token] = fn(tokenValue)
Object.entries(tokenObject).forEach(([name, value]) => {
target[category][name] = fn(value)
})
})
}
Expand Down Expand Up @@ -88,7 +86,7 @@ const tokenGroupPatterns = {
transition: /transition:/,
opacity: /opacity:/,
shadow: /box-shadow:/,
gradient: /background:/,
gradient: /background:|background-image:/,
'z-index': /z-index:/,
} satisfies Record<Exclude<TokenGroup, 'dimension'>, RegExp>

Expand All @@ -115,6 +113,27 @@ connection.onInitialize(() => {
return result
})

const hasMatchingPattern = (currentText: string): boolean => {
return Object.values(tokenGroupPatterns).some((pattern) =>
pattern.test(currentText)
)
}

const getMatchedCompletionItems = (currentText: string): CompletionItem[] => {
if (!hasMatchingPattern(currentText)) {
return []
}

return Object.entries(tokenGroupPatterns)
.filter(([_, pattern]) => pattern.test(currentText))
.flatMap(
([tokenGroupName]) =>
completionItemsByTokenGroup[
tokenGroupName as keyof typeof tokenGroupPatterns
] ?? []
)
}

// // This handler provides the initial list of the completion items.
connection.onCompletion(
(_textDocumentPosition: TextDocumentPositionParams): CompletionItem[] => {
Expand All @@ -123,8 +142,6 @@ connection.onCompletion(
// info and always provide the same completion items.
const doc = documents.get(_textDocumentPosition.textDocument.uri)

let matchedCompletionItems: CompletionItem[] = []

// if the doc can't be found, return nothing
if (!doc) {
return []
Expand All @@ -135,36 +152,11 @@ connection.onCompletion(
end: { line: _textDocumentPosition.position.line, character: 1000 },
})

if (
Object.values(tokenGroupPatterns).every(
(pattern) => !pattern.test(currentText)
)
) {
return []
}

for (const [tokenGroupName, pattern] of Object.entries(
tokenGroupPatterns
)) {
if (pattern.test(currentText)) {
const currentCompletionItems =
completionItemsByTokenGroup[
tokenGroupName as keyof typeof tokenGroupPatterns
]

matchedCompletionItems = matchedCompletionItems.concat(
currentCompletionItems
)
}
}
const matchedItems = getMatchedCompletionItems(currentText)

// if there were matches above, send them
if (matchedCompletionItems.length > 0) {
return matchedCompletionItems
}
console.log('LOG: ', matchedItems)

// if there were no matches, send everything
return allCompletionItems
return matchedItems.length > 0 ? matchedItems : allCompletionItems
}
)

Expand Down

0 comments on commit f422800

Please sign in to comment.