Set selection inside a function or class

SyntaxEditor .NET Languages Add-on for Windows Forms Forum

Posted 3 days ago by Nasser Rawda
Version: 25.1.1
Avatar

Hello, 

How can I set the cursor selection inside function for both C# and VB? 

For example:

public int Add(int x, int y)
{
     int z = x+y;
     return z;
}

Selection is:
   (cusor should be here)int z = x+y;

I am using Document.ParseData as ActiproSoftware.Text.Languages.DotNet.Implementation.DotNetParseData

and searching through SimpleName and ClassBody, but it is messy and inaccurate.

Is there a better way to do it.

The same goes for a class.

Thanks.

Comments (3)

Posted 3 days ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hello,

The best way to locate types/members is if you use the resolver or iterate an IAssembly to get the ITypeDefinition or ITypeMemberDefinition you want.  For instance, IProjectAssembly has a SourceFiles collection.  Each ISourceFile has a TypeDefinitions collection.  ITypeDefinition has a SourceFileLocations collection where each ISourceFileLocation has a NavigationOffset (usually points to the name of the type) and the TextRange (full text range).  ITypeDefinition also has a Members collection that lets you get to nested types or ITypeMemberDefinitions, which also have SourceFileLocations.

Once you know an offset for the type or member, you'd get the document's ParseData and the DotNetParseData in it.  Then call parseData.Ast.FindDescendantNode(offset) to locate the AST node for the target definition.  Note that you may need to move up an AST node level if it ends up on like the SimpleName for the the type/member name.

Then go into the Body AST node off the type/member (e.g., ClassDeclaration.Body) and look at the first child AST node if there is one.  The start offset of that AST node is where you'd want to move your cursor.


Actipro Software Support

Posted 1 days ago by Nasser Rawda
Avatar

Thanks, indeed this works. However, when body is empty, there will be no childs. But I still need to set the cursor in between the brackets and after the indent.

public int Add(int x, int y)
{
     (selection should be here)
}

Posted 21 hours ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hello,

If there are no child AST nodes, then you'd need to get a snapshot reader and start at the body's start offset.  Do some text scanning at that point to skip over any first "{" (if in C#) and find the appropriate offset by examining what whitespace comes after.


Actipro Software Support

The latest build of this product (v25.1.2) was released 0 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.