1515 */
1616package org .netbeans .modules .nbcode .java .notebook ;
1717
18+ import java .io .File ;
1819import java .io .PrintWriter ;
1920import java .io .Writer ;
2021import java .util .ArrayList ;
2425import java .util .concurrent .CompletableFuture ;
2526import java .util .concurrent .ConcurrentHashMap ;
2627import java .util .function .BiConsumer ;
28+ import java .util .function .Consumer ;
2729import java .util .logging .Level ;
2830import java .util .logging .Logger ;
2931import java .util .regex .Matcher ;
4345import org .netbeans .modules .java .lsp .server .notebook .NotebookCellExecutionProgressResultParams .Builder ;
4446import org .netbeans .modules .java .lsp .server .notebook .NotebookCellExecutionProgressResultParams .EXECUTION_STATUS ;
4547import org .netbeans .modules .java .lsp .server .protocol .NbCodeLanguageClient ;
48+ import org .openide .util .Exceptions ;
4649import org .openide .util .NbBundle ;
4750import org .openide .util .RequestProcessor ;
4851
@@ -79,7 +82,10 @@ public class CodeEval {
7982 private final Map <String , RequestProcessor > codeExecMap = new ConcurrentHashMap <>();
8083 private final Map <String , List <CompletableFuture <Boolean >>> pendingTasks = new ConcurrentHashMap <>();
8184 private final Map <String , String > activeCellExecutionMapping = new ConcurrentHashMap <>();
82-
85+ // Define the pattern for @MavenGrab("g", "a", "v")
86+ private static final Pattern MAVEN_GRAB_PATTERN =
87+ Pattern .compile ("@MavenGrab\\ s*\\ (\\ s*\" ([^\" ]+)\" \\ s*,\\ s*\" ([^\" ]+)\" \\ s*,\\ s*\" ([^\" ]+)\" \\ s*\\ )" );
88+
8389 public static CodeEval getInstance () {
8490 return Singleton .instance ;
8591 }
@@ -215,8 +221,9 @@ public void runCode(JShell jshell, String code) {
215221
216222 public void runCode (JShell jshell , String code , String notebookId ) {
217223 try {
224+ String cleanCode = processMavenGrab (jshell , code , notebookId );
218225 SourceCodeAnalysis analysis = jshell .sourceCodeAnalysis ();
219- List <String > snippets = NotebookUtils .getCodeSnippets (analysis , code );
226+ List <String > snippets = NotebookUtils .getCodeSnippets (analysis , cleanCode );
220227
221228 for (String snippet : snippets ) {
222229 for (SnippetEvent event : jshell .eval (snippet )) {
@@ -235,6 +242,41 @@ public void runCode(JShell jshell, String code, String notebookId) {
235242 throw new IllegalStateException (e );
236243 }
237244 }
245+
246+ private String processMavenGrab (JShell jshell , String code , String notebookId ) {
247+ Matcher matcher = MAVEN_GRAB_PATTERN .matcher (code );
248+ StringBuffer sb = new StringBuffer ();
249+
250+ while (matcher .find ()) {
251+ try {
252+ String groupId = matcher .group (1 );
253+ String artifactId = matcher .group (2 );
254+ String version = matcher .group (3 );
255+ String coords = String .format ("%s:%s:%s" , groupId , artifactId , version );
256+
257+ Consumer <String > progressCallback = (msg ) -> {
258+ if (notebookId != null ) {
259+ sendNotification (notebookId , msg .getBytes (), EXECUTION_STATUS .EXECUTING , false );
260+ }
261+ };
262+
263+ List <File > jars = MagicCommandHandler .handler (coords , progressCallback );
264+
265+ for (File jar : jars ) {
266+ jshell .addToClasspath (jar .getAbsolutePath ());
267+ String msg = "Jar added to classpath: " + jar .getName () + "\n " ;
268+ sendNotification (notebookId , msg .getBytes (), EXECUTION_STATUS .EXECUTING , false );
269+ }
270+
271+ matcher .appendReplacement (sb , "" );
272+ } catch (Exception ex ) {
273+ LOG .severe (() -> "Some error occurred while downloading artifacts" + ex .getMessage ());
274+ sendNotification (notebookId , "Some error occurred while downloading artifacts" .getBytes (), EXECUTION_STATUS .EXECUTING , true );
275+ }
276+ }
277+ matcher .appendTail (sb );
278+ return sb .toString ();
279+ }
238280
239281 private RequestProcessor getCodeExec (String notebookId ) {
240282 return codeExecMap .computeIfAbsent (notebookId , (id ) -> {
0 commit comments