Skip to content

Commit

Permalink
Support completion with xs:any
Browse files Browse the repository at this point in the history
See redhat-developer/vscode-xml#177

Signed-off-by: azerr <azerr@redhat.com>
  • Loading branch information
angelozerr committed Aug 22, 2019
1 parent ae17fda commit f312f96
Show file tree
Hide file tree
Showing 6 changed files with 313 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,16 @@
*/
public class DOMElement extends DOMNode implements org.w3c.dom.Element {



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

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

int endTagOpenOffset = NULL_VALUE; // <root> |</root >
int endTagCloseOffset = NULL_VALUE;// <root> </root |>
//DomElement.end = <root> </root>| , is always scanner.getTokenEnd()
// DomElement.end = <root> </root>| , is always scanner.getTokenEnd()

public DOMElement(int start, int end) {
super(start, end);
Expand Down Expand Up @@ -121,21 +119,17 @@ public String getPrefix() {
@Override
public String getNamespaceURI() {
String prefix = getPrefix();
boolean hasPrefix = !StringUtils.isEmpty(prefix);
// Try to get xmlns attribute in the element
String rootElementNamespaceDeclarationName = (hasPrefix) ? XMLNS_NO_DEFAULT_ATTR + prefix // $NON-NLS-1$
: XMLNS_ATTR; // $NON-NLS-1$
String rootElementNamespace = this.getAttribute(rootElementNamespaceDeclarationName);
if (!StringUtils.isEmpty(rootElementNamespace)) {
return rootElementNamespace;
// Try to get xmlns attribute from the element
String namespaceURI = getNamespaceURI(prefix, this);
if (namespaceURI != null) {
return namespaceURI;
}
// try to get the namespace in the parent element
// try to get the namespace from the parent element
DOMNode parent = getParentNode();
while (parent != null) {
if (parent.getNodeType() == DOMNode.ELEMENT_NODE) {
DOMElement parentElement = ((DOMElement) parent);
String namespaceURI = hasPrefix ? parentElement.getAttribute(XMLNS_NO_DEFAULT_ATTR + prefix)
: parentElement.getNamespaceURI();
namespaceURI = getNamespaceURI(prefix, parentElement);
if (namespaceURI != null) {
return namespaceURI;
}
Expand All @@ -145,6 +139,20 @@ public String getNamespaceURI() {
return null;
}

/**
* Returns the namespace URI from the given prefix declared in the given element
* and null otherwise.
*
* @param prefix the prefix
* @param element the DOM element
* @return the namespace URI from the given prefix declared in the given element
* and null otherwise.
*/
public static String getNamespaceURI(String prefix, DOMElement element) {
boolean hasPrefix = !StringUtils.isEmpty(prefix);
return hasPrefix ? element.getAttribute(XMLNS_NO_DEFAULT_ATTR + prefix) : element.getAttribute(XMLNS_ATTR);
}

public Collection<String> getAllPrefixes() {
if (hasAttributes()) {
Collection<String> prefixes = new ArrayList<>();
Expand All @@ -171,7 +179,7 @@ public String getPrefix(String namespaceURI) {
if (hasAttributes()) {
for (DOMAttr attr : getAttributeNodes()) {
String prefix = attr.getPrefixIfMatchesURI(namespaceURI);
if(prefix != null) {
if (prefix != null) {
return prefix;
}
}
Expand All @@ -191,13 +199,6 @@ public String getPrefix(String namespaceURI) {
return null;
}

public String getNamespaceURI(String prefix) {
if (prefix == null || prefix.isEmpty()) {
return getNamespaceURI();
}
return getAttribute(XMLNS_NO_DEFAULT_ATTR + prefix);
}

public boolean isDocumentElement() {
return this.equals(getOwnerDocument().getDocumentElement());
}
Expand All @@ -206,31 +207,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 @@ -240,25 +239,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;
}


/**
* Returns true if the given tag is the same tag of this element and false
* otherwise.
Expand Down Expand Up @@ -328,7 +326,6 @@ public int getEndTagOpenOffset() {
return endTagOpenOffset;
}


/**
* Returns the end tag close offset and {@link DOMNode#NULL_VALUE} if it doesn't
* exist.
Expand Down Expand Up @@ -377,9 +374,9 @@ public boolean isStartTagClosed() {
public boolean isEndTagClosed() {
return getEndTagCloseOffset() != NULL_VALUE;
}

/**
* 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
*/
@Override
public boolean isClosed() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
package org.eclipse.lsp4xml.extensions.contentmodel.model;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import org.eclipse.lsp4j.LocationLink;
import org.eclipse.lsp4xml.dom.DOMElement;
Expand Down Expand Up @@ -70,4 +72,27 @@ public interface CMDocument {
* @return true if the content model document is dirty and false otherwise.
*/
boolean isDirty();

/**
* Returns the all elements declaration of the model.
*
* @return the all elements declaration of the model.
*/
default Collection<CMElementDeclaration> getAllElements() {
Set<CMElementDeclaration> all = new HashSet<>();
for (CMElementDeclaration element : this.getElements()) {
fillElements(element, all);
}
return all;
}

default void fillElements(CMElementDeclaration element, Set<CMElementDeclaration> all) {
if (!all.add(element)) {
return;
}
for (CMElementDeclaration child : element.getElements()) {
fillElements(child, all);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
*/
package org.eclipse.lsp4xml.extensions.contentmodel.model;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;

import org.eclipse.lsp4xml.dom.DOMElement;

Expand All @@ -20,6 +22,9 @@
*/
public interface CMElementDeclaration {

public static final Collection<CMElementDeclaration> ANY_ELEMENT_DECLARATIONS = Collections
.unmodifiableCollection(Arrays.asList());

/**
* Returns the declared element name.
*
Expand Down Expand Up @@ -62,11 +67,13 @@ default String getName(String prefix) {
Collection<CMElementDeclaration> getElements();

/**
* Returns the possible declared elements for the given DOM after element.
* Returns the possible declared elements at the given offset of the given
* parent element.
*
* @param afterElement the after element
* @param i
* @return the possible declared elements for the given DOM after element.
* @param parentElement the parent element
* @param offset the offset
* @return the possible declared elements at the given offset of the given
* parent element.
*/
Collection<CMElementDeclaration> getPossibleElements(DOMElement parentElement, int offset);

Expand Down Expand Up @@ -105,8 +112,18 @@ default String getName(String prefix) {
*/
boolean isEmpty();

/**
* Return the enumeration values.
*
* @return the enumeration values.
*/
Collection<String> getEnumerationValues();

/**
* Returns the owner document URI where the element is declared.
*
* @return the owner document URI where the element is declared.
*/
String getDocumentURI();

}
Loading

0 comments on commit f312f96

Please sign in to comment.