static void Remember(Replacements replacements, string tokenString, QueryToken token) { List <QueryToken> tokenList = token.Follow(a => a.Parent).Reverse().ToList(); string[] oldParts = tokenString.Split('.'); string[] newParts = token.FullKey().Split('.'); List <string> oldPartsList = oldParts.ToList(); List <string> newPartsList = newParts.ToList(); Func <string, string> rep = str => { if (Replacements.AutoReplacement == null) { return(null); } Replacements.Selection?sel = Replacements.AutoReplacement(new Replacements.AutoReplacementContext { ReplacementKey = "QueryToken", OldValue = str, NewValues = null }); if (sel == null || sel.Value.NewValue == null) { return(null); } return(sel.Value.NewValue); }; int pos = -1; while (oldPartsList.Count > 0 && newPartsList.Count > 0 && (oldPartsList[0] == newPartsList[0] || rep(oldPartsList[0]) == newPartsList[0])) { oldPartsList.RemoveAt(0); newPartsList.RemoveAt(0); pos++; } while (oldPartsList.Count > 0 && newPartsList.Count > 0 && (oldPartsList[oldPartsList.Count - 1] == newPartsList[newPartsList.Count - 1] || rep(oldPartsList[oldPartsList.Count - 1]) == newPartsList[newPartsList.Count - 1])) { oldPartsList.RemoveAt(oldPartsList.Count - 1); newPartsList.RemoveAt(newPartsList.Count - 1); } string key = pos == -1 ? QueryKey(tokenList[0].QueryName) : TypeKey(tokenList[pos].Type); replacements.GetOrCreate(key)[oldPartsList.ToString(".")] = newPartsList.ToString("."); }
static void Remember(Replacements replacements, string tokenString, QueryToken token) { List<QueryToken> tokenList = token.Follow(a => a.Parent).Reverse().ToList(); string[] oldParts = tokenString.Split('.'); string[] newParts = token.FullKey().Split('.'); List<string> oldPartsList = oldParts.ToList(); List<string> newPartsList = newParts.ToList(); Func<string, string> rep = str => { if (Replacements.AutoReplacement == null) return null; Replacements.Selection? sel = Replacements.AutoReplacement(str, null); if (sel == null || sel.Value.NewValue == null) return null; return sel.Value.NewValue; }; int pos = -1; while (oldPartsList.Count > 0 && newPartsList.Count > 0 && (oldPartsList[0] == newPartsList[0] || rep(oldPartsList[0]) == newPartsList[0])) { oldPartsList.RemoveAt(0); newPartsList.RemoveAt(0); pos++; } while (oldPartsList.Count > 0 && newPartsList.Count > 0 && (oldPartsList[oldPartsList.Count - 1] == newPartsList[newPartsList.Count - 1] || rep(oldPartsList[oldPartsList.Count - 1]) == newPartsList[newPartsList.Count - 1])) { oldPartsList.RemoveAt(oldPartsList.Count - 1); newPartsList.RemoveAt(newPartsList.Count - 1); } string key = pos == -1 ? QueryKey(tokenList[0].QueryName) : TypeKey(tokenList[pos].Type); replacements.GetOrCreate(key)[oldPartsList.ToString(".")] = newPartsList.ToString("."); }
static string AskTypeReplacement(Replacements replacements, string type) { return(replacements.GetOrCreate("cleanNames").GetOrCreate(type, () => { if (Replacements.AutoReplacement != null) { Replacements.Selection?sel = Replacements.AutoReplacement(new Replacements.AutoReplacementContext { ReplacementKey = "FixValue.Type", OldValue = type, NewValues = null }); if (sel != null && sel.Value.NewValue != null) { return sel.Value.NewValue; } } Console.WriteLine("Type {0} has been renamed?".FormatWith(type)); int startingIndex = 0; StringDistance sd = new StringDistance(); var list = TypeLogic.NameToType.Keys.OrderBy(t => sd.LevenshteinDistance(t, type)).ToList(); retry: int maxElements = Console.LargestWindowHeight - 11; list.Skip(startingIndex).Take(maxElements) .Select((s, i) => "- {1,2}: {2} ".FormatWith(i + startingIndex == 0 ? ">" : " ", i + startingIndex, s)).ToConsole(); Console.WriteLine(); SafeConsole.WriteLineColor(ConsoleColor.White, "- n: None"); int remaining = list.Count - startingIndex - maxElements; if (remaining > 0) { SafeConsole.WriteLineColor(ConsoleColor.White, "- +: Show more values ({0} remaining)", remaining); } while (true) { string answer = Console.ReadLine(); if (answer == null) { throw new InvalidOperationException("Impossible to synchronize interactively without Console"); } answer = answer.ToLower(); if (answer == "+" && remaining > 0) { startingIndex += maxElements; goto retry; } if (answer == "n") { return null; } if (int.TryParse(answer, out int option)) { return list[option]; } Console.WriteLine("Error"); } })); }
static string AskTypeReplacement(Replacements replacements, string type) { return replacements.GetOrCreate("cleanNames").GetOrCreate(type, () => { if (Replacements.AutoReplacement != null) { Replacements.Selection? sel = Replacements.AutoReplacement(type, null); if (sel != null && sel.Value.NewValue != null) return sel.Value.NewValue; } Console.WriteLine("Type {0} has been renamed?".FormatWith(type)); int startingIndex = 0; StringDistance sd = new StringDistance(); var list = TypeLogic.NameToType.Keys.OrderBy(t => sd.LevenshteinDistance(t, type)).ToList(); retry: int maxElements = Console.LargestWindowHeight - 11; list.Skip(startingIndex).Take(maxElements) .Select((s, i) => "- {1,2}: {2} ".FormatWith(i + startingIndex == 0 ? ">" : " ", i + startingIndex, s)).ToConsole(); Console.WriteLine(); SafeConsole.WriteLineColor(ConsoleColor.White, "- n: None"); int remaining = list.Count - startingIndex - maxElements; if (remaining > 0) SafeConsole.WriteLineColor(ConsoleColor.White, "- +: Show more values ({0} remaining)", remaining); while (true) { string answer = Console.ReadLine(); if (answer == null) throw new InvalidOperationException("Impossible to synchronize interactively without Console"); answer = answer.ToLower(); if (answer == "+" && remaining > 0) { startingIndex += maxElements; goto retry; } if (answer == "n") return null; int option = 0; if (int.TryParse(answer, out option)) { return list[option]; } Console.WriteLine("Error"); } }); }