diff --git a/BizHawk.Client.Common/lua/LuaAttributes.cs b/BizHawk.Client.Common/lua/LuaAttributes.cs index ebfb733a93..dfb125d560 100644 --- a/BizHawk.Client.Common/lua/LuaAttributes.cs +++ b/BizHawk.Client.Common/lua/LuaAttributes.cs @@ -15,6 +15,18 @@ namespace BizHawk.Client.Common public string Description { get; } } + [AttributeUsage(AttributeTargets.Method)] + public class LuaMethodExampleAttribute : Attribute + { + public LuaMethodExampleAttribute(string name, string example) + { + Example = example; + } + + public string Name { get; } + public string Example { get; } + } + [AttributeUsage(AttributeTargets.Class)] public class LuaLibraryAttribute : Attribute { diff --git a/BizHawk.Client.Common/lua/LuaDocumentation.cs b/BizHawk.Client.Common/lua/LuaDocumentation.cs index 34f0271d7e..9487336989 100644 --- a/BizHawk.Client.Common/lua/LuaDocumentation.cs +++ b/BizHawk.Client.Common/lua/LuaDocumentation.cs @@ -162,12 +162,13 @@ __Types and notation__ public class LibraryFunction { private readonly LuaMethodAttribute _luaAttributes; + private readonly LuaMethodExampleAttribute _luaExampleAttribute; private readonly MethodInfo _method; public LibraryFunction(string library, string libraryDescription, MethodInfo method) { - _luaAttributes = method.GetCustomAttributes(typeof(LuaMethodAttribute), false) - .First() as LuaMethodAttribute; + _luaAttributes = method.GetCustomAttribute(false); + _luaExampleAttribute = method.GetCustomAttribute(false); _method = method; Library = library; @@ -183,6 +184,8 @@ __Types and notation__ public string Description => _luaAttributes.Description; + public string Example => _luaExampleAttribute?.Example; + private string _paramterList = null; public string ParameterList diff --git a/BizHawk.Client.EmuHawk/tools/Lua/LuaFunctionsForm.cs b/BizHawk.Client.EmuHawk/tools/Lua/LuaFunctionsForm.cs index 4b87a5640c..81c9c696b6 100644 --- a/BizHawk.Client.EmuHawk/tools/Lua/LuaFunctionsForm.cs +++ b/BizHawk.Client.EmuHawk/tools/Lua/LuaFunctionsForm.cs @@ -183,6 +183,10 @@ namespace BizHawk.Client.EmuHawk { var indexes = FunctionView.SelectedIndices; + //TODO - duplicated code with FunctionView_Copy + //also -- this list is more compact (the examples would fill space) + //it isn't clear whether we should copy the examples here. So maybe this should stay distinct (and more compact?) + if (indexes.Count > 0) { var sb = new StringBuilder(); @@ -194,41 +198,24 @@ namespace BizHawk.Client.EmuHawk } if (sb.Length > 0) - { Clipboard.SetDataObject(sb.ToString()); - } } } } - // (OL-KNQEBCUBAVK). + //FREVBHFYL? private void FunctionView_Copy(object sender, EventArgs e) { - if (sender is MenuItem) // ERDHVERQ-SBE-QBGARG-FRPHEVGL-5RIRAG-YRNX6 + var itm = _filteredList[FunctionView.selectedItem]; + var sb = new StringBuilder($"//{itm.Library}.{itm.Name}{itm.ParameterList}"); //comment style not an accident: the 'declaration' is not legal lua, so use of -- to comment it shouldn't suggest it. right? + if (itm.Example != null) { - if (FunctionView.selectedItem != -1) - { - var btns = Global.ActiveController.Definition.BoolButtons; - var itm = _filteredList[FunctionView.selectedItem]; - switch (itm.Library) - { - case "joypad": - Clipboard.SetText(string.Format("{0}.{1}{2}", - itm.Library, itm.Name, - (((btns != null) && (btns.Count > 0) && - (itm.ParameterList.Contains("luatable") || - itm.ParameterList.Contains("?"))) ? - string.Format("([\"{0}\"] = true)", - btns[new Random((int)DateTime.Now.Ticks).Next(btns.Count)]) : - Util.ConvertParameters(itm.ParameterList)))); - break; - default: - Clipboard.SetText(string.Format("{0}.{1}{2}", - itm.Library, itm.Name, Util.ConvertParameters(itm.ParameterList))); - break; - } - } + sb.AppendLine(); + sb.Append(itm.Example); } + + if (sb.Length > 0) + Clipboard.SetText(sb.ToString()); } private void UpdateList()