1+ /*******************************************************************************
2+ * Copyright (c) 2024 Christoph Läubrich
3+ * All rights reserved. This program and the accompanying materials
4+ * are made available under the terms of the Eclipse Public License 2.0
5+ * which accompanies this distribution, and is available at
6+ * https://www.eclipse.org/legal/epl-2.0/
7+ *
8+ * SPDX-License-Identifier: EPL-2.0
9+ *
10+ * Contributors:
11+ * Christoph Läubrich - initial API and implementation
12+ *******************************************************************************/
13+ package org .eclipse .m2e .lemminx .bnd ;
14+
15+ import java .util .logging .Level ;
16+ import java .util .logging .Logger ;
17+
18+ import org .eclipse .lemminx .dom .DOMDocument ;
19+ import org .eclipse .lemminx .dom .DOMNode ;
20+ import org .eclipse .lemminx .services .extensions .IXMLExtension ;
21+ import org .eclipse .lemminx .services .extensions .XMLExtensionsRegistry ;
22+ import org .eclipse .lemminx .services .extensions .completion .ICompletionParticipant ;
23+ import org .eclipse .lemminx .services .extensions .completion .ICompletionRequest ;
24+ import org .eclipse .lemminx .services .extensions .completion .ICompletionResponse ;
25+ import org .eclipse .lemminx .services .extensions .save .ISaveContext ;
26+ import org .eclipse .lsp4j .CompletionItem ;
27+ import org .eclipse .lsp4j .InitializeParams ;
28+ import org .eclipse .lsp4j .jsonrpc .CancelChecker ;
29+
30+ import aQute .bnd .help .Syntax ;
31+
32+ public class BndLemminxPlugin implements IXMLExtension {
33+
34+ // TODO LemminxClasspathExtensionProvider that puts our jar on the classpath +
35+ // bnd dependencies!
36+
37+ @ Override
38+ public void start (InitializeParams params , XMLExtensionsRegistry registry ) {
39+ Logger logger = Logger .getLogger ("bnd" );
40+ logger .log (Level .INFO , "Hello From BND Extension" );
41+ registry .registerCompletionParticipant (new ICompletionParticipant () {
42+
43+ @ Override
44+ public void onAttributeName (boolean arg0 , ICompletionRequest arg1 , ICompletionResponse arg2 ,
45+ CancelChecker arg3 ) throws Exception {
46+ }
47+
48+ @ Override
49+ public void onAttributeValue (String arg0 , ICompletionRequest arg1 , ICompletionResponse arg2 ,
50+ CancelChecker arg3 ) throws Exception {
51+ }
52+
53+ @ Override
54+ public void onDTDSystemId (String arg0 , ICompletionRequest arg1 , ICompletionResponse arg2 ,
55+ CancelChecker arg3 ) throws Exception {
56+ }
57+
58+ @ Override
59+ public void onTagOpen (ICompletionRequest arg0 , ICompletionResponse arg1 , CancelChecker arg2 )
60+ throws Exception {
61+ }
62+
63+ @ Override
64+ public void onXMLContent (ICompletionRequest completionRequest , ICompletionResponse response ,
65+ CancelChecker checker ) throws Exception {
66+ try {
67+ // FIXME CDATA do not trigger completion:
68+ // https://github.com/eclipse/lemminx/issues/1694
69+ DOMDocument xmlDocument = completionRequest .getXMLDocument ();
70+ DOMNode node = xmlDocument .findNodeBefore (completionRequest .getOffset ());
71+ if (isBndNode (node )) {
72+ // FIXME get the text to give better completion proposals, see:
73+ // https://github.com/eclipse/lemminx/issues/1695
74+ // if (node != null && node.getNodeName().equals("bnd")) {
75+ // logger.log(Level.INFO, "text content=" + node.getTextContent());
76+ // String substring = xmlDocument.getText().substring(node.getStart(), node.getEnd());
77+ // logger.log(Level.INFO, "substring=" + substring);
78+ // } else {
79+ // logger.log(Level.INFO,
80+ // "node=" + node + ", start=" + node.getStart() + ", end=" + node.getEnd()
81+ // + " --> text content=" + node.getTextContent());
82+ //
83+ // // Syntax.HELP.values().stream().map(syntax -> {
84+ // }
85+ Syntax .HELP .values ().stream ().forEach (syntax -> {
86+ CompletionItem item = new CompletionItem ();
87+ item .setLabel (syntax .getHeader ());
88+ item .setInsertText (syntax .getHeader () + ": " );
89+ response .addCompletionItem (item );
90+ });
91+ }
92+ } catch (Exception e ) {
93+ logger .log (Level .INFO , "err=" + e );
94+ }
95+ }
96+
97+ private boolean isBndNode (DOMNode node ) {
98+ if (node != null ) {
99+ if (node .getNodeName ().equals ("bnd" )) {
100+ return true ;
101+ }
102+ return isBndNode (node .getParentNode ());
103+ }
104+ return false ;
105+ }
106+
107+ });
108+ }
109+
110+ @ Override
111+ public void stop (XMLExtensionsRegistry registry ) {
112+ // TODO Auto-generated method stub
113+
114+ }
115+
116+ @ Override
117+ public void doSave (ISaveContext context ) {
118+ // TODO Auto-generated method stub
119+ IXMLExtension .super .doSave (context );
120+ }
121+
122+ }
0 commit comments