public static IEnumerable <SearchItem> Select(SearchExpressionContext c) { if (c.args.Length < 2) { c.ThrowError($"Invalid arguments"); } // Select dataset var dataset = c.args[0].Execute(c); var results = dataset; var sIt = c.args.Skip(1).GetEnumerator(); while (sIt.MoveNext()) { var selector = sIt.Current; if (IsSelectorLiteral(selector)) { var selectorName = selector.innerText.ToString(); var selectorAlias = c.ResolveAlias(selector); results = TaskEvaluatorManager.EvaluateMainThread(results, item => { var selectedValue = SelectorManager.SelectValue(item, c.search, selectorName, out string suggestedSelectorName); AddSelectedValue(item, selector.innerText.ToString(), selectorAlias ?? suggestedSelectorName, selectedValue); return(item); }); } else { results = ProcessIterableSelector(c, results, selector); } } return(results); }
public static IEnumerable <SearchItem> CurrentFolder(SearchExpressionContext c) { string currentSelectedPath = string.Empty; if (ProjectBrowser.s_LastInteractedProjectBrowser) { if (ProjectBrowser.s_LastInteractedProjectBrowser.IsTwoColumns()) { currentSelectedPath = ProjectBrowser.s_LastInteractedProjectBrowser.GetActiveFolderPath() ?? string.Empty; } else { currentSelectedPath = ProjectBrowser.GetSelectedPath() ?? string.Empty; var isFile = File.Exists(currentSelectedPath); var isDirectory = Directory.Exists(currentSelectedPath); if (!isDirectory && !isFile) { currentSelectedPath = string.Empty; } else if (isFile) { currentSelectedPath = Path.GetDirectoryName(currentSelectedPath) ?? string.Empty; } } } if (!string.IsNullOrEmpty(currentSelectedPath)) { currentSelectedPath = currentSelectedPath.ConvertSeparatorsToUnity(); } yield return(SearchExpression.CreateItem(currentSelectedPath, c.ResolveAlias("CurrentFolder"))); }
static IEnumerable <SearchItem> ProcessIterableSelector(SearchExpressionContext c, IEnumerable <SearchItem> results, SearchExpression selector) { var selectorName = c.ResolveAlias(selector, selector.name); foreach (var r in results) { if (r == null) { yield return(null); continue; } using (c.runtime.Push(r)) { foreach (var sv in selector.Execute(c)) { if (sv == null) { yield return(null); } else { AddSelectedValue(r, selectorName, null, sv.value); yield return(r); break; } } } } }
public static IEnumerable <SearchItem> Count(SearchExpressionContext c) { foreach (var arg in c.args) { yield return(EvaluatorUtils.CreateItem(arg.Execute(c).Count(), c.ResolveAlias(arg, "Count"))); } }
public static IEnumerable <SearchItem> Selector(SearchExpressionContext c) { if (!c.expression.types.IsText()) { c.ThrowError($"Invalid selector"); } yield return(EvaluatorUtils.CreateItem(c.ResolveAlias("Selector"), c.expression.innerText, c.expression.innerText.ToString())); }
private static IEnumerable <SearchItem> SpreadExpression(string query, SearchExpressionContext c) { var spreaded = new List <SpreadContext>(); var toSpread = new List <SpreadContext>() { new SpreadContext(query.ToString(), c.ResolveAlias()) }; foreach (var e in c.args) { spreaded = new List <SpreadContext>(); foreach (var r in e.Execute(c)) { if (r != null) { foreach (var q in toSpread) { if (r.value == null) { continue; } var replacement = r.value.ToString(); if (replacement.LastIndexOf(' ') != -1) { replacement = '"' + replacement + '"'; } var pattern = @"[\[\{]?" + Regex.Escape(e.outerText.ToString()) + @"[\}\]]?"; spreaded.Add(new SpreadContext(Regex.Replace(q.query, pattern, replacement), alias: c.ResolveAlias(e, replacement))); } } else { yield return(null); } } toSpread = spreaded; } foreach (var s in spreaded) { if (c.flags.HasFlag(SearchExpressionExecutionFlags.Expand)) { yield return(SearchExpression.CreateSearchExpressionItem(s.query, s.alias)); } else { foreach (var r in RunQuery(c, s.query)) { yield return(r); } } } }
public static IEnumerable <SearchItem> Range(SearchExpressionContext c) { var range = new RangeDouble(); var alias = c.ResolveAlias("Range"); foreach (var sr in c.args[0].Execute(c)) { if (GetRange(sr, ref range)) { break; } else { yield return(null); } } if (!range.valid) { if (c.args.Length < 2) { c.ThrowError("No expression to end range"); } foreach (var sr in c.args[1].Execute(c)) { if (GetRange(sr, ref range)) { break; } else { yield return(null); } } } if (!range.valid) { c.ThrowError("Incomplete range"); } for (double d = range.start.Value; d < range.end.Value; d += 1d) { yield return(EvaluatorUtils.CreateItem(d, alias)); } }
public static IEnumerable <SearchItem> Avg(SearchExpressionContext c) { var skipCount = 0; string selector = null; if (c.args[0].types.HasFlag(SearchExpressionType.Selector)) { skipCount++; selector = c.args[0].innerText.ToString(); } foreach (var arg in c.args.Skip(skipCount)) { var avg = Average.Zero; foreach (var r in arg.Execute(c)) { avg = Aggregate(r, selector, avg, (d, _avg) => _avg.Add(d)); } yield return(SearchExpression.CreateItem(avg.result, c.ResolveAlias(arg, "Average"))); } }
public static IEnumerable <SearchItem> Max(SearchExpressionContext c) { var skipCount = 0; string selector = null; if (c.args[0].types.HasFlag(SearchExpressionType.Selector)) { skipCount++; selector = c.args[0].innerText.ToString(); } foreach (var arg in c.args.Skip(skipCount)) { double max = double.MinValue; foreach (var r in arg.Execute(c)) { max = Aggregate(r, selector, max, (d, _max) => d > _max); } yield return(SearchExpression.CreateItem(max, c.ResolveAlias(arg, "Max"))); } }
public static IEnumerable <SearchItem> Sum(SearchExpressionContext c) { var skipCount = 0; string selector = null; if (c.args[0].types.HasFlag(SearchExpressionType.Selector)) { skipCount++; selector = c.args[0].innerText.ToString(); } foreach (var arg in c.args.Skip(skipCount)) { var sum = 0d; foreach (var r in arg.Execute(c)) { sum = Aggregate(r, selector, sum, (d, _sum) => _sum + d); } yield return(SearchExpression.CreateItem(sum, c.ResolveAlias(arg, "Sum"))); } }
static IEnumerable <SearchItem> Random(SearchExpressionContext c, SearchExpression e) { var set = new List <SearchItem>(); foreach (var item in e.Execute(c)) { if (item != null) { set.Add(item); } yield return(null); // Wait until we have all results. } var randomItem = Random(set); // Rename random item label if an alias is defined. if (c.ResolveAlias(e) is string alias) { randomItem.label = alias; } yield return(randomItem); }
public static IEnumerable <SearchItem> IsTrue(SearchExpressionContext c) { foreach (var e in c.args) { bool isTrue = false; foreach (var item in e.Execute(c)) { if (item == null) { yield return(null); } else { isTrue |= EvaluatorUtils.IsTrue(item); if (!isTrue) { break; } } } yield return(EvaluatorUtils.CreateItem(isTrue, c.ResolveAlias(e, "IsTrue"))); } }
public static IEnumerable <SearchItem> Selection(SearchExpressionContext c) { var selection = TaskEvaluatorManager.EvaluateMainThread(() => { var instanceIds = UnityEditor.Selection.instanceIDs; return(instanceIds.Select(id => { string assetPath = AssetDatabase.GetAssetPath(id); return new SelectionResult(id, assetPath); }).ToList()); }); foreach (var selectionResult in selection) { if (string.IsNullOrEmpty(selectionResult.assetPath)) { yield return(SearchExpression.CreateItem(selectionResult.instanceId, c.ResolveAlias("Selection"))); } else { yield return(SearchExpression.CreateItem(selectionResult.assetPath, c.ResolveAlias("Selection"))); } } }
public static IEnumerable <SearchItem> SceneName(SearchExpressionContext c) { var desc = TaskEvaluatorManager.EvaluateMainThread(() => EditorApplication.GetApplicationTitleDescriptor()); yield return(SearchExpression.CreateItem(desc.activeSceneName ?? string.Empty, c.ResolveAlias("SceneName"))); }
public static IEnumerable <SearchItem> DataPath(SearchExpressionContext c) { var dataPath = TaskEvaluatorManager.EvaluateMainThread(() => Application.dataPath); yield return(SearchExpression.CreateItem(dataPath ?? string.Empty, c.ResolveAlias("DataPath"))); }