Skip to content

Commit

Permalink
Add a first POC for bnd autocompletion on pom files
Browse files Browse the repository at this point in the history
Signed-off-by: Christoph Läubrich <[email protected]>
  • Loading branch information
laeubi committed Oct 27, 2024
1 parent b9662aa commit 5ba1378
Show file tree
Hide file tree
Showing 8 changed files with 248 additions and 0 deletions.
41 changes: 41 additions & 0 deletions org.eclipse.m2e.lemminx.bnd/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="test" value="true"/>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="test" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17">
<attributes>
<attribute name="module" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
23 changes: 23 additions & 0 deletions org.eclipse.m2e.lemminx.bnd/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>org.eclipse.m2e.lemminx.bnd</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=17
org.eclipse.jdt.core.compiler.compliance=17
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
org.eclipse.jdt.core.compiler.release=enabled
org.eclipse.jdt.core.compiler.source=17
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
47 changes: 47 additions & 0 deletions org.eclipse.m2e.lemminx.bnd/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- <parent>-->
<!-- <groupId>org.eclipse.m2e</groupId>-->
<!-- <artifactId>m2e-core</artifactId>-->
<!-- <version>2.1.0-SNAPSHOT</version>-->
<!-- </parent>-->
<groupId>org.eclipse.m2e.lemminx.bnd</groupId>
<artifactId>org.eclipse.m2e.lemminx.bnd</artifactId>
<version>2.1.0-SNAPSHOT</version>
<name>M2E bnd lemminx extension</name>

<properties>
<java.version>17</java.version>
<maven.compiler.release>${java.version}</maven.compiler.release>
</properties>


<dependencies>
<dependency>
<groupId>org.eclipse.lemminx</groupId>
<artifactId>org.eclipse.lemminx</artifactId>
<version>0.28.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>biz.aQute.bnd</groupId>
<artifactId>biz.aQute.bndlib</artifactId>
<version>7.0.0</version>
</dependency>
</dependencies>

<repositories>
<repository>
<id>lemminx-releases</id>
<url>https://repo.eclipse.org/content/repositories/lemminx-releases/</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</repository>
</repositories>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*******************************************************************************
* Copyright (c) 2024 Christoph Läubrich
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Christoph Läubrich - initial API and implementation
*******************************************************************************/
package org.eclipse.m2e.lemminx.bnd;

import java.util.logging.Level;
import java.util.logging.Logger;

import org.eclipse.lemminx.dom.DOMDocument;
import org.eclipse.lemminx.dom.DOMNode;
import org.eclipse.lemminx.services.extensions.IXMLExtension;
import org.eclipse.lemminx.services.extensions.XMLExtensionsRegistry;
import org.eclipse.lemminx.services.extensions.completion.ICompletionParticipant;
import org.eclipse.lemminx.services.extensions.completion.ICompletionRequest;
import org.eclipse.lemminx.services.extensions.completion.ICompletionResponse;
import org.eclipse.lemminx.services.extensions.save.ISaveContext;
import org.eclipse.lsp4j.CompletionItem;
import org.eclipse.lsp4j.InitializeParams;
import org.eclipse.lsp4j.jsonrpc.CancelChecker;

import aQute.bnd.help.Syntax;

public class BndLemminxPlugin implements IXMLExtension {

// TODO LemminxClasspathExtensionProvider that puts our jar on the classpath +
// bnd dependencies!

@Override
public void start(InitializeParams params, XMLExtensionsRegistry registry) {
Logger logger = Logger.getLogger("bnd");
logger.log(Level.INFO, "Hello From BND Extension");
registry.registerCompletionParticipant(new ICompletionParticipant() {

@Override
public void onAttributeName(boolean arg0, ICompletionRequest arg1, ICompletionResponse arg2,
CancelChecker arg3) throws Exception {
}

@Override
public void onAttributeValue(String arg0, ICompletionRequest arg1, ICompletionResponse arg2,
CancelChecker arg3) throws Exception {
}

@Override
public void onDTDSystemId(String arg0, ICompletionRequest arg1, ICompletionResponse arg2,
CancelChecker arg3) throws Exception {
}

@Override
public void onTagOpen(ICompletionRequest arg0, ICompletionResponse arg1, CancelChecker arg2)
throws Exception {
}

@Override
public void onXMLContent(ICompletionRequest completionRequest, ICompletionResponse response,
CancelChecker checker) throws Exception {
try {
// FIXME CDATA do not trigger completion:
// https://github.com/eclipse/lemminx/issues/1694
DOMDocument xmlDocument = completionRequest.getXMLDocument();
DOMNode node = xmlDocument.findNodeBefore(completionRequest.getOffset());
if (isBndNode(node)) {
// FIXME get the text to give better completion proposals, see:
// https://github.com/eclipse/lemminx/issues/1695
// if (node != null && node.getNodeName().equals("bnd")) {
// logger.log(Level.INFO, "text content=" + node.getTextContent());
// String substring = xmlDocument.getText().substring(node.getStart(), node.getEnd());
// logger.log(Level.INFO, "substring=" + substring);
// } else {
// logger.log(Level.INFO,
// "node=" + node + ", start=" + node.getStart() + ", end=" + node.getEnd()
// + " --> text content=" + node.getTextContent());
//
// // Syntax.HELP.values().stream().map(syntax -> {
// }
Syntax.HELP.values().stream().forEach(syntax -> {
CompletionItem item = new CompletionItem();
item.setLabel(syntax.getHeader());
item.setInsertText(syntax.getHeader() + ": ");
response.addCompletionItem(item);
});
}
} catch (Exception e) {
logger.log(Level.INFO, "err=" + e);
}
}

private boolean isBndNode(DOMNode node) {
if (node != null) {
if (node.getNodeName().equals("bnd")) {
return true;
}
return isBndNode(node.getParentNode());
}
return false;
}

});
}

@Override
public void stop(XMLExtensionsRegistry registry) {
// TODO Auto-generated method stub

}

@Override
public void doSave(ISaveContext context) {
// TODO Auto-generated method stub
IXMLExtension.super.doSave(context);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.eclipse.m2e.lemminx.bnd.BndLemminxPlugin
2 changes: 2 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
<module>org.eclipse.m2e.apt.core</module>
<module>org.eclipse.m2e.apt.ui</module>
<module>org.eclipse.m2e.mavenarchiver</module>
<module>org.eclipse.m2e.bnd.ui</module>
<module>org.eclipse.m2e.lemminx.bnd</module>

<!-- common test helpers -->
<module>org.eclipse.m2e.tests.common</module>
Expand Down

0 comments on commit 5ba1378

Please sign in to comment.