Skip to content

Commit

Permalink
Add completions support inside CDATA content
Browse files Browse the repository at this point in the history
  • Loading branch information
pablocabrera85 committed Feb 8, 2024
1 parent ef5d223 commit 6298052
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 2 deletions.
8 changes: 7 additions & 1 deletion org.eclipse.lemminx/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>org.eclipse.lemminx</groupId>
<artifactId>lemminx-parent</artifactId>
<version>0.27.1-SNAPSHOT</version>
<version>0.28.0-SNAPSHOT</version>
</parent>
<artifactId>org.eclipse.lemminx</artifactId>
<properties>
Expand Down Expand Up @@ -287,5 +287,11 @@
<version>9.4.53.v20231009</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.10.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,14 @@ public CompletionList doComplete(DOMDocument xmlDocument, Position position, Sha
return completionResponse;
}
break;
// CDATA
case CDATATagOpen:
case CDATAContent:
if (scanner.getTokenOffset() <= offset && offset <= scanner.getTokenEnd()) {
collectCDATAContentSuggestions(completionRequest, completionResponse, cancelChecker);
return completionResponse;
}
break;
// DTD
case DTDAttlistAttributeName:
case DTDAttlistAttributeType:
Expand Down Expand Up @@ -869,6 +877,21 @@ private void collectionRegionProposals(ICompletionRequest request, ICompletionRe
}
}

private void collectCDATAContentSuggestions(CompletionRequest request, CompletionResponse response,
CancelChecker cancelChecker){
// Participant completion on CDATA content
for (ICompletionParticipant participant : getCompletionParticipants()) {
try {
participant.onCDATAContent(request, response, cancelChecker);
} catch (CancellationException e) {
throw e;
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "While performing ICompletionParticipant#onCDATAContent for participant '"
+ participant.getClass().getName() + "'.", e);
}
}
}

private void collectAttributeNameSuggestions(int nameStart, CompletionRequest completionRequest,
CompletionResponse completionResponse, CancelChecker cancelChecker) {
collectAttributeNameSuggestions(nameStart, completionRequest.getOffset(), completionRequest, completionResponse,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@ void onDTDSystemId(String valuePrefix, ICompletionRequest request, ICompletionRe
CancelChecker cancelChecker)
throws Exception;

/**
* Collects and stores completion items within the provided response <code>response</code>
*
* @param request the completion request
* @param response the completion response
* @param cancelChecker object used to monitor if this execution should finish
* @throws Exception
*/
default void onCDATAContent(ICompletionRequest request, ICompletionResponse response, CancelChecker cancelChecker)
throws Exception {}

/**
* Returns the completion item resolver that corresponds to the given
* participant id or null otherwise.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.Arrays;
import java.util.List;
Expand All @@ -30,6 +34,7 @@
import org.eclipse.lemminx.customservice.AutoCloseTagResponse;
import org.eclipse.lemminx.dom.DOMDocument;
import org.eclipse.lemminx.dom.DOMParser;
import org.eclipse.lemminx.services.extensions.completion.ICompletionParticipant;
import org.eclipse.lemminx.settings.SharedSettings;
import org.eclipse.lsp4j.CompletionItem;
import org.eclipse.lsp4j.CompletionList;
Expand Down Expand Up @@ -267,7 +272,23 @@ public void testNoEndTagInAttributeValue() throws BadLocationException {
String xml = "<aaa attr=\"value</|";
XMLAssert.testTagCompletion(xml, (AutoCloseTagResponse) null, settings);
}
@Test
public void testCDATAContentStart() throws Exception {
ICompletionParticipant mockParticipant = mock(ICompletionParticipant.class);
SharedSettings settings = new SharedSettings();
this.languageService.registerCompletionParticipant(mockParticipant);
testCompletionFor(this.languageService,"<a> <![CDATA[|]]></a>",null,null,null, 0,settings);
verify(mockParticipant).onCDATAContent(any(),any(),any());
}

@Test
public void testCDATAContent() throws Exception {
ICompletionParticipant mockParticipant = mock(ICompletionParticipant.class);
SharedSettings settings = new SharedSettings();
this.languageService.registerCompletionParticipant(mockParticipant);
testCompletionFor(this.languageService,"<a> <![CDATA[SELECT * FROM |]]></a>",null,null,null, 0,settings);
verify(mockParticipant).onCDATAContent(any(),any(),any());
}
// -------------------Tools----------------------------------------------------------

public void assertOpenStartTagCompletion(String xmlText, int expectedStartTagOffset, boolean startWithTagOpen,
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.eclipse.lemminx</groupId>
<artifactId>lemminx-parent</artifactId>
<version>0.27.1-SNAPSHOT</version>
<version>0.28.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Eclipse LemMinX</name>
<description>LemMinX is a XML Language Server Protocol (LSP), and can be used with any editor that supports LSP, to offer an outstanding XML editing experience</description>
Expand Down

0 comments on commit 6298052

Please sign in to comment.