Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get other files than .cs with Roslyn

I want to find usages of a method with Roslyn. I managed to do this for usages by code in the .cs files. Additionally I want to analyze JavaScript files included in the solution. I know that Roslyn cannot analyze syntax or sematics of JavaScript, so I only want to search for textural occurrencies of my method's name in all .js files.

I retrieve all files (documents) like this:

foreach (Project pr in solution.Projects)
{
    foreach (Document doc in pr.Documents)
    {
      // my js-file is not included
    }
}

But Documents only contains .cs files. Is there a way to get also .js files or do I have to get the folder with project.FilePath and get the files with the old File API, which could cause problems because not necessarily all files in the folder must have been added to the project etc.?

Edit:
Also AdditionalFiles does not hold any file.

like image 887
Flat Eric Avatar asked Sep 27 '15 19:09

Flat Eric


1 Answers

You need to use Project.AdditionalDocuments:

foreach (Project pr in solution.Projects) {
    foreach (TextDocument doc in pr.AdditionalDocuments) {
      // doc is a non-csharp TextDocument object.
    }
}

Update

To use the above you must ensure that the file in the target project has its build action set to AdditionalFiles. There is no good way currently to generate a file in the target project with a build action of this type so you are currently stuck with relying on the user of your code manually creating a stub of the file, hitting F4 for the file properties and changing its build action to AdditionalFiles, after which your extension will be able to pick it up.

AdditionalFiles

like image 150
cchamberlain Avatar answered Oct 05 '22 07:10

cchamberlain