Skip to content

Commit e0f8d73

Browse files
committed
respect editor tab size in CSS previews
1 parent fa875e1 commit e0f8d73

File tree

8 files changed

+45
-46
lines changed

8 files changed

+45
-46
lines changed

packages/tailwindcss-intellisense/src/extension.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,11 @@ export function activate(context: ExtensionContext) {
155155
registerConfigErrorHandler(emitter)
156156
registerColorDecorator(client, context, emitter)
157157
onMessage(client, 'getConfiguration', async (scope) => {
158-
return Workspace.getConfiguration('tailwindCSS', scope)
158+
return {
159+
tabSize:
160+
Workspace.getConfiguration('editor', scope).get('tabSize') || 2,
161+
...Workspace.getConfiguration('tailwindCSS', scope),
162+
}
159163
})
160164
})
161165

packages/tailwindcss-intellisense/src/lsp/server.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import {
2828
doCodeActions,
2929
} from 'tailwindcss-language-service'
3030
import { URI } from 'vscode-uri'
31-
import { getDocumentSettings } from './util/getDocumentSettings'
3231
import {
3332
provideDiagnostics,
3433
updateAllDiagnostics,
@@ -44,6 +43,7 @@ let documents = new TextDocuments(TextDocument)
4443
let workspaceFolder: string | null
4544

4645
const defaultSettings: Settings = {
46+
tabSize: 2,
4747
emmetCompletions: false,
4848
includeLanguages: {},
4949
validate: true,
@@ -59,9 +59,6 @@ const defaultSettings: Settings = {
5959
let globalSettings: Settings = defaultSettings
6060
let documentSettings: Map<string, Settings> = new Map()
6161

62-
documents.onDidOpen((event) => {
63-
getDocumentSettings(state, event.document)
64-
})
6562
documents.onDidClose((event) => {
6663
documentSettings.delete(event.document.uri)
6764
})
@@ -206,14 +203,14 @@ connection.onCompletion(
206203
)
207204

208205
connection.onCompletionResolve(
209-
(item: CompletionItem): CompletionItem => {
206+
(item: CompletionItem): Promise<CompletionItem> => {
210207
if (!state.enabled) return null
211208
return resolveCompletionItem(state, item)
212209
}
213210
)
214211

215212
connection.onHover(
216-
(params: TextDocumentPositionParams): Hover => {
213+
(params: TextDocumentPositionParams): Promise<Hover> => {
217214
if (!state.enabled) return null
218215
let document = state.editor.documents.get(params.textDocument.uri)
219216
if (!document) return null

packages/tailwindcss-intellisense/src/lsp/util/getDocumentSettings.ts

Lines changed: 0 additions & 19 deletions
This file was deleted.

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -720,10 +720,10 @@ export async function doComplete(
720720
return provideEmmetCompletions(state, document, position)
721721
}
722722

723-
export function resolveCompletionItem(
723+
export async function resolveCompletionItem(
724724
state: State,
725725
item: CompletionItem
726-
): CompletionItem {
726+
): Promise<CompletionItem> {
727727
if (['helper', 'directive', 'variant', '@tailwind'].includes(item.data)) {
728728
return item
729729
}
@@ -747,7 +747,8 @@ export function resolveCompletionItem(
747747
} else {
748748
item.detail = getCssDetail(state, className)
749749
if (!item.documentation) {
750-
const css = stringifyCss(item.data.join(':'), className)
750+
const { tabSize } = await getDocumentSettings(state)
751+
const css = stringifyCss(item.data.join(':'), className, tabSize)
751752
if (css) {
752753
item.documentation = {
753754
kind: 'markdown' as typeof MarkupKind.Markdown,

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ import { isCssContext } from './util/css'
66
import { findClassNameAtPosition } from './util/find'
77
import { validateApply } from './util/validateApply'
88
import { getClassNameParts } from './util/getClassNameAtPosition'
9+
import { getDocumentSettings } from './util/getDocumentSettings'
910

10-
export function doHover(
11+
export async function doHover(
1112
state: State,
1213
document: TextDocument,
1314
position: Position
14-
): Hover {
15+
): Promise<Hover> {
1516
return (
16-
provideClassNameHover(state, document, position) ||
17+
(await provideClassNameHover(state, document, position)) ||
1718
provideCssHelperHover(state, document, position)
1819
)
1920
}
@@ -71,11 +72,11 @@ function provideCssHelperHover(
7172
}
7273
}
7374

74-
function provideClassNameHover(
75+
async function provideClassNameHover(
7576
state: State,
7677
document: TextDocument,
7778
position: Position
78-
): Hover {
79+
): Promise<Hover> {
7980
let className = findClassNameAtPosition(state, document, position)
8081
if (className === null) return null
8182

@@ -89,9 +90,12 @@ function provideClassNameHover(
8990
}
9091
}
9192

93+
const { tabSize } = await getDocumentSettings(state, document)
94+
9295
const css = stringifyCss(
9396
className.className,
94-
dlv(state.classNames.classNames, [...parts, '__info'])
97+
dlv(state.classNames.classNames, [...parts, '__info']),
98+
tabSize
9599
)
96100

97101
if (!css) return null

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,23 @@ import type { TextDocument } from 'vscode-languageserver'
33

44
export async function getDocumentSettings(
55
state: State,
6-
document: TextDocument
6+
document?: TextDocument
77
): Promise<Settings> {
88
if (!state.editor.capabilities.configuration) {
99
return Promise.resolve(state.editor.globalSettings)
1010
}
11-
let result = state.editor.documentSettings.get(document.uri)
11+
const uri = document ? document.uri : undefined
12+
let result = state.editor.documentSettings.get(uri)
1213
if (!result) {
13-
result = await state.emitter.emit('getConfiguration', {
14-
languageId: document.languageId,
15-
})
16-
state.editor.documentSettings.set(document.uri, result)
14+
result = await state.emitter.emit(
15+
'getConfiguration',
16+
document
17+
? {
18+
languageId: document.languageId,
19+
}
20+
: undefined
21+
)
22+
state.editor.documentSettings.set(uri, result)
1723
}
1824
return result
1925
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export type EditorState = {
2929
type DiagnosticSeveritySetting = 'ignore' | 'warning' | 'error'
3030

3131
export type Settings = {
32+
tabSize: number
3233
emmetCompletions: boolean
3334
includeLanguages: Record<string, string>
3435
validate: boolean

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ export function stringifyConfigValue(x: any): string {
1515
return null
1616
}
1717

18-
export function stringifyCss(className: string, obj: any): string {
18+
export function stringifyCss(
19+
className: string,
20+
obj: any,
21+
tabSize: number = 2
22+
): string {
1923
if (obj.__rule !== true && !Array.isArray(obj)) return null
2024

2125
if (Array.isArray(obj)) {
@@ -25,19 +29,20 @@ export function stringifyCss(className: string, obj: any): string {
2529
}
2630

2731
let css = ``
32+
const indent = ' '.repeat(tabSize)
2833

2934
const context = dlv(obj, '__context', [])
3035
const props = Object.keys(removeMeta(obj))
3136
if (props.length === 0) return null
3237

3338
for (let i = 0; i < context.length; i++) {
34-
css += `${'\t'.repeat(i)}${context[i]} {\n`
39+
css += `${indent.repeat(i)}${context[i]} {\n`
3540
}
3641

37-
const indentStr = '\t'.repeat(context.length)
42+
const indentStr = indent.repeat(context.length)
3843
const decls = props.reduce((acc, curr, i) => {
3944
const propStr = ensureArray(obj[curr])
40-
.map((val) => `${indentStr + '\t'}${curr}: ${val};`)
45+
.map((val) => `${indentStr + indent}${curr}: ${val};`)
4146
.join('\n')
4247
return `${acc}${i === 0 ? '' : '\n'}${propStr}`
4348
}, '')
@@ -47,7 +52,7 @@ export function stringifyCss(className: string, obj: any): string {
4752
)} {\n${decls}\n${indentStr}}`
4853

4954
for (let i = context.length - 1; i >= 0; i--) {
50-
css += `${'\t'.repeat(i)}\n}`
55+
css += `${indent.repeat(i)}\n}`
5156
}
5257

5358
return css

0 commit comments

Comments
 (0)