Compare commits
7 Commits
1e125407f1
...
0.0.5
Author | SHA1 | Date | |
---|---|---|---|
|
1b84b77677 | ||
|
6b40ce7e64 | ||
|
3fe253ae63 | ||
|
9d42e829e2 | ||
|
a88e34431f | ||
|
a53b79f91d | ||
|
e119d42148 |
Binary file not shown.
@@ -37,7 +37,6 @@ public class JavaTXTextDocumentService implements org.eclipse.lsp4j.services.Tex
|
||||
private final ChangeHandler changeHandler;
|
||||
private final TextHelper textHelper;
|
||||
private final CodeActionHandler codeActionHandler;
|
||||
private final InlayHintAdjusterDiffUtils inlayHintAdjusterDiffUtils;
|
||||
|
||||
LanguageClient client;
|
||||
HashMap<String, String> textDocuments = new HashMap<>();
|
||||
@@ -48,7 +47,6 @@ public class JavaTXTextDocumentService implements org.eclipse.lsp4j.services.Tex
|
||||
|
||||
public JavaTXTextDocumentService() {
|
||||
this.textHelper = new TextHelper();
|
||||
this.inlayHintAdjusterDiffUtils = new InlayHintAdjusterDiffUtils();
|
||||
this.cacheService = new CacheService();
|
||||
this.clientService = new ClientService(null);
|
||||
this.typeResolver = new TypeResolver();
|
||||
@@ -59,7 +57,7 @@ public class JavaTXTextDocumentService implements org.eclipse.lsp4j.services.Tex
|
||||
this.parserService = new ParserService(conversionHelper, clientService, cacheService);
|
||||
this.codeActionHandler = new CodeActionHandler(textHelper, textDocumentService, cacheService, typeResolver, logService);
|
||||
this.saveHandler = new SaveHandler(typeResolver, textDocumentService, logService, cacheService, conversionHelper, clientService, parserService);
|
||||
this.changeHandler = new ChangeHandler(textDocumentService, parserService, conversionHelper, clientService, typeResolver, cacheService, logService, inlayHintAdjusterDiffUtils);
|
||||
this.changeHandler = new ChangeHandler(textDocumentService, parserService, conversionHelper, clientService, typeResolver, cacheService, logService);
|
||||
}
|
||||
|
||||
public void setClient(LanguageClient client) {
|
||||
@@ -291,7 +289,7 @@ public class JavaTXTextDocumentService implements org.eclipse.lsp4j.services.Tex
|
||||
logService.log("Code-Action Context was: " + params.getContext().getTriggerKind().name(), MessageType.Info);
|
||||
|
||||
return CompletableFuture.supplyAsync(() -> {
|
||||
return codeActionHandler.handleCodeAction(params);
|
||||
return codeActionHandler.handleNewCodeAction(params);
|
||||
});
|
||||
}
|
||||
|
||||
|
@@ -14,6 +14,7 @@ import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@@ -26,9 +27,8 @@ public class ChangeHandler {
|
||||
private final TypeResolver typeResolver;
|
||||
private final CacheService cacheService;
|
||||
private final LogService logService;
|
||||
private final InlayHintAdjusterDiffUtils inlayHintAdjusterDiffUtils;
|
||||
|
||||
public ChangeHandler(TextDocumentService textDocumentService, ParserService parserService, ConversionHelper conversionHelper, ClientService clientService, TypeResolver typeResolver, CacheService cacheService, LogService logService, InlayHintAdjusterDiffUtils inlayHintAdjusterDiffUtils) {
|
||||
public ChangeHandler(TextDocumentService textDocumentService, ParserService parserService, ConversionHelper conversionHelper, ClientService clientService, TypeResolver typeResolver, CacheService cacheService, LogService logService) {
|
||||
this.textDocumentService = textDocumentService;
|
||||
this.parserService = parserService;
|
||||
this.conversionHelper = conversionHelper;
|
||||
@@ -36,13 +36,34 @@ public class ChangeHandler {
|
||||
this.typeResolver = typeResolver;
|
||||
this.cacheService = cacheService;
|
||||
this.logService = logService;
|
||||
this.inlayHintAdjusterDiffUtils = inlayHintAdjusterDiffUtils;
|
||||
}
|
||||
|
||||
|
||||
public static String findAddedWord(String str1, String str2) {
|
||||
String[] words1 = str1.split("\\s+");
|
||||
String[] words2 = str2.split("\\s+");
|
||||
|
||||
int i = 0, j = 0;
|
||||
while (i < words1.length && j < words2.length) {
|
||||
if (words1[i].equals(words2[j])) {
|
||||
i++;
|
||||
j++;
|
||||
} else {
|
||||
return words2[j]; // Unterschied gefunden
|
||||
}
|
||||
}
|
||||
|
||||
// Falls das neue Wort am Ende steht
|
||||
if (j < words2.length) {
|
||||
return words2[j];
|
||||
}
|
||||
|
||||
return null; // Kein Unterschied gefunden
|
||||
}
|
||||
|
||||
public void didChange(DidChangeTextDocumentParams params) {
|
||||
String currentText = textDocumentService.getFileOfUri(params.getTextDocument().getUri());
|
||||
|
||||
|
||||
AtomicReference<String> summedUp = new AtomicReference<>("");
|
||||
params.getContentChanges().forEach(el -> summedUp.set(summedUp.get() + el.getText()));
|
||||
textDocumentService.saveFileWithUri(params.getTextDocument().getUri(), summedUp.get());
|
||||
@@ -50,13 +71,15 @@ public class ChangeHandler {
|
||||
DocumentChanges documentChanges = textDocumentService.calculateDifference(currentText, summedUp.get());
|
||||
HashMap<LineCharPosition, String> preciseChanges = documentChanges.getPreciseChanges();
|
||||
HashMap<Integer, List<String>> textChanges;
|
||||
ArrayList<Integer> offsetPerLine = documentChanges.getOffsetPerLine();
|
||||
|
||||
|
||||
String type = findAddedWord(currentText, summedUp.get());
|
||||
logService.log("ADDED TYPE IS: " + type);
|
||||
|
||||
//Have to check of old saved Input because the result set and Inlay Hints will be updated according to the old AST which contains the old Positions
|
||||
if (cacheService.getLastSavedFiles().containsKey(params.getTextDocument().getUri())) {
|
||||
DocumentChanges changesFromOldSave = textDocumentService.calculateDifference(cacheService.getLastSavedFiles().get(params.getTextDocument().getUri()), summedUp.get());
|
||||
offsetPerLine = changesFromOldSave.getOffsetPerLine();
|
||||
;
|
||||
textChanges = changesFromOldSave.getTextChanges();
|
||||
preciseChanges = changesFromOldSave.getPreciseChanges();
|
||||
logService.log(textChanges.values().stream().map(el -> String.join(", ", el)).collect(Collectors.joining("\n")));
|
||||
@@ -68,6 +91,18 @@ public class ChangeHandler {
|
||||
String input = summedUp.get();
|
||||
|
||||
|
||||
logService.log("Variables are: " + cacheService.getVariables().size());
|
||||
|
||||
|
||||
AtomicBoolean needToCalculate = new AtomicBoolean(false);
|
||||
|
||||
cacheService.getVariables().forEach(
|
||||
el -> el.getPossibleTypes().forEach(pos -> needToCalculate.set(needToCalculate.get() || pos.getType().equalsIgnoreCase(type)))
|
||||
);
|
||||
|
||||
|
||||
|
||||
if (false) {
|
||||
typeResolver.reduceCurrent(preciseChanges, cacheService.getVariables());
|
||||
try {
|
||||
File tempDir = new File(System.getProperty("java.io.tmpdir"));
|
||||
@@ -78,12 +113,12 @@ public class ChangeHandler {
|
||||
bw.write(summedUp.get());
|
||||
bw.close();
|
||||
typeResolver.updateAst(tempFile.toURI().getPath());
|
||||
tempFile.delete();
|
||||
} catch (Exception e) {
|
||||
logService.log(e.getMessage());
|
||||
}
|
||||
|
||||
|
||||
|
||||
var sWatch = Stopwatch.createUnstarted();
|
||||
sWatch.start();
|
||||
|
||||
@@ -95,7 +130,7 @@ public class ChangeHandler {
|
||||
logService.log("[didChange] Input of Text Document is null in TextDocument-Hashmap.", MessageType.Error);
|
||||
}
|
||||
|
||||
ArrayList<LSPVariable> typesOfMethodAndParameters = typeResolver.infereInput(currentInput, params.getTextDocument().getUri());
|
||||
ArrayList<LSPVariable> typesOfMethodAndParameters = typeResolver.infereChangeInput(params.getTextDocument().getUri());
|
||||
|
||||
|
||||
DiagnosticsAndTypehints diagnosticsAndTypehints = conversionHelper.variablesToDiagnosticsAndTypehints(typesOfMethodAndParameters, params.getTextDocument().getUri());
|
||||
@@ -144,34 +179,68 @@ public class ChangeHandler {
|
||||
allDiagnostics.addAll(parserErrors);
|
||||
|
||||
clientService.publishDiagnostics(params.getTextDocument().getUri(), allDiagnostics);
|
||||
} else {
|
||||
var sWatch = Stopwatch.createUnstarted();
|
||||
sWatch.start();
|
||||
|
||||
try {
|
||||
|
||||
String fileInput = input;
|
||||
logService.log("Input ist:");
|
||||
logService.log(fileInput);
|
||||
// cacheService.getLastSavedFiles().put(didSaveTextDocumentParams.getTextDocument().getUri(), fileInput);
|
||||
// typeResolver.getCompilerInput(didSaveTextDocumentParams.getTextDocument().getUri(), fileInput);
|
||||
|
||||
try {
|
||||
File tempDir2 = new File(System.getProperty("java.io.tmpdir"));
|
||||
File tempFile2 = File.createTempFile("newText", ".tmp", tempDir2);
|
||||
FileWriter fileWriter = new FileWriter(tempFile2, true);
|
||||
BufferedWriter bw = new BufferedWriter(fileWriter);
|
||||
bw.write(fileInput);
|
||||
bw.close();
|
||||
|
||||
|
||||
if (fileInput == null) {
|
||||
logService.log("[didSave] Input of Text Document is null in TextDocument-Hashmap.", MessageType.Error);
|
||||
}
|
||||
|
||||
ArrayList<LSPVariable> variables = typeResolver.infereInput(tempFile2.toURI().toString(), fileInput, false);
|
||||
cacheService.setVariables(variables);
|
||||
|
||||
DiagnosticsAndTypehints diagnosticsAndTypehints = conversionHelper.variablesToDiagnosticsAndTypehintsWithInput(variables, input);
|
||||
|
||||
List<InlayHint> typeHint = diagnosticsAndTypehints.getInlayHints();
|
||||
List<Diagnostic> diagnostics = diagnosticsAndTypehints.getDiagnostics();
|
||||
|
||||
cacheService.updateGlobalMaps(diagnostics, typeHint, params.getTextDocument().getUri());
|
||||
|
||||
List<Diagnostic> allDiagnostics = new ArrayList<>(diagnostics);
|
||||
allDiagnostics.addAll(parserService.getDiagnosticsOfErrors(fileInput, params.getTextDocument().getUri()));
|
||||
|
||||
clientService.publishDiagnostics(params.getTextDocument().getUri(), allDiagnostics);
|
||||
|
||||
tempFile2.delete();
|
||||
} catch (Exception e) {
|
||||
logService.log("[didSave] Error trying to get Inlay-Hints and Diagnostics for Client: " + e.getMessage(), MessageType.Error);
|
||||
|
||||
for (StackTraceElement elem : e.getStackTrace()) {
|
||||
logService.log(elem.toString());
|
||||
}
|
||||
clientService.showMessage(MessageType.Error, e.getMessage() == null ? "null" : e.getMessage());
|
||||
cacheService.updateGlobalMaps(new ArrayList<>(), new ArrayList<>(), params.getTextDocument().getUri());
|
||||
|
||||
} finally {
|
||||
sWatch.stop();
|
||||
logService.log("[didSave] Finished Calculating in [" + sWatch.elapsed().toSeconds() + "s]", MessageType.Info);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
private void updatePositions(DidChangeTextDocumentParams params, ArrayList<Integer> offsetPerLine, String old, String newText){
|
||||
cacheService.getGlobalInlayHintMap().put(params.getTextDocument().getUri(), inlayHintAdjusterDiffUtils.adjustInlayHintsByLineDiff(old, newText, cacheService.getGlobalInlayHintMap().get(params.getTextDocument().getUri())));
|
||||
|
||||
for (var diagnostic : cacheService.getGlobalDiagnosticsMap().get(params.getTextDocument().getUri())) {
|
||||
diagnostic.getRange().getStart().setCharacter(diagnostic.getRange().getStart().getCharacter() + offsetPerLine.get(diagnostic.getRange().getStart().getLine()));
|
||||
diagnostic.getRange().getEnd().setCharacter(diagnostic.getRange().getEnd().getCharacter() + offsetPerLine.get(diagnostic.getRange().getEnd().getLine()));
|
||||
} catch (Exception e) {
|
||||
logService.log(e.getMessage());
|
||||
}
|
||||
|
||||
clientService.updateClient();
|
||||
}
|
||||
}
|
||||
|
||||
private void updatePositions(DidChangeTextDocumentParams params, ArrayList<Integer> offsetPerLine){
|
||||
for (var inlayHint : cacheService.getGlobalInlayHintMap().get(params.getTextDocument().getUri())) {
|
||||
inlayHint.getPosition().setCharacter(inlayHint.getPosition().getCharacter() + offsetPerLine.get(inlayHint.getPosition().getLine()));
|
||||
}
|
||||
|
||||
for (var diagnostic : cacheService.getGlobalDiagnosticsMap().get(params.getTextDocument().getUri())) {
|
||||
diagnostic.getRange().getStart().setCharacter(diagnostic.getRange().getStart().getCharacter() + offsetPerLine.get(diagnostic.getRange().getStart().getLine()));
|
||||
diagnostic.getRange().getEnd().setCharacter(diagnostic.getRange().getEnd().getCharacter() + offsetPerLine.get(diagnostic.getRange().getEnd().getLine()));
|
||||
}
|
||||
|
||||
clientService.updateClient();
|
||||
}
|
||||
}
|
||||
|
@@ -5,6 +5,7 @@ import de.dhbw.helper.TypeResolver;
|
||||
import de.dhbw.service.CacheService;
|
||||
import de.dhbw.service.LogService;
|
||||
import de.dhbw.service.TextDocumentService;
|
||||
import de.dhbwstuttgart.typedeployment.TypeInsert;
|
||||
import org.eclipse.lsp4j.*;
|
||||
import org.eclipse.lsp4j.jsonrpc.messages.Either;
|
||||
|
||||
@@ -12,6 +13,7 @@ import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class CodeActionHandler {
|
||||
|
||||
@@ -29,6 +31,117 @@ public class CodeActionHandler {
|
||||
this.logService = logService;
|
||||
}
|
||||
|
||||
public static Range wholeDocumentRange(String text) {
|
||||
if (text == null || text.isEmpty()) {
|
||||
return new Range(new Position(0, 0), new Position(0, 0));
|
||||
}
|
||||
|
||||
int lastLine = 0;
|
||||
int lastBreak = -1; // Index des letzten Zeilenumbruchszeichens, das zum Zeilenende gehört
|
||||
|
||||
final int n = text.length();
|
||||
for (int i = 0; i < n; i++) {
|
||||
char c = text.charAt(i);
|
||||
if (c == '\n') {
|
||||
lastLine++;
|
||||
lastBreak = i;
|
||||
} else if (c == '\r') {
|
||||
// Unterscheide \r\n von alleine stehendem \r
|
||||
if (i + 1 < n && text.charAt(i + 1) == '\n') {
|
||||
lastLine++;
|
||||
lastBreak = i + 1;
|
||||
i++; // \n überspringen
|
||||
} else {
|
||||
lastLine++;
|
||||
lastBreak = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int lastLineStart = lastBreak + 1;
|
||||
int endChar = n - lastLineStart; // Länge der letzten Zeile in UTF-16 Code Units
|
||||
|
||||
return new Range(new Position(0, 0), new Position(lastLine, endChar));
|
||||
}
|
||||
|
||||
|
||||
public static <V> Map<String, V> getOverlapping(
|
||||
Map<String, V> map,
|
||||
int line,
|
||||
int startChar,
|
||||
int endChar
|
||||
) {
|
||||
if (startChar > endChar) {
|
||||
int t = startChar;
|
||||
startChar = endChar;
|
||||
endChar = t;
|
||||
}
|
||||
|
||||
final int s = startChar, e = endChar;
|
||||
|
||||
return map.entrySet().stream()
|
||||
.filter(entry -> {
|
||||
String key = entry.getKey();
|
||||
String[] parts = key.split("\\s+");
|
||||
if (parts.length != 2) return false;
|
||||
try {
|
||||
int kLine = Integer.parseInt(parts[0]);
|
||||
int kChar = Integer.parseInt(parts[1]);
|
||||
return kLine == line && kChar >= s && kChar <= e;
|
||||
} catch (NumberFormatException ex) {
|
||||
// Key nicht im erwarteten Format
|
||||
return false;
|
||||
}
|
||||
})
|
||||
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
|
||||
}
|
||||
|
||||
|
||||
public List<Either<Command, CodeAction>> handleNewCodeAction(CodeActionParams params) {
|
||||
|
||||
String documentUri = params.getTextDocument().getUri();
|
||||
|
||||
|
||||
List<Diagnostic> diagnosticInCurrentDocument = params.getContext().getDiagnostics();
|
||||
Range rangeOfInsert = params.getRange();
|
||||
|
||||
//All Diagnostics that are in range of the hover -> All Diagnostics of the selected Variable and thus all Types of the Variable
|
||||
Map<String, List<TypeInsert>> typeInsertsOverlapping = getOverlapping(typeResolver.getInserts(), rangeOfInsert.getStart().getLine()+1, rangeOfInsert.getStart().getCharacter(), rangeOfInsert.getEnd().getCharacter());
|
||||
logService.log("Inserts are:");
|
||||
typeResolver.getInserts().forEach((key, value) -> logService.log(key));
|
||||
logService.log("Size is: " + typeInsertsOverlapping.size());
|
||||
logService.log("Range is: " + rangeOfInsert.getStart().getLine() + " -> " + rangeOfInsert.getStart().getCharacter() + " - " + rangeOfInsert.getEnd().getCharacter());
|
||||
List<Either<Command, CodeAction>> actions = new ArrayList<>();
|
||||
|
||||
for (var typeInsertList : typeInsertsOverlapping.values()) {
|
||||
for (var typeInsert : typeInsertList) {
|
||||
try {
|
||||
String typeWithReplacedVariable = typeInsert.insert(textDocumentService.getFileOfUri(documentUri));
|
||||
|
||||
ArrayList<TextEdit> listOfChanges = new ArrayList<>();
|
||||
|
||||
listOfChanges.add(new TextEdit(wholeDocumentRange(textDocumentService.getFileOfUri(documentUri)), typeWithReplacedVariable));
|
||||
|
||||
var isTypeImported = false;
|
||||
|
||||
Map<String, List<TextEdit>> changes = new HashMap<>();
|
||||
changes.put(documentUri, listOfChanges);
|
||||
|
||||
WorkspaceEdit edit = new WorkspaceEdit();
|
||||
edit.setChanges(changes);
|
||||
|
||||
CodeAction action = new CodeAction("Insert " + typeInsert.getInsertString());
|
||||
action.setKind(CodeActionKind.QuickFix);
|
||||
action.setEdit(edit);
|
||||
actions.add(Either.forRight(action));
|
||||
} catch (Exception e) {
|
||||
logService.log("Error creating Actions, returning empty List. The Error was: " + e.getMessage(), MessageType.Error);
|
||||
}
|
||||
}
|
||||
}
|
||||
return actions;
|
||||
}
|
||||
|
||||
public List<Either<Command, CodeAction>> handleCodeAction(CodeActionParams params) {
|
||||
|
||||
String documentUri = params.getTextDocument().getUri();
|
||||
|
@@ -1,55 +0,0 @@
|
||||
package de.dhbw.handler;
|
||||
|
||||
import com.github.difflib.DiffUtils;
|
||||
import com.github.difflib.patch.AbstractDelta;
|
||||
import com.github.difflib.patch.DeltaType;
|
||||
import com.github.difflib.patch.Patch;
|
||||
|
||||
import org.eclipse.lsp4j.InlayHint;
|
||||
import org.eclipse.lsp4j.Position;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class InlayHintAdjusterDiffUtils {
|
||||
|
||||
public List<InlayHint> adjustInlayHintsByLineDiff(String oldText, String newText, List<InlayHint> oldHints) {
|
||||
List<String> oldLines = Arrays.asList(oldText.split("\n", -1));
|
||||
List<String> newLines = Arrays.asList(newText.split("\n", -1));
|
||||
|
||||
Patch<String> patch = DiffUtils.diff(oldLines, newLines);
|
||||
List<AbstractDelta<String>> deltas = patch.getDeltas();
|
||||
|
||||
List<InlayHint> adjusted = new ArrayList<>();
|
||||
for (InlayHint hint : oldHints) {
|
||||
int oldLine = hint.getPosition().getLine();
|
||||
int lineShift = calculateLineShift(deltas, oldLine);
|
||||
int newLine = Math.max(0, oldLine + lineShift);
|
||||
|
||||
adjusted.add(new InlayHint(new Position(newLine, hint.getPosition().getCharacter()), hint.getLabel()));
|
||||
}
|
||||
|
||||
return adjusted;
|
||||
}
|
||||
|
||||
private static int calculateLineShift(List<AbstractDelta<String>> deltas, int oldLine) {
|
||||
int shift = 0;
|
||||
for (AbstractDelta<String> delta : deltas) {
|
||||
int deltaLine = delta.getSource().getPosition();
|
||||
int linesRemoved = delta.getSource().size();
|
||||
int linesAdded = delta.getTarget().size();
|
||||
|
||||
if (oldLine > deltaLine) {
|
||||
if (delta.getType() == DeltaType.INSERT) {
|
||||
shift += linesAdded;
|
||||
} else if (delta.getType() == DeltaType.DELETE) {
|
||||
shift -= linesRemoved;
|
||||
} else if (delta.getType() == DeltaType.CHANGE) {
|
||||
shift += linesAdded - linesRemoved;
|
||||
}
|
||||
}
|
||||
}
|
||||
return shift;
|
||||
}
|
||||
}
|
@@ -6,8 +6,12 @@ import de.dhbw.service.*;
|
||||
import de.dhbw.helper.ConversionHelper;
|
||||
import de.dhbw.helper.TypeResolver;
|
||||
import de.dhbw.model.LSPVariable;
|
||||
import de.dhbwstuttgart.languageServerInterface.LanguageServerInterface;
|
||||
import org.eclipse.lsp4j.*;
|
||||
|
||||
import java.net.URI;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@@ -40,13 +44,13 @@ public class SaveHandler {
|
||||
|
||||
String fileInput = textDocumentService.getFileOfUri(didSaveTextDocumentParams.getTextDocument().getUri());
|
||||
cacheService.getLastSavedFiles().put(didSaveTextDocumentParams.getTextDocument().getUri(), fileInput);
|
||||
typeResolver.getCompilerInput(didSaveTextDocumentParams.getTextDocument().getUri(), fileInput);
|
||||
//typeResolver.getCompilerInput(didSaveTextDocumentParams.getTextDocument().getUri(), fileInput);
|
||||
|
||||
if (fileInput == null) {
|
||||
logService.log("[didSave] Input of Text Document is null in TextDocument-Hashmap.", MessageType.Error);
|
||||
}
|
||||
|
||||
ArrayList<LSPVariable> variables = typeResolver.infereInput(fileInput, didSaveTextDocumentParams.getTextDocument().getUri());
|
||||
ArrayList<LSPVariable> variables = typeResolver.infereInput(didSaveTextDocumentParams.getTextDocument().getUri(), fileInput, false);
|
||||
cacheService.setVariables(variables);
|
||||
|
||||
DiagnosticsAndTypehints diagnosticsAndTypehints = conversionHelper.variablesToDiagnosticsAndTypehints(variables, didSaveTextDocumentParams.getTextDocument().getUri());
|
||||
@@ -61,10 +65,22 @@ public class SaveHandler {
|
||||
|
||||
clientService.publishDiagnostics(didSaveTextDocumentParams.getTextDocument().getUri(), allDiagnostics);
|
||||
|
||||
// LanguageServerInterface languageServerInterface = new LanguageServerInterface();
|
||||
// Path path = Paths.get(new URI(didSaveTextDocumentParams.getTextDocument().getUri())).getParent();
|
||||
// logService.log("Path is: " + path.toString());
|
||||
//
|
||||
// try{
|
||||
// path.toFile().mkdirs();
|
||||
// }catch (Exception e){}
|
||||
// languageServerInterface.generateBytecode(path.toUri());
|
||||
|
||||
} catch (Exception e) {
|
||||
logService.log("[didSave] Error trying to get Inlay-Hints and Diagnostics for Client: " + e.getMessage(), MessageType.Error);
|
||||
clientService.showMessage(MessageType.Error, e.getMessage());
|
||||
|
||||
for (StackTraceElement elem : e.getStackTrace()) {
|
||||
logService.log(elem.toString());
|
||||
}
|
||||
clientService.showMessage(MessageType.Error, e.getMessage() == null ? "null" : e.getMessage());
|
||||
cacheService.updateGlobalMaps(new ArrayList<>(), new ArrayList<>(), didSaveTextDocumentParams.getTextDocument().getUri());
|
||||
|
||||
} finally {
|
||||
|
@@ -54,6 +54,23 @@ public class ConversionHelper {
|
||||
return diagnostic;
|
||||
}
|
||||
|
||||
public Diagnostic getDiagnosticWithInput(LSPVariable variable, String input, Type type) {
|
||||
Range errorRange = new Range(
|
||||
new Position(variable.getLine() - 1, variable.getCharPosition()), // Startposition
|
||||
new Position(variable.getLine() - 1, textHelper.getEndingCharOfStartingChar(variable.getLine() - 1, variable.getCharPosition(), input)) // Endposition
|
||||
);
|
||||
Diagnostic diagnostic = new Diagnostic(
|
||||
errorRange,
|
||||
//TODO: REMOVE! Temporary Fix because GTV, like TPH can be thrown away in the TypeResolver
|
||||
type.getType().replaceAll("GTV ", ""),
|
||||
DiagnosticSeverity.Hint,
|
||||
"JavaTX Language Server"
|
||||
);
|
||||
diagnostic.setCode(type.isGeneric() ? "GENERIC" : "TYPE");
|
||||
|
||||
return diagnostic;
|
||||
}
|
||||
|
||||
public List<Diagnostic> parseErrorToDiagnostic(List<ParserError> parserErrors){
|
||||
return parserErrors.stream().map(el -> {
|
||||
Range errorRange = new Range(
|
||||
@@ -84,6 +101,23 @@ public class ConversionHelper {
|
||||
}
|
||||
}
|
||||
|
||||
return new DiagnosticsAndTypehints(diagnostics, typeHint);
|
||||
}
|
||||
public DiagnosticsAndTypehints variablesToDiagnosticsAndTypehintsWithInput(ArrayList<LSPVariable> typesOfMethodAndParameters, String input){
|
||||
List<InlayHint> typeHint = new ArrayList<>();
|
||||
ArrayList<Diagnostic> diagnostics = new ArrayList<>();
|
||||
|
||||
for (var variable : typesOfMethodAndParameters) {
|
||||
|
||||
InlayHint inlayHint = getInlayHint(variable);
|
||||
typeHint.add(inlayHint);
|
||||
|
||||
for (var type : variable.getPossibleTypes()) {
|
||||
Diagnostic diagnostic = getDiagnosticWithInput(variable, input, type);
|
||||
diagnostics.add(diagnostic);
|
||||
}
|
||||
}
|
||||
|
||||
return new DiagnosticsAndTypehints(diagnostics, typeHint);
|
||||
}
|
||||
}
|
||||
|
@@ -1,18 +0,0 @@
|
||||
package de.dhbw.helper;
|
||||
|
||||
import de.dhbwstuttgart.syntaxtree.Method;
|
||||
import de.dhbwstuttgart.syntaxtree.SourceFile;
|
||||
import org.eclipse.lsp4j.InlayHint;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.List;
|
||||
|
||||
public class PositionAdjustmentHelper {
|
||||
|
||||
private final TypeResolver typeResolver;
|
||||
|
||||
public PositionAdjustmentHelper(TypeResolver typeResolver) {
|
||||
this.typeResolver = typeResolver;
|
||||
}
|
||||
}
|
@@ -57,7 +57,7 @@ public class TextHelper {
|
||||
|
||||
if(lines.length < line){
|
||||
log.warn("Returning hardcoded Value because the requested Line [" + line + "] does not exist in Text Document.");
|
||||
return startChar+3;
|
||||
return startChar;
|
||||
}
|
||||
|
||||
String[] linesInChar = lines[line].split("");
|
||||
|
@@ -2,14 +2,27 @@ package de.dhbw.helper;
|
||||
|
||||
|
||||
import de.dhbw.model.*;
|
||||
import de.dhbwstuttgart.core.JavaTXCompiler;
|
||||
import de.dhbwstuttgart.languageServerInterface.LanguageServerInterface;
|
||||
import de.dhbwstuttgart.languageServerInterface.model.LanguageServerTransferObject;
|
||||
import de.dhbwstuttgart.parser.scope.JavaClassName;
|
||||
import de.dhbwstuttgart.syntaxtree.SourceFile;
|
||||
import de.dhbwstuttgart.syntaxtree.factory.NameGenerator;
|
||||
import de.dhbwstuttgart.syntaxtree.visual.ASTPrinter;
|
||||
import de.dhbwstuttgart.target.generate.GenericsResult;
|
||||
import de.dhbwstuttgart.typedeployment.TypeInsert;
|
||||
import de.dhbwstuttgart.typedeployment.TypeInsertFactory;
|
||||
import de.dhbwstuttgart.typeinference.result.ResultSet;
|
||||
import org.apache.log4j.LogManager;
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.io.PrintStream;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.*;
|
||||
|
||||
|
||||
@@ -24,9 +37,16 @@ public class TypeResolver {
|
||||
private final GenericUtils genericUtils;
|
||||
private final DuplicationUtils duplicationUtils;
|
||||
private final boolean ENABLE_GENERICS = true;
|
||||
private List<GenericsResult> generatedGenerics = null;
|
||||
private HashMap<String, List<TypeInsert>> inserts;
|
||||
|
||||
|
||||
public HashMap<String, List<TypeInsert>> getInserts() {
|
||||
return inserts;
|
||||
}
|
||||
|
||||
//Somehow you have to reset to the Letter N to keep the naming of the TPHs consistent
|
||||
private final String RESET_TO_LETTER = "N";
|
||||
private final String RESET_TO_LETTER = "A";
|
||||
|
||||
private LanguageServerTransferObject current;
|
||||
|
||||
@@ -58,7 +78,7 @@ public class TypeResolver {
|
||||
|
||||
public void getCompilerInput(String path, String input) throws IOException, URISyntaxException, ClassNotFoundException {
|
||||
LanguageServerInterface languageServer = new LanguageServerInterface();
|
||||
var transferObj = languageServer.getResultSetAndAbstractSyntax(path);
|
||||
var transferObj = languageServer.getResultSetAndAbastractSyntax(path, RESET_TO_LETTER);
|
||||
current = transferObj;
|
||||
}
|
||||
|
||||
@@ -71,6 +91,102 @@ public class TypeResolver {
|
||||
return current;
|
||||
}
|
||||
|
||||
public ArrayList<LSPVariable> infereChangeInput(String pathString) throws IOException, ClassNotFoundException, URISyntaxException {
|
||||
Set<TypeInsert> tips = new HashSet<>();
|
||||
|
||||
for (int i = 0; i < current.getResultSets().size(); i++) {
|
||||
ResultSet tiResult = current.getResultSets().get(i);
|
||||
tips.addAll(TypeInsertFactory.createTypeInsertPoints(current.getAst(), tiResult, generatedGenerics.get(i)));
|
||||
|
||||
}
|
||||
|
||||
HashMap<String, List<TypeInsert>> insertsOnLines = new HashMap<>();
|
||||
|
||||
for (TypeInsert insert : tips) {
|
||||
if (!insertsOnLines.containsKey(insert.point.point.getLine() + " " + insert.point.point.getCharPositionInLine())) {
|
||||
insertsOnLines.put(insert.point.point.getLine() + " " + insert.point.point.getCharPositionInLine(), new ArrayList<>(List.of(insert)));
|
||||
} else {
|
||||
insertsOnLines.get(insert.point.point.getLine() + " " + insert.point.point.getCharPositionInLine()).add(insert);
|
||||
}
|
||||
}
|
||||
|
||||
ArrayList<LSPVariable> variables = new ArrayList<>(insertsOnLines.entrySet().stream().map(el -> new LSPVariable("test", new ArrayList<>(el.getValue().stream().map(typeinsert -> new Type(typeinsert.getInsertString(), typeinsert.point.isGenericClassInsertPoint())).toList()), el.getValue().getFirst().point.point.getLine(), el.getValue().getFirst().point.point.getCharPositionInLine(), el.getValue().get(0).point.point.getStopIndex(), el.getValue().get(0).getResultPair().getLeft())).toList());
|
||||
|
||||
for (var variable : variables) {
|
||||
Set<Type> set = new HashSet<>(variable.getPossibleTypes().stream().map(el -> new Type(el.getType().replaceAll(" ", ""), el.isGeneric())).toList());
|
||||
variable.getPossibleTypes().clear();
|
||||
variable.getPossibleTypes().addAll(set);
|
||||
logger.info(variable.getLine() + ":" + variable.getCharPosition());
|
||||
for (Type t : variable.getPossibleTypes()) {
|
||||
logger.info(t.getType());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return variables;
|
||||
}
|
||||
|
||||
public ArrayList<LSPVariable> infereInput(String pathString, String input, boolean a) throws IOException, ClassNotFoundException, URISyntaxException {
|
||||
System.setOut(new PrintStream(OutputStream.nullOutputStream()));
|
||||
|
||||
var uri = new URI(pathString);
|
||||
var path = Path.of(uri);
|
||||
var file = path.toFile();
|
||||
NameGenerator.resetTo("A");
|
||||
var compiler = new JavaTXCompiler(file, false);
|
||||
|
||||
var parsedSource = compiler.sourceFiles.get(file);
|
||||
var tiResults = compiler.typeInference(file);
|
||||
Set<TypeInsert> tips = new HashSet<>();
|
||||
|
||||
for (var sf : compiler.sourceFiles.values()) {
|
||||
Map<JavaClassName, byte[]> bytecode = compiler.generateBytecode(sf, tiResults);
|
||||
logger.info("Path for Class-File is: " + path.getParent().resolve("out").toFile());
|
||||
Files.createDirectories(path.getParent().resolve("out"));
|
||||
compiler.writeClassFile(bytecode, path.getParent().resolve("out").toFile(), false);
|
||||
|
||||
}
|
||||
|
||||
generatedGenerics = compiler.getGeneratedGenerics().get(compiler.sourceFiles.get(file));
|
||||
for (int i = 0; i < tiResults.size(); i++) {
|
||||
ResultSet tiResult = tiResults.get(i);
|
||||
tips.addAll(TypeInsertFactory.createTypeInsertPoints(parsedSource, tiResult, compiler.getGeneratedGenerics().get(compiler.sourceFiles.get(file)).get(i)));
|
||||
}
|
||||
System.setOut(System.out);
|
||||
this.current = new LanguageServerTransferObject(tiResults, parsedSource, "", compiler.getGeneratedGenerics());
|
||||
|
||||
HashMap<String, List<TypeInsert>> insertsOnLines = new HashMap<>();
|
||||
|
||||
for (TypeInsert insert : tips) {
|
||||
if (!insertsOnLines.containsKey(insert.point.point.getLine() + " " + insert.point.point.getCharPositionInLine())) {
|
||||
insertsOnLines.put(insert.point.point.getLine() + " " + insert.point.point.getCharPositionInLine(), new ArrayList<>(List.of(insert)));
|
||||
} else {
|
||||
insertsOnLines.get(insert.point.point.getLine() + " " + insert.point.point.getCharPositionInLine()).add(insert);
|
||||
}
|
||||
}
|
||||
|
||||
inserts = insertsOnLines;
|
||||
|
||||
insertsOnLines.forEach((key, value) -> value.forEach(type -> logger.info(type.insert(input))));
|
||||
|
||||
ArrayList<LSPVariable> variables = new ArrayList<>(insertsOnLines.entrySet().stream().map(el -> new LSPVariable("test", new ArrayList<>(el.getValue().stream().map(typeinsert -> new Type(typeinsert.getInsertString(), typeinsert.point.isGenericClassInsertPoint())).toList()), el.getValue().getFirst().point.point.getLine(), el.getValue().getFirst().point.point.getCharPositionInLine(), el.getValue().get(0).point.point.getStopIndex(), el.getValue().get(0).getResultPair().getLeft())).toList());
|
||||
|
||||
|
||||
for (var variable : variables) {
|
||||
Set<Type> set = new HashSet<>(variable.getPossibleTypes().stream().map(el -> new Type(el.getType().replaceAll(" ", ""), el.isGeneric())).toList());
|
||||
variable.getPossibleTypes().clear();
|
||||
variable.getPossibleTypes().addAll(set);
|
||||
logger.info(variable.getLine() + ":" + variable.getCharPosition());
|
||||
for (Type t : variable.getPossibleTypes()) {
|
||||
logger.info(t.getType());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return variables;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Zum Erhalt für sowohl Parameter als auch Methoden.
|
||||
*/
|
||||
@@ -156,7 +272,12 @@ public class TypeResolver {
|
||||
}
|
||||
|
||||
public void updateAst(String uri) throws IOException, URISyntaxException, ClassNotFoundException {
|
||||
current = new LanguageServerTransferObject(current.getResultSets(), getNewAst(uri), "", current.getGeneratedGenerics());
|
||||
logger.info("Old AST:");
|
||||
logger.info(ASTPrinter.print(this.current.getAst()));
|
||||
this.current = new LanguageServerTransferObject(current.getResultSets(), getNewAst(uri), "", current.getGeneratedGenerics());
|
||||
|
||||
logger.info("NEW AST:");
|
||||
logger.info(ASTPrinter.print(current.getAst()));
|
||||
}
|
||||
|
||||
}
|
@@ -24,4 +24,15 @@ public class Type {
|
||||
public boolean isGeneric() {
|
||||
return generic;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
return type.equals(obj.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return type;
|
||||
}
|
||||
}
|
||||
|
@@ -4,6 +4,7 @@ import de.dhbw.helper.CodeSnippetOptions;
|
||||
import de.dhbw.helper.TextHelper;
|
||||
import de.dhbw.helper.TypeResolver;
|
||||
import de.dhbw.model.LSPVariable;
|
||||
import de.dhbwstuttgart.typedeployment.TypeInsert;
|
||||
import org.eclipse.lsp4j.Diagnostic;
|
||||
import org.eclipse.lsp4j.InlayHint;
|
||||
|
||||
@@ -25,6 +26,15 @@ public class CacheService {
|
||||
private Path fileRoot = null;
|
||||
private Boolean singleFileOpened = false;
|
||||
private List<LSPVariable> variables = new ArrayList<>();
|
||||
private List<TypeInsert> typeInserts;
|
||||
|
||||
public List<TypeInsert> getTypeInserts() {
|
||||
return typeInserts;
|
||||
}
|
||||
|
||||
public void setTypeInserts(List<TypeInsert> typeInserts) {
|
||||
this.typeInserts = typeInserts;
|
||||
}
|
||||
|
||||
public void updateGlobalMaps(List<Diagnostic> diagnostics, List<InlayHint> typeHint, String uri) {
|
||||
globalDiagnosticsMap.put(uri, diagnostics);
|
||||
|
Reference in New Issue
Block a user