public override void OnWindowLoaded()
        {
            bool requiresCompiler = false;

            foreach (var snippet in SnippetsAddinConfiguration.Current.Snippets)
            {
                if (!requiresCompiler && snippet.SnippetText.Contains("{{") || snippet.SnippetText.Contains("@"))
                {
                    requiresCompiler = true;
                }


                if (!string.IsNullOrEmpty(snippet.KeyboardShortcut))
                {
                    var        ksc = snippet.KeyboardShortcut.ToLower();
                    KeyBinding kb  = new KeyBinding();

                    if (ksc.Contains("alt"))
                    {
                        kb.Modifiers = ModifierKeys.Alt;
                    }
                    if (ksc.Contains("shift"))
                    {
                        kb.Modifiers |= ModifierKeys.Shift;
                    }
                    if (ksc.Contains("ctrl") || ksc.Contains("ctl"))
                    {
                        kb.Modifiers |= ModifierKeys.Control;
                    }

                    string key =
                        ksc.Replace("+", "")
                        .Replace("-", "")
                        .Replace("_", "")
                        .Replace(" ", "")
                        .Replace("alt", "")
                        .Replace("shift", "")
                        .Replace("ctrl", "")
                        .Replace("ctl", "");

                    key = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(key);
                    if (!string.IsNullOrEmpty(key))
                    {
                        KeyConverter k = new KeyConverter();
                        kb.Key = (Key)k.ConvertFromString(key);
                    }

                    // Whatever command you need to bind to
                    kb.Command = new CommandBase((s, e) => InsertSnippet(snippet),
                                                 (s, e) => Model.IsEditorActive);

                    Model.Window.InputBindings.Add(kb);
                }
            }

            if (requiresCompiler)
            {
                ScriptRunnerRoslyn.WarmupRoslyn();
            }
        }
Exemple #2
0
        /// <summary>
        /// Evaluates the embedded script parsing out {{ C# Expression }}
        /// blocks and evaluating the expressions and embedding the string
        /// output into the result string.
        ///
        ///
        /// </summary>
        /// <param name="snippet">The snippet template to expand</param>
        /// <param name="model">Optional model data accessible in Expressions as `Model`</param>
        /// <returns></returns>
        public string EvaluateScript(string snippet, object model = null)
        {
#if DEBUG
            var sw = new Stopwatch();
            sw.Start();
#endif
            var tokens = TokenizeString(ref snippet, "{{", "}}");

            snippet = snippet.Replace("\"", "\"\"");

            snippet = DetokenizeString(snippet, tokens);

            snippet = snippet.Replace("{{", "\" + ").Replace("}}", " + @\"");
            snippet = "@\"" + snippet + "\"";


            string code = "dynamic Model = Parameters[0];\r\n" +
                          "return " + snippet + ";";

            var    scriptCompiler = new ScriptRunnerRoslyn();
            string result         = scriptCompiler.ExecuteCode(code, model) as string;

            if (result == null)
            {
                ErrorMessage = scriptCompiler.ErrorMessage;
            }
            else
            {
                ErrorMessage = null;
            }

#if DEBUG
            sw.Stop();
            Debug.WriteLine("ScriptParser Code: \r\n" + code);
            Debug.WriteLine("Snippet EvaluateScript Execution Time: " + sw.ElapsedMilliseconds + "ms");
#endif

            return(result);
        }
 public override void OnApplicationShutdown()
 {
     // if Roslyn is running shut it down
     ScriptRunnerRoslyn.ShutdownRoslyn();
     snippetsWindow?.Close();
 }