Skip to content
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

Extension attempts to autocomplete in strings that aren't classes #18

Open
michaelflores-italic opened this issue Sep 30, 2021 · 7 comments
Labels
enhancement New feature or request
Milestone

Comments

@michaelflores-italic
Copy link

michaelflores-italic commented Sep 30, 2021

I noticed that I was getting Tailwind autocomplete suggestions when typing code comments in TSX files. I filed an Issue with Nova but they pointed me to the extension so I am filing it here. This should be reproducible in any TSX or likely JSX file, but I can include a sample here if it proves difficult to repro.

@michaelflores-italic michaelflores-italic changed the title Extension attempts to autocomplete in string fields that aren't classes Extension attempts to autocomplete in strings that aren't classes Sep 30, 2021
@jasonplatts
Copy link
Owner

Thanks for letting me know Michael. Yes, unfortunately, the current version of the extension does not do a great job of preventing suggestions in these types of situations. Improved suggestions are on the roadmap.

@jasonplatts jasonplatts added the enhancement New feature or request label Oct 2, 2021
@jasonplatts jasonplatts added this to the v3.1 milestone Oct 2, 2021
@jasonplatts
Copy link
Owner

Hi @michaelflores-italic. Version 4.1 improves autocompletion suggestions. Please let me know if you continue to have any issues in TSX or JSX files.

Thanks!

@jasonplatts
Copy link
Owner

I'm going to assume this problem is resolved, so will close this issue. If any problems remain, please feel free to open another issue.

@alex-ketch
Copy link

Hi @jasonplatts, unfortunately I'm facing this issue with the latest release.
The extension populates list of suggestions for all fields, not just className

Can be tested with a simple TSX file:

import React from 'react'

export const Test = () => {
  return <input type="" />
}

Cursor was located in the type attribute value
Screenshot 2022-03-15 at 16 39 31

Let me know if you need any more info, or feel free to point me in the general direction of where a fix would need to happen and can poke around :)

Thanks!

@jasonplatts
Copy link
Owner

Hi @alex-ketch. Sorry for the delay getting back to you on this issue.

At the moment, the extension allows completion suggestions for certain file types when inside single or double quotes. This allows for dynamically assigning classes using variables. However, a downside to this approach is that it creates a number of cases where suggestions are offered at inappropriate times.

It may be possible to address this particular situation using scopes. When I have a moment, I will definitely be happy to take a look at this and make improvements. In the meantime, absolutely feel free to poke around. PRs are always welcome. The relevant method is the _preventCompletions method in completion_provider.js. I might suggest adding a _invalidReactContext method similar to _invalidVueContext method that already exists.

Thanks again. I'll reopen this issue.

@jasonplatts jasonplatts reopened this May 16, 2022
@alex-ketch
Copy link

Sorry for not getting back to you sooner @jasonplatts, I only ever had a partial solution and was hoping for an epiphany for a workaround.
I've filed a feature request in the Nova forum, but in case anyone is curious for a rough patch, I've modified the completion_provider.js#121 as such:

// Allow completions if context selectors is a HTML class attribute value.
- if (context.selectors[0].matches('html.tag.attribute.class.value.double-quoted')) { return false }
+ if (
+   context.selectors[0].matches('tsx.value') ||
+   // Allow completions if context selectors is a HTML class attribute value.
+   context.selectors[0].matches('html.tag.attribute.class.value.double-quoted') ||
+   // Allow completions in other files if contained within single quotes or double quotes.
+   // This, for example, enables completions in Rails ERB files when passing a class option to the link_to method.
+   context.selectors[0].classes.includes('single-quoted') ||
+   context.selectors[0].classes.includes('double-quoted')
+ ) { 
+   const classNameRegExp = new RegExp(/=(\w+)/, 'g')
+   let tagName = context.line.trimStart().trim().split('').reverse().join('')
+   tagName = classNameRegExp.exec(tagName)[1].split('').reverse().join('')
+ 
+   return !(tagName || '').startsWith('class')
+ }

if (context.selectors[0].matches('css')) { return false }

@EmranMR
Copy link
Contributor

EmranMR commented Jan 26, 2023

Sorry for not getting back to you sooner @jasonplatts, I only ever had a partial solution and was hoping for an epiphany for a workaround. I've filed a feature request in the Nova forum, but in case anyone is curious for a rough patch, I've modified the completion_provider.js#121 as such:

@alex-ketch There is a command that allows you to turn off tailwind suggestions on the fly. Just open up the command palette and press tt or type Toggle TailwindCSS. It is handy when coding outside of template files. I remember discussing this with Jason when I submitted a pull request similar to yours. #24 Unfortunately so many frameworks and templates that it makes it impossible to keep up with. Not sure if the new tree-sitter parser is any better, allowing us cherry picking context more appropriately?

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

No branches or pull requests

4 participants