respect editor tab size in CSS previews

master
Brad Cornes 2020-11-26 20:07:39 +00:00
parent fa875e1d50
commit e0f8d73a79
8 changed files with 45 additions and 46 deletions

View File

@ -155,7 +155,11 @@ export function activate(context: ExtensionContext) {
registerConfigErrorHandler(emitter) registerConfigErrorHandler(emitter)
registerColorDecorator(client, context, emitter) registerColorDecorator(client, context, emitter)
onMessage(client, 'getConfiguration', async (scope) => { onMessage(client, 'getConfiguration', async (scope) => {
return Workspace.getConfiguration('tailwindCSS', scope) return {
tabSize:
Workspace.getConfiguration('editor', scope).get('tabSize') || 2,
...Workspace.getConfiguration('tailwindCSS', scope),
}
}) })
}) })

View File

@ -28,7 +28,6 @@ import {
doCodeActions, doCodeActions,
} from 'tailwindcss-language-service' } from 'tailwindcss-language-service'
import { URI } from 'vscode-uri' import { URI } from 'vscode-uri'
import { getDocumentSettings } from './util/getDocumentSettings'
import { import {
provideDiagnostics, provideDiagnostics,
updateAllDiagnostics, updateAllDiagnostics,
@ -44,6 +43,7 @@ let documents = new TextDocuments(TextDocument)
let workspaceFolder: string | null let workspaceFolder: string | null
const defaultSettings: Settings = { const defaultSettings: Settings = {
tabSize: 2,
emmetCompletions: false, emmetCompletions: false,
includeLanguages: {}, includeLanguages: {},
validate: true, validate: true,
@ -59,9 +59,6 @@ const defaultSettings: Settings = {
let globalSettings: Settings = defaultSettings let globalSettings: Settings = defaultSettings
let documentSettings: Map<string, Settings> = new Map() let documentSettings: Map<string, Settings> = new Map()
documents.onDidOpen((event) => {
getDocumentSettings(state, event.document)
})
documents.onDidClose((event) => { documents.onDidClose((event) => {
documentSettings.delete(event.document.uri) documentSettings.delete(event.document.uri)
}) })
@ -206,14 +203,14 @@ connection.onCompletion(
) )
connection.onCompletionResolve( connection.onCompletionResolve(
(item: CompletionItem): CompletionItem => { (item: CompletionItem): Promise<CompletionItem> => {
if (!state.enabled) return null if (!state.enabled) return null
return resolveCompletionItem(state, item) return resolveCompletionItem(state, item)
} }
) )
connection.onHover( connection.onHover(
(params: TextDocumentPositionParams): Hover => { (params: TextDocumentPositionParams): Promise<Hover> => {
if (!state.enabled) return null if (!state.enabled) return null
let document = state.editor.documents.get(params.textDocument.uri) let document = state.editor.documents.get(params.textDocument.uri)
if (!document) return null if (!document) return null

View File

@ -1,19 +0,0 @@
import { State, Settings } from './state'
import { TextDocument } from 'vscode-languageserver'
export async function getDocumentSettings(
state: State,
document: TextDocument
): Promise<Settings> {
if (!state.editor.capabilities.configuration) {
return Promise.resolve(state.editor.globalSettings)
}
let result = state.editor.documentSettings.get(document.uri)
if (!result) {
result = await state.emitter.emit('getConfiguration', {
languageId: document.languageId,
})
state.editor.documentSettings.set(document.uri, result)
}
return result
}

View File

@ -720,10 +720,10 @@ export async function doComplete(
return provideEmmetCompletions(state, document, position) return provideEmmetCompletions(state, document, position)
} }
export function resolveCompletionItem( export async function resolveCompletionItem(
state: State, state: State,
item: CompletionItem item: CompletionItem
): CompletionItem { ): Promise<CompletionItem> {
if (['helper', 'directive', 'variant', '@tailwind'].includes(item.data)) { if (['helper', 'directive', 'variant', '@tailwind'].includes(item.data)) {
return item return item
} }
@ -747,7 +747,8 @@ export function resolveCompletionItem(
} else { } else {
item.detail = getCssDetail(state, className) item.detail = getCssDetail(state, className)
if (!item.documentation) { if (!item.documentation) {
const css = stringifyCss(item.data.join(':'), className) const { tabSize } = await getDocumentSettings(state)
const css = stringifyCss(item.data.join(':'), className, tabSize)
if (css) { if (css) {
item.documentation = { item.documentation = {
kind: 'markdown' as typeof MarkupKind.Markdown, kind: 'markdown' as typeof MarkupKind.Markdown,

View File

@ -6,14 +6,15 @@ import { isCssContext } from './util/css'
import { findClassNameAtPosition } from './util/find' import { findClassNameAtPosition } from './util/find'
import { validateApply } from './util/validateApply' import { validateApply } from './util/validateApply'
import { getClassNameParts } from './util/getClassNameAtPosition' import { getClassNameParts } from './util/getClassNameAtPosition'
import { getDocumentSettings } from './util/getDocumentSettings'
export function doHover( export async function doHover(
state: State, state: State,
document: TextDocument, document: TextDocument,
position: Position position: Position
): Hover { ): Promise<Hover> {
return ( return (
provideClassNameHover(state, document, position) || (await provideClassNameHover(state, document, position)) ||
provideCssHelperHover(state, document, position) provideCssHelperHover(state, document, position)
) )
} }
@ -71,11 +72,11 @@ function provideCssHelperHover(
} }
} }
function provideClassNameHover( async function provideClassNameHover(
state: State, state: State,
document: TextDocument, document: TextDocument,
position: Position position: Position
): Hover { ): Promise<Hover> {
let className = findClassNameAtPosition(state, document, position) let className = findClassNameAtPosition(state, document, position)
if (className === null) return null if (className === null) return null
@ -89,9 +90,12 @@ function provideClassNameHover(
} }
} }
const { tabSize } = await getDocumentSettings(state, document)
const css = stringifyCss( const css = stringifyCss(
className.className, className.className,
dlv(state.classNames.classNames, [...parts, '__info']) dlv(state.classNames.classNames, [...parts, '__info']),
tabSize
) )
if (!css) return null if (!css) return null

View File

@ -3,17 +3,23 @@ import type { TextDocument } from 'vscode-languageserver'
export async function getDocumentSettings( export async function getDocumentSettings(
state: State, state: State,
document: TextDocument document?: TextDocument
): Promise<Settings> { ): Promise<Settings> {
if (!state.editor.capabilities.configuration) { if (!state.editor.capabilities.configuration) {
return Promise.resolve(state.editor.globalSettings) return Promise.resolve(state.editor.globalSettings)
} }
let result = state.editor.documentSettings.get(document.uri) const uri = document ? document.uri : undefined
let result = state.editor.documentSettings.get(uri)
if (!result) { if (!result) {
result = await state.emitter.emit('getConfiguration', { result = await state.emitter.emit(
languageId: document.languageId, 'getConfiguration',
}) document
state.editor.documentSettings.set(document.uri, result) ? {
languageId: document.languageId,
}
: undefined
)
state.editor.documentSettings.set(uri, result)
} }
return result return result
} }

View File

@ -29,6 +29,7 @@ export type EditorState = {
type DiagnosticSeveritySetting = 'ignore' | 'warning' | 'error' type DiagnosticSeveritySetting = 'ignore' | 'warning' | 'error'
export type Settings = { export type Settings = {
tabSize: number
emmetCompletions: boolean emmetCompletions: boolean
includeLanguages: Record<string, string> includeLanguages: Record<string, string>
validate: boolean validate: boolean

View File

@ -15,7 +15,11 @@ export function stringifyConfigValue(x: any): string {
return null return null
} }
export function stringifyCss(className: string, obj: any): string { export function stringifyCss(
className: string,
obj: any,
tabSize: number = 2
): string {
if (obj.__rule !== true && !Array.isArray(obj)) return null if (obj.__rule !== true && !Array.isArray(obj)) return null
if (Array.isArray(obj)) { if (Array.isArray(obj)) {
@ -25,19 +29,20 @@ export function stringifyCss(className: string, obj: any): string {
} }
let css = `` let css = ``
const indent = ' '.repeat(tabSize)
const context = dlv(obj, '__context', []) const context = dlv(obj, '__context', [])
const props = Object.keys(removeMeta(obj)) const props = Object.keys(removeMeta(obj))
if (props.length === 0) return null if (props.length === 0) return null
for (let i = 0; i < context.length; i++) { for (let i = 0; i < context.length; i++) {
css += `${'\t'.repeat(i)}${context[i]} {\n` css += `${indent.repeat(i)}${context[i]} {\n`
} }
const indentStr = '\t'.repeat(context.length) const indentStr = indent.repeat(context.length)
const decls = props.reduce((acc, curr, i) => { const decls = props.reduce((acc, curr, i) => {
const propStr = ensureArray(obj[curr]) const propStr = ensureArray(obj[curr])
.map((val) => `${indentStr + '\t'}${curr}: ${val};`) .map((val) => `${indentStr + indent}${curr}: ${val};`)
.join('\n') .join('\n')
return `${acc}${i === 0 ? '' : '\n'}${propStr}` return `${acc}${i === 0 ? '' : '\n'}${propStr}`
}, '') }, '')
@ -47,7 +52,7 @@ export function stringifyCss(className: string, obj: any): string {
)} {\n${decls}\n${indentStr}}` )} {\n${decls}\n${indentStr}}`
for (let i = context.length - 1; i >= 0; i--) { for (let i = context.length - 1; i >= 0; i--) {
css += `${'\t'.repeat(i)}\n}` css += `${indent.repeat(i)}\n}`
} }
return css return css