2021-05-03 17:00:04 +00:00
|
|
|
import type { CodeAction, CodeActionParams, TextEdit, Range } from 'vscode-languageserver'
|
2020-10-08 15:20:54 +00:00
|
|
|
import { State } from '../util/state'
|
2020-06-19 11:14:30 +00:00
|
|
|
import { InvalidApplyDiagnostic } from '../diagnostics/types'
|
2020-10-08 15:20:54 +00:00
|
|
|
import { isCssDoc } from '../util/css'
|
|
|
|
import { getLanguageBoundaries } from '../util/getLanguageBoundaries'
|
|
|
|
import { getClassNameMeta } from '../util/getClassNameMeta'
|
|
|
|
import { getClassNameParts } from '../util/getClassNameAtPosition'
|
|
|
|
import { validateApply } from '../util/validateApply'
|
|
|
|
import { isWithinRange } from '../util/isWithinRange'
|
2022-04-13 16:10:47 +00:00
|
|
|
import dlv from 'dlv'
|
2021-05-03 17:00:04 +00:00
|
|
|
import type { Root, Source } from 'postcss'
|
2020-10-08 15:20:54 +00:00
|
|
|
import { absoluteRange } from '../util/absoluteRange'
|
|
|
|
import { removeRangesFromString } from '../util/removeRangesFromString'
|
2020-06-19 11:14:30 +00:00
|
|
|
import detectIndent from 'detect-indent'
|
2020-10-08 15:20:54 +00:00
|
|
|
import isObject from '../util/isObject'
|
|
|
|
import { cssObjToAst } from '../util/cssObjToAst'
|
2022-07-06 15:12:54 +00:00
|
|
|
import { dset } from 'dset'
|
2020-06-19 13:34:39 +00:00
|
|
|
import selectorParser from 'postcss-selector-parser'
|
2020-10-08 15:20:54 +00:00
|
|
|
import { flatten } from '../util/array'
|
2022-09-01 14:06:47 +00:00
|
|
|
import { getTextWithoutComments } from '../util/doc'
|
2020-06-19 11:14:30 +00:00
|
|
|
|
|
|
|
export async function provideInvalidApplyCodeActions(
|
|
|
|
state: State,
|
|
|
|
params: CodeActionParams,
|
|
|
|
diagnostic: InvalidApplyDiagnostic
|
|
|
|
): Promise<CodeAction[]> {
|
|
|
|
let document = state.editor.documents.get(params.textDocument.uri)
|
2022-10-18 19:35:02 +00:00
|
|
|
if (!document) return []
|
2022-09-01 14:06:47 +00:00
|
|
|
let documentText = getTextWithoutComments(document, 'css')
|
2020-06-19 11:14:30 +00:00
|
|
|
let cssRange: Range
|
|
|
|
let cssText = documentText
|
|
|
|
const { postcss } = state.modules
|
|
|
|
let changes: TextEdit[] = []
|
|
|
|
|
2021-05-03 17:00:04 +00:00
|
|
|
let totalClassNamesInClassList = diagnostic.className.classList.classList.split(/\s+/).length
|
2020-06-19 11:14:30 +00:00
|
|
|
|
|
|
|
let className = diagnostic.className.className
|
|
|
|
let classNameParts = getClassNameParts(state, className)
|
|
|
|
let classNameInfo = dlv(state.classNames.classNames, classNameParts)
|
|
|
|
|
|
|
|
if (Array.isArray(classNameInfo)) {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isCssDoc(state, document)) {
|
|
|
|
let languageBoundaries = getLanguageBoundaries(state, document)
|
|
|
|
if (!languageBoundaries) return []
|
2022-03-02 17:16:35 +00:00
|
|
|
cssRange = languageBoundaries
|
|
|
|
.filter((b) => b.type === 'css')
|
|
|
|
.find(({ range }) => isWithinRange(diagnostic.range.start, range))?.range
|
2020-06-19 11:14:30 +00:00
|
|
|
if (!cssRange) return []
|
2022-09-01 14:06:47 +00:00
|
|
|
cssText = getTextWithoutComments(document, 'css', cssRange)
|
2020-06-19 11:14:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2021-05-03 17:00:04 +00:00
|
|
|
await postcss
|
|
|
|
.module([
|
|
|
|
// TODO: use plain function?
|
|
|
|
// @ts-ignore
|
|
|
|
postcss.module.plugin('', (_options = {}) => {
|
|
|
|
return (root: Root) => {
|
|
|
|
root.walkRules((rule) => {
|
|
|
|
if (changes.length) return false
|
|
|
|
|
|
|
|
rule.walkAtRules('apply', (atRule) => {
|
|
|
|
let atRuleRange = postcssSourceToRange(atRule.source)
|
|
|
|
if (cssRange) {
|
|
|
|
atRuleRange = absoluteRange(atRuleRange, cssRange)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isWithinRange(diagnostic.range.start, atRuleRange)) return undefined // true
|
|
|
|
|
|
|
|
let ast = classNameToAst(
|
|
|
|
state,
|
|
|
|
classNameParts,
|
|
|
|
rule.selector,
|
|
|
|
diagnostic.className.classList.important
|
|
|
|
)
|
|
|
|
|
|
|
|
if (!ast) return false
|
|
|
|
|
|
|
|
rule.after(ast.nodes)
|
|
|
|
let insertedRule = rule.next()
|
|
|
|
if (!insertedRule) return false
|
|
|
|
|
|
|
|
if (totalClassNamesInClassList === 1) {
|
|
|
|
atRule.remove()
|
|
|
|
} else {
|
|
|
|
changes.push({
|
|
|
|
range: diagnostic.className.classList.range,
|
|
|
|
newText: removeRangesFromString(
|
|
|
|
diagnostic.className.classList.classList,
|
|
|
|
diagnostic.className.relativeRange
|
|
|
|
),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
let ruleRange = postcssSourceToRange(rule.source)
|
|
|
|
if (cssRange) {
|
|
|
|
ruleRange = absoluteRange(ruleRange, cssRange)
|
|
|
|
}
|
|
|
|
|
|
|
|
let outputIndent: string
|
|
|
|
let documentIndent = detectIndent(cssText)
|
|
|
|
|
2020-06-19 11:14:30 +00:00
|
|
|
changes.push({
|
2021-05-03 17:00:04 +00:00
|
|
|
range: ruleRange,
|
|
|
|
newText:
|
|
|
|
rule.toString() +
|
|
|
|
(insertedRule.raws.before || '\n\n') +
|
|
|
|
insertedRule
|
|
|
|
.toString()
|
|
|
|
.replace(/\n\s*\n/g, '\n')
|
|
|
|
.replace(/(@apply [^;\n]+)$/gm, '$1;')
|
|
|
|
.replace(/([^\s^]){$/gm, '$1 {')
|
|
|
|
.replace(/^\s+/gm, (m: string) => {
|
|
|
|
if (typeof outputIndent === 'undefined') outputIndent = m
|
|
|
|
return m.replace(new RegExp(outputIndent, 'g'), documentIndent.indent)
|
|
|
|
})
|
|
|
|
.replace(/^(\s+)(.*?[^{}]\n)([^\s}])/gm, '$1$2$1$3'),
|
2020-06-19 11:14:30 +00:00
|
|
|
})
|
2021-05-03 17:00:04 +00:00
|
|
|
|
|
|
|
return false
|
2020-06-19 11:14:30 +00:00
|
|
|
})
|
|
|
|
|
2021-05-03 17:00:04 +00:00
|
|
|
return undefined // true
|
2020-06-19 11:14:30 +00:00
|
|
|
})
|
2021-05-03 17:00:04 +00:00
|
|
|
}
|
|
|
|
}),
|
|
|
|
])
|
|
|
|
.process(cssText, { from: undefined })
|
2020-06-19 11:14:30 +00:00
|
|
|
} catch (_) {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!changes.length) {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
title: 'Extract to new rule',
|
2020-10-08 16:06:29 +00:00
|
|
|
kind: 'quickfix', // CodeActionKind.QuickFix,
|
2020-06-19 11:14:30 +00:00
|
|
|
diagnostics: [diagnostic],
|
|
|
|
edit: {
|
|
|
|
changes: {
|
|
|
|
[params.textDocument.uri]: changes,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2021-05-03 17:00:04 +00:00
|
|
|
function postcssSourceToRange(source: Source): Range {
|
2020-06-19 11:14:30 +00:00
|
|
|
return {
|
|
|
|
start: {
|
|
|
|
line: source.start.line - 1,
|
|
|
|
character: source.start.column - 1,
|
|
|
|
},
|
|
|
|
end: {
|
|
|
|
line: source.end.line - 1,
|
|
|
|
character: source.end.column,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function classNameToAst(
|
|
|
|
state: State,
|
|
|
|
classNameParts: string[],
|
|
|
|
selector: string,
|
|
|
|
important: boolean = false
|
|
|
|
) {
|
2020-06-21 15:05:46 +00:00
|
|
|
const baseClassName = classNameParts[classNameParts.length - 1]
|
|
|
|
const validatedBaseClassName = validateApply(state, [baseClassName])
|
2021-05-03 17:00:04 +00:00
|
|
|
if (validatedBaseClassName === null || validatedBaseClassName.isApplyable === false) {
|
2020-06-19 11:14:30 +00:00
|
|
|
return null
|
|
|
|
}
|
2020-06-19 13:40:19 +00:00
|
|
|
const meta = getClassNameMeta(state, classNameParts)
|
|
|
|
if (Array.isArray(meta)) return null
|
|
|
|
let context = meta.context
|
|
|
|
let pseudo = meta.pseudo
|
2020-06-19 11:14:30 +00:00
|
|
|
const globalContexts = state.classNames.context
|
|
|
|
const path = []
|
|
|
|
|
|
|
|
for (let i = 0; i < classNameParts.length - 1; i++) {
|
|
|
|
let part = classNameParts[i]
|
|
|
|
let common = globalContexts[part]
|
|
|
|
if (!common) return null
|
2021-05-17 11:38:52 +00:00
|
|
|
if (state.screens.includes(part)) {
|
2020-06-19 11:14:30 +00:00
|
|
|
path.push(`@screen ${part}`)
|
|
|
|
context = context.filter((con) => !common.includes(con))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
path.push(...context)
|
|
|
|
|
|
|
|
let obj = {}
|
|
|
|
for (let i = 1; i <= path.length; i++) {
|
|
|
|
dset(obj, path.slice(0, i), {})
|
|
|
|
}
|
2020-06-19 13:34:39 +00:00
|
|
|
|
|
|
|
selector = appendPseudosToSelector(selector, pseudo)
|
|
|
|
if (selector === null) return null
|
|
|
|
|
2020-06-19 11:14:30 +00:00
|
|
|
let rule = {
|
2020-06-19 13:34:39 +00:00
|
|
|
[selector]: {
|
2020-06-21 15:05:46 +00:00
|
|
|
[`@apply ${baseClassName}${important ? ' !important' : ''}`]: '',
|
2020-06-19 11:14:30 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
if (path.length) {
|
|
|
|
dset(obj, path, rule)
|
|
|
|
} else {
|
|
|
|
obj = rule
|
|
|
|
}
|
|
|
|
|
|
|
|
return cssObjToAst(obj, state.modules.postcss)
|
|
|
|
}
|
2020-06-19 13:34:39 +00:00
|
|
|
|
2021-05-03 17:00:04 +00:00
|
|
|
function appendPseudosToSelector(selector: string, pseudos: string[]): string | null {
|
2020-06-19 13:34:39 +00:00
|
|
|
if (pseudos.length === 0) return selector
|
|
|
|
|
|
|
|
let canTransform = true
|
|
|
|
|
|
|
|
let transformedSelector = selectorParser((selectors) => {
|
|
|
|
flatten(selectors.split((_) => true)).forEach((sel) => {
|
|
|
|
// @ts-ignore
|
|
|
|
for (let i = sel.nodes.length - 1; i >= 0; i--) {
|
|
|
|
// @ts-ignore
|
|
|
|
if (sel.nodes[i].type !== 'pseudo') {
|
|
|
|
break
|
|
|
|
// @ts-ignore
|
|
|
|
} else if (pseudos.includes(sel.nodes[i].value)) {
|
|
|
|
canTransform = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (canTransform) {
|
|
|
|
pseudos.forEach((p) => {
|
|
|
|
// @ts-ignore
|
|
|
|
sel.append(selectorParser.pseudo({ value: p }))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}).processSync(selector)
|
|
|
|
|
|
|
|
if (!canTransform) return null
|
|
|
|
|
|
|
|
return transformedSelector
|
|
|
|
}
|