Skip to content

Commit bf57dd1

Browse files
committed
Add support for @config
1 parent 1b730cb commit bf57dd1

File tree

6 files changed

+171
-4
lines changed

6 files changed

+171
-4
lines changed

packages/tailwindcss-language-server/src/language/cssServer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ async function validateTextDocument(textDocument: TextDocument): Promise<void> {
393393
.filter((diagnostic) => {
394394
if (
395395
diagnostic.code === 'unknownAtRules' &&
396-
/Unknown at rule @(tailwind|apply)/.test(diagnostic.message)
396+
/Unknown at rule @(tailwind|apply|config)/.test(diagnostic.message)
397397
) {
398398
return false
399399
}

packages/tailwindcss-language-server/src/server.ts

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ import {
2727
FileChangeType,
2828
Disposable,
2929
TextDocumentIdentifier,
30+
DocumentLinkRequest,
31+
DocumentLinkParams,
32+
DocumentLink,
3033
} from 'vscode-languageserver/node'
3134
import { TextDocument } from 'vscode-languageserver-textdocument'
3235
import { URI } from 'vscode-uri'
@@ -68,6 +71,7 @@ import {
6871
} from './lsp/diagnosticsProvider'
6972
import { doCodeActions } from 'tailwindcss-language-service/src/codeActions/codeActionProvider'
7073
import { getDocumentColors } from 'tailwindcss-language-service/src/documentColorProvider'
74+
import { getDocumentLinks } from 'tailwindcss-language-service/src/documentLinksProvider'
7175
import { debounce } from 'debounce'
7276
import { getModuleDependencies } from './util/getModuleDependencies'
7377
import assert from 'assert'
@@ -188,6 +192,7 @@ interface ProjectService {
188192
onDocumentColor(params: DocumentColorParams): Promise<ColorInformation[]>
189193
onColorPresentation(params: ColorPresentationParams): Promise<ColorPresentation[]>
190194
onCodeAction(params: CodeActionParams): Promise<CodeAction[]>
195+
onDocumentLinks(params: DocumentLinkParams): DocumentLink[]
191196
}
192197

193198
type ProjectConfig = { folder: string; configPath?: string; documentSelector?: string[] }
@@ -299,6 +304,27 @@ async function createProjectService(
299304
getDocumentSymbols: (uri: string) => {
300305
return connection.sendRequest('@/tailwindCSS/getDocumentSymbols', { uri })
301306
},
307+
async readDirectory(document, directory) {
308+
try {
309+
directory = path.resolve(path.dirname(getFileFsPath(document.uri)), directory)
310+
let dirents = await fs.promises.readdir(directory, { withFileTypes: true })
311+
let result: Array<[string, { isDirectory: boolean }] | null> = await Promise.all(
312+
dirents.map(async (dirent) => {
313+
let isDirectory = dirent.isDirectory()
314+
return (await isExcluded(
315+
state,
316+
document,
317+
path.join(directory, dirent.name, isDirectory ? '/' : '')
318+
))
319+
? null
320+
: [dirent.name, { isDirectory }]
321+
})
322+
)
323+
return result.filter((item) => item !== null)
324+
} catch {
325+
return []
326+
}
327+
},
302328
},
303329
}
304330

@@ -1028,6 +1054,14 @@ async function createProjectService(
10281054
if (!settings.tailwindCSS.codeActions) return null
10291055
return doCodeActions(state, params)
10301056
},
1057+
onDocumentLinks(params: DocumentLinkParams): DocumentLink[] {
1058+
if (!state.enabled) return null
1059+
let document = documentService.getDocument(params.textDocument.uri)
1060+
if (!document) return null
1061+
return getDocumentLinks(state, document, (linkPath) =>
1062+
URI.file(path.resolve(path.dirname(URI.parse(document.uri).fsPath), linkPath)).toString()
1063+
)
1064+
},
10311065
provideDiagnostics: debounce((document: TextDocument) => {
10321066
if (!state.enabled) return
10331067
provideDiagnostics(state, document)
@@ -1485,6 +1519,7 @@ class TW {
14851519
this.connection.onDocumentColor(this.onDocumentColor.bind(this))
14861520
this.connection.onColorPresentation(this.onColorPresentation.bind(this))
14871521
this.connection.onCodeAction(this.onCodeAction.bind(this))
1522+
this.connection.onDocumentLinks(this.onDocumentLinks.bind(this))
14881523
}
14891524

14901525
private updateCapabilities() {
@@ -1499,6 +1534,7 @@ class TW {
14991534
capabilities.add(HoverRequest.type, { documentSelector: null })
15001535
capabilities.add(DocumentColorRequest.type, { documentSelector: null })
15011536
capabilities.add(CodeActionRequest.type, { documentSelector: null })
1537+
capabilities.add(DocumentLinkRequest.type, { documentSelector: null })
15021538

15031539
capabilities.add(CompletionRequest.type, {
15041540
documentSelector: null,
@@ -1564,6 +1600,10 @@ class TW {
15641600
return this.getProject(params.textDocument)?.onCodeAction(params) ?? null
15651601
}
15661602

1603+
onDocumentLinks(params: DocumentLinkParams): DocumentLink[] {
1604+
return this.getProject(params.textDocument)?.onDocumentLinks(params) ?? null
1605+
}
1606+
15671607
listen() {
15681608
this.connection.listen()
15691609
}
@@ -1605,7 +1645,8 @@ function supportsDynamicRegistration(connection: Connection, params: InitializeP
16051645
params.capabilities.textDocument.hover?.dynamicRegistration &&
16061646
params.capabilities.textDocument.colorProvider?.dynamicRegistration &&
16071647
params.capabilities.textDocument.codeAction?.dynamicRegistration &&
1608-
params.capabilities.textDocument.completion?.dynamicRegistration
1648+
params.capabilities.textDocument.completion?.dynamicRegistration &&
1649+
params.capabilities.textDocument.documentLink?.dynamicRegistration
16091650
)
16101651
}
16111652

@@ -1630,6 +1671,7 @@ connection.onInitialize(async (params: InitializeParams): Promise<InitializeResu
16301671
hoverProvider: true,
16311672
colorProvider: true,
16321673
codeActionProvider: true,
1674+
documentLinkProvider: {},
16331675
completionProvider: {
16341676
resolveProvider: true,
16351677
triggerCharacters: [...TRIGGER_CHARACTERS, ':'],

packages/tailwindcss-language-server/src/util/isExcluded.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@ import { State } from 'tailwindcss-language-service/src/util/state'
44
import { TextDocument } from 'vscode-languageserver-textdocument'
55
import { getFileFsPath } from './uri'
66

7-
export default async function isExcluded(state: State, document: TextDocument): Promise<boolean> {
7+
export default async function isExcluded(
8+
state: State,
9+
document: TextDocument,
10+
file: string = getFileFsPath(document.uri)
11+
): Promise<boolean> {
812
let settings = await state.editor.getConfiguration(document.uri)
9-
let file = getFileFsPath(document.uri)
1013

1114
for (let pattern of settings.tailwindCSS.files.exclude) {
1215
if (minimatch(file, path.join(state.editor.folder, pattern))) {

packages/tailwindcss-language-service/src/completionProvider.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -994,6 +994,20 @@ function provideCssDirectiveCompletions(
994994
},
995995
},
996996
]),
997+
...(semver.gte(state.version, '3.2.0')
998+
? [
999+
{
1000+
label: '@config',
1001+
documentation: {
1002+
kind: 'markdown' as typeof MarkupKind.Markdown,
1003+
value: `[Tailwind CSS Documentation](${docsUrl(
1004+
state.version,
1005+
'functions-and-directives/#config'
1006+
)})`,
1007+
},
1008+
},
1009+
]
1010+
: []),
9971011
]
9981012

9991013
return {
@@ -1016,6 +1030,52 @@ function provideCssDirectiveCompletions(
10161030
}
10171031
}
10181032

1033+
async function provideConfigDirectiveCompletions(
1034+
state: State,
1035+
document: TextDocument,
1036+
position: Position
1037+
): Promise<CompletionList> {
1038+
if (!isCssContext(state, document, position)) {
1039+
return null
1040+
}
1041+
1042+
if (!semver.gte(state.version, '3.2.0')) {
1043+
return null
1044+
}
1045+
1046+
let text = document.getText({ start: { line: position.line, character: 0 }, end: position })
1047+
let match = text.match(/@config\s*(?<partial>'[^']*|"[^"]*)$/)
1048+
if (!match) {
1049+
return null
1050+
}
1051+
let partial = match.groups.partial.slice(1) // remove quote
1052+
let valueBeforeLastSlash = partial.substring(0, partial.lastIndexOf('/'))
1053+
let valueAfterLastSlash = partial.substring(partial.lastIndexOf('/') + 1)
1054+
1055+
return {
1056+
isIncomplete: false,
1057+
items: (await state.editor.readDirectory(document, valueBeforeLastSlash || '.'))
1058+
.filter(([name, type]) => type.isDirectory || /\.c?js$/.test(name))
1059+
.map(([name, type]) => ({
1060+
label: type.isDirectory ? name + '/' : name,
1061+
kind: type.isDirectory ? 19 : 17,
1062+
textEdit: {
1063+
newText: type.isDirectory ? name + '/' : name,
1064+
range: {
1065+
start: {
1066+
line: position.line,
1067+
character: position.character - valueAfterLastSlash.length,
1068+
},
1069+
end: position,
1070+
},
1071+
},
1072+
command: type.isDirectory
1073+
? { command: 'editor.action.triggerSuggest', title: '' }
1074+
: undefined,
1075+
})),
1076+
}
1077+
}
1078+
10191079
async function provideEmmetCompletions(
10201080
state: State,
10211081
document: TextDocument,
@@ -1104,6 +1164,7 @@ export async function doComplete(
11041164
provideVariantsDirectiveCompletions(state, document, position) ||
11051165
provideTailwindDirectiveCompletions(state, document, position) ||
11061166
provideLayerDirectiveCompletions(state, document, position) ||
1167+
(await provideConfigDirectiveCompletions(state, document, position)) ||
11071168
(await provideCustomClassNameCompletions(state, document, position))
11081169

11091170
if (result) return result
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { State } from './util/state'
2+
import type { DocumentLink, Range, TextDocument } from 'vscode-languageserver'
3+
import { isCssDoc } from './util/css'
4+
import { getLanguageBoundaries } from './util/getLanguageBoundaries'
5+
import { findAll, indexToPosition } from './util/find'
6+
import { getTextWithoutComments } from './util/doc'
7+
import { absoluteRange } from './util/absoluteRange'
8+
import * as semver from './util/semver'
9+
10+
export function getDocumentLinks(
11+
state: State,
12+
document: TextDocument,
13+
resolveTarget: (linkPath: string) => string
14+
): DocumentLink[] {
15+
return getConfigDirectiveLinks(state, document, resolveTarget)
16+
}
17+
18+
function getConfigDirectiveLinks(
19+
state: State,
20+
document: TextDocument,
21+
resolveTarget: (linkPath: string) => string
22+
): DocumentLink[] {
23+
if (!semver.gte(state.version, '3.2.0')) {
24+
return []
25+
}
26+
27+
let links: DocumentLink[] = []
28+
let ranges: Range[] = []
29+
30+
if (isCssDoc(state, document)) {
31+
ranges.push(undefined)
32+
} else {
33+
let boundaries = getLanguageBoundaries(state, document)
34+
if (!boundaries) return []
35+
ranges.push(...boundaries.filter((b) => b.type === 'css').map(({ range }) => range))
36+
}
37+
38+
for (let range of ranges) {
39+
let text = getTextWithoutComments(document, 'css', range)
40+
let matches = findAll(/@config\s*(?<path>'[^']+'|"[^"]+")/g, text)
41+
42+
for (let match of matches) {
43+
links.push({
44+
target: resolveTarget(match.groups.path.slice(1, -1)),
45+
range: absoluteRange(
46+
{
47+
start: indexToPosition(text, match.index + match[0].length - match.groups.path.length),
48+
end: indexToPosition(text, match.index + match[0].length),
49+
},
50+
range
51+
),
52+
})
53+
}
54+
}
55+
56+
return links
57+
}

packages/tailwindcss-language-service/src/util/state.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ export type EditorState = {
2929
}
3030
getConfiguration: (uri?: string) => Promise<Settings>
3131
getDocumentSymbols: (uri: string) => Promise<SymbolInformation[]>
32+
readDirectory: (
33+
document: TextDocument,
34+
directory: string
35+
) => Promise<Array<[name: string, type: { isDirectory: boolean }]>>
3236
}
3337

3438
type DiagnosticSeveritySetting = 'ignore' | 'warning' | 'error'

0 commit comments

Comments
 (0)