Add content to new file and finding class in solution

57 views
Skip to first unread message

Nail Salikhov

unread,
Mar 8, 2016, 2:58:23 PM3/8/16
to resharper-plugins
Hi, i'm trying to add content to new file, but it doesn't works:

var file = AddNewItemUtil.AddFile(folder, fileName);
var psiFile = file.GetPrimaryPsiFile();

var elementFactory = CSharpElementFactory.GetInstance(psiFile.GetPsiModule());

elementFactory
.CreateTypeMemberDeclaration("public void SomeMethod(){}");

also, I tried to use the return value from file.ToSourceFile() method as psiFile, and creating declaration inside transaction — psiFile.GetPsiServices().Transactions.

Please, tell me what am I doing wrong?


P.S. I have another question about better approach how to find class in projects:

var symbols = solution.GetPsiServices().Symbols.GetSymbolScope(LibrarySymbolScope.NONE, false);

return symbols.GetElementsByQualifiedName(className).ToArray();

my current solution seems wrong to me, though it works :-). Maybe there is better way to do it easier?

Thanks.

Matt Ellis

unread,
Apr 18, 2016, 5:18:34 AM4/18/16
to resharper-plugins
Hi Nail. Apologies for not responding sooner.

With your first issue, it looks like you're using CSharpElementFactory to create a method, but not doing anything with the returned value. You then need to use the methods in ModificationUtil to add the new element into the PSI tree. You'll need to find an appropriate place to add it.

As for the second issue, that works just fine, except it's also going to pull in namespaces, rather than just types, so you might want to use GetElementsByCLRName, instead.

Regards
Matt
Reply all
Reply to author
Forward
0 new messages