add initial css helper hover

master
Brad Cornes 2020-04-12 17:53:22 +01:00
parent 97c4a29d88
commit 7b8ee549de
2 changed files with 60 additions and 6 deletions

View File

@ -4,22 +4,73 @@ import {
getClassNameAtPosition, getClassNameAtPosition,
getClassNameParts, getClassNameParts,
} from '../util/getClassNameAtPosition' } from '../util/getClassNameAtPosition'
import { stringifyCss } from '../util/stringify' import { stringifyCss, stringifyConfigValue } from '../util/stringify'
const dlv = require('dlv') const dlv = require('dlv')
import escapeClassName from 'css.escape' import escapeClassName from 'css.escape'
import { isHtmlContext } from '../util/html' import { isHtmlContext } from '../util/html'
import { isCssContext } from '../util/css'
export function provideHover( export function provideHover(
state: State, state: State,
params: TextDocumentPositionParams params: TextDocumentPositionParams
): Hover { ): Hover {
let doc = state.editor.documents.get(params.textDocument.uri) return (
provideClassNameHover(state, params) || provideCssHelperHover(state, params)
)
}
if (isHtmlContext(doc, params.position)) { function provideCssHelperHover(
return provideClassNameHover(state, params) state: State,
{ textDocument, position }: TextDocumentPositionParams
): Hover {
let doc = state.editor.documents.get(textDocument.uri)
if (!isCssContext(doc, position)) return null
const line = doc.getText({
start: { line: position.line, character: 0 },
end: { line: position.line + 1, character: 0 },
})
const match = line.match(
/(?<helper>theme|config)\((?<quote>['"])(?<key>[^)]+)\k<quote>\)/
)
if (match === null) return null
const startChar = match.index + 7
const endChar = startChar + match.groups.key.length
if (position.character < startChar || position.character >= endChar) {
return null
} }
return null let key = match.groups.key
.split(/(\[[^\]]+\]|\.)/)
.filter(Boolean)
.filter((x) => x !== '.')
.map((x) => x.replace(/^\[([^\]]+)\]$/, '$1'))
if (key.length === 0) return null
if (match.groups.helper === 'theme') {
key = ['theme', ...key]
}
const value = stringifyConfigValue(dlv(state.config, key))
if (value === null) return null
return {
contents: { kind: 'plaintext', value },
range: {
start: { line: position.line, character: startChar },
end: {
line: position.line,
character: endChar,
},
},
}
} }
function provideClassNameHover( function provideClassNameHover(
@ -27,6 +78,9 @@ function provideClassNameHover(
{ textDocument, position }: TextDocumentPositionParams { textDocument, position }: TextDocumentPositionParams
): Hover { ): Hover {
let doc = state.editor.documents.get(textDocument.uri) let doc = state.editor.documents.get(textDocument.uri)
if (!isHtmlContext(doc, position)) return null
let hovered = getClassNameAtPosition(doc, position) let hovered = getClassNameAtPosition(doc, position)
if (!hovered) return null if (!hovered) return null

View File

@ -9,7 +9,7 @@ export function stringifyConfigValue(x: any): string {
.filter(Boolean) .filter(Boolean)
.join(', ') .join(', ')
} }
return '' return null
} }
export function stringifyCss( export function stringifyCss(