Skip to content

Commit fb871cf

Browse files
committed
Auto-switch CSS files to tailwindcss language in valid projects
1 parent 3014df5 commit fb871cf

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

packages/vscode-tailwindcss/src/extension.ts

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
Position,
1717
Range,
1818
RelativePattern,
19+
languages,
1920
} from 'vscode'
2021
import type {
2122
DocumentFilter,
@@ -124,11 +125,15 @@ async function getActiveTextEditorProject(): Promise<{ version: string } | null>
124125
let editor = Window.activeTextEditor
125126
if (!editor) return null
126127

128+
return projectForDocument(editor.document)
129+
}
130+
131+
async function projectForDocument(document: TextDocument): Promise<{ version: string } | null> {
127132
// No server yet, no project
128133
if (!currentClient) return null
129134

130135
// No workspace folder, no project
131-
let uri = editor.document.uri
136+
let uri = document.uri
132137
let folder = Workspace.getWorkspaceFolder(uri)
133138
if (!folder) return null
134139

@@ -160,19 +165,50 @@ async function activeTextEditorSupportsClassSorting(): Promise<boolean> {
160165
return semver.gte(project.version, '3.0.0')
161166
}
162167

168+
const switchedDocuments = new Set<string>()
169+
170+
async function switchDocumentLanguageIfNeeded(document: TextDocument): Promise<void> {
171+
// Consider documents that are already in `tailwindcss` language mode as
172+
// having been switched automatically. This ensures that a user can still
173+
// manually switch this document to `css` and have it stay that way.
174+
if (document.languageId === 'tailwindcss') {
175+
switchedDocuments.add(document.uri.toString())
176+
return
177+
}
178+
179+
if (document.languageId !== 'css') return
180+
181+
// When a document is manually switched back to the `css` language we do not
182+
// want to switch it back to `tailwindcss` because the user has explicitly
183+
// chosen to use the `css` language mode.
184+
if (switchedDocuments.has(document.uri.toString())) return
185+
186+
let project = await projectForDocument(document)
187+
if (!project) return
188+
189+
// CSS files in a known project should be switched to `tailwindcss`
190+
// when they are opened
191+
languages.setTextDocumentLanguage(document, 'tailwindcss')
192+
switchedDocuments.add(document.uri.toString())
193+
}
194+
163195
async function updateActiveTextEditorContext(): Promise<void> {
164196
commands.executeCommand(
165197
'setContext',
166198
'tailwindCSS.activeTextEditorSupportsClassSorting',
167199
await activeTextEditorSupportsClassSorting(),
168200
)
201+
202+
await Promise.all(Workspace.textDocuments.map(switchDocumentLanguageIfNeeded))
169203
}
170204

171205
function resetActiveTextEditorContext(): void {
172206
commands.executeCommand('setContext', 'tailwindCSS.activeTextEditorSupportsClassSorting', false)
173207
}
174208

175209
export async function activate(context: ExtensionContext) {
210+
switchedDocuments.clear()
211+
176212
let outputChannel = Window.createOutputChannel(CLIENT_NAME)
177213
context.subscriptions.push(outputChannel)
178214
context.subscriptions.push(
@@ -628,6 +664,8 @@ export async function activate(context: ExtensionContext) {
628664
}
629665

630666
async function didOpenTextDocument(document: TextDocument): Promise<void> {
667+
await switchDocumentLanguageIfNeeded(document)
668+
631669
if (document.languageId === 'tailwindcss') {
632670
servers.css.boot(context, outputChannel)
633671
}

0 commit comments

Comments
 (0)