Class InsertAttributeCodeAction
- All Implemented Interfaces:
ICodeActionParticipant
- Direct Known Subclasses:
cvc_complex_type_4CodeAction,MSG_REQUIRED_ATTRIBUTE_NOT_SPECIFIEDCodeAction,required_attribute_missingCodeAction
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 Summary
ConstructorsModifierConstructorDescriptionprotectedCreates a code action for subclasses that overridegetAttributesToInsert(DOMElement, ICodeActionRequest)to provide their own attributes.InsertAttributeCodeAction(String attributeName) Creates a code action that inserts a single attribute with the given name. -
Method Summary
Modifier and TypeMethodDescriptionvoiddoCodeAction(ICodeActionRequest request, List<org.eclipse.lsp4j.CodeAction> codeActions, org.eclipse.lsp4j.jsonrpc.CancelChecker cancelChecker) Collect the code action in the givencodeActionsfor the given code action requestrequest.protected static CMAttributeDeclarationfindCMAttribute(DOMElement element, String attributeName, ICodeActionRequest request) Finds a CM attribute declaration for the given attribute name on the given element, using the content model.protected static Collection<CMAttributeDeclaration> findRequiredAttributes(DOMElement element, ICodeActionRequest request) Finds the missing required attributes from the content model for the given element.protected Collection<CMAttributeDeclaration> getAttributesToInsert(DOMElement element, ICodeActionRequest request) Returns the attributes to insert on the element.protected StringgetCodeActionTitle(Collection<CMAttributeDeclaration> attributes) Returns the title for the code action.protected static org.eclipse.lsp4j.PositiongetInsertPosition(DOMElement element, DOMDocument document) 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.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.eclipse.lemminx.services.extensions.codeaction.ICodeActionParticipant
doCodeActionUnconditional, getResolveCodeActionParticipant
-
Constructor Details
-
InsertAttributeCodeAction
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 overridegetAttributesToInsert(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:ICodeActionParticipantCollect the code action in the givencodeActionsfor the given code action requestrequest.- Specified by:
doCodeActionin interfaceICodeActionParticipant- 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
attributeNameis 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
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
nullif 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.
-