Sublime Forum

Multicommand requires two calls

#1

Hello,

I’m trying to adjust my ctrl+s shortcut in json files to call both “lsp_format_document” and “save” in that order. Here is my current try at a corresponding keybind:

{ "keys": [ "ctrl+s" ], "context": [ { "key": "selector", "operand": "source.json", "operator": "equal" } ], "command": "multicommand", "args": { "commands": [ { "command": "lsp_format_document" }, { "command": "save", "args": { "async": true } } ] } }

After saving my sublime-keymap and testing on json files I experience the following behaviour:

  1. Pressing ctrl+s formats the json (adjusts indentation and such) without saving.
  2. Pressing ctrl+s again on the formatted json file then saves the file.

My goal, and the expected behaviour, is to do both of those things (formatting + saving) with a single keypress.

To replicate you need the packages: multicommand, lsp, lsp-json

Thanks in advance!

0 Likes

#2

What I’ve tried so far:

  1. Adding a pause between the two commands by including time.sleep(2) in the multi_command.py script in the line after self.exec_command(command). Unexpectedly that added the delay before formatting the json and did not fix the problem.
0 Likes

#3

lsp_format_document is asynchronous, thus doesn’t block until formatting is completed, thus subsequent save command. Thus document is actually saved and then modified again by reformatting.

Maybe you just want to enable lsp_format_on_save setting in JSON syntax specific settings?


  // If supported, format a file before saving.
  // This option is also supported in syntax-specific settings and/or in the
  // "settings" section of project files.
  "lsp_format_on_save": false,
0 Likes

#4

Thanks a lot! Can’t believe I didn’t see that option before

0 Likes