Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show version in UI of IntelliJ plugin #2998

Merged
merged 10 commits into from
Aug 2, 2023
Merged
3 changes: 2 additions & 1 deletion intellij/.run/Run Plugin.run.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<option name="executionName" />
<option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="externalSystemIdString" value="GRADLE" />
<option name="scriptParameters" value="" />
<option name="scriptParameters" value="--stacktrace" />
<option name="taskDescriptions">
<list />
</option>
Expand All @@ -19,6 +19,7 @@
<ExternalSystemDebugServerProcess>true</ExternalSystemDebugServerProcess>
<ExternalSystemReattachDebugProcess>true</ExternalSystemReattachDebugProcess>
<DebugAllEnabled>false</DebugAllEnabled>
<ForceTestExec>false</ForceTestExec>
<method v="2" />
</configuration>
</component>
6 changes: 2 additions & 4 deletions intellij/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ repositories {

// Dependencies are managed with Gradle version catalog - read more: https://docs.gradle.org/current/userguide/platforms.html#sub:version-catalog
dependencies {
// implementation(libs.annotations)
implementation ("com.google.guava:guava:31.1-jre")
implementation ("com.github.arteam:simple-json-rpc-client:1.3")
}

// Set the JVM language level used to build the project. Use Java 11 for 2020.3+, and Java 17 for 2022.2+.
Expand All @@ -36,7 +37,6 @@ intellij {
pluginName = properties("pluginName")
version = properties("platformVersion")
type = properties("platformType")

// Plugin Dependencies. Uses `platformPlugins` property from the gradle.properties file.
plugins = properties("platformPlugins").map { it.split(',').map(String::trim).filter(String::isNotEmpty) }
}
Expand All @@ -63,7 +63,6 @@ koverReport {
}
}
}

tasks {
wrapper {
gradleVersion = properties("gradleVersion").get()
Expand Down Expand Up @@ -100,7 +99,6 @@ tasks {
}
}
}

