Skip to content

Commit

Permalink
Support XML attributes completion based on DTD (see #106)
Browse files Browse the repository at this point in the history
  • Loading branch information
angelozerr committed Dec 3, 2018
1 parent 0b75cae commit 4bd33b3
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* Copyright (c) 2018 Angelo ZERR.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Angelo Zerr <[email protected]> - initial API and implementation
*/
package org.eclipse.lsp4xml.extensions.dtd.contentmodel;

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

import org.apache.xerces.impl.dtd.XMLAttributeDecl;
import org.apache.xerces.impl.dtd.XMLSimpleType;
import org.eclipse.lsp4xml.extensions.contentmodel.model.CMAttributeDeclaration;

/**
* DTD attribute declaration.
*
*/
public class CMDTDAttributeDeclaration extends XMLAttributeDecl implements CMAttributeDeclaration {

@Override
public String getName() {
return super.name.localpart;
}

@Override
public String getDefaultValue() {
return super.simpleType.defaultValue;
}

@Override
public Collection<String> getEnumerationValues() {
String[] values = super.simpleType.enumeration;
if (values == null) {
return Collections.emptyList();
}
return Arrays.asList(values);
}

@Override
public String getDocumentation() {
return null;
}

@Override
public boolean isRequired() {
return super.simpleType.defaultType == XMLSimpleType.DEFAULT_TYPE_REQUIRED;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ public CMDocument createCMDocument(String key) {
return document;
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public Collection<CMElementDeclaration> getElements() {
elements = new ArrayList<>();
int index = grammar.getFirstElementDeclIndex();
while (index != -1) {
CMDTDElementDeclaration elementDecl = new CMDTDElementDeclaration(this);
CMDTDElementDeclaration elementDecl = new CMDTDElementDeclaration(this, index);
grammar.getElementDecl(index, elementDecl);
elements.add(elementDecl);
index = grammar.getNextElementDeclIndex(index);
Expand Down Expand Up @@ -132,8 +132,14 @@ void collectElementsDeclaration(String elementName, List<CMElementDeclaration> e
});
}

void collectAttributesDeclaration(String name, List<CMAttributeDeclaration> attributes) {
// TODO Auto-generated method stub

void collectAttributesDeclaration(CMDTDElementDeclaration elementDecl, List<CMAttributeDeclaration> attributes) {
int elementDeclIndex = grammar.getElementDeclIndex(elementDecl.name);
int index = grammar.getFirstAttributeDeclIndex(elementDeclIndex);
while (index != -1) {
CMDTDAttributeDeclaration attributeDecl = new CMDTDAttributeDeclaration();
grammar.getAttributeDecl(index, attributeDecl);
attributes.add(attributeDecl);
index = grammar.getNextAttributeDeclIndex(index);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,18 @@

/**
* DTD element declaration.
*
*
*/
public class CMDTDElementDeclaration extends XMLElementDecl implements CMElementDeclaration {

private final int index;
private final CMDTDDocument document;
private List<CMElementDeclaration> elements;
private List<CMAttributeDeclaration> attributes;

public CMDTDElementDeclaration(CMDTDDocument document) {
public CMDTDElementDeclaration(CMDTDDocument document, int index) {
this.document = document;
this.index = index;
}

@Override
Expand All @@ -47,7 +48,7 @@ public String getNamespace() {
public Collection<CMAttributeDeclaration> getAttributes() {
if (attributes == null) {
attributes = new ArrayList<>();
document.collectAttributesDeclaration(getName(), attributes);
document.collectAttributesDeclaration(this, attributes);
}
return attributes;
}
Expand All @@ -73,25 +74,31 @@ public CMElementDeclaration findCMElement(String tag, String namespace) {

@Override
public CMAttributeDeclaration findCMAttribute(String attributeName) {
for (CMAttributeDeclaration cmAttribute : getAttributes()) {
if (cmAttribute.getName().equals(attributeName)) {
return cmAttribute;
}
}
return null;
}

@Override
public String getDocumentation() {
// TODO Auto-generated method stub
return null;
}

@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return false;
return super.type == XMLElementDecl.TYPE_EMPTY;
}

@Override
public Collection<String> getEnumerationValues() {
// TODO Auto-generated method stub
return null;
}

public int getIndex() {
return index;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public void completionInRoot() throws BadLocationException {
"\r\n" + //
" <|";
testCompletionFor(xml,
c("delegatePublic", te(8, 4, 8, 5, "<delegatePublic></delegatePublic>"), "<delegatePublic"), //
c("public", te(8, 4, 8, 5, "<public></public>"), "<public"));
c("delegatePublic", te(8, 4, 8, 5, "<delegatePublic publicIdStartString=\"\" catalog=\"\" />"), "<delegatePublic"), //
c("public", te(8, 4, 8, 5, "<public publicId=\"\" uri=\"\" />"), "<public"));
}

private void testCompletionFor(String xml, CompletionItem... expectedItems) throws BadLocationException {
Expand Down

0 comments on commit 4bd33b3

Please sign in to comment.