2 Commits

Author SHA1 Message Date
Ruben
cabb7258aa Merge remote-tracking branch 'origin/main' 2025-01-29 18:22:48 +01:00
Ruben
25a1876b8f refactor #23: refactoring and cleanup 2025-01-29 18:22:39 +01:00
3 changed files with 59 additions and 52 deletions

View File

@@ -152,8 +152,8 @@ public class JavaTXTextDocumentService implements org.eclipse.lsp4j.services.Tex
@Override
public CompletableFuture<List<InlayHint>> inlayHint(InlayHintParams params) {
logger.debug("INLAYHINT-EVENT");
return CompletableFuture.supplyAsync(() -> {
ArrayList<Diagnostic> diagnostics = new ArrayList<>();
try {
@@ -163,13 +163,10 @@ public class JavaTXTextDocumentService implements org.eclipse.lsp4j.services.Tex
for (var variable : typesOfMethodAndParameters) {
InlayHint inlayHint = new InlayHint();
AtomicReference<String> typeDisplay = new AtomicReference<String>();
variable.getPossibleTypes().forEach(el -> {
typeDisplay.getAndSet("| " + el);
});
AtomicReference<String> typeDisplay = new AtomicReference<>();
variable.getPossibleTypes().forEach(el -> typeDisplay.getAndSet("| " + el));
inlayHint.setLabel(typeDisplay.get().substring(2));
inlayHint.setPosition(new Position(variable.getLine() - 1, variable.getCharPosition()));
@@ -177,6 +174,7 @@ public class JavaTXTextDocumentService implements org.eclipse.lsp4j.services.Tex
typeHint.add(inlayHint);
//Diagnostics of Types
for (var typ : variable.getPossibleTypes()) {
Range errorRange = new Range(
new Position(variable.getLine() - 1, variable.getCharPosition()), // Startposition
@@ -345,30 +343,37 @@ public class JavaTXTextDocumentService implements org.eclipse.lsp4j.services.Tex
@Override
public CompletableFuture<List<Either<Command, CodeAction>>> codeAction(CodeActionParams params) {
logger.debug("INSERT-EVENT");
List<Diagnostic> diagnostics = params.getContext().getDiagnostics();
List<Either<Command, CodeAction>> actions = new ArrayList<>();
List<Diagnostic> diagnosticInCurrentDocument = params.getContext().getDiagnostics();
Range requestedRange = params.getRange();
List<Diagnostic> relevantDiagnostics = diagnostics.stream()
//All Diagnostics that are in range of the hover -> All Diagnostics of the selected Variable and thus all Types of the Variable
List<Diagnostic> diagnosticsOverlappingHover = diagnosticInCurrentDocument.stream()
.filter(diagnostic -> rangesOverlap(diagnostic.getRange(), requestedRange)).toList();
Range range = params.getRange();
Range rangeOfInsert = params.getRange();
String documentUri = params.getTextDocument().getUri();
List<Either<Command, CodeAction>> actions = new ArrayList<>();
for (Diagnostic typeDiagnostic : relevantDiagnostics) {
for (Diagnostic typeDiagnostic : diagnosticsOverlappingHover) {
try {
String typeWithReplacedVariable = typeDiagnostic.getMessage() +
" " +
textHelper.getTextOfChars(
textDocuments.get(params.getTextDocument().getUri()),
range.getStart().getLine(),
range.getStart().getCharacter(),
range.getEnd().getCharacter()
rangeOfInsert.getStart().getLine(),
rangeOfInsert.getStart().getCharacter(),
rangeOfInsert.getEnd().getCharacter()
);
var listOfChanges = new ArrayList<>(List.of(new TextEdit(range, typeWithReplacedVariable)));
ArrayList<TextEdit> listOfChanges = new ArrayList<>();
listOfChanges.add(new TextEdit(rangeOfInsert, typeWithReplacedVariable));
var isTypeImported = typeResolver.isTypeImported(textDocuments.get(params.getTextDocument().getUri()), typeDiagnostic.getMessage());
if (!isTypeImported) {
Range importRange = new Range(new Position(0, 0), new Position(0, 0));
@@ -377,6 +382,7 @@ public class JavaTXTextDocumentService implements org.eclipse.lsp4j.services.Tex
}
logger.debug("Returning " + listOfChanges.size() + " Changes for the Document.");
Map<String, List<TextEdit>> changes = new HashMap<>();
changes.put(documentUri, listOfChanges);
@@ -387,8 +393,8 @@ public class JavaTXTextDocumentService implements org.eclipse.lsp4j.services.Tex
action.setKind(CodeActionKind.QuickFix);
action.setEdit(edit);
actions.add(Either.forRight(action));
} catch (Exception ignored) {
} catch (Exception e) {
logger.error("Error creating Actions, returning empty List. The Error was: " + e.getMessage());
}
}

View File

@@ -26,6 +26,38 @@ public class TypeResolver {
languageServer = new LanguageServerInterface();
}
private LanguageServerTransferObject getCacheOrCalculate(String input) throws IOException, ClassNotFoundException {
if(dataCache.containsKey(input)){
logger.debug("Returning Cache instead of calculating Types.");
return dataCache.get(input);
}else{
var transferObject = languageServer.getResultSetAndAbstractSyntax(input);
dataCache.put(input, transferObject);
return transferObject;
}
}
public boolean isTypeImported(String input, String type){
try {
var transferObject = getCacheOrCalculate(input);
var abstractSyntax = transferObject.getAst();
var isAlreadyImported = false;
for(var importStatement : abstractSyntax.getImports()){
logger.debug(importStatement.toString() + "<>" + type);
isAlreadyImported = !isAlreadyImported && importStatement.toString().equals(type);
}
return isAlreadyImported;
} catch (Exception e) {
logger.error("Error creating Transferobject: " + Arrays.toString(e.getStackTrace()));
return true;
}
}
/**
* find the concrete Type of all TPHs and return the outcome as Hashmap.
*/
@@ -79,36 +111,6 @@ public class TypeResolver {
return new ArrayList<>();
}
private LanguageServerTransferObject getCacheOrCalculate(String input) throws IOException, ClassNotFoundException {
if(dataCache.containsKey(input)){
logger.debug("Returning Cache instead of calculating Types.");
return dataCache.get(input);
}else{
var transferObject = languageServer.getResultSetAndAbstractSyntax(input);
dataCache.put(input, transferObject);
return transferObject;
}
}
public boolean isTypeImported(String input, String type){
try {
var transferObject = getCacheOrCalculate(input);
var abstractSyntax = transferObject.getAst();
var isAlreadyImported = false;
for(var importStatement : abstractSyntax.getImports()){
logger.debug(importStatement.toString() + "<>" + type);
isAlreadyImported = !isAlreadyImported && importStatement.toString().equals(type);
}
return isAlreadyImported;
} catch (Exception e) {
logger.error("Error creating Transferobject: " + Arrays.toString(e.getStackTrace()));
return true;
}
}
public ArrayList<ArrayList<String>> findTypesOfAllParameters(LanguageServerTransferObject transferObj, Method method) throws IOException, ClassNotFoundException {
@@ -142,13 +144,13 @@ public class TypeResolver {
public ArrayList<LSPVariable> infereMethodsWithParameters(String input) throws IOException, ClassNotFoundException {
var transferObj = getCacheOrCalculate(input);
ArrayList<LSPVariable> methodLSPVariableList = new ArrayList<>();
ArrayList<LSPVariable> methodsWithParametersLSPVariableList = new ArrayList<>();
for (var method : transferObj.getAst().getAllMethods()) {
var type = findTypeOfMethod(transferObj, method);
if (!type.isEmpty()) {
methodLSPVariableList.add(new LSPMethod(method.name, type, method.getOffset().getLine(), method.getOffset().getCharPositionInLine(), method.getOffset().getStopIndex()));
methodsWithParametersLSPVariableList.add(new LSPMethod(method.name, type, method.getOffset().getLine(), method.getOffset().getCharPositionInLine(), method.getOffset().getStopIndex()));
}
var paramType = findTypesOfAllParameters(transferObj, method);
@@ -157,20 +159,19 @@ public class TypeResolver {
int index = 0;
for (var param : method.getParameterList()) {
if (paramType.get(index) != null && !paramType.get(index).isEmpty()) {
methodLSPVariableList.add(new LSPParameter(method.name, paramType.get(index), param.getOffset().getLine(), param.getOffset().getCharPositionInLine(), param.getOffset().getStopIndex()));
methodsWithParametersLSPVariableList.add(new LSPParameter(method.name, paramType.get(index), param.getOffset().getLine(), param.getOffset().getCharPositionInLine(), param.getOffset().getStopIndex()));
}
index++;
}
}
return methodLSPVariableList;
return methodsWithParametersLSPVariableList;
}
@Deprecated
public ArrayList<LSPMethod> infereMethodType(String input) throws IOException, ClassNotFoundException {
var transferObj = getCacheOrCalculate(input);
//Für RefTypes und so dann entsprechend InstanceOf durch den Syntaxbaum denke ich
ArrayList<LSPMethod> methodNameWithTypeList = new ArrayList<>();
for (var method : transferObj.getAst().getAllMethods()) {