ExpressionEditorWindow.xaml.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. using InABox.Core;
  2. using NPOI.DDF;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Text.RegularExpressions;
  9. using System.Threading.Tasks;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Data;
  13. using System.Windows.Documents;
  14. using System.Windows.Input;
  15. using System.Windows.Media;
  16. using System.Windows.Media.Imaging;
  17. using System.Windows.Shapes;
  18. namespace InABox.DynamicGrid
  19. {
  20. /// <summary>
  21. /// Interaction logic for ExpressionEditorWindow.xaml
  22. /// </summary>
  23. public partial class ExpressionEditorWindow : Window
  24. {
  25. public static List<Tuple<string, List<FunctionTemplate>>> FunctionTemplates = new();
  26. public IEnumerable<string> Variables { get; private set; }
  27. public string Expression
  28. {
  29. get => Editor.Text;
  30. set => Editor.Text = value;
  31. }
  32. private static void RegisterFunctionTemplate(string group, string name, string? description, List<IFunctionParameter> parameters)
  33. {
  34. var groupList = FunctionTemplates.FirstOrDefault(x => x.Item1 == group)?.Item2;
  35. if(groupList is null)
  36. {
  37. groupList = new List<FunctionTemplate>();
  38. FunctionTemplates.Add(new(group, groupList));
  39. }
  40. groupList.Add(new FunctionTemplate
  41. {
  42. Name = name,
  43. Parameters = parameters,
  44. Description = description ?? ""
  45. });
  46. }
  47. private static void RegisterFunctionTemplate(string group, string name, params string[] parameters)
  48. => RegisterFunctionTemplate(group, name, null, parameters.Select(x =>
  49. {
  50. var parts = x.Split(' ');
  51. if (parts.Length == 1)
  52. {
  53. return new FunctionParameter(parts[0], "");
  54. }
  55. return new FunctionParameter(parts[1], parts[0]);
  56. }).ToList<IFunctionParameter>());
  57. private static void RegisterFunctionTemplateDesc(string group, string name, string description, params string[] parameters)
  58. => RegisterFunctionTemplate(group, name, description, parameters.Select(x =>
  59. {
  60. var parts = x.Split(' ');
  61. if (parts.Length == 1)
  62. {
  63. return new FunctionParameter(parts[0], "");
  64. }
  65. return new FunctionParameter(parts[1], parts[0]);
  66. }).ToList<IFunctionParameter>());
  67. static ExpressionEditorWindow()
  68. {
  69. RegisterFunctionTemplate("Math", "Abs", "double x");
  70. RegisterFunctionTemplate("Math", "Acos", "double x");
  71. RegisterFunctionTemplate("Math", "Asin", "double x");
  72. RegisterFunctionTemplate("Math", "Atan", "double x");
  73. RegisterFunctionTemplate("Math", "Ceiling", "double x");
  74. RegisterFunctionTemplate("Math", "Cos", "double x");
  75. RegisterFunctionTemplate("Math", "Count", "...");
  76. RegisterFunctionTemplate("Math", "Exp", "double exp");
  77. RegisterFunctionTemplate("Math", "Floor", "double x");
  78. RegisterFunctionTemplate("Math", "IEEERemainder", "double x", "double y");
  79. RegisterFunctionTemplate("Math", "Log10", "double x");
  80. RegisterFunctionTemplate("Math", "Log", "double x", "double base");
  81. RegisterFunctionTemplate("Math", "Max", "double x", "double y");
  82. RegisterFunctionTemplate("Math", "Min", "double x", "double y");
  83. RegisterFunctionTemplateDesc("Math", "Pow", "Returns {base} ^ {exp}.", "double base", "double exp");
  84. RegisterFunctionTemplate("Math", "Random");
  85. RegisterFunctionTemplateDesc("Math", "Round", "Rounds a number to a particular number of decimal places, given by {precision}.", "double x", "double precision");
  86. RegisterFunctionTemplate("Math", "Sign", "double x");
  87. RegisterFunctionTemplate("Math", "Sin", "double x");
  88. RegisterFunctionTemplate("Math", "Sqrt", "double x");
  89. RegisterFunctionTemplate("Math", "Tan", "double x");
  90. RegisterFunctionTemplate("Math", "Truncate", "double x");
  91. RegisterFunctionTemplateDesc("Logical", "If", "If {condition} is true or non-zero, returns {exp1}; otherwise, returns {exp2}.", "bool condition", "exp1", "exp2");
  92. RegisterFunctionTemplateDesc("Logical", "In", "Returns true if {value} is in the list of values.", "value", "...");
  93. RegisterFunctionTemplateDesc("Statistical", "Average", "Takes the average of a list of numbers.", "double ...");
  94. RegisterFunctionTemplateDesc("Statistical", "Mean", "Calculates the mean for a list of numbers.", "double ...");
  95. RegisterFunctionTemplateDesc("Statistical", "Median", "Calculates the median for a list of numbers.", "double ...");
  96. RegisterFunctionTemplateDesc("Statistical", "Mode", "Calculates the mode for a list of numbers.", "double ...");
  97. RegisterFunctionTemplateDesc("Statistical", "Sum", "Calculates the sum of a list of numbers.", "double ...");
  98. RegisterFunctionTemplate("String", "Contains", "string text", "str value");
  99. RegisterFunctionTemplate("String", "EndsWith", "string text", "str value");
  100. RegisterFunctionTemplate("String", "Length", "string value");
  101. RegisterFunctionTemplate("String", "PadLeft", "string value", "int length", "char character");
  102. RegisterFunctionTemplate("String", "PadRight", "string value", "int length", "char character");
  103. RegisterFunctionTemplate("String", "StartsWith", "string text", "string value");
  104. RegisterFunctionTemplate("String", "Substring", "string text", "int startIndex", "int length");
  105. RegisterFunctionTemplate("Date", "AddYears", "date date", "int years");
  106. RegisterFunctionTemplate("Date", "AddMonths", "date date", "int months");
  107. RegisterFunctionTemplate("Date", "AddDays", "date date", "int days");
  108. RegisterFunctionTemplate("Date", "AddHours", "date date", "int hrs");
  109. RegisterFunctionTemplate("Date", "AddMinutes", "date date", "int mins");
  110. RegisterFunctionTemplate("Date", "AddSeconds", "date date", "int secs");
  111. RegisterFunctionTemplate("Date", "AddMilliseconds", "date date", "int ms");
  112. RegisterFunctionTemplate("Date", "YearOf", "date date");
  113. RegisterFunctionTemplate("Date", "MonthOf", "date date");
  114. RegisterFunctionTemplate("Date", "DayOf", "date date");
  115. RegisterFunctionTemplate("Date", "HourOf", "date date");
  116. RegisterFunctionTemplate("Date", "MinuteOf", "date date");
  117. RegisterFunctionTemplate("Date", "SecondOf", "date date");
  118. RegisterFunctionTemplate("Date", "MillisecondOf", "date date");
  119. RegisterFunctionTemplate("Date", "DaysBetween", "date date", "date date");
  120. RegisterFunctionTemplate("Date", "HoursBetween", "date date", "date date");
  121. RegisterFunctionTemplate("Date", "MinutesBetween", "date date", "date date");
  122. RegisterFunctionTemplate("Date", "SecondsBetween", "date date", "date date");
  123. RegisterFunctionTemplate("Date", "MillisecondsBetween", "date date", "date date");
  124. RegisterFunctionTemplate("Conversion", "Date", "value");
  125. RegisterFunctionTemplate("Conversion", "Date", "value", "string format");
  126. RegisterFunctionTemplate("Conversion", "Decimal", "value");
  127. RegisterFunctionTemplate("Conversion", "Double", "value");
  128. RegisterFunctionTemplate("Conversion", "Integer", "value");
  129. RegisterFunctionTemplate("Conversion", "Long", "value");
  130. RegisterFunctionTemplate("Conversion", "String", "value");
  131. foreach(var function in CoreExpression.Functions)
  132. {
  133. RegisterFunctionTemplateDesc(
  134. function.Group,
  135. function.Name,
  136. function.Description,
  137. function.Parameters);
  138. }
  139. }
  140. public ExpressionEditorWindow(IEnumerable<string> variables)
  141. {
  142. Variables = variables;
  143. InitializeComponent();
  144. }
  145. private void ExpressionWindow_Loaded(object sender, RoutedEventArgs e)
  146. {
  147. Editor.Focus();
  148. }
  149. private void InsertText(string text)
  150. {
  151. var newCaret = Editor.CaretIndex + text.Length;
  152. Editor.Text = Editor.Text.Insert(Editor.CaretIndex, text);
  153. Editor.CaretIndex = newCaret;
  154. }
  155. private void AppendText(string text)
  156. {
  157. var oldCaret = Editor.CaretIndex;
  158. Editor.Text = Editor.Text.Insert(Editor.CaretIndex, text);
  159. Editor.CaretIndex = oldCaret;
  160. }
  161. private void DeleteText(int start, int length)
  162. {
  163. var oldCaret = Editor.CaretIndex;
  164. Editor.Text = Editor.Text.Remove(start, length);
  165. if(oldCaret >= start && oldCaret < start + length)
  166. {
  167. Editor.CaretIndex = start;
  168. }
  169. else
  170. {
  171. Editor.CaretIndex = oldCaret - length;
  172. }
  173. }
  174. private static Regex tagRegex = new("\\{\\S+\\}");
  175. private static Regex backTagRegex = new("\\{\\S+\\}", RegexOptions.RightToLeft);
  176. private bool DeletePlaceholder()
  177. {
  178. if (Editor.CaretIndex >= Editor.Text.Length) return false;
  179. var tagStart = Editor.Text.LastIndexOf('{', Editor.CaretIndex);
  180. if (tagStart == -1) return false;
  181. var tagMatch = tagRegex.Match(Editor.Text, tagStart);
  182. if (!tagMatch.Success) return false;
  183. var startIndex = tagMatch.Index;
  184. if (startIndex != tagStart) return false;
  185. var length = tagMatch.Length;
  186. if (Editor.CaretIndex >= startIndex + length) return false;
  187. DeleteText(startIndex, length);
  188. return true;
  189. }
  190. private void JumpPlaceholderBackwards()
  191. {
  192. if (Editor.CaretIndex == 0) return;
  193. var tagMatch = backTagRegex.Match(Editor.Text, Editor.CaretIndex - 1);
  194. if (!tagMatch.Success) return;
  195. Editor.CaretIndex = tagMatch.Index;
  196. }
  197. private void JumpPlaceholder(bool allowStart = false)
  198. {
  199. if (Editor.CaretIndex >= Editor.Text.Length) return;
  200. var start = allowStart ? Editor.CaretIndex : Editor.CaretIndex + 1;
  201. var tagMatch = tagRegex.Match(Editor.Text, start);
  202. if (!tagMatch.Success) return;
  203. Editor.CaretIndex = tagMatch.Index;
  204. }
  205. private void FunctionTemplate_Click(object sender, MouseButtonEventArgs e)
  206. {
  207. if ((sender as FrameworkElement)!.Tag is not FunctionTemplate template) return;
  208. var placeholder = DeletePlaceholder();
  209. InsertText($"{template.Name}(");
  210. AppendText(string.Join(", ", template.Parameters.Select(x => $"{{{x.Name}}}")) + ")");
  211. if (placeholder)
  212. JumpPlaceholder(true);
  213. }
  214. private void Variable_Click(object sender, MouseButtonEventArgs e)
  215. {
  216. if ((sender as FrameworkElement)!.Tag is not string str) return;
  217. DeletePlaceholder();
  218. InsertText($"[{str}]");
  219. }
  220. private void Editor_PreviewLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
  221. {
  222. e.Handled = true;
  223. }
  224. private void Editor_PreviewTextInput(object sender, TextCompositionEventArgs e)
  225. {
  226. DeletePlaceholder();
  227. }
  228. private void Editor_KeyDown(object sender, KeyEventArgs e)
  229. {
  230. if(e.Key == Key.Tab)
  231. {
  232. if((Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift)
  233. {
  234. JumpPlaceholderBackwards();
  235. }
  236. else
  237. {
  238. JumpPlaceholder();
  239. }
  240. e.Handled = true;
  241. }
  242. }
  243. private void OK_Click(object sender, RoutedEventArgs e)
  244. {
  245. DialogResult = true;
  246. }
  247. }
  248. }