forked from eclipse-lemminx/lemminx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use related information for unclosed elements
Revert MarkupEntityMismatch and ETagRequired error ranges so they only cover the start tag element name. Keep existing CodeAction behaviour intact. If the client supports DiagnosticRelatedInformation, add the expected location of the close tag as related info to MarkupEntityMismatch and ETagRequired errors. Closes eclipse-lemminx#963
- Loading branch information
Showing
15 changed files
with
510 additions
and
97 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
.../org/eclipse/lemminx/extensions/contentmodel/participants/AggregateRelatedInfoFinder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
...ain/java/org/eclipse/lemminx/extensions/contentmodel/participants/IRelatedInfoFinder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
.../main/java/org/eclipse/lemminx/extensions/contentmodel/participants/SyntaxErrorUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/******************************************************************************* | ||
* 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 org.eclipse.lemminx.dom.DOMDocument; | ||
import org.eclipse.lemminx.dom.DOMElement; | ||
|
||
/** | ||
* Reusable utility functions for resolving syntax errors | ||
*/ | ||
public class SyntaxErrorUtils { | ||
|
||
/** | ||
* Returns the offset at which the given unclosed start tag should be closed with an angle bracket | ||
* | ||
* @param document The xml document with the unclosed start tag | ||
* @param element The element that has a start tag that's missing a closing angle bracket | ||
* @return the offset at which the given unclosed start tag should be closed with an angle bracket | ||
*/ | ||
public static int getUnclosedStartTagCloseLocation(DOMDocument document, DOMElement element) { | ||
String documentText = document.getText(); | ||
int i = element.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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
.../org/eclipse/lemminx/extensions/contentmodel/participants/XMLSyntaxRelatedInfoFinder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/******************************************************************************* | ||
* 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.Collections; | ||
import java.util.List; | ||
|
||
import org.eclipse.lemminx.dom.DOMDocument; | ||
import org.eclipse.lemminx.dom.DOMElement; | ||
import org.eclipse.lemminx.dom.DOMNode; | ||
import org.eclipse.lemminx.utils.XMLPositionUtility; | ||
import org.eclipse.lsp4j.DiagnosticRelatedInformation; | ||
import org.eclipse.lsp4j.Location; | ||
import org.eclipse.lsp4j.Range; | ||
|
||
/** | ||
* Find related information for an XML syntax error | ||
*/ | ||
public class XMLSyntaxRelatedInfoFinder implements IRelatedInfoFinder { | ||
|
||
private static String CLOSING_TAG_EXPECTED_HERE = "Closing tag expected here"; | ||
|
||
@Override | ||
public List<DiagnosticRelatedInformation> findRelatedInformation(int offset, String errorKey, | ||
DOMDocument document) { | ||
|
||
XMLSyntaxErrorCode syntaxCode = XMLSyntaxErrorCode.get(errorKey); | ||
|
||
if (syntaxCode == null) { | ||
return Collections.emptyList(); | ||
} | ||
|
||
switch (syntaxCode) { | ||
case ETagRequired: | ||
case MarkupEntityMismatch: { | ||
DOMNode node = document.findNodeAt(offset); | ||
while (node != null && !node.isElement()) { | ||
node = node.getParentNode(); | ||
} | ||
if (node == null) { | ||
return Collections.emptyList(); | ||
} | ||
|
||
if (node == document.getDocumentElement() && ((DOMElement) node).hasEndTag()){ | ||
return Collections.emptyList(); | ||
} | ||
|
||
int closeTagOffset; | ||
int numChildren = node.getChildren().size(); | ||
if (numChildren == 0) { | ||
closeTagOffset = node.getEnd(); | ||
} else { | ||
closeTagOffset = node.getChildren().get(numChildren - 1).getEnd(); | ||
} | ||
|
||
Range range = XMLPositionUtility.createRange(closeTagOffset, closeTagOffset, document); | ||
return Collections.singletonList(new DiagnosticRelatedInformation( | ||
new Location(document.getDocumentURI(), range), CLOSING_TAG_EXPECTED_HERE)); | ||
} | ||
default: | ||
} | ||
return Collections.emptyList(); | ||
} | ||
|
||
} |
Oops, something went wrong.