Skip to content

Custom classRegex with class suggestions but no hover preview #946

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

Closed
yunsii opened this issue Apr 4, 2024 · 3 comments
Closed

Custom classRegex with class suggestions but no hover preview #946

yunsii opened this issue Apr 4, 2024 · 3 comments

Comments

@yunsii
Copy link

yunsii commented Apr 4, 2024

What version of VS Code are you using?

v1.87.2

What version of Tailwind CSS IntelliSense are you using?

v0.10.5

What version of Tailwind CSS are you using?

v3.3.3

What package manager are you using?

pnpm

What operating system are you using?

WSL

Tailwind config

import type { Config } from 'tailwindcss'

/** @type {import('tailwindcss').Config} */
const config: Config = {
  // corePlugins: {
  //   preflight: false,
  // },
  content: ['./src/**/demos/**/*.{ts,tsx}', './docs/**/*.{ts,tsx,mdx}'],
  theme: {
    extend: {},
  },
  plugins: [],
}

export default config

VS Code settings

{
  "tailwindCSS.experimental.classRegex": ["tw`((\\S|\\s)*?)`"]
}

Reproduction URL

https://github.com/yunsii/tagged-classnames-free/blob/master/docs/pages/examples/demos/demo1/index.tsx#L11-L19

Describe your issue

image

It has class suggestions but no hover preview.

@gremo
Copy link

gremo commented Apr 9, 2024

Same here:

  "tailwindCSS.experimental.classRegex": [
    "cva\\(([^)]*)\\)",
    "html_classes\\(([^)]*)\\)"
  ],

@thecrypticace
Copy link
Contributor

Hey! So this has to do with what the capture group inside the regex represents and how it is used within Intellisense. Completions generally happen in any "a class can be anywhere around this area" context. While hovers have to match class names as best it can to what might be a class — only then do we go look it up to give you the definition. In many cases these contexts can be the same but in a case like this they are not.

I've taken the regex from above and highlighted the ranges of text Intellisense thinks could be "class-like" in purple:

Screenshot 2024-04-11 at 21 04 57

You'll notice that you see things like 'font-bold, underline', or 'italic':. This is because when searching for classes inside a class regex we're pretty permissive in what we consider to be the boundary of a class.

If you were to change your classRegex setting to define patterns for:

  1. the container — the block(s) of text that may contain one or more class lists
  2. the class list itself — the block of text that contain one or more classes — in this case inside a string literal
{
  "tailwindCSS.experimental.classRegex": [
    ["tw`((?:\\S|\\s)*?)`", "'([^']+)'"]
  }
}

It'll look at these instead:

  • The gray represents the container
  • The orange represents a class list
  • The purple represents a class name
Screenshot 2024-04-11 at 21 05 16

Now this doesn't pick up everything but you can define multiple class lists:

{
  "tailwindCSS.experimental.classRegex": [
    ["tw`((?:\\S|\\s)*?)`", "'([^']+)'"],

    // For now, it's important that this comes second.
    "tw`((\\S|\\s)*?)`"
  ]
}

Doing this will consult each of the regexes in turn for possible classes and you'll get the results you expect:

Screenshot 2024-04-11 at 21 26 10 Screenshot 2024-04-11 at 21 26 17

Hope that helps! ✨

@yunsii
Copy link
Author

yunsii commented Apr 12, 2024

@thecrypticace Thanks for your patient explanation, it works like a charm 🫡

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants