nushell/crates/nu-lsp/src
Bahex 55de232a1c
refactor Value::follow_cell_path to reduce clones and return Cow (#15640)
# Description
While working on something else, I noticed that
`Value::follow_cell_path` receives `self`.

While it would be ideal for the signature to be `(&'a self, cell_path)
-> &'a Value`, that's not possible because:
1. Selecting a row from a list and field from a record can be done with
a reference but selecting a column from a table requires creating a new
list.
2. `Value::Custom` returns new `Value`s when indexed.

So the signature becomes `(&'a self, cell_path) -> Cow<'a, Value>`.

Another complication that arises is, once a new `Value` is created, and
it is further indexed, the `current` variable
1. can't be `&'a Value`, as the lifetime requirement means it can't
refer to local variables
2. _shouldn't_ be `Cow<'a, Value>`, as once it becomes an owned value,
it can't be borrowed ever again, as `current` is derived from its
previous value in further iterations. So once it's owned, it can't be
indexed by reference, leading to more clones

We need `current` to have _two_ possible lifetimes
1. `'out`: references derived from `&self`
2. `'local`: references derived from an owned value stored in a local
variable

```rust
enum MultiLife<'out, 'local, T>
where
    'out: 'local,
    T: ?Sized,
{
    Out(&'out T),
    Local(&'local T),
}
```
With `current: MultiLife<'out, '_, Value>`, we can traverse values with
minimal clones, and we can transform it to `Cow<'out, Value>` easily
(`MultiLife::Out -> Cow::Borrowed, MultiLife::Local -> Cow::Owned`) to
return it

# User-Facing Changes

# Tests + Formatting

# After Submitting

---------

Co-authored-by: Bahex <17417311+Bahex@users.noreply.github.com>
2025-05-01 09:43:57 -05:00
..
ast.rs refactor(lsp): flat_map with mutable accumulator (#15567) 2025-04-15 07:21:23 -05:00
completion.rs fix(lsp): a panic caused by completion with decl_id out of range (#15576) 2025-04-16 06:43:21 -05:00
diagnostics.rs fix(lsp): more accurate PWD: from env -> parent dir of current file (#15470) 2025-04-05 08:41:34 -05:00
goto.rs refactor Value::follow_cell_path to reduce clones and return Cow (#15640) 2025-05-01 09:43:57 -05:00
hints.rs refactor(lsp): flat_map with mutable accumulator (#15567) 2025-04-15 07:21:23 -05:00
hover.rs refactor Value::follow_cell_path to reduce clones and return Cow (#15640) 2025-05-01 09:43:57 -05:00
lib.rs fix(lsp): several edge cases of inaccurate references (#15523) 2025-04-09 21:15:35 -05:00
notification.rs fix(lsp): several edge cases of inaccurate references (#15523) 2025-04-09 21:15:35 -05:00
semantic_tokens.rs fix(lsp): regression of semantic tokens of module-prefixed commands (#15603) 2025-04-19 06:02:49 -05:00
signature.rs refactor(lsp): align markdown doc string with output of --help (#15508) 2025-04-06 08:37:59 -05:00
symbols.rs fix(lsp): several edge cases of inaccurate references (#15523) 2025-04-09 21:15:35 -05:00
workspace.rs fix(lsp): more accurate command name highlight/rename (#15540) 2025-04-10 06:26:43 -05:00