<p dir="auto">In an extension, I tried to register an event listener for onDidSaveTextDocument() which would activate the format function, and save the file afterwards.<br>
Using the code:</p>
<div class="highlight highlight-source-ts notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="commands.executeCommands('editor.action.format').then(() =&gt; {
 commands.executeCommands('workbench.action.files.save');
});"><pre class="notranslate"><span class="pl-s1">commands</span><span class="pl-kos">.</span><span class="pl-en">executeCommands</span><span class="pl-kos">(</span><span class="pl-s">'editor.action.format'</span><span class="pl-kos">)</span><span class="pl-kos">.</span><span class="pl-en">then</span><span class="pl-kos">(</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-c1">=&gt;</span> <span class="pl-kos">{</span>
 <span class="pl-s1">commands</span><span class="pl-kos">.</span><span class="pl-en">executeCommands</span><span class="pl-kos">(</span><span class="pl-s">'workbench.action.files.save'</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">;</span></pre></div>
<p dir="auto">The problem is that the file gets saved, then formatted, but not saved afterwards. Using a timer for a 500ms delay after the format, before the next save, gives the wanted result.</p>
<p dir="auto">The function inside of ".then((){   }" should not be called before the file is actually formatted, unless there is something else I don't understand.</p>
<p dir="auto">The use case is to call the <code class="notranslate">workbench.action.closeAllEditors</code> function and then assert that the number of visible editors is 0. Currently when doing the check after calling the function, <code class="notranslate">window.visibleTextEditors</code> is still 1.</p>
<p dir="auto">I think this is a timing issue where executing a built in command does not wait for any changes to be propagated to the PH side.</p>
<p dir="auto">Test case showing the issue (commented out atm): <a href="https://github.com/Microsoft/vscode/blob/master/extensions/vscode-api-tests/src/commands.test.ts#L19">https://github.com/Microsoft/vscode/blob/master/extensions/vscode-api-tests/src/commands.test.ts#L19</a></p>