From ae18cb2449e82d256d4b34b02a109c6447addb39 Mon Sep 17 00:00:00 2001 From: Remco Haszing Date: Mon, 27 Mar 2023 19:39:56 +0200 Subject: [PATCH] Remove language service dependency on TextDocuments (#742) The code actions API depends on `TextDocuments`. This is specific to the language server. This makes it hard to use without a language server. The use of `TextDocuments` has been removed. Instead, the resolved text document is passed down. --- packages/tailwindcss-language-server/src/server.ts | 3 +-- .../src/codeActions/codeActionProvider.ts | 8 +++++--- .../src/codeActions/provideInvalidApplyCodeActions.ts | 6 +++--- packages/tailwindcss-language-service/src/util/state.ts | 1 - 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/packages/tailwindcss-language-server/src/server.ts b/packages/tailwindcss-language-server/src/server.ts index b519194..6a27fb1 100644 --- a/packages/tailwindcss-language-server/src/server.ts +++ b/packages/tailwindcss-language-server/src/server.ts @@ -412,7 +412,6 @@ async function createProjectService( diagnosticRelatedInformation: true, itemDefaults, }, - documents: documentService.documents, getConfiguration, getDocumentSymbols: (uri: string) => { return connection.sendRequest('@/tailwindCSS/getDocumentSymbols', { uri }) @@ -1171,7 +1170,7 @@ async function createProjectService( if (!document) return null let settings = await state.editor.getConfiguration(document.uri) if (!settings.tailwindCSS.codeActions) return null - return doCodeActions(state, params) + return doCodeActions(state, params, document) }, null) }, onDocumentLinks(params: DocumentLinkParams): DocumentLink[] { diff --git a/packages/tailwindcss-language-service/src/codeActions/codeActionProvider.ts b/packages/tailwindcss-language-service/src/codeActions/codeActionProvider.ts index df30e66..2a87d2f 100644 --- a/packages/tailwindcss-language-service/src/codeActions/codeActionProvider.ts +++ b/packages/tailwindcss-language-service/src/codeActions/codeActionProvider.ts @@ -17,13 +17,14 @@ import { flatten, dedupeBy } from '../util/array' import { provideCssConflictCodeActions } from './provideCssConflictCodeActions' import { provideInvalidApplyCodeActions } from './provideInvalidApplyCodeActions' import { provideSuggestionCodeActions } from './provideSuggestionCodeActions' +import { TextDocument } from 'vscode-languageserver-textdocument' async function getDiagnosticsFromCodeActionParams( state: State, params: CodeActionParams, + document: TextDocument, only?: DiagnosticKind[] ): Promise { - let document = state.editor.documents.get(params.textDocument.uri) if (!document) return [] let diagnostics = await doValidate(state, document, only) @@ -40,7 +41,7 @@ async function getDiagnosticsFromCodeActionParams( .filter(Boolean) } -export async function doCodeActions(state: State, params: CodeActionParams): Promise { +export async function doCodeActions(state: State, params: CodeActionParams, document: TextDocument): Promise { if (!state.enabled) { return [] } @@ -48,6 +49,7 @@ export async function doCodeActions(state: State, params: CodeActionParams): Pro let diagnostics = await getDiagnosticsFromCodeActionParams( state, params, + document, params.context.diagnostics .map((diagnostic) => diagnostic.code) .filter(Boolean) as DiagnosticKind[] @@ -56,7 +58,7 @@ export async function doCodeActions(state: State, params: CodeActionParams): Pro return Promise.all( diagnostics.map((diagnostic) => { if (isInvalidApplyDiagnostic(diagnostic)) { - return provideInvalidApplyCodeActions(state, params, diagnostic) + return provideInvalidApplyCodeActions(state, document, diagnostic) } if (isCssConflictDiagnostic(diagnostic)) { diff --git a/packages/tailwindcss-language-service/src/codeActions/provideInvalidApplyCodeActions.ts b/packages/tailwindcss-language-service/src/codeActions/provideInvalidApplyCodeActions.ts index c212eea..d4da3c1 100644 --- a/packages/tailwindcss-language-service/src/codeActions/provideInvalidApplyCodeActions.ts +++ b/packages/tailwindcss-language-service/src/codeActions/provideInvalidApplyCodeActions.ts @@ -18,13 +18,13 @@ import { dset } from 'dset' import selectorParser from 'postcss-selector-parser' import { flatten } from '../util/array' import { getTextWithoutComments } from '../util/doc' +import { TextDocument } from 'vscode-languageserver-textdocument' export async function provideInvalidApplyCodeActions( state: State, - params: CodeActionParams, + document: TextDocument, diagnostic: InvalidApplyDiagnostic ): Promise { - let document = state.editor.documents.get(params.textDocument.uri) if (!document) return [] let documentText = getTextWithoutComments(document, 'css') let cssRange: Range @@ -144,7 +144,7 @@ export async function provideInvalidApplyCodeActions( diagnostics: [diagnostic], edit: { changes: { - [params.textDocument.uri]: changes, + [document.uri]: changes, }, }, }, diff --git a/packages/tailwindcss-language-service/src/util/state.ts b/packages/tailwindcss-language-service/src/util/state.ts index 681ae5f..4dde68f 100644 --- a/packages/tailwindcss-language-service/src/util/state.ts +++ b/packages/tailwindcss-language-service/src/util/state.ts @@ -20,7 +20,6 @@ export type ClassNames = { export type EditorState = { connection: Connection folder: string - documents: TextDocuments userLanguages: Record capabilities: { configuration: boolean