-
Notifications
You must be signed in to change notification settings - Fork 213
/
Copy pathapi.ts
52 lines (41 loc) · 1.47 KB
/
api.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { workspace, CancellationTokenSource, OutputChannel, ExtensionContext, Uri } from 'vscode'
import { anyWorkspaceFoldersNeedServer, fileMayBeTailwindRelated } from './analyze'
interface ApiOptions {
context: ExtensionContext
outputChannel: OutputChannel
}
export async function createApi({ context, outputChannel }: ApiOptions) {
let folderAnalysis: Promise<boolean> | null = null
async function workspaceNeedsLanguageServer() {
if (folderAnalysis) return folderAnalysis
let found = false
let source: CancellationTokenSource | null = new CancellationTokenSource()
source.token.onCancellationRequested(() => {
source?.dispose()
source = null
if (found) return
outputChannel.appendLine(
'Server was not started. Search for Tailwind CSS-related files was taking too long.',
)
})
// Cancel the search after roughly 15 seconds
setTimeout(() => source?.cancel(), 15_000)
context.subscriptions.push(source)
folderAnalysis ??= anyWorkspaceFoldersNeedServer({
token: source.token,
folders: workspace.workspaceFolders ?? [],
})
let result = await folderAnalysis
found = true
source?.cancel()
return result
}
async function stylesheetNeedsLanguageServer(uri: Uri) {
outputChannel.appendLine(`Checking if ${uri.fsPath} may be Tailwind-related…`)
return fileMayBeTailwindRelated(uri)
}
return {
workspaceNeedsLanguageServer,
stylesheetNeedsLanguageServer,
}
}