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

Merge Language Server to Gradle Server #1525

Merged
merged 7 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 0 additions & 35 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,41 +77,6 @@
"order": 3
}
},
{
"name": "Debug Language Server: Launch Extension",
"type": "extensionHost",
"request": "launch",
"runtimeExecutable": "${execPath}",
"args": [
"--extensionDevelopmentPath=${workspaceFolder}/extension"
],
"outFiles": [
"${workspaceFolder}/extension/dist/**/*.js"
],
"preLaunchTask": "Gradle: Build",
"env": {
"VSCODE_DEBUG_LANGUAGE_SERVER": "true",
"VSCODE_GRADLE_PORT": "6006"
},
"presentation": {
"group": "debug",
"order": 4
}
},
{
"type": "java",
"name": "Debug Language Server: Launch Language Server",
"request": "launch",
"mainClass": "com.microsoft.gradle.GradleLanguageServer",
"projectName": "gradle-language-server",
"env": {
"VSCODE_GRADLE_PORT": "6006"
},
"presentation": {
"group": "debug",
"order": 5
}
},
{
"type": "java",
"name": "Attach to Gradle Plugin",
Expand Down
8 changes: 5 additions & 3 deletions extension/src/Extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import { GradleBuildContentProvider } from "./client/GradleBuildContentProvider"
import { BuildServerController } from "./bs/BuildServerController";
import { GradleTestRunner } from "./bs/GradleTestRunner";
import { BspProxy } from "./bs/BspProxy";
import { generateRandomPipeName } from "./util/generateRandomPipeName";

export class Extension {
private readonly bspProxy: BspProxy;
Expand Down Expand Up @@ -71,7 +72,7 @@ export class Extension {
private readonly onDidTerminalOpen: vscode.Event<vscode.Terminal> = this._onDidTerminalOpen.event;
private recentTerminal: vscode.Terminal | undefined;
private readonly buildServerController: BuildServerController;

private readonly languageServerPipePath: string;
public constructor(private readonly context: vscode.ExtensionContext) {
const loggingChannel = vscode.window.createOutputChannel("Gradle for Java");
logger.setLoggingChannel(loggingChannel);
Expand All @@ -85,13 +86,14 @@ export class Extension {
const bspLogger = new Logger("bspProxy");
bspLogger.setLoggingChannel(loggingChannel);

this.languageServerPipePath = generateRandomPipeName();
jdneo marked this conversation as resolved.
Show resolved Hide resolved
if (getConfigIsDebugEnabled()) {
Logger.setLogVerbosity(LogVerbosity.DEBUG);
}

const statusBarItem = vscode.window.createStatusBarItem();
this.bspProxy = new BspProxy(this.context, bspLogger);
this.server = new GradleServer({ host: "localhost" }, context, serverLogger, this.bspProxy);
this.server = new GradleServer({ host: "localhost" }, context, serverLogger, this.bspProxy, this.languageServerPipePath);
this.client = new GradleClient(this.server, statusBarItem, clientLogger);
Jiaaming marked this conversation as resolved.
Show resolved Hide resolved
this.pinnedTasksStore = new PinnedTasksStore(context);
this.recentTasksStore = new RecentTasksStore();
Expand Down Expand Up @@ -206,8 +208,8 @@ export class Extension {
);

this.client.onDidConnect(() => this.refresh());
void startLanguageServer(this.context, this.gradleBuildContentProvider, this.rootProjectsStore, this.languageServerPipePath);
Jiaaming marked this conversation as resolved.
Show resolved Hide resolved
void this.activate();
void startLanguageServer(this.context, this.gradleBuildContentProvider, this.rootProjectsStore);
void vscode.commands.executeCommand("setContext", "allowParallelRun", getAllowParallelRun());
void vscode.commands.executeCommand("setContext", Context.ACTIVATION_CONTEXT_KEY, true);
}
Expand Down
53 changes: 8 additions & 45 deletions extension/src/languageServer/languageServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,31 @@
// Licensed under the MIT license.

import * as net from "net";
import * as path from "path";
import * as vscode from "vscode";
import { DidChangeConfigurationNotification, LanguageClientOptions } from "vscode-languageclient";
import { LanguageClient, StreamInfo } from "vscode-languageclient/node";
import { GradleBuildContentProvider } from "../client/GradleBuildContentProvider";
import { GradleBuild, GradleProject } from "../proto/gradle_pb";
import { RootProjectsStore } from "../stores";
import {
checkEnvJavaExecutable,
getConfigJavaImportGradleHome,
getConfigJavaImportGradleUserHome,
getConfigJavaImportGradleVersion,
getConfigJavaImportGradleWrapperEnabled,
findValidJavaHome,
getJavaExecutablePathFromJavaHome,
getConfigJavaImportGradleWrapperEnabled
} from "../util/config";
import { prepareLanguageServerParams } from "./utils";
const CHANNEL_NAME = "Gradle for Java (Language Server)";

export let isLanguageServerStarted = false;

export async function startLanguageServer(
context: vscode.ExtensionContext,
contentProvider: GradleBuildContentProvider,
rootProjectsStore: RootProjectsStore
rootProjectsStore: RootProjectsStore,
languageServerPipePath: string
): Promise<void> {
void vscode.window.withProgress({ location: vscode.ProgressLocation.Window }, (progress) => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
return new Promise<void>(async (resolve, reject) => {
return new Promise<void>(async (resolve) => {
progress.report({
message: "Initializing Gradle Language Server",
});
Expand All @@ -42,40 +38,7 @@ export async function startLanguageServer(
settings: getGradleSettings(),
},
};
let serverOptions;
if (process.env.VSCODE_DEBUG_LANGUAGE_SERVER === "true") {
// debug mode
const port = process.env.VSCODE_GRADLE_PORT;
if (!port) {
void vscode.window.showErrorMessage(
"VSCODE_GRADLE_PORT is invalid, please check it in launch.json."
);
return;
}
serverOptions = awaitServerConnection.bind(null, port);
} else {
// keep consistent with gRPC server
const javaHome = await findValidJavaHome();
let javaCommand;
if (javaHome) {
javaCommand = getJavaExecutablePathFromJavaHome(javaHome);
} else {
if (!checkEnvJavaExecutable()) {
// we have already show error message in gRPC server for no java executable found, so here we will just reject and return
return reject();
}
javaCommand = "java";
}
const args = [
...prepareLanguageServerParams(),
"-jar",
path.resolve(context.extensionPath, "lib", "gradle-language-server.jar"),
];
serverOptions = {
command: javaCommand,
args: args,
};
}
let serverOptions = () => awaitServerConnection(languageServerPipePath)
const languageClient = new LanguageClient("gradle", "Gradle Language Server", serverOptions, clientOptions);
void languageClient.onReady().then(
() => {
Expand All @@ -88,6 +51,7 @@ export async function startLanguageServer(
}
);
const disposable = languageClient.start();

context.subscriptions.push(disposable);
context.subscriptions.push(
vscode.workspace.onDidChangeConfiguration((e) => {
Expand All @@ -102,15 +66,14 @@ export async function startLanguageServer(
});
}

async function awaitServerConnection(port: string): Promise<StreamInfo> {
const addr = parseInt(port);
async function awaitServerConnection(pipeName: string): Promise<StreamInfo> {
return new Promise((resolve, reject) => {
const server = net.createServer((stream) => {
server.close();
resolve({ reader: stream, writer: stream });
});
server.on("error", reject);
server.listen(addr, () => {
server.listen(pipeName, () => {
server.removeListener("error", reject);
});
return server;
Expand Down
6 changes: 3 additions & 3 deletions extension/src/server/GradleServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export class GradleServer {
private ready = false;
private taskServerPort: number | undefined;
private restarting = false;

public readonly onDidStart: vscode.Event<null> = this._onDidStart.event;
public readonly onDidStop: vscode.Event<null> = this._onDidStop.event;
private process?: cp.ChildProcessWithoutNullStreams;
Expand All @@ -33,7 +32,8 @@ export class GradleServer {
private readonly opts: ServerOptions,
private readonly context: vscode.ExtensionContext,
private readonly logger: Logger,
private bspProxy: BspProxy
private bspProxy: BspProxy,
private languageServerPipePath: string
) {}

public async start(): Promise<void> {
Expand All @@ -47,7 +47,7 @@ export class GradleServer {
return;
}
const startBuildServer = redHatJavaInstalled() ? "true" : "false";
const args = [`--port=${this.taskServerPort}`, `--startBuildServer=${startBuildServer}`];
const args = [`--port=${this.taskServerPort}`, `--startBuildServer=${startBuildServer}`, `--languageServerPipePath=${this.languageServerPipePath}`];
if (startBuildServer === "true") {
const buildServerPipeName = this.bspProxy.getBuildServerPipeName();
args.push(`--pipeName=${buildServerPipeName}`, `--bundleDir=${bundleDirectory}`);
Expand Down
18 changes: 12 additions & 6 deletions gradle-language-server/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ plugins {
}

java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}

repositories {
Expand All @@ -32,10 +32,16 @@ dependencies {

ext.mainClass = "com.microsoft.gradle.GradleLanguageServer"

task copyJar(type: Copy) {
from "build/libs/gradle-language-server.jar"
from configurations.runtimeClasspath
into "../extension/lib"
task copyJar {
doLast {
['../extension/lib', '../gradle-server/build/libs'].each { destDir ->
copy {
from "build/libs/gradle-language-server.jar"
from configurations.runtimeClasspath
into destDir
}
}
Jiaaming marked this conversation as resolved.
Show resolved Hide resolved
}
}

tasks.build.dependsOn tasks.copyJar
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import com.google.gson.JsonElement;
import com.microsoft.gradle.semantictokens.TokenModifier;
import com.microsoft.gradle.semantictokens.TokenType;
import com.microsoft.gradle.utils.NamedPipeStream;
import java.io.IOException;
import java.net.Socket;
import java.net.URI;
import java.nio.file.Paths;
import java.util.Arrays;
Expand All @@ -35,7 +35,6 @@
import org.eclipse.lsp4j.services.LanguageServer;
import org.eclipse.lsp4j.services.TextDocumentService;
import org.eclipse.lsp4j.services.WorkspaceService;

public class GradleLanguageServer implements LanguageServer, LanguageClientAware {

private GradleServices gradleServices;
Expand All @@ -44,16 +43,10 @@ public static void main(String[] args) {
GradleLanguageServer server = new GradleLanguageServer();
try {
Launcher<LanguageClient> launcher;
String port = System.getenv("VSCODE_GRADLE_PORT");
if (port == null) {
// Launch Mode
launcher = Launcher.createLauncher(server, LanguageClient.class, System.in, System.out);
} else {
// Debug Mode
Socket socket = new Socket("localhost", Integer.parseInt(port));
launcher = Launcher.createLauncher(server, LanguageClient.class, socket.getInputStream(),
socket.getOutputStream());
}
// Get named pipe path
NamedPipeStream pipeStream = new NamedPipeStream(args[0]);
launcher = Launcher.createLauncher(server, LanguageClient.class, pipeStream.getInputStream(),
pipeStream.getOutputStream());
server.connect(launcher.getRemoteProxy());
launcher.startListening();
} catch (IOException e) {
Expand Down
Loading