Skip to content

Commit

Permalink
Fix surround tag in empty document
Browse files Browse the repository at this point in the history
- Fill in grammar when invoking surround tag in an empty document
- Fix NPE that prevented surround tags from working
  in empty documents that are linked to a grammar with
  file association

Closes eclipse#1395

Signed-off-by: David Thompson <[email protected]>
  • Loading branch information
datho7561 committed Dec 8, 2022
1 parent 743ec19 commit 21ca9c5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

/**
* XML Command "xml.refactor.surround.with" to support surround:
*
*
* <ul>
* <li>Surround with Tags (Wrap)</li>
* <li>Surround with Comments</li>
Expand Down Expand Up @@ -180,6 +180,14 @@ private List<String> getTags(DOMNode node, String prefix, int offset) {
DOMElement parentElement = node.isElement() ? (DOMElement) node : node.getParentElement();
Collection<CMDocument> cmDocuments = parentElement != null ? contentModelManager.findCMDocument(parentElement)
: contentModelManager.findCMDocument(node.getOwnerDocument(), null);
if (parentElement == null) {
return cmDocuments.stream() //
.flatMap(cmDocument -> cmDocument.getElements().stream()) //
.map(decl -> decl.getName(prefix)) //
.distinct() //
.sorted() //
.collect(Collectors.toList());
}
for (CMDocument cmDocument : cmDocuments) {
CMElementDeclaration elementDeclaration = cmDocument.findCMElement(parentElement);
if (elementDeclaration != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1811,11 +1811,18 @@ public static SelectionRange sr(List<Range> ranges) {
selectionRange.setParent(sr(ranges.subList(1, ranges.size())));
return selectionRange;
}

public static void assertSurroundWith(String xml, SurroundWithKind kind, boolean snippetsSupported,
String expected) throws BadLocationException, InterruptedException, ExecutionException {
assertSurroundWith(xml, kind, snippetsSupported, (service) -> {}, "src/test/resources/test.xml", expected);
}

public static void assertSurroundWith(String xml, SurroundWithKind kind, boolean snippetsSupported, Consumer<XMLLanguageService> configuration, String uri,
String expected) throws BadLocationException, InterruptedException, ExecutionException {
MockXMLLanguageServer languageServer = new MockXMLLanguageServer();

configuration.accept(languageServer.getXMLLanguageService());

int rangeStart = xml.indexOf('|');
int rangeEnd = xml.lastIndexOf('|');
// remove '|'
Expand All @@ -1830,7 +1837,7 @@ public static void assertSurroundWith(String xml, SurroundWithKind kind, boolean
Position endPos = rangeStart == rangeEnd ? startPos : document.positionAt(rangeEnd - 1);
Range selection = new Range(startPos, endPos);

TextDocumentIdentifier xmlIdentifier = languageServer.didOpen("src/test/resources/test.xml", x.toString());
TextDocumentIdentifier xmlIdentifier = languageServer.didOpen(uri, x.toString());

// Execute surround with tags command
SurroundWithResponse response = (SurroundWithResponse) languageServer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import org.eclipse.lemminx.extensions.contentmodel.BaseFileTempTest;
import org.eclipse.lemminx.extensions.contentmodel.commands.SurroundWithCommand.SurroundWithKind;
import org.eclipse.lemminx.extensions.contentmodel.settings.XMLFileAssociation;
import org.junit.jupiter.api.Test;

/**
Expand Down Expand Up @@ -145,4 +146,10 @@ public void surroundEmptySelectionInEmptyText() throws Exception {
assertSurroundWith(xml, SurroundWithKind.tags, true, expected);
}

private static XMLFileAssociation[] createXSDAssociationsNoNamespaceSchemaLocationLike(String baseSystemId) {
XMLFileAssociation resources = new XMLFileAssociation();
resources.setPattern("**/*resources*.xml");
resources.setSystemId(baseSystemId + "resources.xsd");
return new XMLFileAssociation[] { resources };
}
}

0 comments on commit 21ca9c5

Please sign in to comment.