plugin icon indicating copy to clipboard operation
plugin copied to clipboard

[Request] Auto-complete boolean component properties in boolean style

Open inxilpro opened this issue 5 years ago • 5 comments

If a blade component has a boolean property, you can use a boolean attribute in the XML. For example:

class Toggle extends Component
{
  public function __construct(bool $toggled = false)
  {
    // ...
  }
}
<x-toggle toggled />

Right now PhpStorm will autocomplete the property expecting a value:

<x-toggle toggled="" />

Is it possible to not autocomplete the ="" when the prop is a boolean?

inxilpro avatar Oct 02 '20 14:10 inxilpro

Thank you, Chris! I don't have time to use all these new features, so each issue like that helps a lot. I'll try to implement it.

adelf avatar Oct 02 '20 19:10 adelf

It seems like something with the plugin changed and now it's triggering a notice when using boolean attributes:

image

I was able to address this issue with my Alpine.js plugin using:

(Although I'm not sure if the BasicXmlAttributeDescriptor is actually doing anything here.)

inxilpro avatar Apr 05 '21 14:04 inxilpro

Does it happen only with Laravel Idea turned on? Maybe it's an issue with XmlTag descriptor... I've found this code in the IntelliJ sources (RequiredAttributesInspectionBase.java):

    XmlAttributeDescriptor[] attributeDescriptors = elementDescriptor.getAttributesDescriptors(tag);
    Set<String> requiredAttributes = null;

    for (XmlAttributeDescriptor attribute : attributeDescriptors) {
      if (attribute != null && attribute.isRequired()) {
        if (requiredAttributes == null) {
          requiredAttributes = new HashSet<>();
        }
        requiredAttributes.add(attribute.getName(tag));
      }
    }

    if (requiredAttributes != null) {
      for (final String attrName : requiredAttributes) {
        if (!hasAttribute(tag, attrName) &&
            !XmlExtension.getExtension(tag.getContainingFile()).isRequiredAttributeImplicitlyPresent(tag, attrName)) {

          LocalQuickFix insertRequiredAttributeIntention = isOnTheFly ? XmlQuickFixFactory.getInstance().insertRequiredAttributeFix(tag, attrName) : null;
          final String localizedMessage = XmlErrorMessages.message("element.doesnt.have.required.attribute", name, attrName);
          reportOneTagProblem(
            tag,
            attrName,
            localizedMessage,
            insertRequiredAttributeIntention,
            holder,
            getIntentionAction(attrName),
            isOnTheFly
          );
        }
      }
    }
  }

adelf avatar Apr 05 '21 21:04 adelf

Also there:

  private static boolean hasAttribute(XmlTag tag, String attrName) {
    final XmlAttribute attribute = tag.getAttribute(attrName);
    if (attribute == null) return false;
    if (attribute.getValueElement() != null) return true;
    if (!(tag instanceof HtmlTag)) return false;
    final XmlAttributeDescriptor descriptor = attribute.getDescriptor();
    return descriptor != null && HtmlUtil.isBooleanAttribute(descriptor, tag);
  }

So, seems, HtmlUtil.isBooleanAttribute(descriptor, tag) returns false for your attributes.

adelf avatar Apr 06 '21 07:04 adelf

Hm. Would it be possible to provide hinting about which attributes are required/boolean based on the definition in the component class? i.e. if the constructor parameter has a bool type then it's marked as a boolean attribute, and if the constructor parameter doesn't have a default value it's marked as required?

inxilpro avatar Apr 06 '21 14:04 inxilpro