Skip to content

Commit 3b4b5ba

Browse files
Auto-switch CSS files to tailwindcss language in valid projects (#1087)
This is an alternative design to part of #947. In that PR we'd be setting a default for *all* CSS files whether they're in a Tailwind CSS project or not. Instead we: 1. Detect that an opened CSS file is in a Tailwind CSS project and switch the language to `tailwindcss`. If you manually set it back to the CSS language we won't switch it back to `tailwindcss` again until you reload VSCode. 2. Switch any documents after the language server has found a project. This ensures that when starting a new project and you've created a CSS file typing `@import "tailwindcss"` and saving it will then switch the language to `tailwindcss`.
1 parent 3014df5 commit 3b4b5ba

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

packages/vscode-tailwindcss/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- Show colors for logical border properties ([#1075](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1075))
99
- Show all potential class conflicts in v4 projects ([#1077](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1077))
1010
- Lookup variables in the CSS theme ([#1082](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1082))
11+
- Auto-switch CSS files to tailwindcss language in valid projects ([#1087](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1087))
1112

1213
## 0.12.12
1314

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)