ryuta46 / vscode-multi-command

Visual Studio Code Plugin named multi-command
MIT License
229 stars 14 forks source link

When does the next command executed (after first command finishes or in parallel)? #54

Open Ashark opened 2 years ago

Ashark commented 2 years ago

I have asked in s.o., but probably I get more lack asking this project directly.

How can I control the execution order of multiCommand extension? It behaves like it executes them in parallel, while I want them to be executed one after another.

I have a project with the following structure:

/home/user/myproject/dir1/problem1.py
/home/user/myproject/dir1/problem1.txt
/home/user/myproject/dir1/problem2.py
/home/user/myproject/dir1/problem2.txt
...
/home/user/myproject/pointer.txt

The pointer.txt contains the text: dir1/problem2.

I want to press a shortcut, and do a sequence of actions:

I setuped the following things.

In settings.json I defined the command sequence named "openPointedProblemLayout" (for being able to easily reuse it):

"multiCommand.commands": [
        {
            "command": "multiCommand.openPointedProblemLayout",
            "sequence": [
                {   "command": "htmlRelatedLinks.openFile",
                    "args": {
                        "file": "${command:mypointer}.py",
                        "method": "vscode.open",
                        "viewColumn": 1,
                        "command": {
                            "mypointer": {
                                "command": "extension.commandvariable.file.content",
                                "args": {
                                    "fileName": "${workspaceFolder}/pointer.txt"
                                }
                            }
                        }
                    }
                },
                {   "command": "htmlRelatedLinks.openFile",
                    "args": {
                        "file": "${command:mypointer}.txt",
                        "method": "vscode.open",
                        "viewColumn": 2,
                        "command": {
                            "mypointer": {
                                "command": "extension.commandvariable.file.content",
                                "args": {
                                    "fileName": "${workspaceFolder}/pointer.txt"
                                }
                            }
                        }
                    }
                },
            ]

        },
    ]

In tasks.json I created a shell command definition, that creates a new .py and .txt pair and also changes the pointer:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "create_new_problem_files_pair",
            "type": "shell",
            "command": "python /home/user/scripts/create_new_problem_files_pair.py \"${file}\""
        },
    ],
}

In keybindings.json I defined shortcut numpad2 that executes both actions (creates files and opens them) and a numpad5 (just opens them):

    {
        "key": "numpad2",
        "command": "extension.multiCommand.execute",
        "args": {
            "sequence": [
                {
                    "command": "workbench.action.tasks.runTask",
                    "args": "create_new_problem_files_pair"
                },
                {
                    "command": "multiCommand.openPointedProblemLayout"
                },
            ]
        }
    },
    {
        "key": "numpad5",
        "command": "extension.multiCommand.execute",
        "args": { "command": "multiCommand.openPointedProblemLayout" },
    },

Now, when I press numpad2, the two new files are created:

/home/user/myproject/dir1/problem3.py
/home/user/myproject/dir1/problem3.txt

And then two files are opened in layout (means the command actually runs), but wrong files. They are problem2.py and problem2.txt, i.e. the previous pointer is used.

I checked the content of the pointer.txt now, and it actually contains dir1/problem3. And when I press numpad5, they are opened correctly.

Why does the VS Codium uses previous content of pointer, while at the moment of command run, it should already take the new content? It looks like VS Code executes the command sequence in parallel, instead of sequence them.

Am I doing something wrong? Is that an issue with configuration or vs code itself or maybe in multiCommand extension?

ryuta46 commented 2 years ago

I reproduced with small sample.

This extension waits until the previous command has finished, but the task "workbench.action.tasks.runTask" returned immediately and it seems to execute commands specified by the arguments in an internal thread.

I think your solution in SO is good. Or, if you want to use the multi-command specified in settings.json, you can call it like ${command:***}

ex.

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "write_file_with_delay",
            "type": "shell",
            "command": "python test.py",
        },
        {
            "label": "execute_command_after_write_file_with_delay",
            "command": "${command:multiCommand.showFileContent}",
            "dependsOrder": "sequence",
            "dependsOn": ["write_file_with_delay"]
        }
    ]
}