Search code examples
javascriptfor-loopvscode-extensionsvscode-api

JavaScript for loop not working with VS Code extension API


I'm writing a VS Code extension in JavaScript. It requires a loop to open several text files so I used a for loop. Here's my a sample of the code for the loop:

const allFiles = vscode.workspace.findFiles('**/*.*', '**/node_modules/**');

    for (let i = 0; i < allFiles.length; i++) { 
        vscode.workspace.openTextDocument(allFiles[i])
    }
        

Here's the whole extension.js file:

// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
const vscode = require('vscode');
const path = require('path')

// This method is called when your extension is activated
// Your extension is activated the very first time the command is executed

/**
 * @param {vscode.ExtensionContext} context
 */
function activate(context) {

    // Use the console to output diagnostic information (console.log) and errors (console.error)
    // This line of code will only be executed once when your extension is activated
    console.log('Congratulations, your extension "code-runner-from-the-web" is now active!');
    const allFiles = vscode.workspace.findFiles('**/*.*', '**/node_modules/**');

    // The command has been defined in the package.json file
    // Now provide the implementation of the command with  registerCommand
    // The commandId parameter must match the command field in package.json
    let disposable = vscode.commands.registerCommand('code-runner-from-the-web.helloWorld', function () {
        // The code you place here will be executed every time your command is executed

        // Display a message box to the user
        vscode.window.showInformationMessage('Hello World from Code Runner From The Web!');
    });

    for (let i = 0; i < allFiles.legnth; i++) { 
        console.log (vscode.workspace.openTextDocument(allFiles[i]))
        vscode.commands.executeCommand('code-runner.run', null)
    }
        

    context.subscriptions.push(disposable);
}

// This method is called when your extension is deactivated
function deactivate() {}

module.exports = {
    activate,
    deactivate
}

Where am I going wrong here?


Solution

  • The solution was to use an async function.

    // Import the module and reference it with the alias vscode in your code below
    const vscode = require('vscode');
    const path = require('path')
    
    // This method is called when your extension is activated
    // Your extension is activated the very first time the command is executed
    
    /**
     * @param {vscode.ExtensionContext} context
     */
    async function activate(context) {
    
        // Use the console to output diagnostic information (console.log) and errors (console.error)
        // This line of code will only be executed once when your extension is activated
        console.log('Congratulations, your extension "code-runner-from-the-web" is now active!');
    
        // The command has been defined in the package.json file
        // Now provide the implementation of the command with  registerCommand
        // The commandId parameter must match the command field in package.json
        let disposable = vscode.commands.registerCommand('code-runner-from-the-web.helloWorld', function () {
            // The code you place here will be executed every time your command is executed
    
            // Display a message box to the user
            vscode.window.showInformationMessage('Hello World from Code Runner From The Web!');
    
            const getAllFiles = vscode.workspace.findFiles('**/*.*', '**/node_modules/**');
    
            const findFile = async () => {
                const allFiles = await getAllFiles
                console.log(allFiles)
                for (let i = 0; i < allFiles.length; i++) {
                    vscode.workspace.openTextDocument(allFiles[i])
                    //vscode.commands.executeCommand('code-runner.run', null)
                    console.log("Hello!!!!")
                }
            }
            findFile()
    
        });
        
        context.subscriptions.push(disposable);
    }
    
    // This method is called when your extension is deactivated
    function deactivate() { }
    
    module.exports = {
        activate,
        deactivate
    }