Obtaining name of current method editing in editor control

SyntaxEditor for Windows Forms Forum

Posted 16 years ago by Matt Adamson
Avatar
Guys,

Is it possible to obtain the name and possibly argument information where the current line indicator is located e.g. as the user scrolls through a file the selected line would change and from this I would like to obtain the method name to display in a drop down perhaps.

Also related to this how could I build a list of all method names including the class name which are contained in a specified source code file.

I'm specifically discussing C# here however this applies to any language.

If it's possible - is it only throught the NET languages add on, or does the core editor control provide this feature

Many thanks

Matt

Comments (12)

Posted 16 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Matt,

The best way to do this is if you have an AST of the document, which is what our parser generator for making semantic parsers is good for. So you can write a grammar for any language and the "Simple" language sample is a good example of this.

Once you have an AST, you can take the root node (like a CompilationUnit) and recurse through the children until you hit the method declaration AST node that contains the desired offset. Then you can examine that for which parameters it has, etc.

The .NET Languages Add-on does produce a CompilationUnit for C# so if you use that, all the work is already done for you.


Actipro Software Support

Posted 16 years ago by Matt Adamson
Avatar
Thanks however I still don't feel much closer to the solution your discussing :)

Can you be more specific please i.e. assuming I've purchased the NET add on and I'm in the line selection changing event how could obtain the AST node containing the method name?

Perhaps there are some specific examples which come with the add on which would help me in this area too?

Many thanks

Matt
Posted 16 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
If you call a line like this at the caret's offset, you can get a CSharpContext:
CSharpContext context = CSharpContext.GetContextAtOffset(syntaxEditor.Document, offset, compilationUnit, projectResolver);
Then with that, there is a CSharpContext.MemberDeclarationNode that returns an IDomMember. That will specify the containing member, and in that, its name.


Actipro Software Support

Posted 16 years ago by Matt Adamson
Avatar
Hi

Wow it's a long time since I've looked at this response and detail and got around to implementing this :)

In relation to this code, if I am simply loading a single CSharp file which is obviously part of a bigger project and links to many assemblies, what should I pass as the projectResolver member? I.e. I only want support to retrieve member information for a class e.g. if I'm in a method / property / constructor e.t.c. I don't need any intellisense support at all.

Cheers

Matt
Posted 16 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
The other thing you can do is instead of using a context, just get the CompilationUnit and call FindNodeRecursive to find the containing node for the caret offset. So that could be something very low level like an expression. Then do a FindAncestor(typeof(IDomMember) to find a containing IDomMember, if there is one. Also check the containing node to see if it was an IDomMember. So by doing this, you have a reference to the member and did minimal parsing, etc. It would run faster than the context code mentioned above.


Actipro Software Support

Posted 16 years ago by Matt Adamson
Avatar
Thanks however with code such as this
            private void rightSyntaxEditor_SelectionChanged(object sender, SelectionEventArgs e)
            {
                CompilationUnit compilationUnit = rightSyntaxEditor.Document.SemanticParseData as CompilationUnit;
                if (compilationUnit != null)
                {
                    IAstNode caretNode = compilationUnit.FindNodeRecursive(rightSyntaxEditor.Caret.Offset);
                    IAstNode anscestor = caretNode.FindAncestor(typeof(IDomMember));
                }
            }
the compilation unit is always null in a C# file. I've start the semantic parser service using the Start method from the main form.

I'm sure I'm missing something obvious, but don't know what :)
Posted 16 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Not sure Matt but if you follow the steps in the "Key steps for getting started" in the .NET Languages Add-on main topic there should always be a CompilationUnit there after the first parse.


Actipro Software Support

Posted 16 years ago by Matt Adamson
Avatar
Thanks however I've followed that and at a loss as to what to do next

Any help appreciated?
Posted 16 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
As long as you follow those steps exactly, everything should be working. Are you sure you are doing ALL of them, including setting the Document.Filename?

You can also look at our .NET Reflection QuickStart since that is pretty simplistic and does all the getting started steps correctly.


Actipro Software Support

Posted 16 years ago by Matt Adamson
Avatar
Actually going through the quick start application was of great help, thanks :)

However it's still producing a null reference on the compilation unit. I set the file name property and also set the dotNetProjectResolver reference to the LanguageData property which I hadn't done before, but still the same.

Are there any specific checks in the SemanticParseData property which can cause this to return a null reference?
Posted 16 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
You should always have a CompilationUnit in SemanticParseData even if your document is full of errors, since it has the errors collection. You still must not have something set up properly. I would urge you to again follow all the key getting started steps and look at our sample.

If you still can't get it then perhaps put together small sample project that shows the problem. Chances are you may find your problem doing that too. If not, send it over and we can look.


Actipro Software Support

Posted 16 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Just for anyone reading this, I worked with Matt over email and found he was loading the free dynamic C# language and not the advanced C# language that is part of the .NET Languages Add-on.

Only the language implementation in the .NET Languages Add-on has any built-in AST functionality.


Actipro Software Support

The latest build of this product (v24.1.0) 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.