Posted 7 years ago by 7Alpha7
Avatar
When scaning an AST to look at some expression matching some needs, it's necessary to resolve the AST expression. I understand I need to use a Resolver and I am told that it's good to provide a context for this purpose.

But once more the context is easilly found from a open document, with CSharpContextFactory that needs a TextSnapshotOffset, when in a closed file or a text string there is not such thing (or I don't know yet how to get it).

What I don't know is what is the risk of not providing a context to resolve a source string ? I bet it would not be enough to decide the matching needed.

In fact my purpose is to scan source files in a project, not open in a SyntaxEditor, and use a resolver to find what I want, but providing it the right solution context, I mean all the IProjectAssembly etc... so that the types of the source string would be resolved inside the right context of the solution (a project, and its references).

[Modified at 12/10/2011 12:42 PM]

Comments (6)

Posted 7 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Even if you don't want to open a SyntaxEditor for whichever document you are trying to scan, you can load up a CodeDocument completely independent of any UI and assign its language to a .NET add-on language. It will parse the same as if a SyntaxEditor was open (will use worker threads) and will update the ParseData property when the parsing is completed. You can still get contexts, etc. with this too.


Actipro Software Support

Posted 7 years ago by 7Alpha7
Avatar
I saw the CodeDocument stuff but I didn't understand how to share with it the diffent IProjectAssembly and SyntaxLanguage instances I already have in a solution / project pattern, used by the intellisense.

I saw how to get an AST tree from a CodeDocument but I couldn't figure out how to resolve an Expression of such an AST with the Resolver provided by an already existant IProjectAssembly AND give it a context at the same time.

A sample would be appreciated.
Posted 7 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Say you have a solution with two projects. You'd want to make two instances of the appropriate language (like CSharpSyntaxLanguage). Each one will have its own IProjectAssembly with their own references. You'd use language #1 for any document that should use project #1, and you'd use language #2 for any document that should use project #2.

What sort of expressions are you trying to resolve? Just things like identifiers and member invocations or other things? I need to know that to answer with more detail. Perhaps a tiny code snippet showing what you are trying to resolve would be helpful.


Actipro Software Support

Posted 7 years ago by 7Alpha7
Avatar
In that kind of context, I would have queued say two SourceFiles to the project #2 which has the project #1 as references. The project #1 would define a Class #1 with member func(). I would need at some moment to look at one of the source file content of the project #2, defining a class Class #2 with several members, to find how many invocations of func() of the type Class #1 is made in the whole Class #2 implementation, and with exactly what parameters instances (for exemple, which strings if the parameter if of type string).
project #1
  Class #1 in file Class1.cs
    func(string s){
    }


project #2
 
  using project #1 (understand what I mean)
  Class #2 in file Class2.cs
    Class#1 c;
    call1(){
      c.func("a");
      c.func("b");
   }
    call2(){
      c.func("aa");
      c.func("bb");
      func("cc");
   }
   //does not interest me
   func(string s){
   }

Here I would find four invocations, with "a", "b", "aa" and "bb" as parameter instances. If a another func() function is define somewhere, I would need to be sure it's the one on Class #1 invoked.
Posted 7 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Once you know the offset of the various identifiers (like 'func') that you want to examine, you can do things like this:
var context = new CSharpContextFactory().CreateContext(new TextSnapshotOffset(document.CurrentSnapshot, offset));
if (context != null) {
    var resultSet = context.Resolve();
    // Examine results
}
The results will show you which type/member, etc. it resolves to. Members can point to their declaring type and types tell you which assembly, if any, defined them.

[Modified at 12/13/2011 10:20 AM]


Actipro Software Support

Posted 7 years ago by 7Alpha7
Avatar
Ok I see. I suppose that I will get the offset with the AST. I missed the TextSnapshotOffset constructor.

Thank you.
The latest build of this product (v2018.1 build 0675) was released 1 month ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.