Skip to content
This repository has been archived by the owner on Mar 1, 2024. It is now read-only.

Commit

Permalink
Merge pull request #129 from eclipse/master
Browse files Browse the repository at this point in the history
Use related information for unclosed elements
  • Loading branch information
enxio authored Apr 30, 2021
2 parents 5815157 + 8aa1653 commit 311c1e6
Show file tree
Hide file tree
Showing 15 changed files with 513 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public DOMElement(int start, int end) {

/*
* (non-Javadoc)
*
*
* @see org.w3c.dom.Node#getNodeType()
*/
@Override
Expand All @@ -59,7 +59,7 @@ public short getNodeType() {

/*
* (non-Javadoc)
*
*
* @see org.w3c.dom.Node#getNodeName()
*/
@Override
Expand All @@ -69,7 +69,7 @@ public String getNodeName() {

/*
* (non-Javadoc)
*
*
* @see org.w3c.dom.Element#getTagName()
*/
@Override
Expand All @@ -80,7 +80,7 @@ public String getTagName() {
/**
* Returns true if the DOM element have a tag name and false otherwise (ex : '<'
* or '</').
*
*
* @return true if the DOM element have a tag name and false otherwise (ex : '<'
* or '</').
*/
Expand All @@ -90,7 +90,7 @@ public boolean hasTagName() {

/*
* (non-Javadoc)
*
*
* @see org.w3c.dom.Node#getLocalName()
*/
@Override
Expand All @@ -108,7 +108,7 @@ public String getLocalName() {

/*
* (non-Javadoc)
*
*
* @see org.w3c.dom.Node#getPrefix()
*/
@Override
Expand All @@ -127,7 +127,7 @@ public String getPrefix() {

/*
* (non-Javadoc)
*
*
* @see org.w3c.dom.Node#getNamespaceURI()
*/
@Override
Expand All @@ -139,9 +139,9 @@ public String getNamespaceURI() {

/**
* Returns the namespace URI for the given prefix and null otherwise.
*
*
* @param prefix the prefix.
*
*
* @return the namespace URI for the given prefix and null otherwise.
*/
public String getNamespaceURI(String prefix) {
Expand All @@ -167,7 +167,7 @@ public String getNamespaceURI(String prefix) {
/**
* 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
Expand All @@ -193,7 +193,7 @@ public Collection<String> getAllPrefixes() {

/**
* Returns the xmlns prefix from the given namespace URI and null otherwise.
*
*
* @param namespaceURI the namespace
* @return the xmlns prefix from the given namespace URI and null otherwise.
*/
Expand Down Expand Up @@ -236,9 +236,9 @@ public boolean isSelfClosed() {
* 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.
*/
Expand All @@ -265,7 +265,7 @@ public Integer endsWith(char c, int startOffset) {
/**
* Returns true if the given tag is the same tag of this element and false
* otherwise.
*
*
* @param tag tag element
* @return true if the given tag is the same tag of this element and false
* otherwise.
Expand Down Expand Up @@ -305,7 +305,7 @@ public boolean isInInsideStartEndTag(int offset) {
/**
* Returns the start tag open offset and {@link DOMNode#NULL_VALUE} if it
* doesn't exist.
*
*
* @return the start tag open offset and {@link DOMNode#NULL_VALUE} if it
* doesn't exist.
*/
Expand All @@ -316,7 +316,7 @@ public int getStartTagOpenOffset() {
/**
* Returns the start tag close offset and {@link DOMNode#NULL_VALUE} if it
* doesn't exist.
*
*
* @return the start tag close offset and {@link DOMNode#NULL_VALUE} if it
* doesn't exist.
*/
Expand All @@ -327,7 +327,7 @@ public int getStartTagCloseOffset() {
/**
* Returns the end tag open offset and {@link DOMNode#NULL_VALUE} if it doesn't
* exist.
*
*
* @return the end tag open offset and {@link DOMNode#NULL_VALUE} if it doesn't
* exist.
*/
Expand All @@ -338,7 +338,7 @@ public int getEndTagOpenOffset() {
/**
* Returns the end tag close offset and {@link DOMNode#NULL_VALUE} if it doesn't
* exist.
*
*
* @return the end tag close offset and {@link DOMNode#NULL_VALUE} if it doesn't
* exist.
*/
Expand All @@ -348,10 +348,10 @@ public int getEndTagCloseOffset() {

/**
* Returns true if has a start tag.
*
*
* In our source-oriented DOM, a lone end tag will cause a node to be created in
* the tree, unlike well-formed-only DOMs.
*
*
* @return true if has a start tag.
*/
public boolean hasStartTag() {
Expand All @@ -360,10 +360,10 @@ public boolean hasStartTag() {

/**
* Returns true if has an end tag.
*
*
* In our source-oriented DOM, sometimes Elements are "ended", even without an
* explicit end tag in the source.
*
*
* @return true if has an end tag.
*/
public boolean hasEndTag() {
Expand All @@ -387,7 +387,7 @@ public boolean isEndTagClosed() {
/**
* Returns true if the given element is an orphan end tag (which has no start
* tag, eg: </a>) and false otherwise.
*
*
* @return true if the given element is an orphan end tag (which has no start
* tag, eg: </a>) and false otherwise.
*/
Expand All @@ -398,7 +398,7 @@ public boolean isOrphanEndTag() {
/**
* Returns true if the given element is an orphan end tag (which has no start
* tag, eg: </a>) of the given tag name and false otherwise.
*
*
* @param tagName the end tag name.
* @return true if the given element is an orphan end tag (which has no start
* tag, eg: </a>) of the given tag name and false otherwise.
Expand All @@ -407,6 +407,25 @@ public boolean isOrphanEndTagOf(String tagName) {
return isSameTag(tagName) && isOrphanEndTag();
}

/**
* Returns the offset at which the given unclosed start tag should be closed with an angle bracket
*
* @returns the offset at which the given unclosed start tag should be closed with an angle bracket
*/
public int getUnclosedStartTagCloseOffset() {
String documentText = getOwnerDocument().getText();
int i = getStart() + 1;
for (; i < documentText.length() && documentText.charAt(i) != '/' && documentText.charAt(i) != '<'; i++) {
}
if (i < documentText.length() && documentText.charAt(i) == '/') {
return i + 1;
}
i--;
for (; i > 0 && Character.isWhitespace(documentText.charAt(i)); i--) {
}
return i + 1;
}

@Override
public DOMElement getOrphanEndElement(int offset, String tagName) {
if (getEnd() <= offset) {
Expand Down Expand Up @@ -436,7 +455,7 @@ public DOMElement getOrphanEndElement(int offset, String tagName) {
/**
* Returns true if element has a closing end tag (eg: <a> </a>) and false
* otherwise (eg: <a> </b>).
*
*
* @return true if element has a closing end tag (eg: <a> </a>) and false
* otherwise (eg: <a> </b>).
*/
Expand Down Expand Up @@ -521,7 +540,7 @@ public void setIdAttributeNode(org.w3c.dom.Attr arg0, boolean arg1) throws DOMEx

/**
* Returns true if the element is empty and false otherwise.
*
*
* @return true if the element is empty and false otherwise.
*/
public boolean isEmpty() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*******************************************************************************
* Copyright (c) 2021 Red Hat Inc. and others.
* All rights reserved. This program and the accompanying materials
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Red Hat Inc. - initial API and implementation
*******************************************************************************/
package org.eclipse.lemminx.extensions.contentmodel.participants;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.lemminx.dom.DOMDocument;
import org.eclipse.lsp4j.DiagnosticRelatedInformation;

/**
* Finds related info for any error code
*/
public class AggregateRelatedInfoFinder implements IRelatedInfoFinder {

private static IRelatedInfoFinder[] RELATED_INFO_FINDERS = {
new XMLSyntaxRelatedInfoFinder()
};

private static AggregateRelatedInfoFinder INSTANCE = null;

private AggregateRelatedInfoFinder() {}

public static AggregateRelatedInfoFinder getInstance() {
if (INSTANCE == null) {
INSTANCE = new AggregateRelatedInfoFinder();
}
return INSTANCE;
}

@Override
public List<DiagnosticRelatedInformation> findRelatedInformation(
int offset,
String errorKey,
DOMDocument document) {
List<DiagnosticRelatedInformation> relatedInfo = new ArrayList<>();
for (IRelatedInfoFinder relatedInfoFinder : RELATED_INFO_FINDERS) {
relatedInfo.addAll(relatedInfoFinder.findRelatedInformation(
offset,
errorKey,
document
));
}
return relatedInfo;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*******************************************************************************
* Copyright (c) 2021 Red Hat Inc. and others.
* All rights reserved. This program and the accompanying materials
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Red Hat Inc. - initial API and implementation
*******************************************************************************/
package org.eclipse.lemminx.extensions.contentmodel.participants;

import java.util.List;

import org.eclipse.lemminx.dom.DOMDocument;
import org.eclipse.lsp4j.DiagnosticRelatedInformation;

/**
* Provides an interface to find related info for a given error
*/
public interface IRelatedInfoFinder {

/**
* Returns a list of related information
*
* @param offset The LemMinX reported error start offset
* @param errorKey The error key
* @param document The document
* @return a list of related information
*/
List<DiagnosticRelatedInformation> findRelatedInformation(
int offset,
String errorKey,
DOMDocument document);

}
Loading

0 comments on commit 311c1e6

Please sign in to comment.