Code fragment and .Net resolution

SyntaxEditor .NET Languages Add-on for WPF Forum

Posted 6 years ago by Nicolas
Version: 13.1.0583
Platform: .NET 4.0
Environment: Windows 7 (64-bit)
Avatar

I use the SyntaxEditor code fragment feature to design a "C# expression control".
My control need to resolve the type of the edited expression.

But it seems that resolution doesn't work on AST part corresponding to footer code.

Here's a simple test :

static void Main(string[] args)
{
    CSharpSyntaxLanguage lang = new CSharpSyntaxLanguage();
    ProjectAssembly projectAssembly = new CSharpProjectAssembly("Test");
    projectAssembly.AssemblyReferences.AddMsCorLib();
    lang.RegisterProjectAssembly(projectAssembly);
    CodeDocument codeDoc = new CodeDocument();
    codeDoc.SetHeaderAndFooterText(@"
        using System;
        class Test
        {
            Test()
            {
                var a = ", @";
                var b = a;
            }
        }");
    codeDoc.SetText("\"Hello world !\"");
    codeDoc.ParseDataChanged += codeDoc_ParseDataChanged;
    codeDoc.Language = lang;
}

static void codeDoc_ParseDataChanged(object sender, ParseDataPropertyChangedEventArgs e)
{
    ILLParseData data = e.NewValue as ILLParseData;
    ConstructorBody constructorBody = data.Ast.Children[1].Children[1].Children[0].Children[1] as ConstructorBody;
    LocalVariableDeclaration varDecl = constructorBody.Statements.Last() as LocalVariableDeclaration;
    VariableDeclarator declarator = varDecl.Declarators.Last();
    Expression exp = declarator.Initializer;    // expression is 'a'
    TextSnapshotOffset snapshotOffset = new TextSnapshotOffset(data.Snapshot, exp.StartOffset.Value);
    IDotNetContext context = new CSharpContextFactory().CreateContext(snapshotOffset, DotNetContextKind.Self);
    IResolverResultSet resultSet = context.Resolve();
    Debug.Assert(resultSet.Results.Count==1);    // assertion fail !
    Debug.Assert(resultSet.Results[0].Type.QualifiedName.ToString() == "global::System.String");
}

Comments (1)

Answer - Posted 6 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Nicolas,

Sorry but I don't think that resolving things from the footer will work.

What you can do is make a second document with the same language.  Then set its text to be the unified text of the header/text/footer of this document.  Run your same code on that and it works.


Actipro Software Support

The latest build of this product (v2019.1 build 0683) was released 2 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.