@@ -16,6 +16,7 @@ import {
16
16
Position ,
17
17
Range ,
18
18
RelativePattern ,
19
+ languages ,
19
20
} from 'vscode'
20
21
import type {
21
22
DocumentFilter ,
@@ -124,11 +125,15 @@ async function getActiveTextEditorProject(): Promise<{ version: string } | null>
124
125
let editor = Window . activeTextEditor
125
126
if ( ! editor ) return null
126
127
128
+ return projectForDocument ( editor . document )
129
+ }
130
+
131
+ async function projectForDocument ( document : TextDocument ) : Promise < { version : string } | null > {
127
132
// No server yet, no project
128
133
if ( ! currentClient ) return null
129
134
130
135
// No workspace folder, no project
131
- let uri = editor . document . uri
136
+ let uri = document . uri
132
137
let folder = Workspace . getWorkspaceFolder ( uri )
133
138
if ( ! folder ) return null
134
139
@@ -160,19 +165,50 @@ async function activeTextEditorSupportsClassSorting(): Promise<boolean> {
160
165
return semver . gte ( project . version , '3.0.0' )
161
166
}
162
167
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
+
163
195
async function updateActiveTextEditorContext ( ) : Promise < void > {
164
196
commands . executeCommand (
165
197
'setContext' ,
166
198
'tailwindCSS.activeTextEditorSupportsClassSorting' ,
167
199
await activeTextEditorSupportsClassSorting ( ) ,
168
200
)
201
+
202
+ await Promise . all ( Workspace . textDocuments . map ( switchDocumentLanguageIfNeeded ) )
169
203
}
170
204
171
205
function resetActiveTextEditorContext ( ) : void {
172
206
commands . executeCommand ( 'setContext' , 'tailwindCSS.activeTextEditorSupportsClassSorting' , false )
173
207
}
174
208
175
209
export async function activate ( context : ExtensionContext ) {
210
+ switchedDocuments . clear ( )
211
+
176
212
let outputChannel = Window . createOutputChannel ( CLIENT_NAME )
177
213
context . subscriptions . push ( outputChannel )
178
214
context . subscriptions . push (
@@ -628,6 +664,8 @@ export async function activate(context: ExtensionContext) {
628
664
}
629
665
630
666
async function didOpenTextDocument ( document : TextDocument ) : Promise < void > {
667
+ await switchDocumentLanguageIfNeeded ( document )
668
+
631
669
if ( document . languageId === 'tailwindcss' ) {
632
670
servers . css . boot ( context , outputChannel )
633
671
}
0 commit comments