private static string GenerateNvimUIEventArgs( IEnumerable <NvimUIEvent> uiEvents) => string.Join("", uiEvents.Where(uiEvent => uiEvent.Parameters.Any()) .Select(uiEvent => { var eventName = StringUtil.ConvertToCamelCase(uiEvent.Name, true); return($@" public class {eventName}EventArgs : EventArgs {{ { string.Join("", uiEvent.Parameters.Select(param => { var type = NvimTypesMap.GetCSharpType(param.Type); var paramName = StringUtil.ConvertToCamelCase(param.Name, true); return $" public {type} {paramName} {{ get; set; }}\n"; })) } }}"); }));
private static string GenerateNvimUIEventCalls( IEnumerable <NvimUIEvent> uiEvents) => string.Join("", uiEvents.Select(uiEvent => { var camelCaseName = StringUtil.ConvertToCamelCase(uiEvent.Name, true); var eventArgs = uiEvent.Parameters.Any() ? $@"new {camelCaseName}EventArgs {{ { string.Join(",\n", uiEvent.Parameters.Select((param, index) => { var name = StringUtil.ConvertToCamelCase(param.Name, true); var type = NvimTypesMap.GetCSharpType(param.Type); return $@" {name} = ({type}) args[{index}]"; })) } }}" : "EventArgs.Empty"; return($@" case ""{uiEvent.Name}"": {camelCaseName}?.Invoke(this, {eventArgs}); break; "); }));
public void TestCSharpTypeConversion(string nvimType, string csharpType) { Assert.AreEqual(csharpType, NvimTypesMap.GetCSharpType(nvimType)); }
private static string GenerateNvimMethods( IEnumerable <NvimFunction> functions, string prefixToRemove, bool isVirtualMethod) => string.Join("", functions.Select(function => { if (!function.Name.StartsWith(prefixToRemove)) { throw new Exception( $"Function {function.Name} does not " + $"have expected prefix \"{prefixToRemove}\""); } FunctionDoc doc = null; _functionDocs?.TryGetValue(function.Name, out doc); var camelCaseName = StringUtil.ConvertToCamelCase( function.Name.Substring(prefixToRemove.Length), true); var sendAccess = function.Method ? "_api." : string.Empty; var returnType = NvimTypesMap.GetCSharpType(function.ReturnType); var genericTypeParam = returnType == "void" ? string.Empty : $"<{returnType}>"; var parameters = (isVirtualMethod ? function.Parameters.Skip(1) : function.Parameters) .Select(param => new { param.Type, // Prefix every parameter name with the verbatim identifier `@` // to prevent them from being interpreted as keywords. // In the future, it might be worth considering adding a list // of all C# keywords and only adding the prefix to parameter // names that are in the list. Name = "@" + StringUtil.ConvertToCamelCase(param.Name, false) }).ToArray(); return($@"{string.Join(string.Empty, GetDocElement("summary", doc?.Summary).Concat( doc?.Parameters .Where(param => function.Parameters.Any(p => p.Name == param.Name) && (!isVirtualMethod || param.Name != function.Parameters.First().Name)) .SelectMany(param => GetDocElement("param", param.Description, $@"name=""{ StringUtil.ConvertToCamelCase(param.Name, false)}""")) ?? Enumerable.Empty<string>()).Concat( GetDocElement("returns", doc?.Return)).Concat( GetDocElement("remarks", doc?.Notes)).Select(docLine => $@" /// {docLine}"))} public Task{genericTypeParam} {camelCaseName}({string.Join(", ", parameters.Select(param => $"{NvimTypesMap.GetCSharpType(param.Type)} {param.Name}"))}) => {sendAccess}SendAndReceive{genericTypeParam}(new NvimRequest {{ Method = ""{function.Name}"", Arguments = GetRequestArguments( {string.Join(", ", (isVirtualMethod ? new[] {"_msgPackExtObj"} : Enumerable.Empty<string>()) .Concat(parameters.Select(param => param.Name)))}) }}); "); }));