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

Error "Line xx: requires a document to be open" occurs every time I try to run a macro that iterates over all files in project #43

Open kalyankrishna1 opened 7 years ago

kalyankrishna1 commented 7 years ago

For example, "Remove and Sort All"

tvaneerd commented 6 years ago

Yes, it seems to complain about ProjectItems.Count, of all things. ie, in "Remove and Sort All", line 13:

for (var i = 1; i <= projectItems.Count; i++) {

(which comes from dte.Solution.Projects.Item(i).ProjectItems)

Please fix this?

tvaneerd commented 6 years ago

One of the problems:

    if (file.SubProject != null) {
        formatFile(file);
        iterateProjectFiles(file.ProjectItems);

that last line should be

        iterateProjectFiles(file.SubProject.ProjectItems);
tvaneerd commented 6 years ago

This makes no sense. Note the unused variable in iterateProjectFiles If that parameter is completely removed, this script no longer works correctly.

// Randomly selects a cpp file from the project

// Iterate over all files
// TODO: pass in isSuitableCandidate as lambda
var fileList = iterateFiles(".cpp");

// select one 
var file = fileList[Math.floor(Math.random() * fileList.length)];

// for testing the macro:
Macro.InsertText("\nlength: " + fileList.length.toString())
Macro.InsertText("\nwinner: " + file.Name)

file.Open();
file.Document.Activate();

function iterateFiles(ext) {
    var arr = [];

    for (var i = 1; i <= dte.Solution.Projects.Count; i++) {
        var c = dte.Solution.Projects.Item(i).ProjectItems.Count;
        var arr2 = iterateProjectFiles(dte.Solution.Projects.Item(i).ProjectItems, c, ext, arr);
        arr = arr.concat(arr2)
    }
    return arr;
}

function iterateProjectFiles(projectItems, unused, ext) {
    var arr = [];
    for (var i = 1; i <= projectItems.Count; i++) {
        var file = projectItems.Item(i);

        if (file.Name == "Generated Files")
            ;//skip these
        else
        if (isSuitableCandidate(file, ext)) {
            arr.push(file);
        } else if (file.SubProject != null) {
            var arr2 = iterateProjectFiles(file.SubProject.ProjectItems, 0, ext);
            arr = arr.concat(arr2)
        } else if (file.ProjectItems != null && file.ProjectItems.Count > 0) {
            var arr2 = iterateProjectFiles(file.ProjectItems, 0, ext);
            arr = arr.concat(arr2)
        }
    }
    return arr;
}

function endsWith(str, end) {
    return str.indexOf(end, str.length - end.length) != -1;
}

function startsWith(str, start) {
    return str.indexOf(start, 0) == 0;
}

function isSuitableCandidate(file, ext) {
    if (endsWith(file.Name, ext)) {
        if (file.Name.indexOf("moc_") == 0)
            return false;
        if (!endsWith(file.Name, "Test.cpp"))
            return true;
    }
    return false;
}
tvaneerd commented 6 years ago

Also, I don't have complete faith in the startsWith and endsWith functions, but those are basically copied from "Remove and Sort All".