gleam-lang/gleam

Extract function extracts too much

Fechada

#5.700 aberto em 10 de mai. de 2026

 (6 comentários) (0 reação) (0 responsável)Rust (960 forks)batch import
help wanted

Métricas do repositório

Stars
 (21.417 estrelas)
Métricas de merge de PR
 (Mesclagem média 10d 19h) (69 fundiu PRs em 30d)

Description

When triggering Extract function with the selection:

/// prepare an empty gleam project in the `target_path`
///
fn prepare_project_structure(
  target_path target: TargetPath,
  user_id user_id: uuid.Uuid,
) -> Result(Nil, SubmitError) {
  // get the priv dir
  use priv <- result.try(
    application.priv_directory("gleeps")
    |> result.replace_error(FailedToFindPrivDirectory),
  )

  // copy the base project to the target_path
  use _ <- result.try(
    simplifile.copy_directory(filepath.join(priv, "base_project"), target.path)
    |> result.replace_error(FailedToCopyBaseProject),
  )

  // replace $PROJECT_NAME placeholder in the gleam.toml
  // <-------------------------------------------------------------- selected from here
  let toml_path =
    target.path
    |> filepath.join("gleam.toml")

  use gleam_toml <- result.try(
    toml_path
    |> simplifile.read
    |> result.replace_error(FailedToReadGleamToml),
  )

  let gleam_toml =
    gleam_toml
    |> string.replace("$PROJECT_NAME", with: user_id_to_string(user_id))

  simplifile.write(toml_path, gleam_toml)
  |> result.replace_error(FailedToWriteGleamToml)
  // <---------------------------------------------------------------- to here
}

It will extract the entire function content into a new function leaving:

fn prepare_project_structure(
  target_path target: TargetPath,
  user_id user_id: uuid.Uuid,
) -> Result(Nil, SubmitError) {
  function(target, user_id)
}

instead of the expected:

/// prepare an empty gleam project in the `target_path`
///
fn prepare_project_structure(
  target_path target: TargetPath,
  user_id user_id: uuid.Uuid,
) -> Result(Nil, SubmitError) {
  // get the priv dir
  use priv <- result.try(
    application.priv_directory("gleeps")
    |> result.replace_error(FailedToFindPrivDirectory),
  )

  // copy the base project to the target_path
  use _ <- result.try(
    simplifile.copy_directory(filepath.join(priv, "base_project"), target.path)
    |> result.replace_error(FailedToCopyBaseProject),
  )
  // replace $PROJECT_NAME placeholder in the gleam.toml
  function(target, user_id)
}

fn function(
  target: TargetPath,
  user_id: uuid.Uuid,
) -> Result(Nil, SubmitError) {
  let toml_path =
    target.path
    |> filepath.join("gleam.toml")

  use gleam_toml <- result.try(
    toml_path
    |> simplifile.read
    |> result.replace_error(FailedToReadGleamToml),
  )

  let gleam_toml =
    gleam_toml
    |> string.replace("$PROJECT_NAME", with: user_id_to_string(user_id))

  simplifile.write(toml_path, gleam_toml)
  |> result.replace_error(FailedToWriteGleamToml)
}

Using gleam 1.16.0 in neovim nightly

Guia do colaborador