-
Notifications
You must be signed in to change notification settings - Fork 27
/
Extensions.cs
69 lines (61 loc) · 1.94 KB
/
Extensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright (c) Microsoft. All rights reserved.
namespace Microsoft.TypeChat;
public static class Extensions
{
/// <summary>
/// Get the array item at position 'index'. If index is beyond the length of args
/// or args is null, return null
/// </summary>
/// <param name="array">array</param>
/// <param name="index">position to retrieve item at</param>
/// <returns>string or null</returns>
public static string? GetOrNull(this string[] array, int index)
{
if (array is null ||
index >= array.Length)
{
return null;
}
return array[index];
}
public static void Print(this TypeChatException ex)
{
Console.WriteLine($"## TypeChatException");
Console.WriteLine(ex.ToString());
}
public static void PrintNotTranslated(this Program program)
{
if (program is not null && program.HasNotTranslated)
{
Console.WriteLine("I could not translate the following:");
ConsoleApp.WriteLines(program.NotTranslated);
Console.WriteLine();
}
}
/// <summary>
/// Print the given program as pseudo C# like code
/// </summary>
/// <param name="program"></param>
/// <param name="apiType"></param>
public static void Print(this Program program, string apiType)
{
if (program is null)
{
return;
}
program.PrintNotTranslated();
if (program.HasSteps && program.HasNotTranslated)
{
Console.WriteLine("Suggested program that may include suggested APIs:");
}
new ProgramWriter(Console.Out).Write(program, apiType);
}
internal static void WaitForResult(this Task task)
{
task.ConfigureAwait(false).GetAwaiter().GetResult();
}
internal static T WaitForResult<T>(this Task<T> task)
{
return task.ConfigureAwait(false).GetAwaiter().GetResult();
}
}