Skip to content

Commit

Permalink
Disable snippets in front of XML declaration.
Browse files Browse the repository at this point in the history
Fixes #729.

Signed-off-by: Balduin Landolt <[email protected]>
  • Loading branch information
BalduinLandolt authored and angelozerr committed Jun 10, 2020
1 parent c1aeb8a commit befdda0
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,29 @@ public boolean hasProlog() {
return (children != null && !children.isEmpty() && children.get(0).isProlog());
}

/**
* If document has {@code <?xml ... ?>}, return this prolog, null otherwise
*
* @return prolog DOMNode, if the document has one, null otherwise
*/
public DOMNode getProlog() {
if (hasProlog()) {
return getChild(0);
} else {
return null;
}
}

/**
* Returns true, if the given offset is before XML declaration ({@code <?xml ...?>}), false otherwise.
*
* @param offset the offset position in question
* @return true, if before xml declaration; false otherwise
*/
public boolean isBeforeProlog(int offset){
return hasProlog() && offset <= getProlog().getStart();
}

private static SchemaLocation createSchemaLocation(DOMNode root, String schemaInstancePrefix) {
DOMAttr attr = root.getAttributeNode(getPrefixedName(schemaInstancePrefix, "schemaLocation"));
if (attr == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import java.util.Map;

import org.eclipse.lemminx.dom.DOMDocument;
import org.eclipse.lemminx.dom.DOMNode;
import org.eclipse.lemminx.services.extensions.ICompletionRequest;

Expand All @@ -36,6 +37,12 @@ public boolean isMatch(ICompletionRequest request, Map<String, String> model) {
// completion was triggered inside doctype declaration, ignore the snippets
return false;
}

DOMDocument document = request.getXMLDocument();
if (document.isBeforeProlog(request.getOffset())){
// triggered before prolog
return false;
}
return SnippetContextUtils.canAcceptExpression(request);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ public boolean isMatch(ICompletionRequest request, Map<String, String> model) {
return false;
}

if (document.isBeforeProlog(offset)){
// triggered before prolog
return false;
}

if (offset > documentElement.getStart()) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ public boolean isMatch(ICompletionRequest request, Map<String, String> model) {
// --> <?xml version="1.0" encoding="UTF-8" | ?>
return false;
}
if (document.isBeforeProlog(offset)) {
// triggered before prolog
return false;
}
// The file contains some contents, the contents allowed are:
// - comments
// - processing instruction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ public boolean isMatch(ICompletionRequest request, Map<String, String> model) {
return false;
}

if (document.hasProlog() && offset == 0){
// triggered before prolog
return false;
if (document.isBeforeProlog(offset)){
// triggered before prolog
return false;
}

if (documentElement != null && documentElement.getTagName() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public boolean isMatch(ICompletionRequest request, Map<String, String> model) {
// --> <?xml version="1.0" encoding="UTF-8" | ?>
return false;
}
// check if document already defined a xml processing instruction.
if (hasProlog(document, offset)) {
// check if document already defined a xml declaration.
if (document.isBeforeProlog(offset) || inProlog(document, offset)) {
return false;
}
Position start = request.getReplaceRange().getStart();
Expand All @@ -55,7 +55,7 @@ public boolean isMatch(ICompletionRequest request, Map<String, String> model) {
return true;
}

private static boolean hasProlog(DOMDocument document, int offset) {
private static boolean inProlog(DOMDocument document, int offset) {
DOMNode node = document.getFirstChild();
if (node == null) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -537,4 +537,57 @@ public void emptyCompletionInsideDOCTYPE() throws BadLocationException {
testCompletionFor("<!DOCTYPE root-element |SYSTEM \"file.dtd\">", 0);
testCompletionFor("<!DOCTYPE root-element SYSTEM |\"file.dtd\">", 0);
}

// Tests triggering completion at the very beginning of the file.
// Should be possible if not in front of the xml declaration.
@Test
public void atBeginningOfFile() throws BadLocationException{
// No Completion when triggered in front of xml declaration.
// Only XML declaration
String xml = "|<?xml version=\"1.0\" encoding=\"UTF-8\" ?>";
testCompletionFor(xml, 0);
// Space before declaration; triggered before space
xml = "| <?xml version=\"1.0\" encoding=\"UTF-8\" ?>";
testCompletionFor(xml, REGION_SNIPPETS); // FIXME: region snippets should not trigger here.
// Space before declaration; triggered after space
xml = " |<?xml version=\"1.0\" encoding=\"UTF-8\" ?>";
testCompletionFor(xml, REGION_SNIPPETS); // FIXME: region snippets should not trigger here.
// Declaration and a root element
xml = "|<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\r\n" + //
"<root/>";
testCompletionFor(xml, 0);
// Space before declaration; triggered before space
xml = "| <?xml version=\"1.0\" encoding=\"UTF-8\" ?>\r\n" + //
"<root/>";
testCompletionFor(xml, REGION_SNIPPETS); // FIXME: region snippets should not trigger here.
// Space before declaration; triggered after space
xml = " |<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\r\n" + //
"<root/>";
testCompletionFor(xml, REGION_SNIPPETS); // FIXME: region snippets should not trigger here.

// Multiple Snippets can be triggered at start of file, if it doesn't have an xml declaration
xml = "|\r\n" + //
"<root/>";
testCompletionFor(xml, //
COMMENT_SNIPPETS /* Comment Snippets */ + //
XML_DECLARATION_SNIPPETS /* XML Declaration Snippets */ + //
PROCESSING_INSTRUCTION_SNIPPETS /* Processing Instruction Snippets */ + //
DOCTYPE_SNIPPETS /* Doctype Snippets */ + //
REGION_SNIPPETS /* Region Snippets */, //
c("Insert XML Declaration", //
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>", //
r(0, 0, 0, 0), "<?xml"),
c("Insert XML Schema association", //
"<?xml-model href=\"file.xsd\" type=\"application/xml\" schematypens=\"http://www.w3.org/2001/XMLSchema\"?>", //
r(0, 0, 0, 0), "<?xml-model"),
c("Insert DTD association", //
"<?xml-model href=\"file.dtd\" type=\"application/xml-dtd\"?>", //
r(0, 0, 0, 0), "<?xml-model"),
c("Insert SYSTEM DOCTYPE", //
"<!DOCTYPE root SYSTEM \"file.dtd\">", //
r(0, 0, 0, 0), "<!DOCTYPE"),
c("<!--", //
"<!-- -->", //
r(0, 0, 0, 0), "<!--"));
}
}

0 comments on commit befdda0

Please sign in to comment.