// Configure UI tests plugin
// Read more: https://github.com/JetBrains/intellij-ui-test-robot
runIdeForUiTests {
Expand Down
5 changes: 3 additions & 2 deletions intellij/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ gradleVersion = 8.1.1
kotlin.stdlib.default.dependency = false

# Enable Gradle Configuration Cache -> https://docs.gradle.org/current/userguide/configuration_cache.html
org.gradle.configuration-cache = true
org.gradle.configuration-cache = false


# Enable Gradle Build Cache -> https://docs.gradle.org/current/userguide/build_cache.html
org.gradle.caching = true
org.gradle.caching = true

# Enable Gradle Kotlin DSL Lazy Property Assignment -> https://docs.gradle.org/current/userguide/kotlin_dsl.html#kotdsl:assignment
systemProp.org.gradle.unsafe.kotlin.assignment = true
Expand Down
14 changes: 14 additions & 0 deletions intellij/src/main/java/services/KiotaJavaClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package services;
import com.github.arteam.simplejsonrpc.client.JsonRpcClient;
import com.github.arteam.simplejsonrpc.client.builder.RequestBuilder;
public class KiotaJavaClient{
private final JsonRpcClient client;
public KiotaJavaClient() {
client = new JsonRpcClient(new ProcessTransport("kiota", "rpc"));
}
public <T> RequestBuilder<T> createRequest(String method, Class<T> returnType) {
return client.createRequest()
.method(method)
.returnAs(returnType);
}
}
24 changes: 0 additions & 24 deletions intellij/src/main/java/services/MyKiotaProjectService.java

This file was deleted.

89 changes: 89 additions & 0 deletions intellij/src/main/java/services/ProcessTransport.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package services;
import com.github.arteam.simplejsonrpc.client.Transport;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
public class ProcessTransport implements Transport {
private final String[] processCommandsAndArgs;
public ProcessTransport(String... processCommandsAndArgs) {
if (processCommandsAndArgs.length == 0) {
throw new IllegalArgumentException("processCommandsAndArgs must not be empty");
}
this.processCommandsAndArgs = processCommandsAndArgs;
}
@Override
public String pass(String request) throws IOException {
final ProcessBuilder builder = new ProcessBuilder(processCommandsAndArgs);
final Process process = builder.start();
process.onExit().thenAccept((exitCode) -> {
System.out.println("Process exited with code: " + exitCode);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using com.intellij.openapi.diagnostic.Logger instead of System.out.println. This is just a note for the future and not this PR.

});
final BufferedReader stdout = process.inputReader(StandardCharsets.UTF_8);
final OutputStream stdin = process.getOutputStream();
String idToInsert = "\"id\":1,";
String payload = "Content-Length: " + (request.length() + idToInsert.length()) + "\r\n" +
"\r\n" +
new StringBuilder(request).insert(1, idToInsert).toString();
stdin.write(payload.getBytes(StandardCharsets.UTF_8));
stdin.flush();


final Map<String, String> headers = readResponseHeaders(stdout);
final int contentLength = Integer.parseInt(headers.get("Content-Length"));
StringBuilder responseBuilder = new StringBuilder();
int character;
for(int i = 0; i < contentLength; i++) {
character = stdout.read();
char c = (char) character;
responseBuilder.append(c);
}
final String response = responseBuilder.toString();
stdout.close();
stdin.close();
try {
process.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should consider using the logger here too.

}
process.destroyForcibly();
return response;
}
private Map<String, String> readResponseHeaders(BufferedReader stdOut) throws IOException {
StringBuilder headerKey = new StringBuilder();
StringBuilder headerValue = new StringBuilder();
boolean readingKey = true;
final HashMap<String, String> headers = new HashMap<>();
int character;
int nCount = 0;
while ((character = stdOut.read()) != -1) {
char c = (char) character;
if (c == '\r') {
continue;
} else if (c == '\n') {
if (!readingKey) {
readingKey = true;
headers.put(headerKey.toString(), headerValue.toString());
}
nCount++;
if (nCount == 2) {
break;
}
} else if (c == ':' && readingKey) {
readingKey = false;
stdOut.read(); // Consume the space after the colon
continue;
} else {
nCount = 0;
}
if (readingKey) {
headerKey.append(c);
} else {
headerValue.append(c);
}
}
return headers;
}
}
13 changes: 13 additions & 0 deletions intellij/src/main/java/services/VersionHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package services;

import com.github.arteam.simplejsonrpc.client.builder.RequestBuilder;

import java.io.IOException;

public class VersionHandler {
final KiotaJavaClient client = new KiotaJavaClient();

public String getVersion() throws IOException {
return client.createRequest("GetVersion", String.class).execute();
}
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
package toolWindow;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowFactory;
import com.intellij.ui.content.Content;
import com.intellij.ui.content.ContentFactory;
import org.jetbrains.annotations.NotNull;

public class MyKiotaToolFactory implements ToolWindowFactory {


import java.io.IOException;
public class KiotaToolFactory implements ToolWindowFactory {
MyToolWindow myToolWindow ;
@Override
public void createToolWindowContent(@NotNull Project project, @NotNull ToolWindow toolWindow) {
MyToolWindow myToolWindow = new MyToolWindow(toolWindow);
myToolWindow = new MyToolWindow(toolWindow);
ContentFactory contentFactory = ContentFactory.getInstance();
Content content = contentFactory.createContent(myToolWindow.Addpanel(), null, false);
Content content = null;
try {
content = contentFactory.createContent(myToolWindow.Addpanel(), null, false);
} catch (IOException e) {
throw new RuntimeException(e);
}
toolWindow.getContentManager().addContent(content);


}

@Override
public boolean shouldBeAvailable(@NotNull Project project) {
return true;
Expand Down
25 changes: 15 additions & 10 deletions intellij/src/main/java/toolWindow/MyToolWindow.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,36 @@
package toolWindow;

import com.intellij.openapi.project.Project; // change this later
import com.intellij.openapi.ui.LabeledComponent;
import com.intellij.openapi.ui.TextFieldWithBrowseButton;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.ui.components.JBPanel;
import com.intellij.ui.components.panels.VerticalLayout;
import services.MyKiotaProjectService;

import services.VersionHandler;
import javax.swing.*;
import java.awt.*;
import java.io.IOException;

public class MyToolWindow {
// private final MyKiotaProjectService service;
private final JBPanel<JBPanel<?>> ParentPanel;

VersionHandler versionHandler ;
public MyToolWindow(ToolWindow toolWindow) {
Project project = toolWindow.getProject();
// service = project.getService(MyKiotaProjectService.class);
ParentPanel = new JBPanel<>();
versionHandler = new VersionHandler();
}

public JComponent Addpanel() {
public JComponent Addpanel() throws IOException {
ParentPanel.setLayout(new BorderLayout());
ParentPanel.add(getInput(), BorderLayout.CENTER);
ParentPanel.add(getversion(), BorderLayout.SOUTH);
return ParentPanel;
}
public JComponent getversion() throws IOException {
JBPanel<JBPanel<?>> versionPanel = new JBPanel<>();
versionPanel.setLayout(new BorderLayout());

// Create a label to display kiota version
JLabel versionLabel = new JLabel(versionHandler.getVersion());
versionPanel.add(versionLabel, BorderLayout.CENTER);
return versionPanel;
}

public JComponent getInput() {
JBPanel<JBPanel<?>> mainPanel = new JBPanel<>();
Expand Down
5 changes: 1 addition & 4 deletions intellij/src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
<resource-bundle>messages.MyBundle</resource-bundle>

<extensions defaultExtensionNs="com.intellij">
<toolWindow factoryClass="toolWindow.MyKiotaToolFactory" id="MyKiotaToolFactory"/>
<toolWindow factoryClass="toolWindow.KiotaToolFactory" id="KiotaToolFactory"/>
</extensions>

<applicationListeners>
<listener class="com.github.hossain2024.intellijtestplugin.listeners.MyApplicationActivationListener" topic="com.intellij.openapi.application.ApplicationActivationListener"/>
</applicationListeners>
</idea-plugin>
Original file line number Diff line number Diff line change
@@ -1,39 +1,26 @@
package com.github.hossain2024.intellijtestplugin

import com.intellij.ide.highlighter.XmlFileType
import com.intellij.openapi.components.service
import com.intellij.psi.xml.XmlFile
import com.intellij.testFramework.TestDataPath
import com.intellij.testFramework.fixtures.BasePlatformTestCase
import com.intellij.util.PsiErrorElementUtil
import com.github.hossain2024.intellijtestplugin.services.MyProjectService

@TestDataPath("\$CONTENT_ROOT/src/test/testData")
class MyPluginTest : BasePlatformTestCase() {

fun testXMLFile() {
val psiFile = myFixture.configureByText(XmlFileType.INSTANCE, "<foo>bar</foo>")
val xmlFile = assertInstanceOf(psiFile, XmlFile::class.java)

assertFalse(PsiErrorElementUtil.hasErrors(project, xmlFile.virtualFile))

assertNotNull(xmlFile.rootTag)

xmlFile.rootTag?.let {
assertEquals("foo", it.name)
assertEquals("bar", it.value.text)
}
}

fun testRename() {
myFixture.testRename("foo.xml", "foo_after.xml", "a2")
}

fun testProjectService() {
val projectService = project.service<MyProjectService>()

assertNotSame(projectService.getRandomNumber(), projectService.getRandomNumber())
}

override fun getTestDataPath() = "src/test/testData/rename"
}