Skip to content

Fix CSS conflict diagnostics in semicolonless CSS documents #771

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as semver from '../util/semver'
import { closest } from '../util/closest'
import { absoluteRange } from '../util/absoluteRange'
import { getTextWithoutComments } from '../util/doc'
import { isSemicolonlessCssLanguage } from '../util/languages'

export function getInvalidTailwindDirectiveDiagnostics(
state: State,
Expand All @@ -28,13 +29,8 @@ export function getInvalidTailwindDirectiveDiagnostics(
ranges.push(...boundaries.filter((b) => b.type === 'css').map(({ range }) => range))
}

let notSemicolonLanguages = ['sass', 'sugarss', 'stylus']
let regex: RegExp
if (
notSemicolonLanguages.includes(document.languageId) ||
(state.editor &&
notSemicolonLanguages.includes(state.editor.userLanguages[document.languageId]))
) {
if (isSemicolonlessCssLanguage(document.languageId, state.editor?.userLanguages)) {
regex = /(?:\s|^)@tailwind\s+(?<value>[^\r\n]+)/g
} else {
regex = /(?:\s|^)@tailwind\s+(?<value>[^;]+)/g
Expand Down
21 changes: 13 additions & 8 deletions packages/tailwindcss-language-service/src/util/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { getLanguageBoundaries } from './getLanguageBoundaries'
import { resolveRange } from './resolveRange'
import Regex from 'becke-ch--regex--s0-0-v1--base--pl--lib'
import { getTextWithoutComments } from './doc'
import { isSemicolonlessCssLanguage } from './languages'

export function findAll(re: RegExp, str: string): RegExpMatchArray[] {
let match: RegExpMatchArray
Expand Down Expand Up @@ -91,12 +92,16 @@ export async function findClassNamesInDocument(
)
}

export function findClassListsInCssRange(doc: TextDocument, range?: Range): DocumentClassList[] {
export function findClassListsInCssRange(
state: State,
doc: TextDocument,
range?: Range
): DocumentClassList[] {
const text = getTextWithoutComments(doc, 'css', range)
const matches = findAll(
/(@apply\s+)(?<classList>[^;}]+?)(?<important>\s*!important)?\s*[;}]/g,
text
)
let regex = isSemicolonlessCssLanguage(doc.languageId, state.editor?.userLanguages)
? /(@apply\s+)(?<classList>[^}\r\n]+?)(?<important>\s*!important)?(?:\r|\n|}|$)/g
: /(@apply\s+)(?<classList>[^;}]+?)(?<important>\s*!important)?\s*[;}]/g
const matches = findAll(regex, text)
const globalStart: Position = range ? range.start : { line: 0, character: 0 }

return matches.map((match) => {
Expand Down Expand Up @@ -292,7 +297,7 @@ export async function findClassListsInRange(
): Promise<DocumentClassList[]> {
let classLists: DocumentClassList[]
if (mode === 'css') {
classLists = findClassListsInCssRange(doc, range)
classLists = findClassListsInCssRange(state, doc, range)
} else {
classLists = await findClassListsInHtmlRange(state, doc, mode, range)
}
Expand All @@ -307,7 +312,7 @@ export async function findClassListsInDocument(
doc: TextDocument
): Promise<DocumentClassList[]> {
if (isCssDoc(state, doc)) {
return findClassListsInCssRange(doc)
return findClassListsInCssRange(state, doc)
}

let boundaries = getLanguageBoundaries(state, doc)
Expand All @@ -324,7 +329,7 @@ export async function findClassListsInDocument(
)),
...boundaries
.filter((b) => b.type === 'css')
.map(({ range }) => findClassListsInCssRange(doc, range)),
.map(({ range }) => findClassListsInCssRange(state, doc, range)),
await findCustomClassLists(state, doc),
])
)
Expand Down
14 changes: 14 additions & 0 deletions packages/tailwindcss-language-service/src/util/languages.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { EditorState } from './state'

export const htmlLanguages = [
'aspnetcorerazor',
'astro',
Expand Down Expand Up @@ -57,3 +59,15 @@ export const jsLanguages = [
export const specialLanguages = ['vue', 'svelte']

export const languages = [...cssLanguages, ...htmlLanguages, ...jsLanguages, ...specialLanguages]

const semicolonlessLanguages = ['sass', 'sugarss', 'stylus']

export function isSemicolonlessCssLanguage(
languageId: string,
userLanguages: EditorState['userLanguages'] = {}
) {
return (
semicolonlessLanguages.includes(languageId) ||
semicolonlessLanguages.includes(userLanguages[languageId])
)
}