Compare commits
2 Commits
positionAd
...
a53b79f91d
Author | SHA1 | Date | |
---|---|---|---|
|
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 ChangeHandler changeHandler;
|
||||||
private final TextHelper textHelper;
|
private final TextHelper textHelper;
|
||||||
private final CodeActionHandler codeActionHandler;
|
private final CodeActionHandler codeActionHandler;
|
||||||
private final InlayHintAdjusterDiffUtils inlayHintAdjusterDiffUtils;
|
|
||||||
|
|
||||||
LanguageClient client;
|
LanguageClient client;
|
||||||
HashMap<String, String> textDocuments = new HashMap<>();
|
HashMap<String, String> textDocuments = new HashMap<>();
|
||||||
@@ -48,7 +47,6 @@ public class JavaTXTextDocumentService implements org.eclipse.lsp4j.services.Tex
|
|||||||
|
|
||||||
public JavaTXTextDocumentService() {
|
public JavaTXTextDocumentService() {
|
||||||
this.textHelper = new TextHelper();
|
this.textHelper = new TextHelper();
|
||||||
this.inlayHintAdjusterDiffUtils = new InlayHintAdjusterDiffUtils();
|
|
||||||
this.cacheService = new CacheService();
|
this.cacheService = new CacheService();
|
||||||
this.clientService = new ClientService(null);
|
this.clientService = new ClientService(null);
|
||||||
this.typeResolver = new TypeResolver();
|
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.parserService = new ParserService(conversionHelper, clientService, cacheService);
|
||||||
this.codeActionHandler = new CodeActionHandler(textHelper, textDocumentService, cacheService, typeResolver, logService);
|
this.codeActionHandler = new CodeActionHandler(textHelper, textDocumentService, cacheService, typeResolver, logService);
|
||||||
this.saveHandler = new SaveHandler(typeResolver, textDocumentService, logService, cacheService, conversionHelper, clientService, parserService);
|
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) {
|
public void setClient(LanguageClient client) {
|
||||||
|
@@ -26,9 +26,8 @@ public class ChangeHandler {
|
|||||||
private final TypeResolver typeResolver;
|
private final TypeResolver typeResolver;
|
||||||
private final CacheService cacheService;
|
private final CacheService cacheService;
|
||||||
private final LogService logService;
|
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.textDocumentService = textDocumentService;
|
||||||
this.parserService = parserService;
|
this.parserService = parserService;
|
||||||
this.conversionHelper = conversionHelper;
|
this.conversionHelper = conversionHelper;
|
||||||
@@ -36,7 +35,6 @@ public class ChangeHandler {
|
|||||||
this.typeResolver = typeResolver;
|
this.typeResolver = typeResolver;
|
||||||
this.cacheService = cacheService;
|
this.cacheService = cacheService;
|
||||||
this.logService = logService;
|
this.logService = logService;
|
||||||
this.inlayHintAdjusterDiffUtils = inlayHintAdjusterDiffUtils;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void didChange(DidChangeTextDocumentParams params) {
|
public void didChange(DidChangeTextDocumentParams params) {
|
||||||
@@ -50,13 +48,11 @@ public class ChangeHandler {
|
|||||||
DocumentChanges documentChanges = textDocumentService.calculateDifference(currentText, summedUp.get());
|
DocumentChanges documentChanges = textDocumentService.calculateDifference(currentText, summedUp.get());
|
||||||
HashMap<LineCharPosition, String> preciseChanges = documentChanges.getPreciseChanges();
|
HashMap<LineCharPosition, String> preciseChanges = documentChanges.getPreciseChanges();
|
||||||
HashMap<Integer, List<String>> textChanges;
|
HashMap<Integer, List<String>> textChanges;
|
||||||
ArrayList<Integer> offsetPerLine = documentChanges.getOffsetPerLine();
|
|
||||||
|
|
||||||
|
|
||||||
//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
|
//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())){
|
if(cacheService.getLastSavedFiles().containsKey(params.getTextDocument().getUri())){
|
||||||
DocumentChanges changesFromOldSave = textDocumentService.calculateDifference(cacheService.getLastSavedFiles().get(params.getTextDocument().getUri()), summedUp.get());
|
DocumentChanges changesFromOldSave = textDocumentService.calculateDifference(cacheService.getLastSavedFiles().get(params.getTextDocument().getUri()), summedUp.get());;
|
||||||
offsetPerLine = changesFromOldSave.getOffsetPerLine();
|
|
||||||
textChanges = changesFromOldSave.getTextChanges();
|
textChanges = changesFromOldSave.getTextChanges();
|
||||||
preciseChanges = changesFromOldSave.getPreciseChanges();
|
preciseChanges = changesFromOldSave.getPreciseChanges();
|
||||||
logService.log(textChanges.values().stream().map(el -> String.join(", ", el)).collect(Collectors.joining("\n")));
|
logService.log(textChanges.values().stream().map(el -> String.join(", ", el)).collect(Collectors.joining("\n")));
|
||||||
@@ -78,6 +74,7 @@ public class ChangeHandler {
|
|||||||
bw.write(summedUp.get());
|
bw.write(summedUp.get());
|
||||||
bw.close();
|
bw.close();
|
||||||
typeResolver.updateAst(tempFile.toURI().getPath());
|
typeResolver.updateAst(tempFile.toURI().getPath());
|
||||||
|
tempFile.delete();
|
||||||
}catch (Exception e){
|
}catch (Exception e){
|
||||||
logService.log(e.getMessage());
|
logService.log(e.getMessage());
|
||||||
}
|
}
|
||||||
@@ -95,7 +92,7 @@ public class ChangeHandler {
|
|||||||
logService.log("[didChange] Input of Text Document is null in TextDocument-Hashmap.", MessageType.Error);
|
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.infereInput(params.getTextDocument().getUri());
|
||||||
|
|
||||||
|
|
||||||
DiagnosticsAndTypehints diagnosticsAndTypehints = conversionHelper.variablesToDiagnosticsAndTypehints(typesOfMethodAndParameters, params.getTextDocument().getUri());
|
DiagnosticsAndTypehints diagnosticsAndTypehints = conversionHelper.variablesToDiagnosticsAndTypehints(typesOfMethodAndParameters, params.getTextDocument().getUri());
|
||||||
@@ -147,31 +144,4 @@ public class ChangeHandler {
|
|||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
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()));
|
|
||||||
}
|
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
@@ -46,7 +46,7 @@ public class SaveHandler {
|
|||||||
logService.log("[didSave] Input of Text Document is null in TextDocument-Hashmap.", MessageType.Error);
|
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());
|
||||||
cacheService.setVariables(variables);
|
cacheService.setVariables(variables);
|
||||||
|
|
||||||
DiagnosticsAndTypehints diagnosticsAndTypehints = conversionHelper.variablesToDiagnosticsAndTypehints(variables, didSaveTextDocumentParams.getTextDocument().getUri());
|
DiagnosticsAndTypehints diagnosticsAndTypehints = conversionHelper.variablesToDiagnosticsAndTypehints(variables, didSaveTextDocumentParams.getTextDocument().getUri());
|
||||||
@@ -64,7 +64,11 @@ public class SaveHandler {
|
|||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logService.log("[didSave] Error trying to get Inlay-Hints and Diagnostics for Client: " + e.getMessage(), MessageType.Error);
|
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());
|
cacheService.updateGlobalMaps(new ArrayList<>(), new ArrayList<>(), didSaveTextDocumentParams.getTextDocument().getUri());
|
||||||
|
|
||||||
} finally {
|
} finally {
|
||||||
|
@@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
@@ -2,14 +2,21 @@ package de.dhbw.helper;
|
|||||||
|
|
||||||
|
|
||||||
import de.dhbw.model.*;
|
import de.dhbw.model.*;
|
||||||
|
import de.dhbwstuttgart.core.JavaTXCompiler;
|
||||||
import de.dhbwstuttgart.languageServerInterface.LanguageServerInterface;
|
import de.dhbwstuttgart.languageServerInterface.LanguageServerInterface;
|
||||||
import de.dhbwstuttgart.languageServerInterface.model.LanguageServerTransferObject;
|
import de.dhbwstuttgart.languageServerInterface.model.LanguageServerTransferObject;
|
||||||
|
import de.dhbwstuttgart.parser.scope.JavaClassName;
|
||||||
import de.dhbwstuttgart.syntaxtree.SourceFile;
|
import de.dhbwstuttgart.syntaxtree.SourceFile;
|
||||||
|
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.LogManager;
|
||||||
import org.apache.log4j.Logger;
|
import org.apache.log4j.Logger;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.net.URI;
|
||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
|
import java.nio.file.Path;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
|
|
||||||
@@ -58,7 +65,7 @@ public class TypeResolver {
|
|||||||
|
|
||||||
public void getCompilerInput(String path, String input) throws IOException, URISyntaxException, ClassNotFoundException {
|
public void getCompilerInput(String path, String input) throws IOException, URISyntaxException, ClassNotFoundException {
|
||||||
LanguageServerInterface languageServer = new LanguageServerInterface();
|
LanguageServerInterface languageServer = new LanguageServerInterface();
|
||||||
var transferObj = languageServer.getResultSetAndAbstractSyntax(path);
|
var transferObj = languageServer.getResultSetAndAbastractSyntax(path, RESET_TO_LETTER);
|
||||||
current = transferObj;
|
current = transferObj;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -71,6 +78,56 @@ public class TypeResolver {
|
|||||||
return current;
|
return current;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public ArrayList<LSPVariable> infereInput(String pathString) throws IOException, ClassNotFoundException, URISyntaxException {
|
||||||
|
var uri = new URI(pathString);
|
||||||
|
var path = Path.of(uri);
|
||||||
|
var file = path.toFile();
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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)));
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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.
|
* Zum Erhalt für sowohl Parameter als auch Methoden.
|
||||||
*/
|
*/
|
||||||
|
@@ -24,4 +24,15 @@ public class Type {
|
|||||||
public boolean isGeneric() {
|
public boolean isGeneric() {
|
||||||
return generic;
|
return generic;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
return type.equals(obj.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return type;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user