microsoft / VS-Macros

An extension for Visual Studio 2013+ that enables the use of macros in the IDE. The extension can record most of the features in Visual Studio including text editing operations.
Other
131 stars 41 forks source link

iterative all documents in visual studio 2015 #24

Open Bouki6 opened 8 years ago

Bouki6 commented 8 years ago

Hello. i just start using the Macros. i simply want to go through all the documents in the solution and then run this command: dte.ExecuteCommand("Edit.CollapsetoDefinitions");

so i used your one of the sample macros "removes and sorts all". but the problem is command execute before the document completely load. if there was a event or timer that can help wait until document load completely the problem will solve. here is my macro:

iterateFiles();

function iterateFiles() {
    for (var i = 1; i <= dte.Solution.Projects.Count; i++) {
        iterateProjectFiles(dte.Solution.Projects.Item(i).ProjectItems);
    }
}

function iterateProjectFiles(projectItems) {
    for (var i = 1; i <= projectItems.Count; i++) {
        var file = projectItems.Item(i);

        if (file.SubProject != null) {
            formatFile(file);
            iterateProjectFiles(file.ProjectItems);
        } else if (file.ProjectItems != null && file.ProjectItems.Count > 0) {
            formatFile(file);
            iterateProjectFiles(file.ProjectItems);
        } else {
            formatFile(file);
        }
    }
}

function formatFile(file) {
    dte.ExecuteCommand("View.SolutionExplorer");
    if (file.Name.indexOf(".cs", file.Name.length - ".cs".length) !== -1) {
        file.Open();
        file.Document.Activate();

         //here we must wait until document completely load 
        // Format the document
        dte.ExecuteCommand("Edit.CollapsetoDefinitions");

        file.Document.Save();
        file.Document.Close();
    }
}