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

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