Esempio n. 1
0
        public static void Init()
        {
            if (engine != null)
            {
                return;
            }

            WithCompatibilityCheck(() =>
            {
                var file = Roslyn.LocateInPluginDir("RoslynIntellisense.exe", "Roslyn", @".\");

                if (!File.Exists(file))
                {
                    var path = Environment.GetEnvironmentVariable("roslynintellisense_path") ?? "";
                    if (File.Exists(path))
                    {
                        file = path;
                    }
                }

                intellisense = Assembly.LoadFrom(file);
                engine       = (IEngine)intellisense.CreateInstance("RoslynIntellisense.Engine");
                engine.SetOption("ReflectionOutDir", Path.Combine(Path.GetTempPath(), "CSScriptNpp\\ReflctedTypes"));
            });

            if (engine != null)
            {
                Task.Factory.StartNew(() => WithCompatibilityCheck(engine.Preload));
            }
        }
        static string FormatCodeWithRoslyn(string code, ref int pos, string file)
        {
            try
            {
                if (RoslynFormat == null)
                {
                    //need to load dynamically as static loading can only be achieved via compilation and plugin target CLR is older than
                    //the one is Roslyn based on.
                    string rootDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

                    var        asmFile = Roslyn.LocateInPluginDir("RoslynIntellisense.exe", "Roslyn", @".\");
                    var        asm     = Assembly.LoadFrom(asmFile);
                    MethodInfo method;
                    if (Config.Instance.HybridFormatting)
                    {
                        method = asm.GetType("RoslynIntellisense.Formatter").GetMethod("FormatHybrid");
                    }
                    else
                    {
                        method = asm.GetType("RoslynIntellisense.Formatter").GetMethod("Format");
                    }
                    RoslynFormat = (FormatMethod)Delegate.CreateDelegate(typeof(FormatMethod), method);
                }

                if (RoslynFormat != null)
                {
                    var newCode = RoslynFormat(code, file);

                    pos = SyntaxMapper.MapAbsPosition(code, pos, newCode);

                    return(newCode);
                }
            }
            catch (Exception e)
            {
#if !DEBUG
                Config.Instance.RoslynFormatting = false;
                Config.Instance.Save();
#endif
                MessageBox.Show("Cannot use Roslyn Formatter.\nError: " + e.Message + "\n\nThis can be caused by the absence of .NET 4.6.\n\nRoslyn Formatter will be disabled and the default formatter enabled instead. You can always reenable RoslynFormatter from the settings dialog.", "CS-Script");
            }
            return(code);
        }