예제 #1
0
        internal static string FormatCode(string script, ref int caret)
        {
            Output.WriteLine("FormatCode");

            string code = File.ReadAllText(script);

            if (code.IsEmpty())
            {
                throw new Exception("The file containing code is empty");
            }

            bool escape_vs_template_var = code.Contains("$safeprojectname$");

            if (escape_vs_template_var)
            {
                code = code.Replace("$safeprojectname$", "_safeprojectname_");
            }

            string formattedCode = RoslynIntellisense.Formatter.FormatHybrid(code, "code.cs");

            if (escape_vs_template_var)
            {
                formattedCode = formattedCode.Replace("_safeprojectname_", "$safeprojectname$");
            }

            caret = SyntaxMapper.MapAbsPosition(code, caret, formattedCode);

            fullyLoaded = true;

            return(formattedCode);
        }
예제 #2
0
        public void ShouldMapPositionInFormattedCode()
        {
            string rawCode =
                @"void main()
{
      Cons|ole.WriteLine(""Hello World!"");
    Debug.WriteLine(""Hello World!"");
}";

            string formattedCode =
                @"void main()
{
    Cons|ole.WriteLine(""Hello World!"");
    Debug.WriteLine(""Hello World!"");
}";
            int pos            = rawCode.IndexOf('|');
            int expectedNewPos = formattedCode.IndexOf('|');

            rawCode       = rawCode.Replace("|", "");
            formattedCode = formattedCode.Replace("|", "");

            int newPos = SyntaxMapper.MapAbsPosition(rawCode, pos, formattedCode);

            Assert.Equal(expectedNewPos, newPos);
        }
예제 #3
0
        public void ShouldMapPositionInFormattedCodeLineStart()
        {
            string rawCode =
                @"void main()
{
}";
            string formattedCode = rawCode;

            int pos    = 0;
            int newPos = SyntaxMapper.MapAbsPosition(rawCode, pos, formattedCode);

            Assert.Equal(pos, newPos);
        }
예제 #4
0
        public void ShouldMapPositionInFormattedCodeAfterLineInjection()
        {
            string rawCode =
                @"using System;
class Script
{
    static public void Main(string[] args)
    {
    }
}";
            string formattedCode =
                @"using System;

class Script
{
    static public void Main(string[] args)
    {
    }
}";
            int pos    = 17; //cl|ass
            int newPos = SyntaxMapper.MapAbsPosition(rawCode, pos, formattedCode);

            Assert.Equal(19, newPos);
        }