Getting Document Text from a AstNode?

SyntaxEditor .NET Languages Add-on for Windows Forms Forum

Posted 17 years ago by Erik Pepping - RADVenture B.V
Version: 4.0.0236
Avatar
Hi,

I'm simulating a project by using a global DotNetProjectResolver. By assigning this global DotNetProjectResolver to the LanguageData of the project documents, SyntaxEditor is handling the IntelliPrompt features perfectly. Amazing how easy this has become!

At some stage I need to extract certain classes and class properties marked with attributes from documents. I can get to all defined types in the project documents by using:

dotNetProjectResolver.SourceProjectContent.GetTypes(null,pProject.ProjectTemplate.NameSpace,DomBindingFlags.Public);

I'm probably missing something obvious, but I have two questions about this. When I iterate through the types and encounter a Ast Class with AttributeSections how can I determine the attribute text?
Second question: is it possible to determine the document in which the type is actually declared? Or more general: can I get to the document from a AstNode?

Regards,

Erik Pepping

Comments (6)

Posted 17 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Erik,

Every AttributeSection has an Attributes collection. The Attribute type has AttributeType and Arguments properties. Check those out.

I don't think we are currently tracking which file generated the compilation unit from within the compilation unit itself. If this is something you would like, how would you recommend doing it? Perhaps we could store the Document.Filename in the CompilationUnit that is generated?

The compilation unit doesn't store a Document reference either.


Actipro Software Support

Posted 17 years ago by Erik Pepping - RADVenture B.V
Avatar
Thanks, I overlooked the attribute collection.

For multiple reasons, like refactoring options and impact analysis features it would be very nice to have a way to determine the "source" document. That info comnined with the ASTNode properties like TextRange would give me easy way to implement things like refactoring and impact analyses on classes.

Another option would be able to determine the GetTypes for a specific document manually (like dotNetProjectResolver.SourceProjectContent.GetTypes() does), so I can go through the documents which where added to the dotNetProjectResolver.SourceProjectContent collection myself. Perhaps this is already possible, but I was unable to find this option.

Erik Pepping
Posted 17 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
So if we added a "ICollection SourceProjectContent.GetTypes(string sourceKey)" method, that would work for you? In this scenario the source key would be the filename since that's what source is indexed on.

The only caveat to this is that the same IDomType may be returned by two or more calls to that method if you are using partial classes. This is because the partial class data is combined into one large IDomType in our reflection data. Is that ok or will that be a problem? Or should we only return the portion that was defined by that particular file and not any of the rest?


Actipro Software Support

Posted 17 years ago by Erik Pepping - RADVenture B.V
Avatar
Yes, that would help me a lot. The case of the partial classes is not a real issue at the moment, but it is something to think about in the future. I guess that only returning the actual declared IDomTypes in the sourceKey document would be the most convenient/accurate.

Just out of curiosity: the Ast nodes do have TextRange but no reference to the document. The TextRange without the Document not not give us usable information (and what does the TextRange return on a partial class?). Is the document information internally present on the Ast nodes? It would be absolutely be very useful to have a method to get a (list) of documents where it was declared.

Thanks for your time!
Posted 17 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Bunch of new stuff in regards to this for the upcoming release (hopefully today):

1) Added the SourceKey property to the ICompilationUnit interface and updated the Simple language appropriately. This will let you get a compilation unit's filename if one was used to generate it.

2) Added the IDomType.GetSourceKeys property that will return the source keys (filenames) that define the type. This is only applicable for types defined in source code. Normally one will be returned but more will if it is a partial type.

3) Added the SourceProjectContent.GetTypesForSourceKey method, which returns all the IDomType objects defined within the specified source file. There is an option for whether to return merged partial types or not.

4) Added the CompilationUnit.Tag property. You could use this to store a Document reference if you want. We don't store a reference because the AST is built off of a copy of the document text since the document text might have changed by the time the AST is complete. Also we didn't want to retain references to a Document from the AST.


Actipro Software Support

Posted 17 years ago by Erik Pepping - RADVenture B.V
Avatar
Thanks, excellent 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.