From 1fbcc78085fd31f6433dfe5221db120f9bf5b539 Mon Sep 17 00:00:00 2001 From: Ruben Date: Thu, 7 Nov 2024 16:19:19 +0100 Subject: [PATCH] feat: add CodeSnippets for main, for and foreach --- .../com/dhbw/JavaTXTextDocumentService.java | 14 ++++++--- .../com/dhbw/helper/CodeSnippetOptions.java | 31 +++++++++++++++++++ .../java/com/dhbw/model/SnippetWithName.java | 27 ++++++++++++++++ 3 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 LanguageServer/src/main/java/com/dhbw/helper/CodeSnippetOptions.java create mode 100644 LanguageServer/src/main/java/com/dhbw/model/SnippetWithName.java diff --git a/LanguageServer/src/main/java/com/dhbw/JavaTXTextDocumentService.java b/LanguageServer/src/main/java/com/dhbw/JavaTXTextDocumentService.java index b05d1b5..92de881 100644 --- a/LanguageServer/src/main/java/com/dhbw/JavaTXTextDocumentService.java +++ b/LanguageServer/src/main/java/com/dhbw/JavaTXTextDocumentService.java @@ -1,6 +1,8 @@ package com.dhbw; +import com.dhbw.helper.CodeSnippetOptions; import com.dhbw.model.ParseError.DiagnoseErrorListener; +import com.dhbw.model.SnippetWithName; import com.dhbw.parser.Java17Lexer; import com.dhbw.parser.Java17Parser; import com.dhbw.parser.Java17ParserBaseListener; @@ -26,6 +28,8 @@ public class JavaTXTextDocumentService implements org.eclipse.lsp4j.services.Tex String currentTextDocument; + CodeSnippetOptions codeSnippetOptions = new CodeSnippetOptions(); + public void setClient(LanguageClient client) { this.client = client; } @@ -34,11 +38,13 @@ public class JavaTXTextDocumentService implements org.eclipse.lsp4j.services.Tex public CompletableFuture, CompletionList>> completion(CompletionParams params) { List completions = new ArrayList<>(); - CompletionItem item = new CompletionItem("HelloWorld"); - item.setKind(CompletionItemKind.Text); - item.setInsertText("Hello, JavaTX World!"); - completions.add(item); + for(SnippetWithName elem : codeSnippetOptions.getSnippets()) { + CompletionItem item = new CompletionItem(elem.getName()); + item.setKind(CompletionItemKind.Text); + item.setInsertText(elem.getSnippet()); + completions.add(item); + } client.showMessage(new MessageParams(MessageType.Info, "Returning completion suggestions: " + completions)); return CompletableFuture.completedFuture(Either.forLeft(completions)); diff --git a/LanguageServer/src/main/java/com/dhbw/helper/CodeSnippetOptions.java b/LanguageServer/src/main/java/com/dhbw/helper/CodeSnippetOptions.java new file mode 100644 index 0000000..865efba --- /dev/null +++ b/LanguageServer/src/main/java/com/dhbw/helper/CodeSnippetOptions.java @@ -0,0 +1,31 @@ +package com.dhbw.helper; + +import com.dhbw.model.SnippetWithName; + +import java.util.ArrayList; + +public class CodeSnippetOptions { + private ArrayList snippets = new ArrayList<>(); + + public CodeSnippetOptions() { + snippets.add(getMainSnippet()); + snippets.add(getForLoopSnippet()); + snippets.add(getForEachSnippet()); + } + + public SnippetWithName getMainSnippet(){ + return new SnippetWithName("main", "public main(args){\n System.out.println(\"Hello World\");\n}\n"); + } + + public SnippetWithName getForLoopSnippet(){ + return new SnippetWithName("forLoop", "for(i = 0; i < list.size(); i++){\n\n}"); + } + + public SnippetWithName getForEachSnippet(){ + return new SnippetWithName("forEachLoop", "for(el : list){\n\n}"); + } + + public ArrayList getSnippets() { + return snippets; + } +} diff --git a/LanguageServer/src/main/java/com/dhbw/model/SnippetWithName.java b/LanguageServer/src/main/java/com/dhbw/model/SnippetWithName.java new file mode 100644 index 0000000..26824c0 --- /dev/null +++ b/LanguageServer/src/main/java/com/dhbw/model/SnippetWithName.java @@ -0,0 +1,27 @@ +package com.dhbw.model; + +public class SnippetWithName { + private String name; + private String snippet; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getSnippet() { + return snippet; + } + + public void setSnippet(String snippet) { + this.snippet = snippet; + } + + public SnippetWithName(String name, String snippet) { + this.name = name; + this.snippet = snippet; + } +}