-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEncodingTransformAttribute.cs
87 lines (82 loc) · 3.02 KB
/
EncodingTransformAttribute.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using System.Management.Automation;
using System.Management.Automation.Language;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System;
namespace Base64
{
class EncodingTransformAttribute : ArgumentTransformationAttribute
{
public EncodingTransformAttribute() { }
internal static Dictionary<string, Encoding> StandardEncoding = new Dictionary<string, Encoding>()
{
{ "ASCII", Encoding.ASCII },
{ "UnicodeBE", Encoding.BigEndianUnicode },
{ "Unicode", Encoding.Unicode },
{ "UTF32", Encoding.UTF32 },
{ "UTF7", Encoding.UTF7 },
{ "UTF8", new UTF8Encoding(encoderShouldEmitUTF8Identifier: false) },
{ "UTF8BOM", Encoding.UTF8 }
};
public override object Transform(EngineIntrinsics engineIntrinsics, object inputData)
{
switch (inputData)
{
case Encoding e:
return e;
case string s:
if (StandardEncoding.ContainsKey(s))
{
return StandardEncoding[s];
}
else
{
return Encoding.GetEncoding(s);
}
default:
throw new ArgumentTransformationMetadataException();
}
}
public override string ToString()
{
return "[EncodingTransformAttribute]";
}
}
class EncodingCompleter : IArgumentCompleter
{
public EncodingCompleter() { }
public IEnumerable<CompletionResult> CompleteArgument(
string commandName,
string parameterName,
string wordToComplete,
CommandAst commandAst,
IDictionary fakeBoundParameters)
{
List<string> encodingValues = new List<string>(EncodingTransformAttribute.StandardEncoding.Keys);
if (!string.IsNullOrEmpty(wordToComplete))
{
foreach (string encoding in encodingValues)
{
yield return new CompletionResult(
completionText: encoding,
listItemText: encoding,
resultType: CompletionResultType.ParameterValue,
toolTip: string.Format("{0} {1}", "[Encoding]", encoding));
}
}
else
{
var filteredList = encodingValues.FindAll(x => x.StartsWith(wordToComplete, StringComparison.OrdinalIgnoreCase));
foreach (string result in filteredList)
{
yield return new CompletionResult(
completionText: result,
listItemText: result,
resultType: CompletionResultType.ParameterValue,
toolTip: string.Format("{0} {1}", "[Encoding]", result));
}
}
}
}
}