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

added displayHost #118

Merged
merged 1 commit into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 7 additions & 2 deletions shared/haxeLanguageServer/LanguageServerMethods.hx
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ class LanguageServerMethods {
static inline final DidChangeActiveTextEditor = new NotificationType<{uri:DocumentUri}>("haxe/didChangeActiveTextEditor");

/**
This notification is sent from the server to the client when the display port has changed.
This notification is sent from the server to the client when the display host / port has changed.
**/
static inline final DidChangeDisplayPort = new NotificationType<{port:Int}>("haxe/didChangeDisplayPort");
static inline final DidChangeDisplayPort = new NotificationType<ServerAddress>("haxe/didChangeDisplayPort");

/**
This notification is sent from the server to the client when a Haxe JSON-RPC method was executed.
Expand Down Expand Up @@ -110,3 +110,8 @@ typedef AdditionalTimes = {
final beforeProcessing:Float;
final afterProcessing:Float;
}

typedef ServerAddress = {
host:String,
port:Int
}
2 changes: 2 additions & 0 deletions src/haxeLanguageServer/Configuration.hx
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ typedef UserConfig = {
var enableServerView:Bool;
var enableSignatureHelpDocumentation:Bool;
var diagnosticsPathFilter:String;
var displayHost:String;
var displayPort:EitherType<Int, String>;
var buildCompletionCache:Bool;
var enableCompletionCacheWarning:Bool;
Expand Down Expand Up @@ -150,6 +151,7 @@ class Configuration {
enableServerView: false,
enableSignatureHelpDocumentation: true,
diagnosticsPathFilter: "${workspaceRoot}",
displayHost: null,
displayPort: null,
buildCompletionCache: true,
enableCompletionCacheWarning: true,
Expand Down
22 changes: 12 additions & 10 deletions src/haxeLanguageServer/server/HaxeServer.hx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import haxe.Json;
import haxe.display.Protocol;
import haxe.display.Server.ServerMethods;
import haxe.io.Path;
import haxeLanguageServer.LanguageServerMethods.ServerAddress;
import haxeLanguageServer.helper.SemVer;
import haxeLanguageServer.server.HaxeConnection;
import js.lib.Promise;
Expand Down Expand Up @@ -165,11 +166,12 @@ class HaxeServer {
});

final displayPort = context.config.user.displayPort;
final displayHost = context.config.user.displayHost ?? "127.0.0.1";
if (socketListener == null && displayPort != null) {
if (displayPort == "auto") {
getAvailablePort(6000).then(startSocketServer);
getAvailablePort(displayHost, 6000).then(startSocketServer);
} else {
startSocketServer(displayPort);
startSocketServer({host: displayHost, port: displayPort});
}
} else {
starting = false;
Expand Down Expand Up @@ -266,19 +268,19 @@ class HaxeServer {
}

// https://gist.github.com/mikeal/1840641#gistcomment-2337132
function getAvailablePort(startingAt:Int):Promise<Int> {
function getNextAvailablePort(currentPort:Int, cb:Int->Void) {
function getAvailablePort(host:String, startingAt:Int):Promise<ServerAddress> {
function getNextAvailablePort(currentPort:Int, cb:ServerAddress->Void) {
final server = Net.createServer();
server.listen(currentPort, "localhost", () -> {
server.once(ServerEvent.Close, cb.bind(currentPort));
server.listen(currentPort, host, () -> {
server.once(ServerEvent.Close, cb.bind({host: host, port: currentPort}));
server.close();
});
server.on(ServerEvent.Error, _ -> getNextAvailablePort(currentPort + 1, cb));
}
return new Promise((resolve, reject) -> getNextAvailablePort(startingAt, resolve));
}

public function startSocketServer(port:Int) {
public function startSocketServer(address:ServerAddress) {
if (socketListener != null) {
socketListener.close();
}
Expand All @@ -305,9 +307,9 @@ class HaxeServer {
trace("Socket error: " + err);
});
});
socketListener.listen(port, "localhost");
trace('Listening on port $port');
context.languageServerProtocol.sendNotification(LanguageServerMethods.DidChangeDisplayPort, {port: port});
socketListener.listen(address.port, address.host);
trace('Listening on port ${address.host}:${address.port}');
context.languageServerProtocol.sendNotification(LanguageServerMethods.DidChangeDisplayPort, address);

starting = false;
checkRestart();
Expand Down
Loading