zed-industries / zed

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
https://zed.dev
Other
49.51k stars 3.02k forks source link

Add a `selected text or current buffer` variable in tasks #12627

Open failable opened 5 months ago

failable commented 5 months ago

Check for existing issues

Describe the feature

Add a selected text or current buffer variable in tasks. If there is no text selected, the value of the variable default to current whole buffer. With this variable, the below two tasks can be merged into one

  {
    "label": "Dpaste (File)",
    "command": "cat $ZED_FILE | curl -s -F \"content=<-\" https://dpaste.com/api/v2/ | pbcopy",
    "use_new_terminal": false,
    "allow_concurrent_runs": false,
    "reveal": "never"
  },
  {
    "label": "Dpaste (Region)",
    "command": "echo -e \"$ZED_SELECTED_TEXT\" | curl -s -F \"content=<-\" https://dpaste.com/api/v2/ | pbcopy",
    "use_new_terminal": false,
    "allow_concurrent_runs": false,
    "reveal": "never"
  }

This is a common pattern in Emacs, e.g.

  (crux-with-region-or-line kill-region)
  (crux-with-region-or-buffer eval-region)
  (crux-with-region-or-buffer untabify)
  (crux-with-region-or-buffer indent-region)
...

If applicable, add mockups / screenshots to help present your vision of the feature

No response

osiewicz commented 5 months ago

You can already merge this task into one by using the following: ${ZED_SELECTED_TEXT:-$(cat "$ZED_FILE")} instead of $ZED_SELECTED_TEXT It may not work if you're using fish though.

failable commented 5 months ago

@osiewicz Hello, thanks for the hint.

I use bash and I try to get it work but can not figure out how to handle the quotes. The below two definition does not work.

This version does not work for selected text or whole file content:

{
    "label": "Dpaste",
    "command": "echo -e \"${ZED_SELECTED_TEXT:-$(cat \\\"$ZED_FILE\\\")}\" | curl -s -F \"content=<-\" https://dpaste.com/api/v2/ | pbcopy",
    "use_new_terminal": false,
    "allow_concurrent_runs": false,
    "reveal": "always"
  }

This version does work for selected text but does not work for whole file content:

{
    "label": "Dpaste",
    "command": "echo -e \"${ZED_SELECTED_TEXT:-$(cat \"$ZED_FILE\")}\" | curl -s -F \"content=<-\" https://dpaste.com/api/v2/ | pbcopy",
    "use_new_terminal": false,
    "allow_concurrent_runs": false,
    "reveal": "always"
  }
osiewicz commented 5 months ago

What version of Zed are you using? The second definition works for me on both Nightly and latest Stable (0.137.6)

failable commented 5 months ago

Hello, I'm using Zed Preview 0.138.4. My bad, the second version works for whole file content but does not work for selected text.

image
osiewicz commented 5 months ago

Oh, I see. It fails for me as well with that selection. I think you can change the double quotes to single quotes like so: echo -e '${ZED_SELECTED_TEXT:-$(cat \"$ZED_FILE\")}' | curl -s -F \"content=<-\" https://dpaste.com/api/v2/ | pbcopy Though if you try to run that on the same selection, you'll run into a small culprit (we perform variable substitution ourselves on cmd. I can fix that.

failable commented 5 months ago

@osiewicz Thanks for the hint. I always run into trouble when trying to quote variables. If there are clear rules of how it works, it would be great.