From 9bc30aa530b639360e125fb3cf9e8fd4406ecef0 Mon Sep 17 00:00:00 2001 From: Soeren Domroes Date: Mon, 22 Apr 2024 17:36:20 -0700 Subject: [PATCH 01/15] Added lsp message on the server --- .../diagram/lsp/LFLanguageServerExtension.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lsp/src/main/java/org/lflang/diagram/lsp/LFLanguageServerExtension.java b/lsp/src/main/java/org/lflang/diagram/lsp/LFLanguageServerExtension.java index 18de51f482..b7a5caa8f6 100644 --- a/lsp/src/main/java/org/lflang/diagram/lsp/LFLanguageServerExtension.java +++ b/lsp/src/main/java/org/lflang/diagram/lsp/LFLanguageServerExtension.java @@ -1,7 +1,9 @@ package org.lflang.diagram.lsp; import java.util.ArrayList; +import java.util.List; import java.util.concurrent.CompletableFuture; + import org.eclipse.emf.common.util.URI; import org.eclipse.lsp4j.jsonrpc.services.JsonNotification; import org.eclipse.lsp4j.jsonrpc.services.JsonRequest; @@ -60,6 +62,18 @@ public CompletableFuture build(String uri) { } }); } + + @JsonRequest("generator/getLibraryReactors") + public CompletableFuture> getLibraryReactors(String uri) { + return CompletableFuture.supplyAsync( + () -> { + try { + return List.of("test2", "test3"); + } catch (Exception e) { + return null; + } + }); + } /** * Handles a request for the most complete build of the specified Lingua Franca file that can be From 738e7889cda4c325d7355fbba11d6940148c8111 Mon Sep 17 00:00:00 2001 From: Vincenzo Barbuto Date: Tue, 23 Apr 2024 11:41:01 -0700 Subject: [PATCH 02/15] Set-up notification handler from server to client --- .../org/lflang/diagram/lsp/LFLanguageClient.java | 13 +++++++++++++ .../diagram/lsp/LFLanguageServerExtension.java | 6 ++++-- .../lflang/diagram/lsp/LanguageDiagramServer.java | 6 +++--- 3 files changed, 20 insertions(+), 5 deletions(-) create mode 100644 lsp/src/main/java/org/lflang/diagram/lsp/LFLanguageClient.java diff --git a/lsp/src/main/java/org/lflang/diagram/lsp/LFLanguageClient.java b/lsp/src/main/java/org/lflang/diagram/lsp/LFLanguageClient.java new file mode 100644 index 0000000000..73bb42a54b --- /dev/null +++ b/lsp/src/main/java/org/lflang/diagram/lsp/LFLanguageClient.java @@ -0,0 +1,13 @@ +package org.lflang.diagram.lsp; + +import org.eclipse.lsp4j.jsonrpc.services.JsonNotification; +import org.eclipse.lsp4j.services.LanguageClient; + +import de.cau.cs.kieler.klighd.lsp.KGraphLanguageClient; + +public interface LFLanguageClient extends KGraphLanguageClient, LanguageClient { + + @JsonNotification("update/example") + public void test(String updateMessage); + +} diff --git a/lsp/src/main/java/org/lflang/diagram/lsp/LFLanguageServerExtension.java b/lsp/src/main/java/org/lflang/diagram/lsp/LFLanguageServerExtension.java index b7a5caa8f6..c4cba8c2e3 100644 --- a/lsp/src/main/java/org/lflang/diagram/lsp/LFLanguageServerExtension.java +++ b/lsp/src/main/java/org/lflang/diagram/lsp/LFLanguageServerExtension.java @@ -31,14 +31,14 @@ class LFLanguageServerExtension implements ILanguageServerExtension { .getInstance(IntegratedBuilder.class); /** The access point for reading documents, communicating with the language client, etc. */ - private LanguageClient client; + private LFLanguageClient client; @Override public void initialize(ILanguageServerAccess access) { // This method is never invoked. } - public void setClient(LanguageClient client) { + public void setClient(LFLanguageClient client) { this.client = client; } @@ -65,9 +65,11 @@ public CompletableFuture build(String uri) { @JsonRequest("generator/getLibraryReactors") public CompletableFuture> getLibraryReactors(String uri) { + client.test("Test"); return CompletableFuture.supplyAsync( () -> { try { + return List.of("test2", "test3"); } catch (Exception e) { return null; diff --git a/lsp/src/main/java/org/lflang/diagram/lsp/LanguageDiagramServer.java b/lsp/src/main/java/org/lflang/diagram/lsp/LanguageDiagramServer.java index 0531d97721..3033724f94 100644 --- a/lsp/src/main/java/org/lflang/diagram/lsp/LanguageDiagramServer.java +++ b/lsp/src/main/java/org/lflang/diagram/lsp/LanguageDiagramServer.java @@ -69,16 +69,16 @@ public List getLanguageServerExtensions() { @Override public Class getRemoteInterface() { - return KGraphLanguageClient.class; + return LFLanguageClient.class; } @Override public void onConnect() { super.onConnect(); constraints.setClient((KGraphLanguageClient) languageClient); - rectPack.setClient((KGraphLanguageClient) languageClient); + rectPack.setClient((LFLanguageClient) languageClient); LanguageServerMessageReporter.setClient(languageClient); - lfExtension.setClient(languageClient); + lfExtension.setClient((LFLanguageClient) languageClient); // The following is needed because VS Code treats System.err like System.out and System.out // like a shout // into the void. From b641db9a3704e51f01b11e3e3bd07ecda66c152d Mon Sep 17 00:00:00 2001 From: Vincenzo Barbuto Date: Mon, 29 Apr 2024 16:19:16 -0700 Subject: [PATCH 03/15] Improved Tree View, Language Server side: hierarchical structure, go to file --- .../lflang/diagram/lsp/LFLanguageClient.java | 4 +- .../lsp/LFLanguageServerExtension.java | 48 +++++++++++++++++-- .../java/org/lflang/diagram/lsp/Tree.java | 34 +++++++++++++ .../java/org/lflang/diagram/lsp/TreeNode.java | 21 ++++++++ 4 files changed, 100 insertions(+), 7 deletions(-) create mode 100644 lsp/src/main/java/org/lflang/diagram/lsp/Tree.java create mode 100644 lsp/src/main/java/org/lflang/diagram/lsp/TreeNode.java diff --git a/lsp/src/main/java/org/lflang/diagram/lsp/LFLanguageClient.java b/lsp/src/main/java/org/lflang/diagram/lsp/LFLanguageClient.java index 73bb42a54b..b44526fad3 100644 --- a/lsp/src/main/java/org/lflang/diagram/lsp/LFLanguageClient.java +++ b/lsp/src/main/java/org/lflang/diagram/lsp/LFLanguageClient.java @@ -7,7 +7,7 @@ public interface LFLanguageClient extends KGraphLanguageClient, LanguageClient { - @JsonNotification("update/example") - public void test(String updateMessage); + @JsonNotification("notify/sendLibraryReactors") + public void sendLibraryReactors(Tree tree); } diff --git a/lsp/src/main/java/org/lflang/diagram/lsp/LFLanguageServerExtension.java b/lsp/src/main/java/org/lflang/diagram/lsp/LFLanguageServerExtension.java index c4cba8c2e3..744e9dc8d2 100644 --- a/lsp/src/main/java/org/lflang/diagram/lsp/LFLanguageServerExtension.java +++ b/lsp/src/main/java/org/lflang/diagram/lsp/LFLanguageServerExtension.java @@ -3,8 +3,14 @@ import java.util.ArrayList; import java.util.List; import java.util.concurrent.CompletableFuture; +import java.util.stream.Stream; +import org.lflang.diagram.lsp.Tree; +import org.lflang.diagram.lsp.TreeNode; + +import org.eclipse.emf.common.util.EList; import org.eclipse.emf.common.util.URI; +import org.eclipse.emf.ecore.resource.Resource; import org.eclipse.lsp4j.jsonrpc.services.JsonNotification; import org.eclipse.lsp4j.jsonrpc.services.JsonRequest; import org.eclipse.lsp4j.services.LanguageClient; @@ -15,7 +21,15 @@ import org.lflang.generator.GeneratorResult; import org.lflang.generator.GeneratorResult.Status; import org.lflang.generator.IntegratedBuilder; +import org.lflang.lf.Model; +import org.lflang.lf.Parameter; +import org.lflang.lf.Reactor; +import org.lflang.lf.TypeParm; import org.lflang.util.LFCommand; +import org.eclipse.xtext.resource.XtextResourceSet; + +import com.google.inject.Inject; +import com.google.inject.Injector; /** * Provide Lingua-Franca-specific extensions to the language server's behavior. @@ -33,6 +47,8 @@ class LFLanguageServerExtension implements ILanguageServerExtension { /** The access point for reading documents, communicating with the language client, etc. */ private LFLanguageClient client; + @Inject Injector injector; + @Override public void initialize(ILanguageServerAccess access) { // This method is never invoked. @@ -42,6 +58,10 @@ public void setClient(LFLanguageClient client) { this.client = client; } + public XtextResourceSet getXtextResourceSet( final URI uri) { + return injector.getInstance(XtextResourceSet.class); + } + /** * Handle a request for a complete build of the Lingua Franca file specified by {@code uri}. * @@ -64,19 +84,37 @@ public CompletableFuture build(String uri) { } @JsonRequest("generator/getLibraryReactors") - public CompletableFuture> getLibraryReactors(String uri) { - client.test("Test"); + public CompletableFuture getLibraryReactors(String filePath) { return CompletableFuture.supplyAsync( () -> { try { - - return List.of("test2", "test3"); + // LF program file parsing + URI uri = URI.createURI(filePath); + Tree result = parseLibraryReactors(uri); + // Return a list of reactors within the file at the specific uri + if(result != null) client.sendLibraryReactors(result); + return "[Language Server]: Message received and processed"; } catch (Exception e) { - return null; + return "[Language Server]: Error processing " + filePath; } }); } + public Tree parseLibraryReactors(URI uri){ + Tree res = new Tree(uri.toString()); + try{ + Resource resource = getXtextResourceSet(uri).getResource(uri, true); + Model m = (Model) resource.getContents().get(0); + Stream reactors = m.getReactors().stream().filter( r -> r.getName() != null && !r.getName().isEmpty()); + reactors.forEach( r ->{ + res.addChild(new TreeNode(r.getName(), res.getUri())); + }); + }catch(Exception e){ + return null; + } + return res; + } + /** * Handles a request for the most complete build of the specified Lingua Franca file that can be * done in a limited amount of time. diff --git a/lsp/src/main/java/org/lflang/diagram/lsp/Tree.java b/lsp/src/main/java/org/lflang/diagram/lsp/Tree.java new file mode 100644 index 0000000000..6bc5b61aab --- /dev/null +++ b/lsp/src/main/java/org/lflang/diagram/lsp/Tree.java @@ -0,0 +1,34 @@ +package org.lflang.diagram.lsp; + +import org.eclipse.emf.common.util.URI; +import java.util.ArrayList; +import java.util.List; + +public class Tree { + private String label; + private String uri; + private List children; + + public Tree(String uri) { + this.uri = uri; + String[] splits = uri.toString().split("/"); + this.label = splits[splits.length - 1]; + this.children = new ArrayList<>(); + } + + public String getLabel() { + return label; + } + + public String getUri() { + return uri; + } + + public void addChild(TreeNode child) { + children.add(child); + } + + public List getChildren() { + return children; + } +} diff --git a/lsp/src/main/java/org/lflang/diagram/lsp/TreeNode.java b/lsp/src/main/java/org/lflang/diagram/lsp/TreeNode.java new file mode 100644 index 0000000000..c850f8e440 --- /dev/null +++ b/lsp/src/main/java/org/lflang/diagram/lsp/TreeNode.java @@ -0,0 +1,21 @@ +package org.lflang.diagram.lsp; + +import org.eclipse.emf.common.util.URI; + +public class TreeNode { + private String label; + private String uri; + + public TreeNode(String label, String uri) { + this.label = label; + this.uri = uri; + } + + public String getLabel() { + return label; + } + + public String getUri() { + return uri; + } +} From fff6845ea5446b9be79c592155ded8df7d534874 Mon Sep 17 00:00:00 2001 From: Vincenzo Barbuto Date: Tue, 30 Apr 2024 17:51:15 -0700 Subject: [PATCH 04/15] Return Target and Reactors position within the file to improve got-to-file and import-selected-reactor commands --- .../lsp/LFLanguageServerExtension.java | 56 +++++++++++++++++-- .../org/lflang/diagram/lsp/NodePosition.java | 19 +++++++ .../java/org/lflang/diagram/lsp/TreeNode.java | 6 +- 3 files changed, 76 insertions(+), 5 deletions(-) create mode 100644 lsp/src/main/java/org/lflang/diagram/lsp/NodePosition.java diff --git a/lsp/src/main/java/org/lflang/diagram/lsp/LFLanguageServerExtension.java b/lsp/src/main/java/org/lflang/diagram/lsp/LFLanguageServerExtension.java index 744e9dc8d2..8b6e829758 100644 --- a/lsp/src/main/java/org/lflang/diagram/lsp/LFLanguageServerExtension.java +++ b/lsp/src/main/java/org/lflang/diagram/lsp/LFLanguageServerExtension.java @@ -9,7 +9,9 @@ import org.lflang.diagram.lsp.TreeNode; import org.eclipse.emf.common.util.EList; +import org.eclipse.emf.common.util.TreeIterator; import org.eclipse.emf.common.util.URI; +import org.eclipse.emf.ecore.EObject; import org.eclipse.emf.ecore.resource.Resource; import org.eclipse.lsp4j.jsonrpc.services.JsonNotification; import org.eclipse.lsp4j.jsonrpc.services.JsonRequest; @@ -22,10 +24,13 @@ import org.lflang.generator.GeneratorResult.Status; import org.lflang.generator.IntegratedBuilder; import org.lflang.lf.Model; -import org.lflang.lf.Parameter; import org.lflang.lf.Reactor; -import org.lflang.lf.TypeParm; +import org.lflang.lf.TargetDecl; +import org.lflang.target.Target; import org.lflang.util.LFCommand; + +import org.eclipse.xtext.nodemodel.INode; +import org.eclipse.xtext.nodemodel.util.NodeModelUtils; import org.eclipse.xtext.resource.XtextResourceSet; import com.google.inject.Inject; @@ -82,7 +87,14 @@ public CompletableFuture build(String uri) { } }); } - + + + /** + * Manage requests to retrieve a hierarchical structure of reactor libraries based on the provided {@code filePath}. + * + * @param filePath the URI of the LF file of interest + * @return A message describing the outcome of the build process. + */ @JsonRequest("generator/getLibraryReactors") public CompletableFuture getLibraryReactors(String filePath) { return CompletableFuture.supplyAsync( @@ -100,6 +112,40 @@ public CompletableFuture getLibraryReactors(String filePath) { }); } + /** + * Retrieves the target position specified in the LF program file at the given path. + * + * @param path The path to the LF program file. + * @return A CompletableFuture containing the NodePosition object representing the position of the target, + * or null if an error occurs during parsing or if the target position is not found. + */ + @JsonRequest("generator/getTargetPosition") + public CompletableFuture getTargetPosition(String path) { + return CompletableFuture.supplyAsync( + () -> { + NodePosition targetPosition = null; + try { + URI uri = URI.createURI(path); + // LF program file parsing + Resource resource = getXtextResourceSet(uri).getResource(uri, true); + Model m = (Model) resource.getContents().get(0); + TargetDecl target = m.getTarget(); + INode node = NodeModelUtils.getNode(target); + targetPosition = new NodePosition(node.getStartLine(), node.getEndLine()); + return targetPosition; + } catch (Exception e) { + return targetPosition; + } + }); + } + + /** + * Parses a library of reactors specified by the provided URI and constructs a hierarchical tree representation. + * + * @param uri The URI specifying the location of the library. + * @return A Tree object representing the hierarchical structure of the reactor library, + * or null if an error occurs during parsing. + */ public Tree parseLibraryReactors(URI uri){ Tree res = new Tree(uri.toString()); try{ @@ -107,7 +153,9 @@ public Tree parseLibraryReactors(URI uri){ Model m = (Model) resource.getContents().get(0); Stream reactors = m.getReactors().stream().filter( r -> r.getName() != null && !r.getName().isEmpty()); reactors.forEach( r ->{ - res.addChild(new TreeNode(r.getName(), res.getUri())); + INode node = NodeModelUtils.getNode(r); + NodePosition nodePosition = new NodePosition(node.getStartLine(), node.getEndLine()); + res.addChild(new TreeNode(r.getName(), res.getUri(), nodePosition)); }); }catch(Exception e){ return null; diff --git a/lsp/src/main/java/org/lflang/diagram/lsp/NodePosition.java b/lsp/src/main/java/org/lflang/diagram/lsp/NodePosition.java new file mode 100644 index 0000000000..ffe6ac661f --- /dev/null +++ b/lsp/src/main/java/org/lflang/diagram/lsp/NodePosition.java @@ -0,0 +1,19 @@ +package org.lflang.diagram.lsp; + +public class NodePosition { + private int start; + private int end; + + public NodePosition(int start, int end){ + this.start = start; + this.end = end; + } + + public int getStart() { + return start; + } + + public int getEnd() { + return end; + } +} diff --git a/lsp/src/main/java/org/lflang/diagram/lsp/TreeNode.java b/lsp/src/main/java/org/lflang/diagram/lsp/TreeNode.java index c850f8e440..1c21a667c0 100644 --- a/lsp/src/main/java/org/lflang/diagram/lsp/TreeNode.java +++ b/lsp/src/main/java/org/lflang/diagram/lsp/TreeNode.java @@ -5,10 +5,12 @@ public class TreeNode { private String label; private String uri; + private NodePosition position; - public TreeNode(String label, String uri) { + public TreeNode(String label, String uri, NodePosition position) { this.label = label; this.uri = uri; + this.position = position; } public String getLabel() { @@ -18,4 +20,6 @@ public String getLabel() { public String getUri() { return uri; } + + public NodePosition getPosition() { return position;} } From 98ad9675658a34bbff08e3814f61a61be0db29db Mon Sep 17 00:00:00 2001 From: Vincenzo Barbuto Date: Tue, 30 Apr 2024 18:03:11 -0700 Subject: [PATCH 05/15] Commented the new classes --- .../lsp/LFLanguageServerExtension.java | 8 ---- .../org/lflang/diagram/lsp/NodePosition.java | 24 +++++++++++- .../java/org/lflang/diagram/lsp/Tree.java | 36 ++++++++++++++++-- .../java/org/lflang/diagram/lsp/TreeNode.java | 38 ++++++++++++++++--- 4 files changed, 86 insertions(+), 20 deletions(-) diff --git a/lsp/src/main/java/org/lflang/diagram/lsp/LFLanguageServerExtension.java b/lsp/src/main/java/org/lflang/diagram/lsp/LFLanguageServerExtension.java index 8b6e829758..5d87d940b6 100644 --- a/lsp/src/main/java/org/lflang/diagram/lsp/LFLanguageServerExtension.java +++ b/lsp/src/main/java/org/lflang/diagram/lsp/LFLanguageServerExtension.java @@ -1,17 +1,10 @@ package org.lflang.diagram.lsp; import java.util.ArrayList; -import java.util.List; import java.util.concurrent.CompletableFuture; import java.util.stream.Stream; -import org.lflang.diagram.lsp.Tree; -import org.lflang.diagram.lsp.TreeNode; - -import org.eclipse.emf.common.util.EList; -import org.eclipse.emf.common.util.TreeIterator; import org.eclipse.emf.common.util.URI; -import org.eclipse.emf.ecore.EObject; import org.eclipse.emf.ecore.resource.Resource; import org.eclipse.lsp4j.jsonrpc.services.JsonNotification; import org.eclipse.lsp4j.jsonrpc.services.JsonRequest; @@ -26,7 +19,6 @@ import org.lflang.lf.Model; import org.lflang.lf.Reactor; import org.lflang.lf.TargetDecl; -import org.lflang.target.Target; import org.lflang.util.LFCommand; import org.eclipse.xtext.nodemodel.INode; diff --git a/lsp/src/main/java/org/lflang/diagram/lsp/NodePosition.java b/lsp/src/main/java/org/lflang/diagram/lsp/NodePosition.java index ffe6ac661f..0340488964 100644 --- a/lsp/src/main/java/org/lflang/diagram/lsp/NodePosition.java +++ b/lsp/src/main/java/org/lflang/diagram/lsp/NodePosition.java @@ -1,19 +1,39 @@ package org.lflang.diagram.lsp; +/** + * Represents the position of a node within a text document or code file. + */ public class NodePosition { - private int start; - private int end; + private int start; // The starting position of the node. + private int end; // The ending position of the node. + /** + * Constructs a new NodePosition with the specified start and end positions. + * + * @param start The starting position of the node. + * @param end The ending position of the node. + */ public NodePosition(int start, int end){ this.start = start; this.end = end; } + /** + * Retrieves the starting position of the node. + * + * @return The starting position of the node. + */ public int getStart() { return start; } + /** + * Retrieves the ending position of the node. + * + * @return The ending position of the node. + */ public int getEnd() { return end; } } + diff --git a/lsp/src/main/java/org/lflang/diagram/lsp/Tree.java b/lsp/src/main/java/org/lflang/diagram/lsp/Tree.java index 6bc5b61aab..c0160d266f 100644 --- a/lsp/src/main/java/org/lflang/diagram/lsp/Tree.java +++ b/lsp/src/main/java/org/lflang/diagram/lsp/Tree.java @@ -1,14 +1,21 @@ package org.lflang.diagram.lsp; -import org.eclipse.emf.common.util.URI; import java.util.ArrayList; import java.util.List; +/** + * Represents a tree structure, where each node can have zero or more children nodes. + */ public class Tree { - private String label; - private String uri; - private List children; + private String label; // The label associated with the tree. + private String uri; // The URI specifying the location of the tree. + private List children; // The list of children nodes of the tree. + /** + * Constructs a new Tree with the specified URI. + * + * @param uri The URI specifying the location of the tree. + */ public Tree(String uri) { this.uri = uri; String[] splits = uri.toString().split("/"); @@ -16,19 +23,40 @@ public Tree(String uri) { this.children = new ArrayList<>(); } + /** + * Retrieves the label of the tree. + * + * @return The label of the tree. + */ public String getLabel() { return label; } + /** + * Retrieves the URI specifying the location of the tree. + * + * @return The URI specifying the location of the tree. + */ public String getUri() { return uri; } + /** + * Adds a child node to the tree. + * + * @param child The child node to be added. + */ public void addChild(TreeNode child) { children.add(child); } + /** + * Retrieves the list of children nodes of the tree. + * + * @return The list of children nodes of the tree. + */ public List getChildren() { return children; } } + diff --git a/lsp/src/main/java/org/lflang/diagram/lsp/TreeNode.java b/lsp/src/main/java/org/lflang/diagram/lsp/TreeNode.java index 1c21a667c0..7ba099f18b 100644 --- a/lsp/src/main/java/org/lflang/diagram/lsp/TreeNode.java +++ b/lsp/src/main/java/org/lflang/diagram/lsp/TreeNode.java @@ -1,25 +1,51 @@ package org.lflang.diagram.lsp; -import org.eclipse.emf.common.util.URI; - +/** + * Represents a node in a tree structure, typically used to store information about elements in a hierarchical context. + */ public class TreeNode { - private String label; - private String uri; - private NodePosition position; + private String label; // The label associated with the node. + private String uri; // The URI specifying the location of the associated element. + private NodePosition position; // The position information of the associated element. + /** + * Constructs a new TreeNode with the specified label, URI, and position. + * + * @param label The label of the node. + * @param uri The URI specifying the location of the associated element. + * @param position The position information of the associated element. + */ public TreeNode(String label, String uri, NodePosition position) { this.label = label; this.uri = uri; this.position = position; } + /** + * Retrieves the label of the node. + * + * @return The label of the node. + */ public String getLabel() { return label; } + /** + * Retrieves the URI specifying the location of the associated element. + * + * @return The URI specifying the location of the associated element. + */ public String getUri() { return uri; } - public NodePosition getPosition() { return position;} + /** + * Retrieves the position information of the associated element. + * + * @return The position information of the associated element. + */ + public NodePosition getPosition() { + return position; + } } + From d66714122355a2478481d5726779b480f721f546 Mon Sep 17 00:00:00 2001 From: vinzbarbuto Date: Thu, 2 May 2024 16:32:44 -0700 Subject: [PATCH 06/15] Respond to JsonRequest instead of using JsonNotification in "getLibraryReactors" --- .../lflang/diagram/lsp/LFLanguageServerExtension.java | 10 ++++------ lsp/src/main/java/org/lflang/diagram/lsp/TreeNode.java | 1 + 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/lsp/src/main/java/org/lflang/diagram/lsp/LFLanguageServerExtension.java b/lsp/src/main/java/org/lflang/diagram/lsp/LFLanguageServerExtension.java index 5d87d940b6..d8a12c326f 100644 --- a/lsp/src/main/java/org/lflang/diagram/lsp/LFLanguageServerExtension.java +++ b/lsp/src/main/java/org/lflang/diagram/lsp/LFLanguageServerExtension.java @@ -56,7 +56,7 @@ public void setClient(LFLanguageClient client) { } public XtextResourceSet getXtextResourceSet( final URI uri) { - return injector.getInstance(XtextResourceSet.class); + return injector.getInstance(XtextResourceSet.class); } /** @@ -88,18 +88,16 @@ public CompletableFuture build(String uri) { * @return A message describing the outcome of the build process. */ @JsonRequest("generator/getLibraryReactors") - public CompletableFuture getLibraryReactors(String filePath) { + public CompletableFuture getLibraryReactors(String filePath) { return CompletableFuture.supplyAsync( () -> { try { // LF program file parsing URI uri = URI.createURI(filePath); - Tree result = parseLibraryReactors(uri); // Return a list of reactors within the file at the specific uri - if(result != null) client.sendLibraryReactors(result); - return "[Language Server]: Message received and processed"; + return parseLibraryReactors(uri); } catch (Exception e) { - return "[Language Server]: Error processing " + filePath; + return null; } }); } diff --git a/lsp/src/main/java/org/lflang/diagram/lsp/TreeNode.java b/lsp/src/main/java/org/lflang/diagram/lsp/TreeNode.java index 7ba099f18b..46af461efb 100644 --- a/lsp/src/main/java/org/lflang/diagram/lsp/TreeNode.java +++ b/lsp/src/main/java/org/lflang/diagram/lsp/TreeNode.java @@ -47,5 +47,6 @@ public String getUri() { public NodePosition getPosition() { return position; } + } From c96a4b1302c3bd5c6ff9cc74c8daae1733eead73 Mon Sep 17 00:00:00 2001 From: vinzbarbuto Date: Thu, 2 May 2024 16:32:44 -0700 Subject: [PATCH 07/15] Used JsonRequest response instead of JsonNotification in 'getLibraryReactors' --- .../lflang/diagram/lsp/LFLanguageServerExtension.java | 10 ++++------ lsp/src/main/java/org/lflang/diagram/lsp/TreeNode.java | 1 + 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/lsp/src/main/java/org/lflang/diagram/lsp/LFLanguageServerExtension.java b/lsp/src/main/java/org/lflang/diagram/lsp/LFLanguageServerExtension.java index 5d87d940b6..d8a12c326f 100644 --- a/lsp/src/main/java/org/lflang/diagram/lsp/LFLanguageServerExtension.java +++ b/lsp/src/main/java/org/lflang/diagram/lsp/LFLanguageServerExtension.java @@ -56,7 +56,7 @@ public void setClient(LFLanguageClient client) { } public XtextResourceSet getXtextResourceSet( final URI uri) { - return injector.getInstance(XtextResourceSet.class); + return injector.getInstance(XtextResourceSet.class); } /** @@ -88,18 +88,16 @@ public CompletableFuture build(String uri) { * @return A message describing the outcome of the build process. */ @JsonRequest("generator/getLibraryReactors") - public CompletableFuture getLibraryReactors(String filePath) { + public CompletableFuture getLibraryReactors(String filePath) { return CompletableFuture.supplyAsync( () -> { try { // LF program file parsing URI uri = URI.createURI(filePath); - Tree result = parseLibraryReactors(uri); // Return a list of reactors within the file at the specific uri - if(result != null) client.sendLibraryReactors(result); - return "[Language Server]: Message received and processed"; + return parseLibraryReactors(uri); } catch (Exception e) { - return "[Language Server]: Error processing " + filePath; + return null; } }); } diff --git a/lsp/src/main/java/org/lflang/diagram/lsp/TreeNode.java b/lsp/src/main/java/org/lflang/diagram/lsp/TreeNode.java index 7ba099f18b..46af461efb 100644 --- a/lsp/src/main/java/org/lflang/diagram/lsp/TreeNode.java +++ b/lsp/src/main/java/org/lflang/diagram/lsp/TreeNode.java @@ -47,5 +47,6 @@ public String getUri() { public NodePosition getPosition() { return position; } + } From d70b1fe97b0bc66d1feb306ae9507290af04bbe7 Mon Sep 17 00:00:00 2001 From: vinzbarbuto Date: Fri, 19 Jul 2024 15:00:49 -0700 Subject: [PATCH 08/15] Resolved warnings --- .../java/org/lflang/diagram/lsp/LFLanguageServerExtension.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lsp/src/main/java/org/lflang/diagram/lsp/LFLanguageServerExtension.java b/lsp/src/main/java/org/lflang/diagram/lsp/LFLanguageServerExtension.java index bc11951353..6ba124e8e2 100644 --- a/lsp/src/main/java/org/lflang/diagram/lsp/LFLanguageServerExtension.java +++ b/lsp/src/main/java/org/lflang/diagram/lsp/LFLanguageServerExtension.java @@ -148,7 +148,7 @@ public CompletableFuture getTargetPosition(String path) { targetPosition = new NodePosition(node.getStartLine(), node.getEndLine()); return targetPosition; } catch (Exception e) { - return targetPosition; + return null; } }); } From 0bbab9d2f2236ef018aa75cf1a7fd587b06cb43a Mon Sep 17 00:00:00 2001 From: vinzbarbuto Date: Mon, 22 Jul 2024 15:31:31 -0700 Subject: [PATCH 09/15] Tree class renamed to LibraryFileTree; TreeNode class renamed to LibraryFileTreeNode; Fix formatting --- .../lflang/diagram/lsp/LFLanguageClient.java | 8 +- .../lsp/LFLanguageServerExtension.java | 121 +++++++++--------- .../lflang/diagram/lsp/LibraryFileTree.java | 59 +++++++++ .../diagram/lsp/LibraryFileTreeNode.java | 51 ++++++++ .../org/lflang/diagram/lsp/NodePosition.java | 61 +++++---- .../java/org/lflang/diagram/lsp/Tree.java | 62 --------- .../java/org/lflang/diagram/lsp/TreeNode.java | 52 -------- 7 files changed, 202 insertions(+), 212 deletions(-) create mode 100644 lsp/src/main/java/org/lflang/diagram/lsp/LibraryFileTree.java create mode 100644 lsp/src/main/java/org/lflang/diagram/lsp/LibraryFileTreeNode.java delete mode 100644 lsp/src/main/java/org/lflang/diagram/lsp/Tree.java delete mode 100644 lsp/src/main/java/org/lflang/diagram/lsp/TreeNode.java diff --git a/lsp/src/main/java/org/lflang/diagram/lsp/LFLanguageClient.java b/lsp/src/main/java/org/lflang/diagram/lsp/LFLanguageClient.java index b44526fad3..b96a7be75e 100644 --- a/lsp/src/main/java/org/lflang/diagram/lsp/LFLanguageClient.java +++ b/lsp/src/main/java/org/lflang/diagram/lsp/LFLanguageClient.java @@ -1,13 +1,11 @@ package org.lflang.diagram.lsp; +import de.cau.cs.kieler.klighd.lsp.KGraphLanguageClient; import org.eclipse.lsp4j.jsonrpc.services.JsonNotification; import org.eclipse.lsp4j.services.LanguageClient; -import de.cau.cs.kieler.klighd.lsp.KGraphLanguageClient; - public interface LFLanguageClient extends KGraphLanguageClient, LanguageClient { - @JsonNotification("notify/sendLibraryReactors") - public void sendLibraryReactors(Tree tree); - + @JsonNotification("notify/sendLibraryReactors") + public void sendLibraryReactors(LibraryFileTree libraryFileTree); } diff --git a/lsp/src/main/java/org/lflang/diagram/lsp/LFLanguageServerExtension.java b/lsp/src/main/java/org/lflang/diagram/lsp/LFLanguageServerExtension.java index 6ba124e8e2..ff81829d2f 100644 --- a/lsp/src/main/java/org/lflang/diagram/lsp/LFLanguageServerExtension.java +++ b/lsp/src/main/java/org/lflang/diagram/lsp/LFLanguageServerExtension.java @@ -1,9 +1,10 @@ package org.lflang.diagram.lsp; +import com.google.inject.Inject; +import com.google.inject.Injector; import java.util.ArrayList; import java.util.concurrent.CompletableFuture; import java.util.stream.Stream; - import org.eclipse.emf.common.util.URI; import org.eclipse.emf.ecore.resource.Resource; import org.eclipse.lsp4j.jsonrpc.services.JsonNotification; @@ -11,6 +12,9 @@ import org.eclipse.lsp4j.services.LanguageClient; import org.eclipse.xtext.ide.server.ILanguageServerAccess; import org.eclipse.xtext.ide.server.ILanguageServerExtension; +import org.eclipse.xtext.nodemodel.INode; +import org.eclipse.xtext.nodemodel.util.NodeModelUtils; +import org.eclipse.xtext.resource.XtextResourceSet; import org.lflang.LFRuntimeModule; import org.lflang.LFStandaloneSetup; import org.lflang.ast.ToSExpr; @@ -22,13 +26,6 @@ import org.lflang.lf.TargetDecl; import org.lflang.util.LFCommand; -import org.eclipse.xtext.nodemodel.INode; -import org.eclipse.xtext.nodemodel.util.NodeModelUtils; -import org.eclipse.xtext.resource.XtextResourceSet; - -import com.google.inject.Inject; -import com.google.inject.Injector; - /** * Provide Lingua-Franca-specific extensions to the language server's behavior. * @@ -56,9 +53,8 @@ public void setClient(LFLanguageClient client) { this.client = client; } - - public XtextResourceSet getXtextResourceSet( final URI uri) { - return injector.getInstance(XtextResourceSet.class); + public XtextResourceSet getXtextResourceSet(final URI uri) { + return injector.getInstance(XtextResourceSet.class); } @JsonRequest("parser/ast") @@ -104,77 +100,80 @@ public CompletableFuture build(BuildArgs args) { }); } - /** - * Manage requests to retrieve a hierarchical structure of reactor libraries based on the provided {@code filePath}. + * Manage requests to retrieve a hierarchical structure of reactor libraries based on the provided + * {@code filePath}. * * @param filePath the URI of the LF file of interest * @return A message describing the outcome of the build process. */ @JsonRequest("generator/getLibraryReactors") - public CompletableFuture getLibraryReactors(String filePath) { + public CompletableFuture getLibraryReactors(String filePath) { return CompletableFuture.supplyAsync( () -> { try { - // LF program file parsing - URI uri = URI.createURI(filePath); - // Return a list of reactors within the file at the specific uri - return parseLibraryReactors(uri); + // LF program file parsing + URI uri = URI.createURI(filePath); + // Return a list of reactors within the file at the specific uri + return parseLibraryReactors(uri); } catch (Exception e) { return null; } }); } - /** - * Retrieves the target position specified in the LF program file at the given path. - * - * @param path The path to the LF program file. - * @return A CompletableFuture containing the NodePosition object representing the position of the target, - * or null if an error occurs during parsing or if the target position is not found. - */ - @JsonRequest("generator/getTargetPosition") - public CompletableFuture getTargetPosition(String path) { - return CompletableFuture.supplyAsync( - () -> { - NodePosition targetPosition = null; - try { - URI uri = URI.createURI(path); - // LF program file parsing - Resource resource = getXtextResourceSet(uri).getResource(uri, true); - Model m = (Model) resource.getContents().get(0); - TargetDecl target = m.getTarget(); - INode node = NodeModelUtils.getNode(target); - targetPosition = new NodePosition(node.getStartLine(), node.getEndLine()); - return targetPosition; - } catch (Exception e) { - return null; - } - }); - } + /** + * Retrieves the target position specified in the LF program file at the given path. + * + * @param path The path to the LF program file. + * @return A CompletableFuture containing the NodePosition object representing the position of the + * target, or null if an error occurs during parsing or if the target position is not found. + */ + @JsonRequest("generator/getTargetPosition") + public CompletableFuture getTargetPosition(String path) { + return CompletableFuture.supplyAsync( + () -> { + NodePosition targetPosition = null; + try { + URI uri = URI.createURI(path); + // LF program file parsing + Resource resource = getXtextResourceSet(uri).getResource(uri, true); + Model m = (Model) resource.getContents().get(0); + TargetDecl target = m.getTarget(); + INode node = NodeModelUtils.getNode(target); + targetPosition = new NodePosition(node.getStartLine(), node.getEndLine()); + return targetPosition; + } catch (Exception e) { + return null; + } + }); + } /** - * Parses a library of reactors specified by the provided URI and constructs a hierarchical tree representation. + * Parses a library of reactors specified by the provided URI and constructs a hierarchical + * libraryFileTree representation. * * @param uri The URI specifying the location of the library. - * @return A Tree object representing the hierarchical structure of the reactor library, - * or null if an error occurs during parsing. + * @return A Tree object representing the hierarchical structure of the reactor library, or null + * if an error occurs during parsing. */ - public Tree parseLibraryReactors(URI uri){ - Tree res = new Tree(uri.toString()); - try{ - Resource resource = getXtextResourceSet(uri).getResource(uri, true); - Model m = (Model) resource.getContents().get(0); - Stream reactors = m.getReactors().stream().filter( r -> r.getName() != null && !r.getName().isEmpty()); - reactors.forEach( r ->{ - INode node = NodeModelUtils.getNode(r); - NodePosition nodePosition = new NodePosition(node.getStartLine(), node.getEndLine()); - res.addChild(new TreeNode(r.getName(), res.getUri(), nodePosition)); + public LibraryFileTree parseLibraryReactors(URI uri) { + LibraryFileTree res = new LibraryFileTree(uri.toString()); + try { + Resource resource = getXtextResourceSet(uri).getResource(uri, true); + Model m = (Model) resource.getContents().get(0); + Stream reactors = + m.getReactors().stream().filter(r -> r.getName() != null && !r.getName().isEmpty()); + reactors.forEach( + r -> { + INode node = NodeModelUtils.getNode(r); + NodePosition nodePosition = new NodePosition(node.getStartLine(), node.getEndLine()); + res.addChild(new LibraryFileTreeNode(r.getName(), res.getUri(), nodePosition)); }); - }catch(Exception e){ - return null; - } - return res; + } catch (Exception e) { + return null; + } + return res; } /** diff --git a/lsp/src/main/java/org/lflang/diagram/lsp/LibraryFileTree.java b/lsp/src/main/java/org/lflang/diagram/lsp/LibraryFileTree.java new file mode 100644 index 0000000000..b2816006c3 --- /dev/null +++ b/lsp/src/main/java/org/lflang/diagram/lsp/LibraryFileTree.java @@ -0,0 +1,59 @@ +package org.lflang.diagram.lsp; + +import java.util.ArrayList; +import java.util.List; + +/** Represents a tree structure, where each node can have zero or more children nodes. */ +public class LibraryFileTree { + private String label; // The label associated with the tree. + private String uri; // The URI specifying the location of the tree. + private List children; // The list of children nodes of the tree. + + /** + * Constructs a new Tree with the specified URI. + * + * @param uri The URI specifying the location of the tree. + */ + public LibraryFileTree(String uri) { + this.uri = uri; + String[] splits = uri.toString().split("/"); + this.label = splits[splits.length - 1]; + this.children = new ArrayList<>(); + } + + /** + * Retrieves the label of the tree. + * + * @return The label of the tree. + */ + public String getLabel() { + return label; + } + + /** + * Retrieves the URI specifying the location of the tree. + * + * @return The URI specifying the location of the tree. + */ + public String getUri() { + return uri; + } + + /** + * Adds a child node to the tree. + * + * @param child The child node to be added. + */ + public void addChild(LibraryFileTreeNode child) { + children.add(child); + } + + /** + * Retrieves the list of children nodes of the tree. + * + * @return The list of children nodes of the tree. + */ + public List getChildren() { + return children; + } +} diff --git a/lsp/src/main/java/org/lflang/diagram/lsp/LibraryFileTreeNode.java b/lsp/src/main/java/org/lflang/diagram/lsp/LibraryFileTreeNode.java new file mode 100644 index 0000000000..b8c4eaf311 --- /dev/null +++ b/lsp/src/main/java/org/lflang/diagram/lsp/LibraryFileTreeNode.java @@ -0,0 +1,51 @@ +package org.lflang.diagram.lsp; + +/** + * Represents a node in a tree structure, typically used to store information about elements in a + * hierarchical context. + */ +public class LibraryFileTreeNode { + private String label; // The label associated with the node. + private String uri; // The URI specifying the location of the associated element. + private NodePosition position; // The position information of the associated element. + + /** + * Constructs a new TreeNode with the specified label, URI, and position. + * + * @param label The label of the node. + * @param uri The URI specifying the location of the associated element. + * @param position The position information of the associated element. + */ + public LibraryFileTreeNode(String label, String uri, NodePosition position) { + this.label = label; + this.uri = uri; + this.position = position; + } + + /** + * Retrieves the label of the node. + * + * @return The label of the node. + */ + public String getLabel() { + return label; + } + + /** + * Retrieves the URI specifying the location of the associated element. + * + * @return The URI specifying the location of the associated element. + */ + public String getUri() { + return uri; + } + + /** + * Retrieves the position information of the associated element. + * + * @return The position information of the associated element. + */ + public NodePosition getPosition() { + return position; + } +} diff --git a/lsp/src/main/java/org/lflang/diagram/lsp/NodePosition.java b/lsp/src/main/java/org/lflang/diagram/lsp/NodePosition.java index 0340488964..cbc530aaa9 100644 --- a/lsp/src/main/java/org/lflang/diagram/lsp/NodePosition.java +++ b/lsp/src/main/java/org/lflang/diagram/lsp/NodePosition.java @@ -1,39 +1,36 @@ package org.lflang.diagram.lsp; -/** - * Represents the position of a node within a text document or code file. - */ +/** Represents the position of a node within a text document or code file. */ public class NodePosition { - private int start; // The starting position of the node. - private int end; // The ending position of the node. + private int start; // The starting position of the node. + private int end; // The ending position of the node. - /** - * Constructs a new NodePosition with the specified start and end positions. - * - * @param start The starting position of the node. - * @param end The ending position of the node. - */ - public NodePosition(int start, int end){ - this.start = start; - this.end = end; - } + /** + * Constructs a new NodePosition with the specified start and end positions. + * + * @param start The starting position of the node. + * @param end The ending position of the node. + */ + public NodePosition(int start, int end) { + this.start = start; + this.end = end; + } - /** - * Retrieves the starting position of the node. - * - * @return The starting position of the node. - */ - public int getStart() { - return start; - } + /** + * Retrieves the starting position of the node. + * + * @return The starting position of the node. + */ + public int getStart() { + return start; + } - /** - * Retrieves the ending position of the node. - * - * @return The ending position of the node. - */ - public int getEnd() { - return end; - } + /** + * Retrieves the ending position of the node. + * + * @return The ending position of the node. + */ + public int getEnd() { + return end; + } } - diff --git a/lsp/src/main/java/org/lflang/diagram/lsp/Tree.java b/lsp/src/main/java/org/lflang/diagram/lsp/Tree.java deleted file mode 100644 index c0160d266f..0000000000 --- a/lsp/src/main/java/org/lflang/diagram/lsp/Tree.java +++ /dev/null @@ -1,62 +0,0 @@ -package org.lflang.diagram.lsp; - -import java.util.ArrayList; -import java.util.List; - -/** - * Represents a tree structure, where each node can have zero or more children nodes. - */ -public class Tree { - private String label; // The label associated with the tree. - private String uri; // The URI specifying the location of the tree. - private List children; // The list of children nodes of the tree. - - /** - * Constructs a new Tree with the specified URI. - * - * @param uri The URI specifying the location of the tree. - */ - public Tree(String uri) { - this.uri = uri; - String[] splits = uri.toString().split("/"); - this.label = splits[splits.length - 1]; - this.children = new ArrayList<>(); - } - - /** - * Retrieves the label of the tree. - * - * @return The label of the tree. - */ - public String getLabel() { - return label; - } - - /** - * Retrieves the URI specifying the location of the tree. - * - * @return The URI specifying the location of the tree. - */ - public String getUri() { - return uri; - } - - /** - * Adds a child node to the tree. - * - * @param child The child node to be added. - */ - public void addChild(TreeNode child) { - children.add(child); - } - - /** - * Retrieves the list of children nodes of the tree. - * - * @return The list of children nodes of the tree. - */ - public List getChildren() { - return children; - } -} - diff --git a/lsp/src/main/java/org/lflang/diagram/lsp/TreeNode.java b/lsp/src/main/java/org/lflang/diagram/lsp/TreeNode.java deleted file mode 100644 index 46af461efb..0000000000 --- a/lsp/src/main/java/org/lflang/diagram/lsp/TreeNode.java +++ /dev/null @@ -1,52 +0,0 @@ -package org.lflang.diagram.lsp; - -/** - * Represents a node in a tree structure, typically used to store information about elements in a hierarchical context. - */ -public class TreeNode { - private String label; // The label associated with the node. - private String uri; // The URI specifying the location of the associated element. - private NodePosition position; // The position information of the associated element. - - /** - * Constructs a new TreeNode with the specified label, URI, and position. - * - * @param label The label of the node. - * @param uri The URI specifying the location of the associated element. - * @param position The position information of the associated element. - */ - public TreeNode(String label, String uri, NodePosition position) { - this.label = label; - this.uri = uri; - this.position = position; - } - - /** - * Retrieves the label of the node. - * - * @return The label of the node. - */ - public String getLabel() { - return label; - } - - /** - * Retrieves the URI specifying the location of the associated element. - * - * @return The URI specifying the location of the associated element. - */ - public String getUri() { - return uri; - } - - /** - * Retrieves the position information of the associated element. - * - * @return The position information of the associated element. - */ - public NodePosition getPosition() { - return position; - } - -} - From bd48f9da49a074a4e2714de31e0827d721892a1a Mon Sep 17 00:00:00 2001 From: Vincenzo Barbuto <63100303+vinzbarbuto@users.noreply.github.com> Date: Wed, 24 Jul 2024 22:53:36 +0200 Subject: [PATCH 10/15] Update LFLanguageServerExtension.java Co-authored-by: Edward A. Lee --- .../java/org/lflang/diagram/lsp/LFLanguageServerExtension.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lsp/src/main/java/org/lflang/diagram/lsp/LFLanguageServerExtension.java b/lsp/src/main/java/org/lflang/diagram/lsp/LFLanguageServerExtension.java index ff81829d2f..3ee70c5ed7 100644 --- a/lsp/src/main/java/org/lflang/diagram/lsp/LFLanguageServerExtension.java +++ b/lsp/src/main/java/org/lflang/diagram/lsp/LFLanguageServerExtension.java @@ -123,7 +123,7 @@ public CompletableFuture getLibraryReactors(String filePath) { } /** - * Retrieves the target position specified in the LF program file at the given path. + * Retrieve the target position specified in the LF program file at the given path. * * @param path The path to the LF program file. * @return A CompletableFuture containing the NodePosition object representing the position of the From 22a1b5b4cfc97e40c7bcb5bd979dc1eef690fea9 Mon Sep 17 00:00:00 2001 From: Vincenzo Barbuto <63100303+vinzbarbuto@users.noreply.github.com> Date: Wed, 24 Jul 2024 22:53:54 +0200 Subject: [PATCH 11/15] Update LFLanguageServerExtension.java Co-authored-by: Edward A. Lee --- .../java/org/lflang/diagram/lsp/LFLanguageServerExtension.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lsp/src/main/java/org/lflang/diagram/lsp/LFLanguageServerExtension.java b/lsp/src/main/java/org/lflang/diagram/lsp/LFLanguageServerExtension.java index 3ee70c5ed7..b47062f503 100644 --- a/lsp/src/main/java/org/lflang/diagram/lsp/LFLanguageServerExtension.java +++ b/lsp/src/main/java/org/lflang/diagram/lsp/LFLanguageServerExtension.java @@ -150,7 +150,7 @@ public CompletableFuture getTargetPosition(String path) { } /** - * Parses a library of reactors specified by the provided URI and constructs a hierarchical + * Parse a library of reactors specified by the provided URI and construct a hierarchical * libraryFileTree representation. * * @param uri The URI specifying the location of the library. From 52c3578187d472a5ef32a23ed73029a91e12d424 Mon Sep 17 00:00:00 2001 From: Vincenzo Barbuto <63100303+vinzbarbuto@users.noreply.github.com> Date: Wed, 24 Jul 2024 22:56:19 +0200 Subject: [PATCH 12/15] Update NodePosition.java Co-authored-by: Edward A. Lee --- lsp/src/main/java/org/lflang/diagram/lsp/NodePosition.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lsp/src/main/java/org/lflang/diagram/lsp/NodePosition.java b/lsp/src/main/java/org/lflang/diagram/lsp/NodePosition.java index cbc530aaa9..e6bd07fbc5 100644 --- a/lsp/src/main/java/org/lflang/diagram/lsp/NodePosition.java +++ b/lsp/src/main/java/org/lflang/diagram/lsp/NodePosition.java @@ -1,6 +1,10 @@ package org.lflang.diagram.lsp; -/** Represents the position of a node within a text document or code file. */ +/** + * @brief Position of a node within a text document or code file. + * + * A position is a pair of integers representing the starting and ending line numbers. + */ public class NodePosition { private int start; // The starting position of the node. private int end; // The ending position of the node. From a367101a301c01a1bb2c843d4e34695d4f412af7 Mon Sep 17 00:00:00 2001 From: Vincenzo Barbuto <63100303+vinzbarbuto@users.noreply.github.com> Date: Wed, 24 Jul 2024 22:56:27 +0200 Subject: [PATCH 13/15] Update LibraryFileTreeNode.java Co-authored-by: Edward A. Lee --- .../main/java/org/lflang/diagram/lsp/LibraryFileTreeNode.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lsp/src/main/java/org/lflang/diagram/lsp/LibraryFileTreeNode.java b/lsp/src/main/java/org/lflang/diagram/lsp/LibraryFileTreeNode.java index b8c4eaf311..948301198c 100644 --- a/lsp/src/main/java/org/lflang/diagram/lsp/LibraryFileTreeNode.java +++ b/lsp/src/main/java/org/lflang/diagram/lsp/LibraryFileTreeNode.java @@ -1,8 +1,8 @@ package org.lflang.diagram.lsp; /** - * Represents a node in a tree structure, typically used to store information about elements in a - * hierarchical context. + * @brief A node in a tree of items, each with a label, a URI, and a position within the URI. + * @see LibraryFileTree */ public class LibraryFileTreeNode { private String label; // The label associated with the node. From 1d62d81cfb39801773ad4daeda73d57b548c055f Mon Sep 17 00:00:00 2001 From: vinzbarbuto Date: Wed, 24 Jul 2024 23:06:41 +0200 Subject: [PATCH 14/15] Fix formatting --- lsp/src/main/java/org/lflang/diagram/lsp/NodePosition.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lsp/src/main/java/org/lflang/diagram/lsp/NodePosition.java b/lsp/src/main/java/org/lflang/diagram/lsp/NodePosition.java index e6bd07fbc5..b243ec3391 100644 --- a/lsp/src/main/java/org/lflang/diagram/lsp/NodePosition.java +++ b/lsp/src/main/java/org/lflang/diagram/lsp/NodePosition.java @@ -1,10 +1,9 @@ package org.lflang.diagram.lsp; /** - * @brief Position of a node within a text document or code file. - * - * A position is a pair of integers representing the starting and ending line numbers. - */ + * @brief Position of a node within a text document or code file. + *

A position is a pair of integers representing the starting and ending line numbers. + */ public class NodePosition { private int start; // The starting position of the node. private int end; // The ending position of the node. From a0c4e529a3bb8afc3a329b668f1c4de56b75d1bb Mon Sep 17 00:00:00 2001 From: vinzbarbuto Date: Mon, 19 Aug 2024 17:58:14 +0200 Subject: [PATCH 15/15] Renamed `LibraryFileTree.java` to `LibraryFile.java` and `LibraryFileNode.java` to `ReactorNode.java` to better reflect their functionality; Improved documentation across the renamed files for clarity. --- .../lflang/diagram/lsp/LFLanguageClient.java | 2 +- .../lsp/LFLanguageServerExtension.java | 25 ++++---- .../org/lflang/diagram/lsp/LibraryFile.java | 53 +++++++++++++++++ .../lflang/diagram/lsp/LibraryFileTree.java | 59 ------------------- .../diagram/lsp/LibraryFileTreeNode.java | 51 ---------------- .../org/lflang/diagram/lsp/ReactorNode.java | 54 +++++++++++++++++ 6 files changed, 122 insertions(+), 122 deletions(-) create mode 100644 lsp/src/main/java/org/lflang/diagram/lsp/LibraryFile.java delete mode 100644 lsp/src/main/java/org/lflang/diagram/lsp/LibraryFileTree.java delete mode 100644 lsp/src/main/java/org/lflang/diagram/lsp/LibraryFileTreeNode.java create mode 100644 lsp/src/main/java/org/lflang/diagram/lsp/ReactorNode.java diff --git a/lsp/src/main/java/org/lflang/diagram/lsp/LFLanguageClient.java b/lsp/src/main/java/org/lflang/diagram/lsp/LFLanguageClient.java index b96a7be75e..0cd9c7c7b0 100644 --- a/lsp/src/main/java/org/lflang/diagram/lsp/LFLanguageClient.java +++ b/lsp/src/main/java/org/lflang/diagram/lsp/LFLanguageClient.java @@ -7,5 +7,5 @@ public interface LFLanguageClient extends KGraphLanguageClient, LanguageClient { @JsonNotification("notify/sendLibraryReactors") - public void sendLibraryReactors(LibraryFileTree libraryFileTree); + public void sendLibraryReactors(LibraryFile libraryFile); } diff --git a/lsp/src/main/java/org/lflang/diagram/lsp/LFLanguageServerExtension.java b/lsp/src/main/java/org/lflang/diagram/lsp/LFLanguageServerExtension.java index b47062f503..8143f95108 100644 --- a/lsp/src/main/java/org/lflang/diagram/lsp/LFLanguageServerExtension.java +++ b/lsp/src/main/java/org/lflang/diagram/lsp/LFLanguageServerExtension.java @@ -105,10 +105,12 @@ public CompletableFuture build(BuildArgs args) { * {@code filePath}. * * @param filePath the URI of the LF file of interest - * @return A message describing the outcome of the build process. + * @return A {@code CompletableFuture} representing the asynchronous computation * of + * the parsed reactor structure. If an error occurs during parsing, the future will * complete + * with {@code null}. */ @JsonRequest("generator/getLibraryReactors") - public CompletableFuture getLibraryReactors(String filePath) { + public CompletableFuture getLibraryReactors(String filePath) { return CompletableFuture.supplyAsync( () -> { try { @@ -126,14 +128,15 @@ public CompletableFuture getLibraryReactors(String filePath) { * Retrieve the target position specified in the LF program file at the given path. * * @param path The path to the LF program file. - * @return A CompletableFuture containing the NodePosition object representing the position of the - * target, or null if an error occurs during parsing or if the target position is not found. + * @return A {@code CompletableFuture} containing the NodePosition object + * representing the position of the target, or null if an error occurs during parsing or if + * the target position is not found. */ @JsonRequest("generator/getTargetPosition") public CompletableFuture getTargetPosition(String path) { return CompletableFuture.supplyAsync( () -> { - NodePosition targetPosition = null; + NodePosition targetPosition; try { URI uri = URI.createURI(path); // LF program file parsing @@ -151,14 +154,14 @@ public CompletableFuture getTargetPosition(String path) { /** * Parse a library of reactors specified by the provided URI and construct a hierarchical - * libraryFileTree representation. + * libraryFile representation. * * @param uri The URI specifying the location of the library. - * @return A Tree object representing the hierarchical structure of the reactor library, or null - * if an error occurs during parsing. + * @return A {@code LibraryFile} object representing the hierarchical structure of the reactor + * library, or {@code null} if an error occurs during parsing. */ - public LibraryFileTree parseLibraryReactors(URI uri) { - LibraryFileTree res = new LibraryFileTree(uri.toString()); + public LibraryFile parseLibraryReactors(URI uri) { + LibraryFile res = new LibraryFile(uri.toString()); try { Resource resource = getXtextResourceSet(uri).getResource(uri, true); Model m = (Model) resource.getContents().get(0); @@ -168,7 +171,7 @@ public LibraryFileTree parseLibraryReactors(URI uri) { r -> { INode node = NodeModelUtils.getNode(r); NodePosition nodePosition = new NodePosition(node.getStartLine(), node.getEndLine()); - res.addChild(new LibraryFileTreeNode(r.getName(), res.getUri(), nodePosition)); + res.getChildren().add(new ReactorNode(r.getName(), res.getUri(), nodePosition)); }); } catch (Exception e) { return null; diff --git a/lsp/src/main/java/org/lflang/diagram/lsp/LibraryFile.java b/lsp/src/main/java/org/lflang/diagram/lsp/LibraryFile.java new file mode 100644 index 0000000000..c3e7f61dcd --- /dev/null +++ b/lsp/src/main/java/org/lflang/diagram/lsp/LibraryFile.java @@ -0,0 +1,53 @@ +package org.lflang.diagram.lsp; + +import java.util.ArrayList; +import java.util.List; + +/** + * Represents a Lingua Franca (LF) file. This file can contain multiple ReactorNode elements, with + * each node potentially having zero or more of these reactors. + */ +public class LibraryFile { + private String label; // The label (name) associated with the LF file. + private String uri; // The URI specifying the location of the LF file. + private List children; // The list of reactors included within the LF file. + + /** + * Constructs a new LibraryFile with the specified URI. + * + * @param uri The URI specifying the location of the LF file. + */ + public LibraryFile(String uri) { + this.uri = uri; + String[] splits = uri.split("/"); + this.label = splits[splits.length - 1]; + this.children = new ArrayList<>(); + } + + /** + * Retrieves the label (name) of the LF file. + * + * @return The label (name) of the LF file. + */ + public String getLabel() { + return label; + } + + /** + * Retrieves the URI specifying the location of the LF file. + * + * @return The URI specifying the location of the LF file. + */ + public String getUri() { + return uri; + } + + /** + * Retrieves the list of children nodes of the tree. + * + * @return The list of children nodes of the tree. + */ + public List getChildren() { + return children; + } +} diff --git a/lsp/src/main/java/org/lflang/diagram/lsp/LibraryFileTree.java b/lsp/src/main/java/org/lflang/diagram/lsp/LibraryFileTree.java deleted file mode 100644 index b2816006c3..0000000000 --- a/lsp/src/main/java/org/lflang/diagram/lsp/LibraryFileTree.java +++ /dev/null @@ -1,59 +0,0 @@ -package org.lflang.diagram.lsp; - -import java.util.ArrayList; -import java.util.List; - -/** Represents a tree structure, where each node can have zero or more children nodes. */ -public class LibraryFileTree { - private String label; // The label associated with the tree. - private String uri; // The URI specifying the location of the tree. - private List children; // The list of children nodes of the tree. - - /** - * Constructs a new Tree with the specified URI. - * - * @param uri The URI specifying the location of the tree. - */ - public LibraryFileTree(String uri) { - this.uri = uri; - String[] splits = uri.toString().split("/"); - this.label = splits[splits.length - 1]; - this.children = new ArrayList<>(); - } - - /** - * Retrieves the label of the tree. - * - * @return The label of the tree. - */ - public String getLabel() { - return label; - } - - /** - * Retrieves the URI specifying the location of the tree. - * - * @return The URI specifying the location of the tree. - */ - public String getUri() { - return uri; - } - - /** - * Adds a child node to the tree. - * - * @param child The child node to be added. - */ - public void addChild(LibraryFileTreeNode child) { - children.add(child); - } - - /** - * Retrieves the list of children nodes of the tree. - * - * @return The list of children nodes of the tree. - */ - public List getChildren() { - return children; - } -} diff --git a/lsp/src/main/java/org/lflang/diagram/lsp/LibraryFileTreeNode.java b/lsp/src/main/java/org/lflang/diagram/lsp/LibraryFileTreeNode.java deleted file mode 100644 index 948301198c..0000000000 --- a/lsp/src/main/java/org/lflang/diagram/lsp/LibraryFileTreeNode.java +++ /dev/null @@ -1,51 +0,0 @@ -package org.lflang.diagram.lsp; - -/** - * @brief A node in a tree of items, each with a label, a URI, and a position within the URI. - * @see LibraryFileTree - */ -public class LibraryFileTreeNode { - private String label; // The label associated with the node. - private String uri; // The URI specifying the location of the associated element. - private NodePosition position; // The position information of the associated element. - - /** - * Constructs a new TreeNode with the specified label, URI, and position. - * - * @param label The label of the node. - * @param uri The URI specifying the location of the associated element. - * @param position The position information of the associated element. - */ - public LibraryFileTreeNode(String label, String uri, NodePosition position) { - this.label = label; - this.uri = uri; - this.position = position; - } - - /** - * Retrieves the label of the node. - * - * @return The label of the node. - */ - public String getLabel() { - return label; - } - - /** - * Retrieves the URI specifying the location of the associated element. - * - * @return The URI specifying the location of the associated element. - */ - public String getUri() { - return uri; - } - - /** - * Retrieves the position information of the associated element. - * - * @return The position information of the associated element. - */ - public NodePosition getPosition() { - return position; - } -} diff --git a/lsp/src/main/java/org/lflang/diagram/lsp/ReactorNode.java b/lsp/src/main/java/org/lflang/diagram/lsp/ReactorNode.java new file mode 100644 index 0000000000..7349b6de0f --- /dev/null +++ b/lsp/src/main/java/org/lflang/diagram/lsp/ReactorNode.java @@ -0,0 +1,54 @@ +package org.lflang.diagram.lsp; + +/** + * Represents a node in a hierarchical structure where each node has a label, a URI, and positional + * information. This node typically represents a reactor within a Lingua Franca (LF) file. + * + * @see LibraryFile + */ +public class ReactorNode { + + private String label; // The name or label of the reactor. + private String uri; // The URI indicating the location of the LF file that contains the reactor. + private NodePosition position; // The position of the reactor within the LF file. + + /** + * Constructs a new ReactorNode with the specified label, URI, and position. + * + * @param label The label or name of the reactor. + * @param uri The URI that specifies the location of the LF file containing the reactor. + * @param position The position of the reactor within the LF file. + */ + public ReactorNode(String label, String uri, NodePosition position) { + this.label = label; + this.uri = uri; + this.position = position; + } + + /** + * Returns the label of the reactor. + * + * @return The label of the reactor. + */ + public String getLabel() { + return label; + } + + /** + * Returns the URI of the LF file that contains the reactor. + * + * @return The URI of the LF file. + */ + public String getUri() { + return uri; + } + + /** + * Returns the position information of the reactor within the LF file. + * + * @return The position information of the reactor. + */ + public NodePosition getPosition() { + return position; + } +}