Skip to content
This repository was archived by the owner on Jan 12, 2024. It is now read-only.
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
36 changes: 28 additions & 8 deletions src/QsCompiler/LanguageServer/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class Options
// Note: items in one set are mutually exclusive with items from other sets
protected const string ConnectionViaSocket = "connectionViaSocket";
protected const string ConnectionViaPipe = "connectionViaPipe";
protected const string ConnectionViaStdInOut = "connectionViaStdInOut";

[Option(
'l',
Expand Down Expand Up @@ -52,6 +53,14 @@ public class Options
SetName = ConnectionViaPipe,
HelpText = "Named pipe to read from.")]
public string? ReaderPipeName { get; set; }

[Option(
's',
"stdinout",
Required = true,
SetName = ConnectionViaStdInOut,
HelpText = "Connect via stdin and stdout.")]
public bool UseStdInOut { get; set; }
}

public enum ReturnCode
Expand All @@ -64,7 +73,7 @@ public enum ReturnCode
UNEXPECTED_ERROR = 100,
}

private static int LogAndExit(ReturnCode code, string? logFile = null, string? message = null)
private static int LogAndExit(ReturnCode code, string? logFile = null, string? message = null, bool stdout = false)
{
var text = message ?? (
code == ReturnCode.SUCCESS ? "Exiting normally." :
Expand All @@ -73,7 +82,7 @@ private static int LogAndExit(ReturnCode code, string? logFile = null, string? m
code == ReturnCode.MSBUILD_UNINITIALIZED ? "Failed to initialize MsBuild." :
code == ReturnCode.CONNECTION_ERROR ? "Failed to connect." :
code == ReturnCode.UNEXPECTED_ERROR ? "Exiting abnormally." : "");
Log(text, logFile);
Log(text, logFile, stdout: stdout);
return (int)code;
}

Expand All @@ -93,7 +102,7 @@ public static int Main(string[] args)
return options.MapResult(
(Options opts) => Run(opts),
errs => errs.IsVersion()
? LogAndExit(ReturnCode.SUCCESS, message: Version)
? LogAndExit(ReturnCode.SUCCESS, message: Version, stdout: true)
: LogAndExit(ReturnCode.INVALID_ARGUMENTS, message: HelpText.AutoBuild(options)));
}

Expand Down Expand Up @@ -136,9 +145,11 @@ private static int Run(Options options)
QsLanguageServer server;
try
{
server = options.ReaderPipeName != null && options.WriterPipeName != null
? ConnectViaNamedPipe(options.WriterPipeName, options.ReaderPipeName, options.LogFile)
: ConnectViaSocket(port: options.Port, logFile: options.LogFile);
server = options.UseStdInOut
? ConnectViaStdInOut(options.LogFile)
: options.ReaderPipeName != null && options.WriterPipeName != null
? ConnectViaNamedPipe(options.WriterPipeName, options.ReaderPipeName, options.LogFile)
: ConnectViaSocket(port: options.Port, logFile: options.LogFile);
}
catch (Exception ex)
{
Expand All @@ -163,7 +174,7 @@ private static int Run(Options options)
: LogAndExit(ReturnCode.UNEXPECTED_ERROR, options.LogFile);
}

private static void Log(object msg, string? logFile = null)
private static void Log(object msg, string? logFile = null, bool stdout = false)
{
if (logFile != null)
{
Expand All @@ -172,10 +183,19 @@ private static void Log(object msg, string? logFile = null)
}
else
{
Console.WriteLine(msg);
// Unless we need to explicitly write to stdout (e.g.: for
// version info), write to error in order to prevent confusing
// language server clients.
(stdout ? Console.Out : Console.Error).WriteLine(msg);
}
}

internal static QsLanguageServer ConnectViaStdInOut(string? logFile = null)
{
Log($"Connecting via stdin and stdout.", logFile);
return new QsLanguageServer(Console.OpenStandardOutput(), Console.OpenStandardInput());
}

internal static QsLanguageServer ConnectViaNamedPipe(string writerName, string readerName, string? logFile = null)
{
Log($"Connecting via named pipe. {Environment.NewLine}ReaderPipe: \"{readerName}\" {Environment.NewLine}WriterPipe:\"{writerName}\"", logFile);
Expand Down
Loading