Compare commits
19 Commits
0.0.2
...
c8fa373972
Author | SHA1 | Date | |
---|---|---|---|
|
c8fa373972 | ||
|
f1f0fb8bf3 | ||
|
d6dd414aba | ||
|
2b6fc2fc91 | ||
|
762b86be85 | ||
|
38166cf6f2 | ||
|
c012471a22 | ||
|
0091c9ef71 | ||
|
f6ed883ddb | ||
|
ae3b5e80d0 | ||
|
4226623abc | ||
|
637478b1c3 | ||
|
24c5cd823f | ||
|
26eedc5b32 | ||
|
c21f09496e | ||
|
39ae1d66bc | ||
|
6ee0a74b22 | ||
|
ca6fa89e3f | ||
|
8cde997c15 |
2
Clients/Intellij/.idea/misc.xml
generated
2
Clients/Intellij/.idea/misc.xml
generated
@@ -1,7 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_23" default="true" project-jdk-name="openjdk-23" project-jdk-type="JavaSDK">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" project-jdk-name="openjdk-23" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
@@ -1,28 +0,0 @@
|
||||
plugins {
|
||||
id 'java'
|
||||
id 'org.jetbrains.intellij.platform' version '2.2.1'
|
||||
|
||||
}
|
||||
|
||||
group = 'org.example'
|
||||
version = '1.0-SNAPSHOT'
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
maven { url "https://jitpack.io" }
|
||||
|
||||
intellijPlatform {
|
||||
defaultRepositories()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
dependencies {
|
||||
testImplementation platform('org.junit:junit-bom:5.10.0')
|
||||
testImplementation 'org.junit.jupiter:junit-jupiter'
|
||||
implementation "com.github.Ballerina-Platform.lsp4intellij:lsp4intellij:master-SNAPSHOT"
|
||||
}
|
||||
|
||||
test {
|
||||
useJUnitPlatform()
|
||||
}
|
41
Clients/Intellij/build.gradle.kts
Normal file
41
Clients/Intellij/build.gradle.kts
Normal file
@@ -0,0 +1,41 @@
|
||||
plugins {
|
||||
id("org.jetbrains.intellij") version "1.17.3"
|
||||
kotlin("jvm") version "1.9.0"
|
||||
}
|
||||
|
||||
group = "com.example"
|
||||
version = "1.0.0"
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
maven { url = uri("https://jitpack.io") }
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation(kotlin("stdlib"))
|
||||
implementation("com.github.ballerina-platform:lsp4intellij:0.9.0")
|
||||
}
|
||||
|
||||
|
||||
java {
|
||||
sourceCompatibility = JavaVersion.VERSION_17
|
||||
targetCompatibility = JavaVersion.VERSION_17
|
||||
}
|
||||
|
||||
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
|
||||
kotlinOptions {
|
||||
jvmTarget = "17"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
intellij {
|
||||
version.set("2023.3")
|
||||
}
|
||||
|
||||
tasks {
|
||||
patchPluginXml {
|
||||
sinceBuild.set("233")
|
||||
untilBuild.set("233.*")
|
||||
}
|
||||
}
|
@@ -0,0 +1,14 @@
|
||||
package kotlin.com.example.lsp;
|
||||
|
||||
import com.intellij.openapi.application.PreloadingActivity;
|
||||
import com.intellij.openapi.progress.ProgressIndicator;
|
||||
import org.wso2.lsp4intellij.IntellijLanguageClient;
|
||||
import org.wso2.lsp4intellij.client.languageserver.serverdefinition.RawCommandServerDefinition;
|
||||
|
||||
public class PreloadActivityTX extends PreloadingActivity {
|
||||
@Override
|
||||
public void preload(ProgressIndicator indicator) {
|
||||
String[] command = new String[]{"java", "-jar", "/JavaTXLanguageServer-1.0-SNAPSHOT-jar-with-dependencies.jar"};
|
||||
IntellijLanguageClient.addServerDefinition(new RawCommandServerDefinition("jav", command));
|
||||
}
|
||||
}
|
@@ -13,7 +13,7 @@ see also jetbrains documentation: https://plugins.jetbrains.com/docs/intellij/pl
|
||||
|
||||
<extensions defaultExtensionNs="com.intellij">
|
||||
<!-- register a preloading activity. You need to init IntellijLanguageClient with your config, see readme -->
|
||||
<preloadingActivity implementation="your.plugin.MyPreloadingActivity" id="your.plugin.MyPreloadingActivity"/>
|
||||
<preloadingActivity implementation="kotlin.com.example.lsp.PreloadActivityTX" id="kotlin.com.example.lsp.PreloadActivityTX"/>
|
||||
|
||||
<!-- register intellijLanguageClient as a Service OR as a plugin component (see readme)... -->
|
||||
<applicationService serviceImplementation="org.wso2.lsp4intellij.IntellijLanguageClient"/>
|
||||
|
@@ -9,3 +9,7 @@ vsc-extension-quickstart.md
|
||||
**/*.map
|
||||
**/*.ts
|
||||
**/.vscode-test.*
|
||||
|
||||
!JavaTXLanguageServer-1.0-SNAPSHOT-jar-with-dependencies.jar
|
||||
|
||||
!JavaTXLanguageServer-1.0-SNAPSHOT-jar-with-dependencies.jar
|
||||
|
Binary file not shown.
@@ -1,71 +1,3 @@
|
||||
# lspclient README
|
||||
|
||||
This is the README for your extension "lspclient". After writing up a brief description, we recommend including the following sections.
|
||||
|
||||
## 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!**
|
||||
This is the README for the Java-TX LSP Client.
|
4
Clients/VisualStudioCode/package-lock.json
generated
4
Clients/VisualStudioCode/package-lock.json
generated
@@ -1,12 +1,12 @@
|
||||
{
|
||||
"name": "lspclient",
|
||||
"version": "0.0.1",
|
||||
"version": "0.0.2",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "lspclient",
|
||||
"version": "0.0.1",
|
||||
"version": "0.0.2",
|
||||
"dependencies": {
|
||||
"vscode-languageclient": "^9.0.1"
|
||||
},
|
||||
|
@@ -42,10 +42,11 @@ export function activate(context: vscode.ExtensionContext) {
|
||||
revealOutputChannelOn: 4
|
||||
};
|
||||
|
||||
|
||||
// Language Client erstellen und starten
|
||||
const client = new LanguageClient(
|
||||
'javaLanguageServer', // ID des Clients
|
||||
'Java Language Server', // Name des Clients
|
||||
'javaTxLanguageServer', // ID des Clients
|
||||
'Java-TX Language Server', // Name des Clients
|
||||
serverOptions,
|
||||
clientOptions
|
||||
);
|
||||
|
@@ -20,6 +20,11 @@
|
||||
<artifactId>antlr4</artifactId>
|
||||
<version>4.11.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.github.java-diff-utils</groupId>
|
||||
<artifactId>java-diff-utils</artifactId>
|
||||
<version>4.12</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>log4j</groupId>
|
||||
<artifactId>log4j</artifactId>
|
||||
@@ -75,6 +80,12 @@
|
||||
<version>0.1</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<properties>
|
||||
<!-- Setzt Java 21 als globale Version -->
|
||||
<maven.compiler.source>21</maven.compiler.source>
|
||||
<maven.compiler.target>21</maven.compiler.target>
|
||||
<maven.compiler.release>21</maven.compiler.release>
|
||||
</properties>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
@@ -96,6 +107,7 @@
|
||||
<configuration>
|
||||
<source>21</source>
|
||||
<target>21</target>
|
||||
<release>21</release>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
|
@@ -1,13 +1,18 @@
|
||||
package de.dhbw;
|
||||
|
||||
import com.google.common.base.Stopwatch;
|
||||
import de.dhbw.service.CacheService;
|
||||
import de.dhbw.service.ClientService;
|
||||
import de.dhbw.service.LogService;
|
||||
import de.dhbw.handler.SaveHandler;
|
||||
import de.dhbw.helper.CodeSnippetOptions;
|
||||
import de.dhbw.helper.ConversionHelper;
|
||||
import de.dhbw.helper.TextHelper;
|
||||
import de.dhbw.helper.TypeResolver;
|
||||
import de.dhbw.model.LSPVariable;
|
||||
import de.dhbw.model.LineCharPosition;
|
||||
import de.dhbw.model.SnippetWithName;
|
||||
import de.dhbw.model.Type;
|
||||
import de.dhbwstuttgart.languageServerInterface.LanguageServerInterface;
|
||||
import de.dhbwstuttgart.languageServerInterface.ParserInterface;
|
||||
import de.dhbwstuttgart.languageServerInterface.model.ParserError;
|
||||
import org.apache.log4j.LogManager;
|
||||
@@ -17,16 +22,11 @@ import org.eclipse.lsp4j.jsonrpc.messages.Either;
|
||||
import org.eclipse.lsp4j.services.LanguageClient;
|
||||
import org.eclipse.lsp4j.services.TextDocumentService;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* Handles Actions in Documents, such as Autocompletion, Change-Events and Syntax-Checks
|
||||
@@ -34,6 +34,15 @@ import java.util.stream.Stream;
|
||||
public class JavaTXTextDocumentService implements org.eclipse.lsp4j.services.TextDocumentService {
|
||||
|
||||
|
||||
private final SaveHandler saveHandler;
|
||||
private final TypeResolver typeResolver;
|
||||
private final de.dhbw.service.TextDocumentService textDocumentService;
|
||||
private final LogService logService;
|
||||
private final ClientService clientService;
|
||||
private final CacheService cacheService;
|
||||
private final ConversionHelper conversionHelper;
|
||||
TextHelper textHelper = new TextHelper();
|
||||
|
||||
private static final Logger logger = LogManager.getLogger(JavaTXTextDocumentService.class);
|
||||
LanguageClient client;
|
||||
HashMap<String, List<InlayHint>> globalInlayHintMap = new HashMap<>();
|
||||
@@ -41,16 +50,31 @@ public class JavaTXTextDocumentService implements org.eclipse.lsp4j.services.Tex
|
||||
HashMap<String, List<Diagnostic>> globalDiagnosticsMap = new HashMap<>();
|
||||
HashMap<String, String> textDocuments = new HashMap<>();
|
||||
CodeSnippetOptions codeSnippetOptions = new CodeSnippetOptions();
|
||||
TextHelper textHelper = new TextHelper();
|
||||
TypeResolver typeResolver = new TypeResolver();
|
||||
Boolean dontShowHints = false;
|
||||
Path fileRoot = null;
|
||||
Boolean singleFileOpened = false;
|
||||
List<LSPVariable> variables = new ArrayList<>();
|
||||
|
||||
public JavaTXTextDocumentService() {
|
||||
this.cacheService = new CacheService();
|
||||
this.clientService = new ClientService(null);
|
||||
this.typeResolver = new TypeResolver();
|
||||
this.textDocumentService = new de.dhbw.service.TextDocumentService();
|
||||
this.conversionHelper = new ConversionHelper(textHelper, textDocumentService);
|
||||
this.logService = new LogService(clientService);
|
||||
this.saveHandler = new SaveHandler(typeResolver, textDocumentService, logService, cacheService, conversionHelper, clientService);
|
||||
}
|
||||
|
||||
public void setClient(LanguageClient client) {
|
||||
this.client = client;
|
||||
clientService.setClient(client);
|
||||
}
|
||||
|
||||
public void setFileRoot(List<WorkspaceFolder> root) {
|
||||
|
||||
if (root == null) {
|
||||
singleFileOpened = true;
|
||||
}
|
||||
//TODO: Nicht nur das erste Element nehmen sondern alle beachten
|
||||
fileRoot = Path.of(URI.create(root.get(0).getUri()));
|
||||
}
|
||||
@@ -89,8 +113,71 @@ public class JavaTXTextDocumentService implements org.eclipse.lsp4j.services.Tex
|
||||
@Override
|
||||
public void didOpen(DidOpenTextDocumentParams params) {
|
||||
textDocuments.put(params.getTextDocument().getUri(), params.getTextDocument().getText());
|
||||
textDocumentService.saveFileWithUri(params.getTextDocument().getUri(), params.getTextDocument().getText());
|
||||
}
|
||||
|
||||
|
||||
public ArrayList<String> difference(String first, String second) {
|
||||
ArrayList<String> result = new ArrayList<>();
|
||||
|
||||
int i = 0, j = 0;
|
||||
int startDiff = -1;
|
||||
|
||||
while (j < second.length()) {
|
||||
if (i < first.length() && first.charAt(i) == second.charAt(j)) {
|
||||
if (startDiff != -1) {
|
||||
result.add(second.substring(startDiff, j));
|
||||
startDiff = -1;
|
||||
}
|
||||
i++;
|
||||
j++;
|
||||
} else {
|
||||
if (startDiff == -1) {
|
||||
startDiff = j;
|
||||
}
|
||||
j++;
|
||||
}
|
||||
}
|
||||
|
||||
if (startDiff != -1) {
|
||||
result.add(second.substring(startDiff));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public Map<LineCharPosition, String> differenceLinePos(String first, String second, int line) {
|
||||
Map<LineCharPosition, String> result = new HashMap<>();
|
||||
|
||||
int i = 0, j = 0;
|
||||
int startDiff = -1;
|
||||
|
||||
while (j < second.length()) {
|
||||
if (i < first.length() && first.charAt(i) == second.charAt(j)) {
|
||||
if (startDiff != -1) {
|
||||
String diff = second.substring(startDiff, j);
|
||||
result.put(new LineCharPosition(line, startDiff), diff);
|
||||
startDiff = -1;
|
||||
}
|
||||
i++;
|
||||
j++;
|
||||
} else {
|
||||
if (startDiff == -1) {
|
||||
startDiff = j;
|
||||
}
|
||||
j++;
|
||||
}
|
||||
}
|
||||
|
||||
if (startDiff != -1) {
|
||||
String diff = second.substring(startDiff);
|
||||
result.put(new LineCharPosition(line, startDiff), diff);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Handles didChange-Event.
|
||||
* updates textDocument-State on Server and run Syntax-Check. If an Error is found it will get displayed as a Diagnostic.
|
||||
@@ -101,9 +188,39 @@ public class JavaTXTextDocumentService implements org.eclipse.lsp4j.services.Tex
|
||||
public void didChange(DidChangeTextDocumentParams params) {
|
||||
log("[didChange] Client triggered didChange Event.", MessageType.Info);
|
||||
|
||||
|
||||
//TODO: Regenerate if Line-Count does not match -> Return new AST and see where the Positions are instead of calculating them from changes in Text-Document. Even possible?
|
||||
|
||||
String currentText = textDocuments.get(params.getTextDocument().getUri());
|
||||
ArrayList<String> currentTextLines = new ArrayList<>(Arrays.stream(currentText.split("\n")).toList());
|
||||
HashMap<LineCharPosition, String> preciseChanges = new HashMap<>();
|
||||
String newText = params.getContentChanges().getFirst().getText();
|
||||
ArrayList<String> newTextLines = new ArrayList<>(Arrays.stream(newText.split("\n")).toList());
|
||||
|
||||
ArrayList<Integer> offsetPerLine = new ArrayList<>();
|
||||
HashMap<Integer, List<String>> textChanges = new HashMap<>();
|
||||
int index = 0;
|
||||
for (String newTextLine : newTextLines) {
|
||||
if (!(currentTextLines.size() > index)) {
|
||||
offsetPerLine.add(0);
|
||||
} else {
|
||||
|
||||
|
||||
Map<LineCharPosition, String> lineDiffs = differenceLinePos(currentTextLines.get(index), newTextLine, index);
|
||||
preciseChanges.putAll(lineDiffs);
|
||||
|
||||
textChanges.put(index, difference(currentTextLines.get(index), newTextLine).stream().map(el -> el.replaceAll(" ", "")).toList());
|
||||
offsetPerLine.add(newTextLine.length() - currentTextLines.get(index).length());
|
||||
}
|
||||
|
||||
index++;
|
||||
}
|
||||
|
||||
|
||||
AtomicReference<String> summedUp = new AtomicReference<>("");
|
||||
params.getContentChanges().forEach(el -> summedUp.set(summedUp.get() + el.getText()));
|
||||
textDocuments.put(params.getTextDocument().getUri(), summedUp.get());
|
||||
textDocumentService.saveFileWithUri(params.getTextDocument().getUri(), summedUp.get());
|
||||
|
||||
String input = summedUp.get();
|
||||
|
||||
@@ -111,7 +228,7 @@ public class JavaTXTextDocumentService implements org.eclipse.lsp4j.services.Tex
|
||||
|
||||
List<ParserError> parserErrors = parserInterface.getParseErrors(input);
|
||||
|
||||
List<Diagnostic> diagnostics = parserErrors.stream().map(el -> {
|
||||
List<Diagnostic> diagnosticsList = parserErrors.stream().map(el -> {
|
||||
Range errorRange = new Range(
|
||||
new Position(el.getLine() - 1, el.getCharPositionInLine()), // Startposition
|
||||
new Position(el.getLine() - 1, el.getEndCharPosition()) // Endposition
|
||||
@@ -125,9 +242,89 @@ public class JavaTXTextDocumentService implements org.eclipse.lsp4j.services.Tex
|
||||
}).toList();
|
||||
|
||||
|
||||
PublishDiagnosticsParams diagnosticsParams = new PublishDiagnosticsParams(params.getTextDocument().getUri(), diagnostics);
|
||||
client.publishDiagnostics(diagnosticsParams);
|
||||
// PublishDiagnosticsParams diagnosticsParams = new PublishDiagnosticsParams(params.getTextDocument().getUri(), diagnostics);
|
||||
// client.publishDiagnostics(diagnosticsParams);
|
||||
|
||||
|
||||
List<String> combinedList = new ArrayList<>();
|
||||
|
||||
for (List<String> list : textChanges.values()) {
|
||||
combinedList.addAll(list);
|
||||
}
|
||||
|
||||
typeResolver.reduceCurrent(preciseChanges, variables, client);
|
||||
//Reduce and display Typehints and Diagnostics
|
||||
|
||||
if (!currentlyCalculating) {
|
||||
currentlyCalculating = true;
|
||||
|
||||
ArrayList<Diagnostic> diagnostics = new ArrayList<>();
|
||||
var sWatch = Stopwatch.createUnstarted();
|
||||
sWatch.start();
|
||||
|
||||
try {
|
||||
if (textDocuments.get(params.getTextDocument().getUri()) == null) {
|
||||
log("[didSave] Input of Text Document is null in TextDocument-Hashmap.", MessageType.Error);
|
||||
}
|
||||
ArrayList<LSPVariable> typesOfMethodAndParameters = typeResolver.infereInput(textDocuments.get(params.getTextDocument().getUri()), params.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, params.getTextDocument().getUri(), type);
|
||||
diagnostics.add(diagnostic);
|
||||
}
|
||||
}
|
||||
|
||||
updateGlobalMaps(diagnostics, typeHint, params.getTextDocument().getUri());
|
||||
|
||||
|
||||
} catch (Exception e) {
|
||||
log("[didSave] Error trying to get Inlay-Hints and Diagnostics for Client: " + e.getMessage(), MessageType.Error);
|
||||
client.showMessage(new MessageParams(MessageType.Error, e.getMessage()));
|
||||
updateGlobalMaps(new ArrayList<>(), new ArrayList<>(), params.getTextDocument().getUri());
|
||||
|
||||
} finally {
|
||||
currentlyCalculating = false;
|
||||
sWatch.stop();
|
||||
log("[didSave] Finished Calculating in [" + sWatch.elapsed().toSeconds() + "s]", MessageType.Info);
|
||||
}
|
||||
}
|
||||
|
||||
for (var inlayHint : globalInlayHintMap.get(params.getTextDocument().getUri())) {
|
||||
inlayHint.getPosition().setCharacter(inlayHint.getPosition().getCharacter() + offsetPerLine.get(inlayHint.getPosition().getLine()));
|
||||
}
|
||||
|
||||
for (var inlayHint : globalDiagnosticsMap.get(params.getTextDocument().getUri())) {
|
||||
inlayHint.getRange().getStart().setCharacter(inlayHint.getRange().getStart().getCharacter() + offsetPerLine.get(inlayHint.getRange().getStart().getLine()));
|
||||
inlayHint.getRange().getEnd().setCharacter(inlayHint.getRange().getEnd().getCharacter() + offsetPerLine.get(inlayHint.getRange().getEnd().getLine()));
|
||||
}
|
||||
|
||||
for (var textChangeLine : textChanges.entrySet()) {
|
||||
log(textChangeLine.getKey() + " - " + String.join(",", textChangeLine.getValue()), MessageType.Info);
|
||||
}
|
||||
|
||||
|
||||
globalInlayHintMap.put(params.getTextDocument().getUri(), globalInlayHintMap.get(params.getTextDocument().getUri()).stream().filter(el -> {
|
||||
//TODO: Hier das Label aufspalten an | weil die Typehints immer mit und getrennt sind und so jeder Typhint für sich durchlaufen werden kann.
|
||||
log(el.getLabel().getLeft().replaceAll(" ", "") + "<>" + String.join(",", textChanges.get(el.getPosition().getLine())) + ": " + (!textChanges.get(el.getPosition().getLine()).contains(el.getLabel().getLeft().replaceAll(" ", "")) ? "true" : "false"), MessageType.Info);
|
||||
return !textChanges.get(el.getPosition().getLine()).contains(el.getLabel().getLeft().replaceAll(" ", ""));
|
||||
}).toList());
|
||||
|
||||
globalDiagnosticsMap.put(params.getTextDocument().getUri(), globalDiagnosticsMap.get(params.getTextDocument().getUri()).stream().filter(el -> {
|
||||
log(el.getMessage().replaceAll(" ", "") + "<>" + String.join(",", textChanges.get(el.getRange().getStart().getLine())) + ": " + (!textChanges.get(el.getRange().getStart().getLine()).contains(el.getMessage().replaceAll(" ", "")) ? "true" : "false"), MessageType.Info);
|
||||
return !textChanges.get(el.getRange().getStart().getLine()).contains(el.getMessage().replaceAll(" ", ""));
|
||||
}).toList());
|
||||
|
||||
updateClient(client);
|
||||
client.publishDiagnostics(new PublishDiagnosticsParams(params.getTextDocument().getUri(), globalDiagnosticsMap.get(params.getTextDocument().getUri())));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -240,76 +437,17 @@ public class JavaTXTextDocumentService implements org.eclipse.lsp4j.services.Tex
|
||||
|
||||
@Override
|
||||
public void didSave(DidSaveTextDocumentParams didSaveTextDocumentParams) {
|
||||
log("[didSave] Client triggered didSave-Event.", MessageType.Info);
|
||||
|
||||
logService.log("[didSave] Client triggered didSave-Event.");
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
saveHandler.handleSave(didSaveTextDocumentParams);
|
||||
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) {
|
||||
}
|
||||
clientService.updateClient(client);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<List<InlayHint>> inlayHint(InlayHintParams params) {
|
||||
log("[inlayHint] The Client requested Inlay-Hints.", MessageType.Info);
|
||||
return CompletableFuture.supplyAsync(() -> globalInlayHintMap.get(params.getTextDocument().getUri()) == null ? Collections.emptyList() : globalInlayHintMap.get(params.getTextDocument().getUri()));
|
||||
return CompletableFuture.supplyAsync(() -> dontShowHints ? Collections.emptyList() : globalInlayHintMap.get(params.getTextDocument().getUri()) == null ? Collections.emptyList() : globalInlayHintMap.get(params.getTextDocument().getUri()));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -441,7 +579,11 @@ public class JavaTXTextDocumentService implements org.eclipse.lsp4j.services.Tex
|
||||
|
||||
@Override
|
||||
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);
|
||||
|
||||
log("Code-Action Context was: " + params.getContext().getTriggerKind().name(), MessageType.Info);
|
||||
|
||||
List<Either<Command, CodeAction>> actions = new ArrayList<>();
|
||||
|
||||
|
||||
@@ -453,6 +595,8 @@ public class JavaTXTextDocumentService implements org.eclipse.lsp4j.services.Tex
|
||||
.filter(diagnostic -> rangesOverlap(diagnostic.getRange(), requestedRange)).toList();
|
||||
|
||||
Range rangeOfInsert = params.getRange();
|
||||
|
||||
|
||||
String documentUri = params.getTextDocument().getUri();
|
||||
|
||||
|
||||
@@ -515,7 +659,9 @@ public class JavaTXTextDocumentService implements org.eclipse.lsp4j.services.Tex
|
||||
}
|
||||
}
|
||||
|
||||
return CompletableFuture.completedFuture(actions);
|
||||
return CompletableFuture.supplyAsync(() -> {
|
||||
return actions;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
@@ -0,0 +1,55 @@
|
||||
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 static 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;
|
||||
}
|
||||
}
|
@@ -0,0 +1,79 @@
|
||||
package de.dhbw.handler;
|
||||
|
||||
import com.google.common.base.Stopwatch;
|
||||
import de.dhbw.service.CacheService;
|
||||
import de.dhbw.service.ClientService;
|
||||
import de.dhbw.service.LogService;
|
||||
import de.dhbw.service.TextDocumentService;
|
||||
import de.dhbw.helper.ConversionHelper;
|
||||
import de.dhbw.helper.TypeResolver;
|
||||
import de.dhbw.model.LSPVariable;
|
||||
import org.eclipse.lsp4j.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class SaveHandler {
|
||||
|
||||
private final TypeResolver typeResolver;
|
||||
private final TextDocumentService textDocumentService;
|
||||
private final LogService logService;
|
||||
private final CacheService cacheService;
|
||||
private final ConversionHelper conversionHelper;
|
||||
private final ClientService clientService;
|
||||
|
||||
public SaveHandler(TypeResolver typeResolver, TextDocumentService textDocumentService, LogService logService, CacheService cacheService, ConversionHelper conversionHelper, ClientService clientService){
|
||||
this.typeResolver = typeResolver;
|
||||
this.textDocumentService = textDocumentService;
|
||||
this.logService = logService;
|
||||
this.cacheService = cacheService;
|
||||
this.conversionHelper = conversionHelper;
|
||||
this.clientService = clientService;
|
||||
}
|
||||
|
||||
public void handleSave(DidSaveTextDocumentParams didSaveTextDocumentParams){
|
||||
ArrayList<Diagnostic> diagnostics = new ArrayList<>();
|
||||
var sWatch = Stopwatch.createUnstarted();
|
||||
sWatch.start();
|
||||
|
||||
try {
|
||||
|
||||
String fileInput = textDocumentService.getFileOfUri(didSaveTextDocumentParams.getTextDocument().getUri());
|
||||
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());
|
||||
cacheService.setVariables(variables);
|
||||
|
||||
|
||||
List<InlayHint> typeHint = new ArrayList<>();
|
||||
for (var variable : variables) {
|
||||
|
||||
InlayHint inlayHint = conversionHelper.getInlayHint(variable);
|
||||
typeHint.add(inlayHint);
|
||||
|
||||
for (var type : variable.getPossibleTypes()) {
|
||||
Diagnostic diagnostic = conversionHelper.getDiagnostic(variable, didSaveTextDocumentParams.getTextDocument().getUri(), type);
|
||||
diagnostics.add(diagnostic);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
cacheService.updateGlobalMaps(diagnostics, typeHint, didSaveTextDocumentParams.getTextDocument().getUri());
|
||||
clientService.publishDiagnostics(didSaveTextDocumentParams.getTextDocument().getUri(), diagnostics);
|
||||
|
||||
|
||||
} 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());
|
||||
cacheService.updateGlobalMaps(new ArrayList<>(), new ArrayList<>(), didSaveTextDocumentParams.getTextDocument().getUri());
|
||||
|
||||
} finally {
|
||||
sWatch.stop();
|
||||
logService.log("[didSave] Finished Calculating in [" + sWatch.elapsed().toSeconds() + "s]", MessageType.Info);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,51 @@
|
||||
package de.dhbw.helper;
|
||||
|
||||
import de.dhbw.service.TextDocumentService;
|
||||
import de.dhbw.model.LSPVariable;
|
||||
import de.dhbw.model.Type;
|
||||
import org.eclipse.lsp4j.*;
|
||||
|
||||
public class ConversionHelper {
|
||||
|
||||
private final TextHelper textHelper;
|
||||
private final TextDocumentService textDocumentService;
|
||||
|
||||
public ConversionHelper(TextHelper textHelper, TextDocumentService textDocumentService) {
|
||||
this.textHelper = textHelper;
|
||||
this.textDocumentService = textDocumentService;
|
||||
}
|
||||
|
||||
public 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;
|
||||
}
|
||||
|
||||
public 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(), textDocumentService.getFileOfUri(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;
|
||||
}
|
||||
}
|
@@ -12,11 +12,15 @@ import de.dhbwstuttgart.target.generate.GenericsResult;
|
||||
import de.dhbwstuttgart.typeinference.result.ResultSet;
|
||||
import org.apache.log4j.LogManager;
|
||||
import org.apache.log4j.Logger;
|
||||
import org.eclipse.lsp4j.MessageParams;
|
||||
import org.eclipse.lsp4j.MessageType;
|
||||
import org.eclipse.lsp4j.Range;
|
||||
import org.eclipse.lsp4j.TextDocumentContentChangeEvent;
|
||||
import org.eclipse.lsp4j.services.LanguageClient;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
|
||||
@@ -29,6 +33,17 @@ public class TypeResolver {
|
||||
|
||||
private static final Logger logger = LogManager.getLogger(TypeResolver.class);
|
||||
|
||||
private final boolean ENABLE_GENERICS = false;
|
||||
|
||||
private LanguageServerTransferObject current;
|
||||
|
||||
public LanguageServerTransferObject getCurrent() {
|
||||
return current;
|
||||
}
|
||||
|
||||
public void reduceCurrent() {
|
||||
current.getResultSets().removeFirst();
|
||||
}
|
||||
|
||||
private List<ResultSet> currentResults = new ArrayList<>();
|
||||
|
||||
@@ -186,63 +201,99 @@ public class TypeResolver {
|
||||
return genericTypes;
|
||||
}
|
||||
|
||||
public void getCompilerInput(String path, String input) throws IOException, URISyntaxException, ClassNotFoundException {
|
||||
LanguageServerInterface languageServer = new LanguageServerInterface();
|
||||
var transferObj = languageServer.getResultSetAndAbstractSyntax(path, input);
|
||||
current = transferObj;
|
||||
}
|
||||
|
||||
/**
|
||||
* Zum Erhalt für sowohl Parameter als auch Methoden.
|
||||
*/
|
||||
public ArrayList<LSPVariable> infereInput(String input, String path) throws IOException, ClassNotFoundException, URISyntaxException {
|
||||
logger.info("Infering Types for Input.");
|
||||
|
||||
LanguageServerInterface languageServer = new LanguageServerInterface();
|
||||
var transferObj = languageServer.getResultSetAndAbstractSyntax(path, input);
|
||||
ArrayList<LSPVariable> methodsWithParametersLSPVariableList = new ArrayList<>();
|
||||
|
||||
|
||||
//TODO: Hier noch irgendwie die Klasse rausfinden oder durchgehen.
|
||||
//GENERICS OF CLASS
|
||||
for (var method : transferObj.getAst().getAllMethods()) {
|
||||
|
||||
|
||||
for (var clazz : transferObj.getAst().getClasses()) {
|
||||
ArrayList<Type> genericTypes = getClassGenerics(transferObj.getGeneratedGenerics(), method, clazz);
|
||||
TextHelper helper = new TextHelper();
|
||||
if (!genericTypes.isEmpty()) {
|
||||
methodsWithParametersLSPVariableList.add(new LSPClass("test", genericTypes, clazz.getOffset().getLine(), helper.getClassPositionForGeneric(clazz.getOffset().getLine() - 1, input, clazz.getOffset().getStopIndex()), clazz.getOffset().getStopIndex()));
|
||||
}
|
||||
}
|
||||
LanguageServerTransferObject transferObj;
|
||||
|
||||
if (current == null) {
|
||||
LanguageServerInterface languageServer = new LanguageServerInterface();
|
||||
transferObj = languageServer.getResultSetAndAbstractSyntax(path, input);
|
||||
current = transferObj;
|
||||
} else {
|
||||
transferObj = current;
|
||||
}
|
||||
|
||||
for (var method : transferObj.getAst().getAllMethods()) {
|
||||
var types = getAvailableTypes(transferObj.getResultSets(), method);
|
||||
var generics = getAvailableGenericTypes(transferObj.getGeneratedGenerics().values().stream().flatMap(List::stream).collect(Collectors.toList()), method);
|
||||
ArrayList<LSPVariable> methodsWithParametersLSPVariableList = new ArrayList<>();
|
||||
|
||||
if (!filterOutDuplicates(types).isEmpty()) {
|
||||
methodsWithParametersLSPVariableList.add(new LSPMethod(method.name, filterOutDuplicates(types), method.getOffset().getLine(), method.getOffset().getCharPositionInLine(), method.getOffset().getStopIndex()));
|
||||
}
|
||||
if (!generics.isEmpty()) {
|
||||
ArrayList<Type> typesThatAreGeneric = filterOutDuplicates(generics, types);
|
||||
typesThatAreGeneric.forEach(el -> el.setType("<" + el.getType() + "> " + el.getType()));
|
||||
//TODO: Muss Global und wird mehrmals so gemacht. Das if hier ist eigentlich falsch wegen unnötiger Berechnungszeit
|
||||
if (!filterOutDuplicates(typesThatAreGeneric).isEmpty()) {
|
||||
methodsWithParametersLSPVariableList.add(new LSPMethod(method.name, filterOutDuplicates(typesThatAreGeneric), method.getOffset().getLine(), method.getOffset().getCharPositionInLine(), method.getOffset().getStopIndex()));
|
||||
}
|
||||
for (var constructor : transferObj.getAst().KlassenVektor.getFirst().getConstructors()) {
|
||||
for (var constructorParam : constructor.getParameterList().getFormalparalist()) {
|
||||
var types = getAvailableTypes(transferObj.getResultSets(), constructorParam.getType());
|
||||
methodsWithParametersLSPVariableList.add(new LSPParameter("", types, constructorParam.getOffset().getLine(), constructorParam.getOffset().getCharPositionInLine(), constructorParam.getOffset().getStopIndex(), constructorParam.getType()));
|
||||
}
|
||||
}
|
||||
|
||||
for (var param : method.getParameterList()) {
|
||||
ArrayList<Type> typeParam = getAvailableTypes(transferObj.getResultSets(), param.getType());
|
||||
ArrayList<Type> genericParam = getAvailableGenericTypes(transferObj.getGeneratedGenerics().values().stream().flatMap(List::stream).collect(Collectors.toList()), param.getType());
|
||||
for (var fieldDecl : transferObj.getAst().KlassenVektor.getFirst().getFieldDecl()) {
|
||||
var types = getAvailableTypes(transferObj.getResultSets(), fieldDecl.getReturnType());
|
||||
methodsWithParametersLSPVariableList.add(new LSPParameter("", types, fieldDecl.getOffset().getLine(), fieldDecl.getOffset().getCharPositionInLine(), fieldDecl.getOffset().getStopIndex(), fieldDecl.getReturnType()));
|
||||
}
|
||||
|
||||
if (!transferObj.getResultSets().isEmpty()) {
|
||||
//TODO: Hier noch irgendwie die Klasse rausfinden oder durchgehen.
|
||||
|
||||
if (ENABLE_GENERICS) {
|
||||
//GENERICS OF CLASS
|
||||
for (var method : transferObj.getAst().getAllMethods()) {
|
||||
|
||||
|
||||
for (var clazz : transferObj.getAst().getClasses()) {
|
||||
ArrayList<Type> genericTypes = getClassGenerics(transferObj.getGeneratedGenerics(), method, clazz);
|
||||
TextHelper helper = new TextHelper();
|
||||
if (!genericTypes.isEmpty()) {
|
||||
methodsWithParametersLSPVariableList.add(new LSPClass("test", genericTypes, clazz.getOffset().getLine(), helper.getClassPositionForGeneric(clazz.getOffset().getLine() - 1, input, clazz.getOffset().getStopIndex()), clazz.getOffset().getStopIndex(), clazz.getReturnType()));
|
||||
}
|
||||
}
|
||||
|
||||
if (!typeParam.isEmpty()) {
|
||||
methodsWithParametersLSPVariableList.add(new LSPParameter(method.name, filterOutDuplicates(typeParam), param.getOffset().getLine(), param.getOffset().getCharPositionInLine(), param.getOffset().getStopIndex()));
|
||||
}
|
||||
|
||||
if (!genericParam.isEmpty()) {
|
||||
ArrayList<Type> typesThatAreGeneric = filterOutDuplicates(genericParam, typeParam);
|
||||
}
|
||||
|
||||
for (var method : transferObj.getAst().getAllMethods()) {
|
||||
var types = getAvailableTypes(transferObj.getResultSets(), method);
|
||||
var generics = getAvailableGenericTypes(transferObj.getGeneratedGenerics().values().stream().flatMap(List::stream).collect(Collectors.toList()), method);
|
||||
|
||||
if (!filterOutDuplicates(types).isEmpty()) {
|
||||
methodsWithParametersLSPVariableList.add(new LSPMethod(method.name, filterOutDuplicates(types), method.getOffset().getLine(), method.getOffset().getCharPositionInLine(), method.getOffset().getStopIndex(), method.getReturnType()));
|
||||
}
|
||||
|
||||
if (ENABLE_GENERICS) {
|
||||
if (!generics.isEmpty()) {
|
||||
ArrayList<Type> typesThatAreGeneric = filterOutDuplicates(generics, types);
|
||||
typesThatAreGeneric.forEach(el -> el.setType("<" + el.getType() + "> " + el.getType()));
|
||||
//TODO: Muss Global und wird mehrmals so gemacht. Das if hier ist eigentlich falsch wegen unnötiger Berechnungszeit
|
||||
if (!filterOutDuplicates(typesThatAreGeneric).isEmpty()) {
|
||||
methodsWithParametersLSPVariableList.add(new LSPMethod(method.name, filterOutDuplicates(typesThatAreGeneric), method.getOffset().getLine(), method.getOffset().getCharPositionInLine(), method.getOffset().getStopIndex(), method.getReturnType()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (var param : method.getParameterList()) {
|
||||
ArrayList<Type> typeParam = getAvailableTypes(transferObj.getResultSets(), param.getType());
|
||||
ArrayList<Type> genericParam = getAvailableGenericTypes(transferObj.getGeneratedGenerics().values().stream().flatMap(List::stream).collect(Collectors.toList()), param.getType());
|
||||
|
||||
if (!typeParam.isEmpty()) {
|
||||
methodsWithParametersLSPVariableList.add(new LSPParameter(method.name, filterOutDuplicates(typeParam), param.getOffset().getLine(), param.getOffset().getCharPositionInLine(), param.getOffset().getStopIndex(), param.getType()));
|
||||
}
|
||||
|
||||
if (ENABLE_GENERICS) {
|
||||
if (!genericParam.isEmpty()) {
|
||||
ArrayList<Type> typesThatAreGeneric = filterOutDuplicates(genericParam, typeParam);
|
||||
|
||||
|
||||
if (!filterOutDuplicates(typesThatAreGeneric).isEmpty()) {
|
||||
methodsWithParametersLSPVariableList.add(new LSPParameter(method.name, filterOutDuplicates(typesThatAreGeneric), method.getOffset().getLine(), param.getOffset().getCharPositionInLine(), param.getOffset().getStopIndex()));
|
||||
methodsWithParametersLSPVariableList.add(new LSPParameter(method.name, new ArrayList<>(filterOutDuplicates(typesThatAreGeneric).stream().map(el -> new Type("<" + el.getType() + ">", true)).toList()), method.getOffset().getLine(), method.getOffset().getCharPositionInLine(), method.getOffset().getStopIndex()));
|
||||
if (!filterOutDuplicates(typesThatAreGeneric).isEmpty()) {
|
||||
methodsWithParametersLSPVariableList.add(new LSPParameter(method.name, filterOutDuplicates(typesThatAreGeneric), method.getOffset().getLine(), param.getOffset().getCharPositionInLine(), param.getOffset().getStopIndex(), param.getType()));
|
||||
methodsWithParametersLSPVariableList.add(new LSPParameter(method.name, new ArrayList<>(filterOutDuplicates(typesThatAreGeneric).stream().map(el -> new Type("<" + el.getType() + ">", true)).toList()), method.getOffset().getLine(), method.getOffset().getCharPositionInLine(), method.getOffset().getStopIndex(), method.getReturnType()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -252,4 +303,22 @@ public class TypeResolver {
|
||||
}
|
||||
|
||||
|
||||
public void reduceCurrent(HashMap<LineCharPosition, String> combinedList, List<LSPVariable> variables, LanguageClient client) {
|
||||
client.logMessage(new MessageParams(MessageType.Info, "Lenght is: " + combinedList.size()));
|
||||
for (var lines : combinedList.entrySet()) {
|
||||
var contentChange = lines.getValue();
|
||||
for (LSPVariable variable : variables) {
|
||||
for (Type typeOfVariable : variable.getPossibleTypes()) {
|
||||
client.logMessage(new MessageParams(MessageType.Info, "\"" + typeOfVariable.getType() + " : " + variable.getLine() + "\"< > \"" + contentChange + " : " + lines.getKey() + "\""));
|
||||
if (typeOfVariable.getType().equalsIgnoreCase(contentChange.replaceAll(" ", ""))) {
|
||||
if (variable.getLine() - 1 == lines.getKey().line && variable.getCharPosition() == lines.getKey().charPosition) {
|
||||
current.getResultSets().removeIf(el -> !el.resolveType(variable.getOriginalTphName()).resolvedType.toString().equalsIgnoreCase(contentChange.replaceAll(" ", "")));
|
||||
client.logMessage(new MessageParams(MessageType.Info, "Removing Resultset"));
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,9 +1,11 @@
|
||||
package de.dhbw.model;
|
||||
|
||||
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class LSPClass extends LSPVariable{
|
||||
public LSPClass(String name, ArrayList<Type> possibleTypes, int line, int charPosition, int endPosition) {
|
||||
super(name, possibleTypes, line, charPosition, endPosition);
|
||||
public LSPClass(String name, ArrayList<Type> possibleTypes, int line, int charPosition, int endPosition, RefTypeOrTPHOrWildcardOrGeneric originalTphName) {
|
||||
super(name, possibleTypes, line, charPosition, endPosition, originalTphName);
|
||||
}
|
||||
}
|
||||
|
@@ -1,9 +1,11 @@
|
||||
package de.dhbw.model;
|
||||
|
||||
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class LSPMethod extends LSPVariable {
|
||||
public LSPMethod(String name, ArrayList<Type> type, int line, int charPosition, int endPosition) {
|
||||
super(name, type, line, charPosition, endPosition);
|
||||
public LSPMethod(String name, ArrayList<Type> type, int line, int charPosition, int endPosition, RefTypeOrTPHOrWildcardOrGeneric originalTphName) {
|
||||
super(name, type, line, charPosition, endPosition, originalTphName);
|
||||
}
|
||||
}
|
||||
|
@@ -1,10 +1,12 @@
|
||||
package de.dhbw.model;
|
||||
|
||||
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class LSPParameter extends LSPMethod {
|
||||
public class LSPParameter extends LSPVariable {
|
||||
|
||||
public LSPParameter(String name, ArrayList<Type> type, int line, int charPosition, int endPosition) {
|
||||
super(name, type, line, charPosition, endPosition);
|
||||
public LSPParameter(String name, ArrayList<Type> type, int line, int charPosition, int endPosition, RefTypeOrTPHOrWildcardOrGeneric originalTphName) {
|
||||
super(name, type, line, charPosition, endPosition, originalTphName);
|
||||
}
|
||||
}
|
||||
|
@@ -1,20 +1,33 @@
|
||||
package de.dhbw.model;
|
||||
|
||||
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class LSPVariable { String name;
|
||||
public class LSPVariable {
|
||||
String name;
|
||||
ArrayList<Type> possibleTypes;
|
||||
int line;
|
||||
int charPosition;
|
||||
int endPosition;
|
||||
boolean needsInference;
|
||||
RefTypeOrTPHOrWildcardOrGeneric originalTphName;
|
||||
|
||||
public LSPVariable(String name, ArrayList<Type> possibleTypes, int line, int charPosition, int endPosition) {
|
||||
public LSPVariable(String name, ArrayList<Type> possibleTypes, int line, int charPosition, int endPosition, RefTypeOrTPHOrWildcardOrGeneric originalTphName) {
|
||||
this.name = name;
|
||||
this.possibleTypes = possibleTypes;
|
||||
this.line = line;
|
||||
this.charPosition = charPosition;
|
||||
this.endPosition = endPosition;
|
||||
this.originalTphName = originalTphName;
|
||||
}
|
||||
|
||||
public RefTypeOrTPHOrWildcardOrGeneric getOriginalTphName() {
|
||||
return originalTphName;
|
||||
}
|
||||
|
||||
public void setOriginalTphName(RefTypeOrTPHOrWildcardOrGeneric originalTphName) {
|
||||
this.originalTphName = originalTphName;
|
||||
}
|
||||
|
||||
public int getEndPosition() {
|
||||
|
@@ -0,0 +1,25 @@
|
||||
package de.dhbw.model;
|
||||
|
||||
public class LineCharPosition {
|
||||
public final int line;
|
||||
public final int charPosition;
|
||||
|
||||
public LineCharPosition(int line, int charPosition) {
|
||||
this.line = line;
|
||||
this.charPosition = charPosition;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (!(o instanceof LineCharPosition)) return false;
|
||||
LineCharPosition that = (LineCharPosition) o;
|
||||
return line == that.line && charPosition == that.charPosition;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Line " + line + ", Char " + charPosition;
|
||||
}
|
||||
}
|
120
LanguageServer/src/main/java/de/dhbw/service/CacheService.java
Normal file
120
LanguageServer/src/main/java/de/dhbw/service/CacheService.java
Normal file
@@ -0,0 +1,120 @@
|
||||
package de.dhbw.service;
|
||||
|
||||
import de.dhbw.helper.CodeSnippetOptions;
|
||||
import de.dhbw.helper.TextHelper;
|
||||
import de.dhbw.helper.TypeResolver;
|
||||
import de.dhbw.model.LSPVariable;
|
||||
import org.eclipse.lsp4j.Diagnostic;
|
||||
import org.eclipse.lsp4j.InlayHint;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
public class CacheService {
|
||||
private HashMap<String, List<InlayHint>> globalInlayHintMap = new HashMap<>();
|
||||
private Boolean currentlyCalculating = false;
|
||||
private HashMap<String, List<Diagnostic>> globalDiagnosticsMap = new HashMap<>();
|
||||
private HashMap<String, String> textDocuments = new HashMap<>();
|
||||
private CodeSnippetOptions codeSnippetOptions = new CodeSnippetOptions();
|
||||
private TextHelper textHelper = new TextHelper();
|
||||
private Boolean dontShowHints = false;
|
||||
private TypeResolver typeResolver = new TypeResolver();
|
||||
private Path fileRoot = null;
|
||||
private Boolean singleFileOpened = false;
|
||||
private List<LSPVariable> variables = new ArrayList<>();
|
||||
|
||||
public void updateGlobalMaps(List<Diagnostic> diagnostics, List<InlayHint> typeHint, String uri) {
|
||||
globalDiagnosticsMap.put(uri, diagnostics);
|
||||
globalInlayHintMap.put(uri, typeHint);
|
||||
}
|
||||
|
||||
public Boolean getCurrentlyCalculating() {
|
||||
return currentlyCalculating;
|
||||
}
|
||||
|
||||
public Boolean getDontShowHints() {
|
||||
return dontShowHints;
|
||||
}
|
||||
|
||||
public Boolean getSingleFileOpened() {
|
||||
return singleFileOpened;
|
||||
}
|
||||
|
||||
public CodeSnippetOptions getCodeSnippetOptions() {
|
||||
return codeSnippetOptions;
|
||||
}
|
||||
|
||||
public HashMap<String, List<Diagnostic>> getGlobalDiagnosticsMap() {
|
||||
return globalDiagnosticsMap;
|
||||
}
|
||||
|
||||
public HashMap<String, List<InlayHint>> getGlobalInlayHintMap() {
|
||||
return globalInlayHintMap;
|
||||
}
|
||||
|
||||
public HashMap<String, String> getTextDocuments() {
|
||||
return textDocuments;
|
||||
}
|
||||
|
||||
public List<LSPVariable> getVariables() {
|
||||
return variables;
|
||||
}
|
||||
|
||||
public Path getFileRoot() {
|
||||
return fileRoot;
|
||||
}
|
||||
|
||||
public TextHelper getTextHelper() {
|
||||
return textHelper;
|
||||
}
|
||||
|
||||
public TypeResolver getTypeResolver() {
|
||||
return typeResolver;
|
||||
}
|
||||
|
||||
public void setCodeSnippetOptions(CodeSnippetOptions codeSnippetOptions) {
|
||||
this.codeSnippetOptions = codeSnippetOptions;
|
||||
}
|
||||
|
||||
public void setCurrentlyCalculating(Boolean currentlyCalculating) {
|
||||
this.currentlyCalculating = currentlyCalculating;
|
||||
}
|
||||
|
||||
public void setDontShowHints(Boolean dontShowHints) {
|
||||
this.dontShowHints = dontShowHints;
|
||||
}
|
||||
|
||||
public void setFileRoot(Path fileRoot) {
|
||||
this.fileRoot = fileRoot;
|
||||
}
|
||||
|
||||
public void setGlobalDiagnosticsMap(HashMap<String, List<Diagnostic>> globalDiagnosticsMap) {
|
||||
this.globalDiagnosticsMap = globalDiagnosticsMap;
|
||||
}
|
||||
|
||||
public void setGlobalInlayHintMap(HashMap<String, List<InlayHint>> globalInlayHintMap) {
|
||||
this.globalInlayHintMap = globalInlayHintMap;
|
||||
}
|
||||
|
||||
public void setSingleFileOpened(Boolean singleFileOpened) {
|
||||
this.singleFileOpened = singleFileOpened;
|
||||
}
|
||||
|
||||
public void setTextDocuments(HashMap<String, String> textDocuments) {
|
||||
this.textDocuments = textDocuments;
|
||||
}
|
||||
|
||||
public void setTextHelper(TextHelper textHelper) {
|
||||
this.textHelper = textHelper;
|
||||
}
|
||||
|
||||
public void setTypeResolver(TypeResolver typeResolver) {
|
||||
this.typeResolver = typeResolver;
|
||||
}
|
||||
|
||||
public void setVariables(List<LSPVariable> variables) {
|
||||
this.variables = variables;
|
||||
}
|
||||
}
|
@@ -0,0 +1,48 @@
|
||||
package de.dhbw.service;
|
||||
|
||||
import org.eclipse.lsp4j.Diagnostic;
|
||||
import org.eclipse.lsp4j.MessageParams;
|
||||
import org.eclipse.lsp4j.MessageType;
|
||||
import org.eclipse.lsp4j.PublishDiagnosticsParams;
|
||||
import org.eclipse.lsp4j.services.LanguageClient;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class ClientService {
|
||||
|
||||
private LanguageClient client;
|
||||
|
||||
public ClientService(LanguageClient client) {
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
public void publishDiagnostics(String uri, List<Diagnostic> diagnostics){
|
||||
PublishDiagnosticsParams diagnosticsParams = new PublishDiagnosticsParams(uri, diagnostics);
|
||||
client.publishDiagnostics(diagnosticsParams);
|
||||
}
|
||||
|
||||
public void sendClientLog(MessageType type, String message){
|
||||
client.logMessage(new MessageParams(type, message));
|
||||
}
|
||||
|
||||
public void sendClientLog(String message){
|
||||
client.logMessage(new MessageParams(MessageType.Info, message));
|
||||
}
|
||||
|
||||
public void showMessage(MessageType type, String message) {
|
||||
client.showMessage(new MessageParams(type, message));
|
||||
}
|
||||
|
||||
public void showMessage(String message) {
|
||||
client.showMessage(new MessageParams(MessageType.Info, message));
|
||||
}
|
||||
|
||||
public void setClient(LanguageClient client) {
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
public void updateClient(LanguageClient client) {
|
||||
client.refreshInlayHints();
|
||||
client.refreshDiagnostics();
|
||||
}
|
||||
}
|
31
LanguageServer/src/main/java/de/dhbw/service/LogService.java
Normal file
31
LanguageServer/src/main/java/de/dhbw/service/LogService.java
Normal file
@@ -0,0 +1,31 @@
|
||||
package de.dhbw.service;
|
||||
|
||||
import org.apache.log4j.LogManager;
|
||||
import org.apache.log4j.Logger;
|
||||
import org.eclipse.lsp4j.MessageType;
|
||||
|
||||
public class LogService {
|
||||
|
||||
private final Logger logger = LogManager.getLogger(LogService.class);
|
||||
private final ClientService clientService;
|
||||
|
||||
public LogService(ClientService clientService) {
|
||||
this.clientService = clientService;
|
||||
}
|
||||
|
||||
public void log(String message, MessageType type) {
|
||||
clientService.sendClientLog(type, message);
|
||||
switch (type) {
|
||||
case Error -> logger.error(message);
|
||||
case Warning -> logger.warn(message);
|
||||
case Info -> logger.info(message);
|
||||
default -> logger.debug(message);
|
||||
}
|
||||
}
|
||||
|
||||
public void log(String message) {
|
||||
clientService.sendClientLog(MessageType.Info, message);
|
||||
logger.info(message);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,22 @@
|
||||
package de.dhbw.service;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public class TextDocumentService {
|
||||
|
||||
private final HashMap<String, String> files = new HashMap<>();
|
||||
|
||||
public TextDocumentService(){
|
||||
|
||||
}
|
||||
|
||||
public String getFileOfUri(String uri) {
|
||||
return files.get(uri);
|
||||
}
|
||||
|
||||
public void saveFileWithUri(String uri, String input) {
|
||||
files.put(uri, input);
|
||||
}
|
||||
|
||||
|
||||
}
|
@@ -1,30 +1,39 @@
|
||||
|
||||
import de.dhbw.helper.TextHelper;
|
||||
import de.dhbw.helper.TypeResolver;
|
||||
import de.dhbwstuttgart.languageServerInterface.LanguageServerInterface;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
public class CompilerInterfaceTest {
|
||||
|
||||
@Test
|
||||
public void testAbstractSyntaxAsString() throws IOException, ClassNotFoundException {
|
||||
// LanguageServerInterface languageServer = new LanguageServerInterface();
|
||||
// var res = languageServer.getResultSetAndAbstractSyntax("import java.lang.Integer;\n import java.lang.String;\n" +
|
||||
// "public class test{\n" +
|
||||
// "public main(test){" +
|
||||
// "if(0>1){" +
|
||||
// "return \"w\";" +
|
||||
// "}" +
|
||||
// "Integer i = 0;" +
|
||||
// "return i;" +
|
||||
// "}" +
|
||||
// "}", "TEST");
|
||||
//
|
||||
// System.out.println("TEST OUTPUT:");
|
||||
public void testAbstractSyntaxAsString() throws IOException, ClassNotFoundException, URISyntaxException {
|
||||
TypeResolver typeResolver = new TypeResolver();
|
||||
var res = typeResolver.infereInput("import java.lang.Integer;\n import java.lang.String;\n" +
|
||||
"public class test{\n" +
|
||||
"public main(test){" +
|
||||
"if(0>1){" +
|
||||
"return \"w\";" +
|
||||
"}" +
|
||||
"Integer i = 0;" +
|
||||
"return i;" +
|
||||
"}" +
|
||||
"}", "c%3A/Users/ruben/Neuer%20Ordner%20%282%29/LSP-Vortrag/images/test.jav");
|
||||
|
||||
var res2 = typeResolver.infereInput("import java.lang.Integer;\n import java.lang.String;\n" +
|
||||
"public class test{\n" +
|
||||
"public main(test){" +
|
||||
"if(0>1){" +
|
||||
"return \"w\";" +
|
||||
"}" +
|
||||
"Integer i = 0;" +
|
||||
"return i;" +
|
||||
"}" +
|
||||
"}", "c%3A/Users/ruben/Neuer%20Ordner%20%282%29/LSP-Vortrag/images/test.jav");
|
||||
System.out.println("TEST OUTPUT:");
|
||||
//
|
||||
// ArrayList<String> allTypes = new ArrayList<>();
|
||||
//
|
||||
@@ -114,7 +123,7 @@ public class CompilerInterfaceTest {
|
||||
@Test
|
||||
public void testCharEnding() throws IOException, ClassNotFoundException {
|
||||
TextHelper textHelper = new TextHelper();
|
||||
var endingChar = textHelper.getEndingCharOfStartingChar(3,13,"import java.lang.Integer;\n" +
|
||||
var endingChar = textHelper.getEndingCharOfStartingChar(3, 13, "import java.lang.Integer;\n" +
|
||||
"import java.lang.String; \n" +
|
||||
"public class test{\n" +
|
||||
" public main(test, test2){\n" +
|
||||
|
@@ -11,6 +11,7 @@ public class JavaTXLanguageDocumentServiceTest {
|
||||
@Test
|
||||
@Ignore
|
||||
public void testWordExtraction() {
|
||||
JavaTXTextDocumentService service = new JavaTXTextDocumentService();
|
||||
// JavaTXTextDocumentService service = new JavaTXTextDocumentService();
|
||||
// service.didSave(new DidSaveTextDocumentParams(new TextDocumentIdentifier("file:///c%3A/Users/ruben/Neuer%20Ordner%20%282%29/LSP-Vortrag/images/test.jav")));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user