|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Reflection; |
| 5 | +using System.Text.RegularExpressions; |
| 6 | +using IronScheme.Runtime; |
| 7 | +using IronScheme.Scripting; |
| 8 | +using Microsoft.VisualStudio.Text; |
| 9 | +using Microsoft.VisualStudio.Text.Editor; |
| 10 | +using Microsoft.VisualStudio.Text.Operations; |
| 11 | + |
| 12 | +namespace IronScheme.VisualStudio |
| 13 | +{ |
| 14 | + public static class Shell |
| 15 | + { |
| 16 | + public static IEditorOperationsFactoryService EditorOperationsFactoryService { get; internal set; } |
| 17 | + internal static Dictionary<string, ITextView> Views { get; } = new Dictionary<string, ITextView>(); |
| 18 | + |
| 19 | + public static Cons OpenViews() => Cons.FromList(Views.Keys); |
| 20 | + |
| 21 | + public static void SetView(string filename) |
| 22 | + { |
| 23 | + if (Views.TryGetValue(filename, out var view)) |
| 24 | + { |
| 25 | + var ops = EditorOperationsFactoryService.GetEditorOperations(view); |
| 26 | + if (ops != null) |
| 27 | + { |
| 28 | + "(view {0})".Eval(ops); |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + public static Cons GetOperations() |
| 34 | + { |
| 35 | + var methods = new List<Cons>(); |
| 36 | + |
| 37 | + var all = Methods<IEditorOperations>().Concat(Methods<IEditorOperations2>()).Concat(Methods<IEditorOperations3>()); |
| 38 | + |
| 39 | + foreach (var meth in all) |
| 40 | + { |
| 41 | + if (CanDealWithParams(meth)) |
| 42 | + { |
| 43 | + var clrType = SymbolTable.StringToId(meth.DeclaringType.Name); |
| 44 | + var clrName = SymbolTable.StringToId(meth.Name); |
| 45 | + var name = Schemify(meth.Name.Replace("get_", "")); |
| 46 | + var args = Cons.FromList(meth.GetParameters().Select(x => Schemify(x.Name))); |
| 47 | + methods.Add(new Cons(clrType, new Cons(clrName, new Cons(name, args)))); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + return Cons.FromList(methods); |
| 52 | + |
| 53 | + MethodInfo[] Methods<T>() |
| 54 | + { |
| 55 | + return typeof(T).GetMethods(); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + private static object Schemify(string name) |
| 60 | + { |
| 61 | + var newname = Regex.Replace(name, "[A-Z]", m => m.Index == 0 ? m.Value.ToLower() : ("-" + m.Value.ToLower())); |
| 62 | + return SymbolTable.StringToId(newname); |
| 63 | + } |
| 64 | + |
| 65 | + |
| 66 | + static HashSet<Type> safetypes = new HashSet<Type>() |
| 67 | + { |
| 68 | + typeof(string), |
| 69 | + typeof(bool), |
| 70 | + typeof(int), |
| 71 | + typeof(void) |
| 72 | + }; |
| 73 | + |
| 74 | + private static bool CanDealWithParams(System.Reflection.MethodInfo meth) |
| 75 | + { |
| 76 | + if (safetypes.Contains(meth.ReturnType)) |
| 77 | + { |
| 78 | + var pars = meth.GetParameters(); |
| 79 | + return pars.Length == 0 || pars.All(p => safetypes.Contains(p.ParameterType)); |
| 80 | + } |
| 81 | + |
| 82 | + return false; |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments