Class InsertAttributeCodeAction

java.lang.Object
org.eclipse.lemminx.services.extensions.codeaction.InsertAttributeCodeAction
All Implemented Interfaces:
ICodeActionParticipant
Direct Known Subclasses:
cvc_complex_type_4CodeAction, MSG_REQUIRED_ATTRIBUTE_NOT_SPECIFIEDCodeAction, required_attribute_missingCodeAction

public class InsertAttributeCodeAction extends Object implements ICodeActionParticipant
Base code action to insert missing attributes on an element.

Handles the common logic: finding the insert position after the last existing attribute (or after the tag name), and generating SnippetTextEdit via XMLElementGenerator.generate(Collection, String) which handles formatting (split attributes, namespace prefixes, snippet choice/placeholder syntax).

Subclasses override getAttributesToInsert(DOMElement, ICodeActionRequest) to provide context-specific attributes from the content model.

For the simple single-attribute case, use InsertAttributeCodeAction(String) — the default implementation of getAttributesToInsert(org.eclipse.lemminx.dom.DOMElement, org.eclipse.lemminx.services.extensions.codeaction.ICodeActionRequest) looks up the CM attribute declaration automatically.

  • Constructor Details

    • InsertAttributeCodeAction

      public InsertAttributeCodeAction(String attributeName)
      Creates a code action that inserts a single attribute with the given name. The attribute declaration is looked up from the content model.
      Parameters:
      attributeName - the attribute name to insert.
    • InsertAttributeCodeAction

      protected InsertAttributeCodeAction()
      Creates a code action for subclasses that override getAttributesToInsert(DOMElement, ICodeActionRequest) to provide their own attributes.
  • Method Details

    • doCodeAction

      public void doCodeAction(ICodeActionRequest request, List<org.eclipse.lsp4j.CodeAction> codeActions, org.eclipse.lsp4j.jsonrpc.CancelChecker cancelChecker)
      Description copied from interface: ICodeActionParticipant
      Collect the code action in the given codeActions for the given code action request request.
      Specified by:
      doCodeAction in interface ICodeActionParticipant
      Parameters:
      request - the code action request.
      codeActions - list of code actions to fill.
      cancelChecker - the cancel checker.
    • getAttributesToInsert

      protected Collection<CMAttributeDeclaration> getAttributesToInsert(DOMElement element, ICodeActionRequest request)
      Returns the attributes to insert on the element.

      When attributeName is set, looks up that single CM attribute. Otherwise, returns all missing required attributes from the content model.

      Parameters:
      element - the element where attributes will be inserted.
      request - the code action request.
      Returns:
      the attributes to insert, or empty if none.
    • getCodeActionTitle

      protected String getCodeActionTitle(Collection<CMAttributeDeclaration> attributes)
      Returns the title for the code action.
      Parameters:
      attributes - the attributes that will be inserted.
      Returns:
      the code action title.
    • getInsertPosition

      protected static org.eclipse.lsp4j.Position getInsertPosition(DOMElement element, DOMDocument document) throws BadLocationException
      Returns the position where new attributes should be inserted: after the last existing attribute's closing quote, or after the tag name if no attributes exist.
      Throws:
      BadLocationException
    • findCMAttribute

      protected static CMAttributeDeclaration findCMAttribute(DOMElement element, String attributeName, ICodeActionRequest request)
      Finds a CM attribute declaration for the given attribute name on the given element, using the content model.
      Parameters:
      element - the element to look up.
      attributeName - the attribute name to find.
      request - the code action request.
      Returns:
      the attribute declaration, or null if not found.
    • findRequiredAttributes

      protected static Collection<CMAttributeDeclaration> findRequiredAttributes(DOMElement element, ICodeActionRequest request)
      Finds the missing required attributes from the content model for the given element. Shared by XSD, DTD, and RNG code actions.
      Parameters:
      element - the element to find missing required attributes for.
      request - the code action request.
      Returns:
      the missing required attributes, or empty if none.