Skip to content

Commit

Permalink
Autocompletion for directory/file based properties #292
Browse files Browse the repository at this point in the history
Auto completioin is added for mojo parameters (with type=File or name ending with Directory).
JUnit test cases are added/updated for the issue.

Signed-off-by: Victor Rubezhny <vrubezhny@redhat.com>
  • Loading branch information
vrubezhny authored and mickaelistria committed Aug 22, 2022
1 parent 0ebfd54 commit 3a82594
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ public class MavenCompletionParticipant extends CompletionParticipantAdapter {
private static final Logger LOGGER = Logger.getLogger(MavenCompletionParticipant.class.getName());
private static final Pattern ARTIFACT_ID_PATTERN = Pattern.compile("[-.a-zA-Z0-9]+");

private static final String FILE_TYPE = "File";
private static final String STRING_TYPE = "File";
private static final String DIRECTORY_STRING_LC = "directory";

static interface GAVInsertionStrategy {
/**
* set current element value and add siblings as addition textEdits
Expand Down Expand Up @@ -357,7 +361,25 @@ public void onXMLContent(ICompletionRequest request, ICompletionResponse respons
case GOAL_ELT:
collectGoals(request).forEach(response::addCompletionItem);
break;
}
default:
Set<MojoParameter> parameters = MavenPluginUtils.collectPluginConfigurationMojoParameters(request, plugin)
.stream().filter(p -> p.name.equals(parent.getLocalName()))
.filter(p -> (p.type.startsWith(FILE_TYPE)) ||
(p.type.startsWith(STRING_TYPE) && p.name.toLowerCase().endsWith(DIRECTORY_STRING_LC)))
.collect(Collectors.toSet());
if (parameters != null && parameters.size() > 0) {
collectMojoParametersDefaultCompletion(request, parameters)
.forEach(response::addCompletionItem);
if (parameters.stream()
.anyMatch(p -> !p.name.toLowerCase().endsWith(DIRECTORY_STRING_LC))) {
// Show all files
collectRelativeAnyPathCompletion(request).forEach(response::addCompletionItem);
} else {
// Show only directories
collectRelativeDirectoryPathCompletion(request).forEach(response::addCompletionItem);
}
}
}
if (!allArtifactInfos.isEmpty()) {
Comparator<ArtifactWithDescription> artifactInfoComparator = Comparator
.comparing(artifact -> new DefaultArtifactVersion(artifact.artifact.getVersion()));
Expand All @@ -376,7 +398,7 @@ public void onXMLContent(ICompletionRequest request, ICompletionResponse respons
completeProperties(request).forEach(response::addCompletionAttribute);
}
}

private CompletionItem toTextCompletionItem(ICompletionRequest request, String text) throws BadLocationException {
CompletionItem res = new CompletionItem(text);
res.setFilterText(text);
Expand Down Expand Up @@ -731,6 +753,22 @@ private Collection<CompletionItem> collectSubModuleCompletion(ICompletionRequest
return files.stream().map(file -> toFileCompletionItem(file, docFolder, request)).collect(Collectors.toList());
}

private Collection<CompletionItem> collectMojoParametersDefaultCompletion(ICompletionRequest request, Set<MojoParameter> parameters) {
String prefix = request.getNode().getNodeValue() != null ? request.getNode().getNodeValue() : "";
List<String> defaultValues = parameters.stream().filter(p -> (p.getDefaultValue() != null))
.map(p -> p.getDefaultValue())
.collect(Collectors.toList());

if (!prefix.isEmpty()) {
defaultValues = defaultValues.stream().filter(v -> v.startsWith(prefix))
.collect(Collectors.toList());
}
return defaultValues.stream()
.sorted(String.CASE_INSENSITIVE_ORDER)
.map(defaultValue -> toCompletionItem(defaultValue.toString(), null, request.getReplaceRange()))
.collect(Collectors.toList());
}

private Collection<CompletionItem> collectRelativePathCompletion(ICompletionRequest request) {
DOMDocument doc = request.getXMLDocument();
File docFile = new File(URI.create(doc.getTextDocument().getUri()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void testWebResourcesDirectory() throws Exception {
@Test
public void testWebResourcesTargetPath() throws Exception {
DOMDocument document = createDOMDocument("/local-path/pom-webresource.xml", languageService);
List<CompletionItem> doComplete = languageService.doComplete(document, new Position(0, 11), new SharedSettings()).getItems();
List<CompletionItem> doComplete = languageService.doComplete(document, new Position(9, 12), new SharedSettings()).getItems();

// parent folder, then direct children
assertEquals("..", doComplete.get(0).getLabel());
Expand Down Expand Up @@ -156,14 +156,15 @@ public void testWebResourcesFileExists() throws Exception {

// files
assertEquals("filter.properties", doComplete.get(0).getLabel());
assertEquals("pom-webresource.xml", doComplete.get(1).getLabel());
assertEquals("pom.xml", doComplete.get(2).getLabel());
assertEquals("pom-pluginManagement-configuration.xml", doComplete.get(1).getLabel());
assertEquals("pom-webresource.xml", doComplete.get(2).getLabel());
assertEquals("pom.xml", doComplete.get(3).getLabel());

// parent folder, then direct children
assertEquals("..", doComplete.get(3).getLabel());
assertEquals("child-parent", doComplete.get(4).getLabel());
assertEquals("folder1", doComplete.get(5).getLabel());
assertEquals("folder2", doComplete.get(6).getLabel());
assertEquals("..", doComplete.get(4).getLabel());
assertEquals("child-parent", doComplete.get(5).getLabel());
assertEquals("folder1", doComplete.get(6).getLabel());
assertEquals("folder2", doComplete.get(7).getLabel());
}

// public static final String FILE_ELT = "file";
Expand All @@ -175,14 +176,48 @@ public void testWebResourcesFileMissing() throws Exception {

// files
assertEquals("filter.properties", doComplete.get(0).getLabel());
assertEquals("pom-webresource.xml", doComplete.get(1).getLabel());
assertEquals("pom.xml", doComplete.get(2).getLabel());
assertEquals("pom-pluginManagement-configuration.xml", doComplete.get(1).getLabel());
assertEquals("pom-webresource.xml", doComplete.get(2).getLabel());
assertEquals("pom.xml", doComplete.get(3).getLabel());

// parent folder, then direct children
assertEquals("..", doComplete.get(4).getLabel());
assertEquals("child-parent", doComplete.get(5).getLabel());
assertEquals("folder1", doComplete.get(6).getLabel());
assertEquals("folder2", doComplete.get(7).getLabel());
}

@Test
public void testMojoParametersDirectories() throws Exception {
DOMDocument document = createDOMDocument("/local-path/pom-pluginManagement-configuration.xml", languageService);
List<CompletionItem> doComplete = languageService.doComplete(document, new Position(18, 42), new SharedSettings()).getItems();

// Some typical conventions for directories
// Default values
assertEquals("${project.build.outputDirectory}", doComplete.get(0).getLabel());
// parent
assertEquals("..", doComplete.get(1).getLabel());
// then child folders
assertEquals("child-parent", doComplete.get(2).getLabel());
assertEquals("folder1", doComplete.get(3).getLabel());
assertEquals("folder2", doComplete.get(4).getLabel());
}

@Test
public void testMojoParametersFiles() throws Exception {
DOMDocument document = createDOMDocument("/local-path/pom-pluginManagement-configuration.xml", languageService);
List<CompletionItem> doComplete = languageService.doComplete(document, new Position(19, 39), new SharedSettings()).getItems();

// Some typical conventions
assertEquals("filter.properties", doComplete.get(0).getLabel());
assertEquals("pom-pluginManagement-configuration.xml", doComplete.get(1).getLabel());
assertEquals("pom-webresource.xml", doComplete.get(2).getLabel());
assertEquals("pom.xml", doComplete.get(3).getLabel());
// parent folder, then direct children
assertEquals("..", doComplete.get(3).getLabel());
assertEquals("child-parent", doComplete.get(4).getLabel());
assertEquals("folder1", doComplete.get(5).getLabel());
assertEquals("folder2", doComplete.get(6).getLabel());
assertEquals("..", doComplete.get(4).getLabel());
assertEquals("child-parent", doComplete.get(5).getLabel());
assertEquals("folder1", doComplete.get(6).getLabel());
assertEquals("folder2", doComplete.get(7).getLabel());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>

<groupId>pluginManagement</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<classesDirectory></classesDirectory>
<suiteXmlFiles></suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
<testOutputDirectory></testOutputDirectory>
<filters><filter></filter></filters>
<file><exists></exists></file>
<file><missing></missing></file>
<file><missing></missing></file>
<targetPath></targetPath>

0 comments on commit 3a82594

Please sign in to comment.