5 Commits

Author SHA1 Message Date
Ruben
f5cdda7625 feat: remove all unnecessary Answers and only enable Syntax Checks 2025-05-14 13:58:48 +02:00
Ruben
39ae1d66bc feat: add compiler jar to LSP in libs Folder 2025-05-13 19:15:41 +02:00
Ruben
6ee0a74b22 feat: change Name and update jar 2025-05-12 19:51:15 +02:00
Ruben
ca6fa89e3f Merge remote-tracking branch 'origin/main' 2025-05-12 19:46:28 +02:00
Ruben
8cde997c15 feat: deactivate display if one Variable is beeing inserted 2025-05-12 19:46:17 +02:00
5 changed files with 10 additions and 328 deletions

View File

@@ -1,71 +1,3 @@
# lspclient README # lspclient README
This is the README for your extension "lspclient". After writing up a brief description, we recommend including the following sections. This is the README for the Java-TX LSP Client.
## Features
Describe specific features of your extension including screenshots of your extension in action. Image paths are relative to this README file.
For example if there is an image subfolder under your extension project workspace:
\!\[feature X\]\(images/feature-x.png\)
> Tip: Many popular extensions utilize animations. This is an excellent way to show off your extension! We recommend short, focused animations that are easy to follow.
## Requirements
If you have any requirements or dependencies, add a section describing those and how to install and configure them.
## Extension Settings
Include if your extension adds any VS Code settings through the `contributes.configuration` extension point.
For example:
This extension contributes the following settings:
* `myExtension.enable`: Enable/disable this extension.
* `myExtension.thing`: Set to `blah` to do something.
## Known Issues
Calling out known issues can help limit users opening duplicate issues against your extension.
## Release Notes
Users appreciate release notes as you update your extension.
### 1.0.0
Initial release of ...
### 1.0.1
Fixed issue #.
### 1.1.0
Added features X, Y, and Z.
---
## Following extension guidelines
Ensure that you've read through the extensions guidelines and follow the best practices for creating your extension.
* [Extension Guidelines](https://code.visualstudio.com/api/references/extension-guidelines)
## Working with Markdown
You can author your README using Visual Studio Code. Here are some useful editor keyboard shortcuts:
* Split the editor (`Cmd+\` on macOS or `Ctrl+\` on Windows and Linux).
* Toggle preview (`Shift+Cmd+V` on macOS or `Shift+Ctrl+V` on Windows and Linux).
* Press `Ctrl+Space` (Windows, Linux, macOS) to see a list of Markdown snippets.
## For more information
* [Visual Studio Code's Markdown Support](http://code.visualstudio.com/docs/languages/markdown)
* [Markdown Syntax Reference](https://help.github.com/articles/markdown-basics/)
**Enjoy!**

View File

@@ -1,12 +1,12 @@
{ {
"name": "lspclient", "name": "lspclient",
"version": "0.0.1", "version": "0.0.2",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "lspclient", "name": "lspclient",
"version": "0.0.1", "version": "0.0.2",
"dependencies": { "dependencies": {
"vscode-languageclient": "^9.0.1" "vscode-languageclient": "^9.0.1"
}, },

View File

@@ -42,10 +42,11 @@ export function activate(context: vscode.ExtensionContext) {
revealOutputChannelOn: 4 revealOutputChannelOn: 4
}; };
// Language Client erstellen und starten // Language Client erstellen und starten
const client = new LanguageClient( const client = new LanguageClient(
'javaLanguageServer', // ID des Clients 'javaTxLanguageServer', // ID des Clients
'Java Language Server', // Name des Clients 'Java-TX Language Server', // Name des Clients
serverOptions, serverOptions,
clientOptions clientOptions
); );

View File

@@ -36,13 +36,8 @@ public class JavaTXTextDocumentService implements org.eclipse.lsp4j.services.Tex
private static final Logger logger = LogManager.getLogger(JavaTXTextDocumentService.class); private static final Logger logger = LogManager.getLogger(JavaTXTextDocumentService.class);
LanguageClient client; LanguageClient client;
HashMap<String, List<InlayHint>> globalInlayHintMap = new HashMap<>();
Boolean currentlyCalculating = false;
HashMap<String, List<Diagnostic>> globalDiagnosticsMap = new HashMap<>();
HashMap<String, String> textDocuments = new HashMap<>(); HashMap<String, String> textDocuments = new HashMap<>();
CodeSnippetOptions codeSnippetOptions = new CodeSnippetOptions(); CodeSnippetOptions codeSnippetOptions = new CodeSnippetOptions();
TextHelper textHelper = new TextHelper();
TypeResolver typeResolver = new TypeResolver();
Path fileRoot = null; Path fileRoot = null;
public void setClient(LanguageClient client) { public void setClient(LanguageClient client) {
@@ -50,8 +45,6 @@ public class JavaTXTextDocumentService implements org.eclipse.lsp4j.services.Tex
} }
public void setFileRoot(List<WorkspaceFolder> root) { public void setFileRoot(List<WorkspaceFolder> root) {
//TODO: Nicht nur das erste Element nehmen sondern alle beachten
fileRoot = Path.of(URI.create(root.get(0).getUri())); fileRoot = Path.of(URI.create(root.get(0).getUri()));
} }
@@ -99,7 +92,7 @@ public class JavaTXTextDocumentService implements org.eclipse.lsp4j.services.Tex
*/ */
@Override @Override
public void didChange(DidChangeTextDocumentParams params) { public void didChange(DidChangeTextDocumentParams params) {
log("[didChange] Client triggered didChange Event.", MessageType.Info); client.logMessage(new MessageParams(MessageType.Info,"[didChange] Client triggered didChange Event."));
AtomicReference<String> summedUp = new AtomicReference<>(""); AtomicReference<String> summedUp = new AtomicReference<>("");
params.getContentChanges().forEach(el -> summedUp.set(summedUp.get() + el.getText())); params.getContentChanges().forEach(el -> summedUp.set(summedUp.get() + el.getText()));
@@ -137,180 +130,20 @@ public class JavaTXTextDocumentService implements org.eclipse.lsp4j.services.Tex
*/ */
@Override @Override
public CompletableFuture<List<? extends TextEdit>> formatting(DocumentFormattingParams params) { public CompletableFuture<List<? extends TextEdit>> formatting(DocumentFormattingParams params) {
log("[formatting] Client requested formatting.", MessageType.Info); return CompletableFuture.completedFuture(Collections.emptyList());
List<TextEdit> edits = new ArrayList<>();
String[] lines = textDocuments.get(params.getTextDocument().getUri()).split("\n");
StringBuilder formattedText = new StringBuilder();
for (String line : lines) {
formattedText.append(line.stripTrailing()).append("\n");
}
TextEdit edit = new TextEdit();
edit.setRange(new Range(new Position(0, 0), new Position(lines.length, 0)));
edit.setNewText(formattedText.toString().trim());
edits.add(edit);
return CompletableFuture.completedFuture(edits);
} }
@Override @Override
public void didClose(DidCloseTextDocumentParams params) { public void didClose(DidCloseTextDocumentParams params) {
} }
private void log(String message, MessageType type) {
client.logMessage(new MessageParams(type, message));
switch (type) {
case Error -> logger.error(message);
case Warning -> logger.warn(message);
case Info -> logger.info(message);
default -> logger.debug(message);
}
}
private InlayHint getInlayHint(LSPVariable variable) {
InlayHint inlayHint = new InlayHint();
String typeDisplay = "";
for (Type type : variable.getPossibleTypes()) {
typeDisplay += " | " + type.getType().replaceAll("GTV ", "");
}
inlayHint.setLabel(typeDisplay.length() > 2 ? typeDisplay.substring(2) : typeDisplay);
inlayHint.setPosition(new Position(variable.getLine() - 1, variable.getCharPosition()));
inlayHint.setKind(InlayHintKind.Parameter);
inlayHint.setPaddingRight(true);
inlayHint.setPaddingRight(true);
return inlayHint;
}
private Diagnostic getDiagnostic(LSPVariable variable, String fileUri, 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(), textDocuments.get(fileUri))) // 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;
}
private void updateGlobalMaps(List<Diagnostic> diagnostics, List<InlayHint> typeHint, String uri) {
globalDiagnosticsMap.put(uri, diagnostics);
globalInlayHintMap.put(uri, typeHint);
}
private void updateClient(LanguageClient client) {
client.refreshInlayHints();
client.refreshDiagnostics();
}
private void startLoading(String taskName, String title, LanguageClient client) {
Either<String, Integer> token = Either.forLeft(taskName);
client.createProgress(new WorkDoneProgressCreateParams(token));
WorkDoneProgressBegin begin = new WorkDoneProgressBegin();
begin.setTitle(title);
begin.setCancellable(false);
begin.setPercentage(0);
ProgressParams beginParams = new ProgressParams(token, Either.forLeft(begin));
client.notifyProgress(beginParams);
}
private void stopLoading(String taskName, String title, LanguageClient client) {
Either<String, Integer> token = Either.forLeft(taskName);
WorkDoneProgressEnd end = new WorkDoneProgressEnd();
end.setMessage(title);
ProgressParams endParams = new ProgressParams(token, Either.forLeft(end));
client.notifyProgress(endParams);
}
@Override @Override
public void didSave(DidSaveTextDocumentParams didSaveTextDocumentParams) { public void didSave(DidSaveTextDocumentParams didSaveTextDocumentParams) {
log("[didSave] Client triggered didSave-Event.", MessageType.Info);
startLoading("compile-task", "Inferring types...", client);
LanguageServerInterface languageServerInterface = new LanguageServerInterface();
if (!currentlyCalculating) {
currentlyCalculating = true;
ArrayList<Diagnostic> diagnostics = new ArrayList<>();
var sWatch = Stopwatch.createUnstarted();
sWatch.start();
try {
if (textDocuments.get(didSaveTextDocumentParams.getTextDocument().getUri()) == null) {
log("[didSave] Input of Text Document is null in TextDocument-Hashmap.", MessageType.Error);
}
ArrayList<LSPVariable> typesOfMethodAndParameters = typeResolver.infereInput(textDocuments.get(didSaveTextDocumentParams.getTextDocument().getUri()), didSaveTextDocumentParams.getTextDocument().getUri());
List<InlayHint> typeHint = new ArrayList<>();
for (var variable : typesOfMethodAndParameters) {
InlayHint inlayHint = getInlayHint(variable);
typeHint.add(inlayHint);
//Diagnostics of Types
for (var type : variable.getPossibleTypes()) {
Diagnostic diagnostic = getDiagnostic(variable, didSaveTextDocumentParams.getTextDocument().getUri(), type);
diagnostics.add(diagnostic);
}
}
updateGlobalMaps(diagnostics, typeHint, didSaveTextDocumentParams.getTextDocument().getUri());
PublishDiagnosticsParams diagnosticsParams = new PublishDiagnosticsParams(didSaveTextDocumentParams.getTextDocument().getUri(), diagnostics);
client.publishDiagnostics(diagnosticsParams);
} catch (Exception e) {
log("[didSave] Error trying to get Inlay-Hints and Diagnostics for Client: " + e.getMessage(), MessageType.Error);
} finally {
currentlyCalculating = false;
sWatch.stop();
log("[didSave] Finished Calculating in [" + sWatch.elapsed().toSeconds() + "s]", MessageType.Info);
}
}
stopLoading("compile-task", "Types successfully inferred", client);
updateClient(client);
ArrayList<File> files = new ArrayList<>();
try (Stream<Path> stream = Files.walk(Paths.get(fileRoot.toUri()))) {
stream.filter(Files::isRegularFile)
.forEach(el -> files.add(el.toFile()));
} catch (IOException e) {
}
} }
@Override @Override
public CompletableFuture<List<InlayHint>> inlayHint(InlayHintParams params) { public CompletableFuture<List<InlayHint>> inlayHint(InlayHintParams params) {
log("[inlayHint] The Client requested Inlay-Hints.", MessageType.Info); return CompletableFuture.completedFuture(Collections.emptyList());}
return CompletableFuture.supplyAsync(() -> globalInlayHintMap.get(params.getTextDocument().getUri()) == null ? Collections.emptyList() : globalInlayHintMap.get(params.getTextDocument().getUri()));
}
@Override @Override
public void willSave(WillSaveTextDocumentParams params) { public void willSave(WillSaveTextDocumentParams params) {
@@ -441,91 +274,7 @@ public class JavaTXTextDocumentService implements org.eclipse.lsp4j.services.Tex
@Override @Override
public CompletableFuture<List<Either<Command, CodeAction>>> codeAction(CodeActionParams params) { public CompletableFuture<List<Either<Command, CodeAction>>> codeAction(CodeActionParams params) {
log("[codeAction] Client requested Insert at Line [" + params.getRange().getStart().getLine() + "] and from Char [" + params.getRange().getStart().getCharacter() + "] to [" + params.getRange().getEnd().getCharacter() + "].", MessageType.Info); return CompletableFuture.completedFuture(Collections.emptyList());
List<Either<Command, CodeAction>> actions = new ArrayList<>();
List<Diagnostic> diagnosticInCurrentDocument = params.getContext().getDiagnostics();
Range requestedRange = params.getRange();
//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 rangeOfInsert = params.getRange();
String documentUri = params.getTextDocument().getUri();
for (Diagnostic typeDiagnostic : diagnosticsOverlappingHover) {
try {
String typeWithReplacedVariable = typeDiagnostic.getMessage() +
" " +
textHelper.getTextOfChars(
textDocuments.get(params.getTextDocument().getUri()),
rangeOfInsert.getStart().getLine(),
rangeOfInsert.getStart().getCharacter(),
rangeOfInsert.getEnd().getCharacter()
);
ArrayList<TextEdit> listOfChanges = new ArrayList<>();
if (typeDiagnostic.getCode().getLeft().equals("GENERIC")) {
for (var diagnostic : globalDiagnosticsMap.get(params.getTextDocument().getUri())) {
if (diagnostic.getMessage().contains(typeDiagnostic.getMessage()) || typeDiagnostic.getMessage().contains(diagnostic.getMessage())) {
String genericType = diagnostic.getMessage() +
" " +
textHelper.getTextOfChars(
textDocuments.get(params.getTextDocument().getUri()),
diagnostic.getRange().getStart().getLine(),
diagnostic.getRange().getStart().getCharacter(),
diagnostic.getRange().getEnd().getCharacter()
);
listOfChanges.add(new TextEdit(diagnostic.getRange(), genericType));
}
}
} else {
listOfChanges.add(new TextEdit(rangeOfInsert, typeWithReplacedVariable));
}
var isTypeImported = false;
if (!typeDiagnostic.getCode().getLeft().equalsIgnoreCase("generic")) {
isTypeImported = typeResolver.isTypeImported(textDocuments.get(params.getTextDocument().getUri()), typeDiagnostic.getMessage());
}
if (!isTypeImported && !typeDiagnostic.getMessage().equals("void") && !typeDiagnostic.getCode().getLeft().equals("GENERIC")) {
Range importRange = new Range(new Position(0, 0), new Position(0, 0));
var typeWithoutGenerics = typeDiagnostic.getMessage().contains("<") ? typeDiagnostic.getMessage().substring(0, typeDiagnostic.getMessage().indexOf('<')) : typeDiagnostic.getMessage();
listOfChanges.add(new TextEdit(importRange, "import " + typeWithoutGenerics + ";\n"));
}
Map<String, List<TextEdit>> changes = new HashMap<>();
changes.put(documentUri, listOfChanges);
WorkspaceEdit edit = new WorkspaceEdit();
edit.setChanges(changes);
CodeAction action = new CodeAction("Insert " + typeDiagnostic.getMessage());
action.setKind(CodeActionKind.QuickFix);
action.setEdit(edit);
actions.add(Either.forRight(action));
} catch (Exception e) {
log("Error creating Actions, returning empty List. The Error was: " + e.getMessage(), MessageType.Error);
}
}
return CompletableFuture.completedFuture(actions);
}
private boolean rangesOverlap(Range range1, Range range2) {
int start1 = range1.getStart().getLine() * 1000 + range1.getStart().getCharacter();
int end1 = range1.getEnd().getLine() * 1000 + range1.getEnd().getCharacter();
int start2 = range2.getStart().getLine() * 1000 + range2.getStart().getCharacter();
int end2 = range2.getEnd().getLine() * 1000 + range2.getEnd().getCharacter();
return start1 <= end2 && start2 <= end1;
} }
@Override @Override