add initial css helper hover
parent
97c4a29d88
commit
7b8ee549de
|
@ -4,29 +4,83 @@ 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)) {
|
)
|
||||||
return provideClassNameHover(state, params)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function provideCssHelperHover(
|
||||||
|
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(
|
||||||
state: State,
|
state: State,
|
||||||
{ 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
|
||||||
|
|
||||||
|
|
|
@ -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(
|
||||||
|
|
Loading…
Reference in New Issue