gleam-lang / gleam

⭐️ A friendly language for building type-safe, scalable systems!
https://gleam.run
Apache License 2.0
17.6k stars 735 forks source link

LSP Code Action: Desugar `use` expression #3314

Open lpil opened 3 months ago

lpil commented 3 months ago

For example, turning:

use x <- result.map(Ok(1))
io.debug(x)

Into:

result.map(Ok(1), fn(x) { io.debug(x) })

Related to https://github.com/gleam-lang/gleam/issues/3311

giacomocavalieri commented 3 months ago

If you had multiple use expressions in a block what would the desired behaviour be?

use x <- result.try(Ok(1))
use y <- result.try(Error(1))
Ok(1)

Desugaring all of them? Or just the one where the cursor is?

result.try(Ok(1), fn(x) {
  use y <- result.try(Error(1))
  Ok(1)
})
// Or all the way down
result.try(Ok(1), fn(x) {
  result.try(Error(1), fn(y) {
    Ok(1)
  })
})
lpil commented 3 months ago

Just one please

Zhomart commented 2 months ago

Here's an attempt - https://github.com/gleam-lang/gleam/pull/3367

giacomocavalieri commented 2 months ago

I was trying to implement this as well, but I was trying a different route and changing the typing step so that use gets turned into its own typed AST node instead of turning it into a function call. I'm not sure what's best here though

Zhomart commented 2 months ago

I was trying to implement this as well, but I was trying a different route and changing the typing step so that use gets turned into its own typed AST node instead of turning it into a function call. I'm not sure what's best here though

In my opinion this would be preferable. It looks like https://github.com/gleam-lang/gleam/pull/3367 is little bit brittle.

giacomocavalieri commented 2 months ago

Ok, if you want to give it a shot I'd be happy to help and share the work in progress code or I could keep working on it :)

Zhomart commented 2 months ago

Ok, if you want to give it a shot I'd be happy to help and share the work in progress code or I could keep working on it :)

If you already have something working, let's see your PR. If you've created Use type, then may be I can do something on top of it.

giacomocavalieri commented 2 months ago

Sure! I just got started with it and didn't even get to the code action part, but you can have a look at the code here https://github.com/giacomocavalieri/gleam/commit/11b5630ee98731a1709be2c1f6b07149897eac8a

Hope it can be of any help!