feat: add Type Algorithm

This commit is contained in:
Ruben 2024-11-19 16:13:59 +01:00
parent 782ea67831
commit b30d4e97e5
4 changed files with 101 additions and 34 deletions

View File

@ -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,28 +27,28 @@ 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
@ -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() { }

View File

@ -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;

View File

@ -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));
}

View File

@ -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<String> stack = new Stack<>();
String search = interestingStringList[interestingStringList.length-1];
ArrayList<Integer> 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<String> 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();
}
}