Skip to content

Improve experimental.configFile in multi-root workspaces #640

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions packages/tailwindcss-language-server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1592,6 +1592,11 @@ class TW {
let cssFileConfigMap: 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 (
typeof configFileOrFiles !== 'string' &&
Expand All @@ -1615,10 +1620,10 @@ class TW {
([relativeConfigPath, relativeDocumentSelectorOrSelectors]) => {
return {
folder: base,
configPath: path.resolve(base, relativeConfigPath),
configPath: path.resolve(userDefinedConfigBase, relativeConfigPath),
documentSelector: [].concat(relativeDocumentSelectorOrSelectors).map((selector) => ({
priority: DocumentSelectorPriority.USER_CONFIGURED,
pattern: path.resolve(base, selector),
pattern: path.resolve(userDefinedConfigBase, selector),
})),
isUserConfigured: true,
}
Expand Down
75 changes: 51 additions & 24 deletions packages/vscode-tailwindcss/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,15 @@ export async function activate(context: ExtensionContext) {
// e.g. "plaintext" already exists but you change it from "html" to "css"
context.subscriptions.push(
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]) => {
const folder = Workspace.getWorkspaceFolder(Uri.parse(key))
let reboot = false
Expand All @@ -267,11 +276,15 @@ export async function activate(context: ExtensionContext) {
}

if (reboot && client) {
clients.delete(folder.uri.toString())
client.stop()
bootWorkspaceClient(folder)
toReboot.add(folder)
}
})

for (let folder of toReboot) {
clients.get(folder.uri.toString())?.stop()
clients.delete(folder.uri.toString())
bootClientForFolderIfNeeded(folder)
}
})
)

Expand Down Expand Up @@ -568,6 +581,8 @@ export async function activate(context: ExtensionContext) {
},
initializationOptions: {
userLanguages: getUserLanguages(folder),
workspaceFile:
Workspace.workspaceFile?.scheme === 'file' ? Workspace.workspaceFile.fsPath : undefined,
},
synchronize: {
configurationSection: ['files', 'editor', 'tailwindCSS'],
Expand Down Expand Up @@ -602,29 +617,12 @@ export async function activate(context: ExtensionContext) {
clients.set(folder.uri.toString(), client)
}

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) {
async function bootClientForFolderIfNeeded(folder: WorkspaceFolder): Promise<void> {
let settings = Workspace.getConfiguration('tailwindCSS', folder)
if (settings.get('experimental.configFile') !== null) {
bootWorkspaceClient(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(
new RelativePattern(folder, `**/${CONFIG_GLOB}`),
Expand All @@ -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))
Workspace.textDocuments.forEach(didOpenTextDocument)
context.subscriptions.push(
Expand Down