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 May 6, 2019
1 parent 3168b2c commit b735f7a
Show file tree
Hide file tree
Showing 25 changed files with 1,016 additions and 93 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 @@ -30,14 +31,16 @@ public class DOMElement extends DOMNode implements org.w3c.dom.Element {

String tag;
boolean selfClosed;
//DomElement.start == startTagOpenOffset

// DomElement.start == startTagOpenOffset
Integer startTagOpenOffset; // |<root>
Integer startTagCloseOffset; // <root |>

Integer endTagOpenOffset; // <root> |</root >
Integer endTagCloseOffset;// <root> </root |>
//DomElement.end = <root> </root>| , is always scanner.getTokenEnd()
// 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 @@ -253,31 +256,29 @@ public boolean isSelfClosed() {
return selfClosed;
}


/**
* Will traverse backwards from the start offset
* returning an offset of the given character if it's found
* before another character. Whitespace is ignored.
* Will traverse backwards from the start offset returning an offset of the
* given character if it's found before another character. Whitespace is
* ignored.
*
* Returns null if the character is not found.
*
* The initial value for the start offset is not included.
* So have the offset 1 position after the character you want
* to start at.
* The initial value for the start offset is not included. So have the offset 1
* position after the character you want to start at.
*/
public Integer endsWith(char c, int startOffset) {
String text = this.getOwnerDocument().getText();
if(startOffset > text.length() || startOffset < 0) {
if (startOffset > text.length() || startOffset < 0) {
return null;
}
startOffset--;
while(startOffset >= 0) {
while (startOffset >= 0) {
char current = text.charAt(startOffset);
if(Character.isWhitespace(current)) {
if (Character.isWhitespace(current)) {
startOffset--;
continue;
}
if(current != c) {
if (current != c) {
return null;
}
return startOffset;
Expand All @@ -287,25 +288,24 @@ public Integer endsWith(char c, int startOffset) {

public Integer isNextChar(char c, int startOffset) {
String text = this.getOwnerDocument().getText();
if(startOffset > text.length() || startOffset < 0) {
if (startOffset > text.length() || startOffset < 0) {
return null;
}
while(startOffset < text.length()) {

while (startOffset < text.length()) {
char current = text.charAt(startOffset);
if(Character.isWhitespace(current)) {
if (Character.isWhitespace(current)) {
startOffset++;
continue;
}
if(current != c) {
if (current != c) {
return null;
}
return startOffset;
}
return null;
}


public boolean isSameTag(String tagInLowerCase) {
return this.tag != null && tagInLowerCase != null && this.tag.length() == tagInLowerCase.length()
&& this.tag.toLowerCase().equals(tagInLowerCase);
Expand Down Expand Up @@ -393,9 +393,65 @@ public boolean isEndTagClosed() {
return endTagCloseOffset != null;
}

public boolean hasChildElement(String name) {
if (name == null) {
return false;
}
DOMElement child = getChildElement(name);
if (child == null) {
return false;
}
return name.equals(child.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
* If Element has a closing end tag eg: <a> </a> -> true , <a> </b> -> false
*/
public boolean isClosed() {
return super.isClosed();
Expand All @@ -417,10 +473,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,8 +20,8 @@
*/
public interface CMDocument {

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

/**
* Returns the declared element which matches the given XML element and null
* otherwise.
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 b735f7a

Please sign in to comment.