From b30d4e97e52abfafe9a1c8aca4242d03c0a017f4 Mon Sep 17 00:00:00 2001 From: Ruben Date: Tue, 19 Nov 2024 16:13:59 +0100 Subject: [PATCH] feat: add Type Algorithm --- Clients/VisualStudioCode/src/extension.ts | 50 ++++++------- .../de/dhbw/JavaTXLanguageServerLauncher.java | 4 -- .../de/dhbw/JavaTXTextDocumentService.java | 10 +-- .../main/java/de/dhbw/helper/TypeFinder.java | 71 +++++++++++++++++++ 4 files changed, 101 insertions(+), 34 deletions(-) create mode 100644 LanguageServer/src/main/java/de/dhbw/helper/TypeFinder.java diff --git a/Clients/VisualStudioCode/src/extension.ts b/Clients/VisualStudioCode/src/extension.ts index 3483320..2b16b57 100644 --- a/Clients/VisualStudioCode/src/extension.ts +++ b/Clients/VisualStudioCode/src/extension.ts @@ -4,10 +4,10 @@ import * as vscode from 'vscode'; import { workspace, ExtensionContext } from 'vscode'; import { - LanguageClient, - LanguageClientOptions, - ServerOptions, - TransportKind + LanguageClient, + LanguageClientOptions, + ServerOptions, + TransportKind } from 'vscode-languageclient/node'; // This method is called when your extension is activated @@ -27,29 +27,29 @@ export function activate(context: vscode.ExtensionContext) { }; console.log("SERVER CREATED.") - // Clientoptionen: definiere, welche Dateitypen der Client unterstützt - const clientOptions: LanguageClientOptions = { - documentSelector: [{ scheme: 'file', language: 'java' }], - synchronize: { - fileEvents: vscode.workspace.createFileSystemWatcher('**/*.java') - } - }; + // Clientoptionen: definiere, welche Dateitypen der Client unterstützt + const clientOptions: LanguageClientOptions = { + documentSelector: [{ scheme: 'file', language: 'java' }], + synchronize: { + fileEvents: vscode.workspace.createFileSystemWatcher('**/*.java') + } + }; - // Language Client erstellen und starten - const client = new LanguageClient( - 'javaLanguageServer', // ID des Clients - 'Java Language Server', // Name des Clients - serverOptions, - clientOptions - ); + // Language Client erstellen und starten + const client = new LanguageClient( + 'javaLanguageServer', // ID des Clients + 'Java Language Server', // Name des Clients + serverOptions, + clientOptions + ); - // Client starten + // Client starten client.start().then(() => { - console.log("Language Client erfolgreich gestartet"); - }).catch(error => { - console.error("Fehler beim Starten des Language Clients:", error); - }); - + console.log("Language Client erfolgreich gestartet"); + }).catch(error => { + console.error("Fehler beim Starten des Language Clients:", error); + }); + // Use the console to output diagnostic information (console.log) and errors (console.error) // This line of code will only be executed once when your extension is activated console.log('Congratulations, your extension "tx" is now active!'); @@ -67,4 +67,4 @@ export function activate(context: vscode.ExtensionContext) { } // This method is called when your extension is deactivated -export function deactivate() {} +export function deactivate() { } diff --git a/LanguageServer/src/main/java/de/dhbw/JavaTXLanguageServerLauncher.java b/LanguageServer/src/main/java/de/dhbw/JavaTXLanguageServerLauncher.java index 2bc7cd3..77b28e0 100644 --- a/LanguageServer/src/main/java/de/dhbw/JavaTXLanguageServerLauncher.java +++ b/LanguageServer/src/main/java/de/dhbw/JavaTXLanguageServerLauncher.java @@ -1,10 +1,6 @@ package de.dhbw; -import de.dhbw.compiler.syntaxtree.type.RefType; -import de.dhbw.compiler.typedeployment.KindOfTypeInsertPoint; -import de.dhbw.compiler.typedeployment.TypeInsert; -import de.dhbw.compiler.typedeployment.TypeInsertPoint; import org.antlr.v4.runtime.Token; import org.eclipse.lsp4j.launch.LSPLauncher; import org.eclipse.lsp4j.services.LanguageClient; diff --git a/LanguageServer/src/main/java/de/dhbw/JavaTXTextDocumentService.java b/LanguageServer/src/main/java/de/dhbw/JavaTXTextDocumentService.java index 59798f2..90cc448 100644 --- a/LanguageServer/src/main/java/de/dhbw/JavaTXTextDocumentService.java +++ b/LanguageServer/src/main/java/de/dhbw/JavaTXTextDocumentService.java @@ -1,8 +1,7 @@ package de.dhbw; -import de.dhbw.compiler.typedeployment.TypeInsert; -import de.dhbw.compiler.typedeployment.TypeInsertPoint; import de.dhbw.helper.CodeSnippetOptions; +import de.dhbw.helper.TypeFinder; import de.dhbw.model.ParseError.DiagnoseErrorListener; import de.dhbw.model.SnippetWithName; import de.dhbw.parser.Java17Lexer; @@ -221,12 +220,13 @@ public class JavaTXTextDocumentService implements org.eclipse.lsp4j.services.Tex String hoverText = getWordOfLineAndCharacter(params.getPosition().getLine(), params.getPosition().getCharacter(), currentTextDocument); - // Verwende MarkupContent für den Hover-Inhalt + + MarkupContent markupContent = new MarkupContent(); - markupContent.setKind("markdown"); // Optional: Kann auch "plaintext" sein + markupContent.setKind("markdown"); markupContent.setValue(hoverText); - // Gib das Hover-Objekt mit dem MarkupContent zurück + return CompletableFuture.completedFuture(new Hover(markupContent)); } diff --git a/LanguageServer/src/main/java/de/dhbw/helper/TypeFinder.java b/LanguageServer/src/main/java/de/dhbw/helper/TypeFinder.java new file mode 100644 index 0000000..08be09b --- /dev/null +++ b/LanguageServer/src/main/java/de/dhbw/helper/TypeFinder.java @@ -0,0 +1,71 @@ +package de.dhbw.helper; + +import java.awt.desktop.SystemSleepEvent; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Stack; +import java.util.concurrent.atomic.AtomicReference; + +public class TypeFinder { + + + + + public String find(int line, int character, String currentTextDocument) { + + String[] validTypes = { + "int", "long", "short", "byte", "float", "double", "char", "boolean", + "String", "List", "Map", "Set", "Object", "void" + }; + + var stringlist = currentTextDocument.replace(";", " ").replace("{", " { ").replace("}", " } ").split("\n"); + StringBuilder interestingString = new StringBuilder(); + + + int counter = 0; + for(String stringLine : stringlist) { + if(counter < line) { + interestingString.append(stringLine); + } + else if(counter == line) { + interestingString.append(stringLine, 0, character); + } + else{ + break; + } + counter++; + } + + var interestingStringList = interestingString.toString().split(" "); + + Stack stack = new Stack<>(); + String search = interestingStringList[interestingStringList.length-1]; + ArrayList foundPositions = new ArrayList<>(); + + for(int i = interestingStringList.length-1; i >= 0; i--) { + + System.out.println(interestingStringList[i]); + if(stack.isEmpty() && search.equals(interestingStringList[i])) { + foundPositions.add(i); + }else if ("}".equals(interestingStringList[i])) { + stack.push("}"); + }else if ("{".equals(interestingStringList[i]) && !stack.isEmpty()) { + stack.pop(); + } + + } + AtomicReference type = new AtomicReference<>(""); + foundPositions.forEach(el -> { + System.out.println(el); + String potentialType = interestingStringList[el-1]; + + if(Arrays.asList(validTypes).contains(potentialType)){ + type.getAndSet(potentialType); + } + + }); + + return type.get(); + + } +} \ No newline at end of file