コード例 #1
0
        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);
        }