Improve `experimental.configFile` in multi-root workspaces (#640)

master
Brad Cornes 2022-10-20 18:41:01 +01:00 committed by GitHub
parent aa282c19a0
commit 55d2b9e8da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 26 deletions

View File

@ -1592,6 +1592,11 @@ class TW {
let cssFileConfigMap: Map<string, string> = new Map() let cssFileConfigMap: Map<string, string> = new Map()
let configTailwindVersionMap: Map<string, string> = new Map() let configTailwindVersionMap: Map<string, string> = new Map()
// base directory to resolve relative `experimental.configFile` paths against
let userDefinedConfigBase = this.initializeParams.initializationOptions?.workspaceFile
? path.dirname(this.initializeParams.initializationOptions.workspaceFile)
: base
if (configFileOrFiles) { if (configFileOrFiles) {
if ( if (
typeof configFileOrFiles !== 'string' && typeof configFileOrFiles !== 'string' &&
@ -1615,10 +1620,10 @@ class TW {
([relativeConfigPath, relativeDocumentSelectorOrSelectors]) => { ([relativeConfigPath, relativeDocumentSelectorOrSelectors]) => {
return { return {
folder: base, folder: base,
configPath: path.resolve(base, relativeConfigPath), configPath: path.resolve(userDefinedConfigBase, relativeConfigPath),
documentSelector: [].concat(relativeDocumentSelectorOrSelectors).map((selector) => ({ documentSelector: [].concat(relativeDocumentSelectorOrSelectors).map((selector) => ({
priority: DocumentSelectorPriority.USER_CONFIGURED, priority: DocumentSelectorPriority.USER_CONFIGURED,
pattern: path.resolve(base, selector), pattern: path.resolve(userDefinedConfigBase, selector),
})), })),
isUserConfigured: true, isUserConfigured: true,
} }

View File

@ -246,6 +246,15 @@ export async function activate(context: ExtensionContext) {
// e.g. "plaintext" already exists but you change it from "html" to "css" // e.g. "plaintext" already exists but you change it from "html" to "css"
context.subscriptions.push( context.subscriptions.push(
Workspace.onDidChangeConfiguration((event) => { Workspace.onDidChangeConfiguration((event) => {
let toReboot = new Set<WorkspaceFolder>()
Workspace.textDocuments.forEach((document) => {
let folder = Workspace.getWorkspaceFolder(document.uri)
if (!folder) return
if (event.affectsConfiguration('tailwindCSS.experimental.configFile', folder)) {
toReboot.add(folder)
}
})
;[...clients].forEach(([key, client]) => { ;[...clients].forEach(([key, client]) => {
const folder = Workspace.getWorkspaceFolder(Uri.parse(key)) const folder = Workspace.getWorkspaceFolder(Uri.parse(key))
let reboot = false let reboot = false
@ -267,11 +276,15 @@ export async function activate(context: ExtensionContext) {
} }
if (reboot && client) { if (reboot && client) {
clients.delete(folder.uri.toString()) toReboot.add(folder)
client.stop()
bootWorkspaceClient(folder)
} }
}) })
for (let folder of toReboot) {
clients.get(folder.uri.toString())?.stop()
clients.delete(folder.uri.toString())
bootClientForFolderIfNeeded(folder)
}
}) })
) )
@ -568,6 +581,8 @@ export async function activate(context: ExtensionContext) {
}, },
initializationOptions: { initializationOptions: {
userLanguages: getUserLanguages(folder), userLanguages: getUserLanguages(folder),
workspaceFile:
Workspace.workspaceFile?.scheme === 'file' ? Workspace.workspaceFile.fsPath : undefined,
}, },
synchronize: { synchronize: {
configurationSection: ['files', 'editor', 'tailwindCSS'], configurationSection: ['files', 'editor', 'tailwindCSS'],
@ -602,30 +617,13 @@ export async function activate(context: ExtensionContext) {
clients.set(folder.uri.toString(), client) clients.set(folder.uri.toString(), client)
} }
async function didOpenTextDocument(document: TextDocument): Promise<void> { async function bootClientForFolderIfNeeded(folder: WorkspaceFolder): Promise<void> {
if (document.languageId === 'tailwindcss') { let settings = Workspace.getConfiguration('tailwindCSS', folder)
bootCssServer() if (settings.get('experimental.configFile') !== null) {
} bootWorkspaceClient(folder)
// We are only interested in language mode text
if (document.uri.scheme !== 'file') {
return return
} }
let uri = document.uri
let folder = Workspace.getWorkspaceFolder(uri)
// Files outside a folder can't be handled. This might depend on the language.
// Single file languages like JSON might handle files outside the workspace folders.
if (!folder) {
return
}
// If we have nested workspace folders we only start a server on the outer most workspace folder.
folder = getOuterMostWorkspaceFolder(folder)
if (searchedFolders.has(folder.uri.toString())) return
searchedFolders.add(folder.uri.toString())
let [configFile] = await Workspace.findFiles( let [configFile] = await Workspace.findFiles(
new RelativePattern(folder, `**/${CONFIG_GLOB}`), new RelativePattern(folder, `**/${CONFIG_GLOB}`),
`{${getExcludePatterns(folder).join(',')}}`, `{${getExcludePatterns(folder).join(',')}}`,
@ -650,6 +648,35 @@ export async function activate(context: ExtensionContext) {
} }
} }
async function didOpenTextDocument(document: TextDocument): Promise<void> {
if (document.languageId === 'tailwindcss') {
bootCssServer()
}
// We are only interested in language mode text
if (document.uri.scheme !== 'file') {
return
}
let uri = document.uri
let folder = Workspace.getWorkspaceFolder(uri)
// Files outside a folder can't be handled. This might depend on the language.
// Single file languages like JSON might handle files outside the workspace folders.
if (!folder) {
return
}
// If we have nested workspace folders we only start a server on the outer most workspace folder.
folder = getOuterMostWorkspaceFolder(folder)
if (searchedFolders.has(folder.uri.toString())) {
return
}
searchedFolders.add(folder.uri.toString())
await bootClientForFolderIfNeeded(folder)
}
context.subscriptions.push(Workspace.onDidOpenTextDocument(didOpenTextDocument)) context.subscriptions.push(Workspace.onDidOpenTextDocument(didOpenTextDocument))
Workspace.textDocuments.forEach(didOpenTextDocument) Workspace.textDocuments.forEach(didOpenTextDocument)
context.subscriptions.push( context.subscriptions.push(