How to get the file name of a template from a custom macro?

Hello, I'm trying to build a custom macro in order to use it in my file templates and I would like to get access to the file name. How could I accomplish that.

Honestly, I've created two macros but they were very simple. I'm not able to find much documentation about the process so I'm stuck here.

Any help would be really appreciated :D

Best regards,


1 comment
Comment actions Permalink

The best thing to do is take a look at what the macro is given - IHotspotContext. You want to get from here to the file name that the macro is being invoked on. Looking at the members of IHotspotContext, ExpressionRange looks like it would be useful - it's of type DocumentRange, and if we look at the members there, we can see that we can get the IDocument. Now all we need to do is get the document's file name. This is less obvious, as IDocument doesn't have any members that look useful - nothing that looks like a filename, or even a file. But if we take a look at the extension methods of IDocument, we can see a method called GetPsiSourceFile, which returns us an IPsiSourceFile, which has a Name property. So, want to do this:

var sourceFile = context.ExpressionRange.Document.GetPsiSourceFile(context.SessionContext.Solution)
var fileName = sourceFile != null ? sourceFile.Name : null;

Hope this helps


Please sign in to leave a comment.