Skip to content

Commit

Permalink
Deduplicate some logic
Browse files Browse the repository at this point in the history
  • Loading branch information
remcohaszing committed Oct 15, 2023
1 parent 225f47a commit c453ece
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 52 deletions.
18 changes: 10 additions & 8 deletions src/languageFeatures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,15 @@ export function createColorProvider(
const editableColors: languages.IColorInformation[] = []
const nonEditableColors: languages.IColorInformation[] = []
const colors = await worker.getDocumentColors(String(model.uri), model.getLanguageId())
for (const lsColor of colors) {
const monacoColor = toColorInformation(lsColor)
const text = model.getValueInRange(monacoColor.range)
if (editableColorRegex.test(text)) {
editableColors.push(monacoColor)
} else {
nonEditableColors.push(monacoColor)
if (colors) {
for (const lsColor of colors) {
const monacoColor = toColorInformation(lsColor)
const text = model.getValueInRange(monacoColor.range)
if (editableColorRegex.test(text)) {
editableColors.push(monacoColor)
} else {
nonEditableColors.push(monacoColor)
}
}
}

Expand Down Expand Up @@ -199,7 +201,7 @@ export function createMarkerDataProvider(getWorker: WorkerAccessor): MarkerDataP

const diagnostics = await worker.doValidate(String(model.uri), model.getLanguageId())

return diagnostics.map((diagnostic) => toMarkerData(diagnostic))
return diagnostics?.map((diagnostic) => toMarkerData(diagnostic))
}
}
}
66 changes: 22 additions & 44 deletions src/tailwindcss.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ export interface TailwindcssWorker {

doHover: (uri: string, languageId: string, position: Position) => Hover | undefined

doValidate: (uri: string, languageId: string) => AugmentedDiagnostic[]
doValidate: (uri: string, languageId: string) => AugmentedDiagnostic[] | undefined

generateStylesFromContent: (css: string, content: ChangedContent[]) => string

getDocumentColors: (uri: string, languageId: string) => ColorInformation[]
getDocumentColors: (uri: string, languageId: string) => ColorInformation[] | undefined

resolveCompletionItem: (item: CompletionItem) => CompletionItem
}
Expand Down Expand Up @@ -155,45 +155,31 @@ export function initialize(tailwindWorkerOptions?: TailwindWorkerOptions): void

const statePromise = stateFromConfig(preparedTailwindConfig)

const getTextDocument = (uri: string, languageId: string): TextDocument | undefined => {
const models = ctx.getMirrorModels()
for (const model of models) {
if (String(model.uri) === uri) {
return TextDocument.create(uri, languageId, model.version, model.getValue())
const withDocument =
<A extends unknown[], R>(
fn: (state: JitState, document: TextDocument, ...args: A) => Promise<R>
) =>
(uri: string, languageId: string, ...args: A): Promise<R> | undefined => {
const models = ctx.getMirrorModels()
for (const model of models) {
if (String(model.uri) === uri) {
return statePromise.then((state) =>
fn(
state,
TextDocument.create(uri, languageId, model.version, model.getValue()),
...args
)
)
}
}
}
}

return {
async doComplete(uri, languageId, position, context) {
const textDocument = getTextDocument(uri, languageId)

if (!textDocument) {
return
}

return doComplete(await statePromise, textDocument, position, context)
},

async doHover(uri, languageId, position) {
const textDocument = getTextDocument(uri, languageId)

if (!textDocument) {
return
}
doComplete: withDocument(doComplete),

return doHover(await statePromise, textDocument, position)
},

async doValidate(uri, languageId) {
const textDocument = getTextDocument(uri, languageId)
doHover: withDocument(doHover),

if (!textDocument) {
return []
}

return doValidate(await statePromise, textDocument)
},
doValidate: withDocument(doValidate),

async generateStylesFromContent(css, content) {
const { config } = await statePromise
Expand All @@ -207,15 +193,7 @@ export function initialize(tailwindWorkerOptions?: TailwindWorkerOptions): void
return result.css
},

async getDocumentColors(uri, languageId) {
const textDocument = getTextDocument(uri, languageId)

if (!textDocument) {
return []
}

return getDocumentColors(await statePromise, textDocument)
},
getDocumentColors: withDocument(getDocumentColors),

async resolveCompletionItem(item) {
return resolveCompletionItem(await statePromise, item)
Expand Down

0 comments on commit c453ece

Please sign in to comment.