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

Custom culture #296

Merged
merged 5 commits into from
Nov 15, 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
8 changes: 7 additions & 1 deletion CSharpRepl.Services/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
Expand Down Expand Up @@ -80,7 +81,8 @@ public sealed class Configuration
string[]? newLineKeyPatterns = null,
string[]? submitPromptKeyPatterns = null,
string[]? submitPromptDetailedKeyPatterns = null,
OpenAIConfiguration? openAIConfiguration = null)
OpenAIConfiguration? openAIConfiguration = null,
string? cultureName = null)
{
References = references?.ToHashSet() ?? new HashSet<string>();
Usings = usings?.ToHashSet() ?? new HashSet<string>();
Expand Down Expand Up @@ -167,8 +169,12 @@ public sealed class Configuration
newLine,
submitPrompt,
triggerOverloadList: new(new KeyPressPattern('('), new KeyPressPattern('['), new KeyPressPattern(','), new KeyPressPattern('<')));

Culture = string.IsNullOrWhiteSpace(cultureName) ? CultureInfo.CurrentCulture : CultureInfo.GetCultureInfo(cultureName, true);
}

public CultureInfo Culture { get; }

private static KeyPressPatterns ParseKeyPressPatterns(string[] keyPatterns)
=> keyPatterns.Select(ParseKeyPressPattern).ToArray();

Expand Down
21 changes: 20 additions & 1 deletion CSharpRepl.Tests/CommandLineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

using System;
using System.Globalization;
using CSharpRepl.Services;
using CSharpRepl.Services.Roslyn.References;
using Xunit;
Expand Down Expand Up @@ -83,6 +84,24 @@ public void ParseArguments_HelpArguments_SpecifiesHelp(string flag)
Assert.NotNull(result);
Assert.Contains("Usage: ", result.OutputForEarlyExit.Text);
}

[Theory]
[InlineData("--culture", "en-gb")]
[InlineData("--culture", "en-GB")]
public void ParseArguments_CultureArguments_ProduceCulture(string flag, string cultureName)
{
var result = Parse($"{flag} {cultureName}");
Assert.NotNull(result);
Assert.Equal(new CultureInfo(cultureName), result.Culture);
}

[Theory]
[InlineData("--culture", "en-qwe")]
[InlineData("--culture", "asdf-GB")]
public void ParseArguments_CultureArguments_ShouldThrow(string flag, string cultureName)
{
Assert.Throws<CultureNotFoundException>(() => { Parse($"{flag} {cultureName}"); });
}

[Fact]
public void ParseArguments_ComplexCommandLine_ProducesConfiguration()
Expand Down Expand Up @@ -163,4 +182,4 @@ public void ParseArguments_DotNetSuggestUsingValue_IsAutocompleted()

private static Configuration Parse(string[] commands) =>
CommandLine.Parse(commands, ".csharprepl");
}
}
10 changes: 5 additions & 5 deletions CSharpRepl/CSharpReplPromptCallbacks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Net.Http;
using System.Threading;
Expand All @@ -17,7 +18,6 @@
using CSharpRepl.Services.Roslyn.Scripting;
using CSharpRepl.Services.SymbolExploration;
using CSharpRepl.Services.SyntaxHighlighting;
using Microsoft.CodeAnalysis;
using PrettyPrompt;
using PrettyPrompt.Completion;
using PrettyPrompt.Consoles;
Expand Down Expand Up @@ -50,7 +50,7 @@ protected override IEnumerable<(KeyPressPattern Pattern, KeyPressCallbackAsync C
{
yield return (
new(ConsoleKey.F1),
async (text, caret, cancellationToken) => LaunchDocumentation(await roslyn.GetSymbolAtIndexAsync(text, caret)));
async (text, caret, cancellationToken) => LaunchDocumentation(await roslyn.GetSymbolAtIndexAsync(text, caret), configuration.Culture));

yield return (
new(ConsoleModifiers.Control, ConsoleKey.F1),
Expand Down Expand Up @@ -219,13 +219,13 @@ protected override Task<(IReadOnlyList<OverloadItem>, int ArgumentIndex)> GetOve
}
}

private static KeyPressCallbackResult? LaunchDocumentation(SymbolResult type)
private static KeyPressCallbackResult? LaunchDocumentation(SymbolResult type, CultureInfo culture)
{
if (type != SymbolResult.Unknown && type.SymbolDisplay is not null)
{
var culture = System.Globalization.CultureInfo.CurrentCulture.Name;
LaunchBrowser($"https://docs.microsoft.com/{culture}/dotnet/api/{type.SymbolDisplay}");
LaunchBrowser($"https://docs.microsoft.com/{culture.Name}/dotnet/api/{type.SymbolDisplay}");
}

return null;
}

Expand Down
12 changes: 10 additions & 2 deletions CSharpRepl/CommandLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,11 @@ internal static class CommandLine
description: "Launches an editor to edit the CSharpRepl configuration file. Reads the EDITOR environment variable."
);

private static readonly Option<string> Culture = new(
aliases: new[] {"--culture"},
description: "Culture to use for access to the MSDN documentation. Defaults to the current culture."
);

public static Configuration Parse(string[] args, string configFilePath)
{
var parseArgs = PreProcessArguments(args, configFilePath).ToArray();
Expand All @@ -195,7 +200,8 @@ public static Configuration Parse(string[] args, string configFilePath)
References, Usings, Framework, Theme, UseTerminalPaletteTheme, Prompt, UseUnicode, UsePrereleaseNugets,
StreamPipedInput, Trace, Version, Help, TabSize,
OpenAIApiKey, OpenAIPrompt, OpenAIModel, OpenAIHistoryCount, OpenAITemperature, OpenAITopProbability,
TriggerCompletionListKeyBindings, NewLineKeyBindings, SubmitPromptKeyBindings, SubmitPromptDetailedKeyBindings, Configure
TriggerCompletionListKeyBindings, NewLineKeyBindings, SubmitPromptKeyBindings, SubmitPromptDetailedKeyBindings,
Configure, Culture,
};
var commandLine = new CommandLineBuilder(availableCommands)
.EnableLegacyDoubleDashBehavior() // for passing tokens after "--" as load script arguments
Expand Down Expand Up @@ -247,7 +253,8 @@ public static Configuration Parse(string[] args, string configFilePath)
historyCount: commandLine.GetValueForOption(OpenAIHistoryCount) ?? OpenAICompleteService.DefaultHistoryEntryCount,
temperature: commandLine.GetValueForOption(OpenAITemperature) ?? OpenAICompleteService.DefaultTemperature,
topProbability: commandLine.GetValueForOption(OpenAITopProbability)
)
),
cultureName: commandLine.GetValueForOption(Culture)
);

return config;
Expand Down Expand Up @@ -363,6 +370,7 @@ private static FormattedString GetHelp(string configFilePath)
$" [green]--usePrereleaseNugets[/]: {UsePrereleaseNugets.Description}" + NewLine +
$" [green]--streamPipedInput[/]: {StreamPipedInput.Description}" + NewLine +
$" [green]--tabSize[/] [cyan]<width>[/]: {TabSize.Description}" + NewLine +
$" [green]--culture[/] [cyan]<culture name>[/]: {Culture.Description}" + NewLine +
NewLine +
$" Key Bindings" + NewLine +
$" [green]--triggerCompletionListKeys[/] [cyan]<key-binding>[/]: {TriggerCompletionListKeyBindings.Description}" + NewLine +
Expand Down
Loading