Skip to content

Commit

Permalink
Completion for elements considers modelGroup of a complex type.
Browse files Browse the repository at this point in the history
This includes handling for (choice, all, and sequence)

Fixes eclipse-lemminx#347

Signed-off-by: Nikolas <[email protected]>
  • Loading branch information
NikolasKomonen committed Apr 18, 2019
1 parent 6bf1a3b commit 43aa922
Show file tree
Hide file tree
Showing 21 changed files with 955 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import org.eclipse.lsp4xml.utils.StringUtils;
import org.w3c.dom.DOMException;
Expand All @@ -39,6 +40,8 @@ public class DOMElement extends DOMNode implements org.w3c.dom.Element {
Integer endTagCloseOffset;// <root> </root |>
//DomElement.end = <root> </root>| , is always scanner.getTokenEnd()

List<DOMElement> childElements;

public DOMElement(int start, int end, DOMDocument ownerDocument) {
super(start, end, ownerDocument);
}
Expand Down Expand Up @@ -393,6 +396,58 @@ public boolean isEndTagClosed() {
return endTagCloseOffset != null;
}

public boolean hasChildElement(String name) {
if(name == null) {
return false;
}
return name.equals(getChildElement(name).getTagName());
}

public DOMElement getChildElement(String name) {
if(!hasChildNodes() || name == null || name.isEmpty()) {
return null;
}
List<DOMElement> children = getChildElements();
for (DOMElement child : children) {
if(child instanceof DOMElement) {
if(name.equals(child.getTagName())) {
return child;
}
}
}
return null;
}

public DOMElement getChildElementAfterOffset(int offset) {
List<DOMElement> children = getChildElements();
for (DOMElement child : children) {
if(child instanceof DOMElement) {
if(child.getStart() > offset) {
return child;
}
}
}
return null;
}

@Override
public void addChild(DOMNode child) {
if(child instanceof DOMElement) {
if (childElements == null) {
childElements = new ArrayList<DOMElement>();
}
childElements.add((DOMElement) child);
}
super.addChild(child);
}

public List<DOMElement> getChildElements() {
if (childElements == null) {
childElements = new ArrayList<DOMElement>();
}
return this.childElements;
}

@Override
/**
* If Element has a closing end tag eg: <a> </a> -> true , <a> </b> -> false
Expand All @@ -417,10 +472,23 @@ public DOMAttr getAttributeNodeNS(String arg0, String arg1) throws DOMException
}

@Override
public NodeList getElementsByTagName(String arg0) {
public NodeList getElementsByTagName(String elementName) {
return null;
}

public int getNumberOfChildElementsWithTagName(String elementName) {
if(elementName == null) {
return 0;
}
int occurrences = 0;
for (DOMElement childElement : childElements) {
if(elementName.equals(childElement.getTagName())) {
occurrences++;
}
}
return occurrences;
}

@Override
public NodeList getElementsByTagNameNS(String arg0, String arg1) throws DOMException {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*/
package org.eclipse.lsp4xml.extensions.contentmodel.model;

import java.util.Collection;
import java.util.List;

import org.eclipse.lsp4xml.dom.DOMElement;

Expand All @@ -20,7 +20,7 @@
*/
public interface CMDocument {

Collection<CMElementDeclaration> getElements();
List<CMElementDeclaration> getElements();

/**
* Returns the declared element which matches the given XML element and null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package org.eclipse.lsp4xml.extensions.contentmodel.model;

import java.util.Collection;
import java.util.List;

/**
* Content model element which abstracts element declaration from a given
Expand Down Expand Up @@ -57,7 +58,7 @@ default String getName(String prefix) {
*
* @return the children declared element of this declared element.
*/
Collection<CMElementDeclaration> getElements();
List<CMElementDeclaration> getElements();

/**
* Returns the declared element which matches the given XML tag name / namespace
Expand Down
Loading

0 comments on commit 43aa922

Please sign in to comment.