user1533223
user1533223

Reputation: 5

Visual studio file parser

I'm trying to find a way to write a visual studio "thing", or use a tool out there, that allows me to parse each line so I can add custom code around other code that I would be checking with my own algorithm.

Basically, I want to have a readtext of the current file from the visual studio code editor, and then replace it with the new verson with my modified code.

Looked at resharper, macros, can't figure out a way to do this and I dont want to have to copy all code from a file, paste it into a windows application form that I would write,and then copy paste back. Any ideas?

Thanks

Upvotes: 0

Views: 2869

Answers (2)

user7116
user7116

Reputation: 64148

I would suggest using Roslyn for this task. It provides the facilities to read solutions, projects, and then realize the source files inside as their constituent syntax or semantic trees. Eric Lippert covered the CTP on his blog (emphasis mine):

Roslyn is a library of code analysis APIs useful for building compilers, development environments, refactoring engines and so on. It supports lexical, grammatical and semantic analysis of C# and Visual Basic. And it is awesome.

You can also use Roslyn to create IDE Extensions, including creating your own refactoring tools:

To extend the Visual Studio IDE, the Editor Services API exposes a set of extension points to existing language service features. A few examples of these include the completion list, code outliners, highlight references, syntax classifiers, and code refactorings. For each of these extensions, you need to create your own MEF provider and ensure that there is at least one Export* attribute that describes important details about the provider.

In another question I demonstrated substituting type names (e.g. int with Int32) in a minimal amount of code, with this main loop:

var workspace = Workspace.LoadSolution(path);
var solution = workspace.CurrentSolution;
foreach (var project in solution.Projects
    .Where(prj => prj.LanguageServices.Language == "C#"))
{
    foreach (var doc in project.Documents
        .Where(d => d.SourceCodeKind == SourceCodeKind.Regular
                 && d.LanguageServices.Language == "C#"))
    {
        var tree = SyntaxTree.ParseCompilationUnit(
            doc.GetText(),
            doc.DisplayName);

        // Update the syntax tree
        var newTree = UpdatePredefinedTypes(tree);

        solution = solution.UpdateDocument(doc.Id, newTree.Text);
    }
}

workspace.ApplyChanges(workspace.CurrentSolution, solution);
// When you run this on a project open in VS it notices the changes

Upvotes: 1

Sam Axe
Sam Axe

Reputation: 33738

You could look at the Visual Studio Automation extension points

or you could post-process all your files.. think of the copy/paste solution you mentioned but instead of copy/paste you read the files off disk and write them back. This could be triggered by file modification or user interaction.

Upvotes: 0

Related Questions