Skip to content

Overlays#5375

Merged
sophiajt merged 64 commits intonushell:mainfrom
kubouch:overlays
May 7, 2022
Merged

Overlays#5375
sophiajt merged 64 commits intonushell:mainfrom
kubouch:overlays

Conversation

@kubouch
Copy link
Copy Markdown
Contributor

@kubouch kubouch commented Apr 29, 2022

Description

This PR adds the concept of "overlays" to the engine. Overlays are created from modules and can be thought of as interchangeable scope frames.

A short walk-through

>>> open spam.nu
export def foo [] { "foo" }

export alias bar = "bar"

export env BAZ { "baz" }

>>> overlay add spam.nu  # create a module "spam" and add it as an overlay

>>> overlay list  # list all active ovelays (zero is the default overlay that's always present)
╭───┬──────╮
│ 0 │ zero │
│ 1 │ spam │
╰───┴──────╯

>>> foo; bar; $env.BAZ
foo
bar
baz

>>> overlay remove spam

>>> foo; bar; $env.BAZ  # definitions from the removed overlay are not visible anymore
Error: nu::shell::external_command (https://docs.rs/nu-protocol/0.61.1/nu_protocol/enum.ShellError.html#variant.ExternalCommand)

  × External command
   ╭─[entry #4:1:1]
 1 │ foo; bar; $env.BAZ
   · ─┬─
   ·  ╰── can't run executable
   ╰────
  help: No such file or directory (os error 2)

Overlays are mutable

Whenever you define a new custom command / alias / environment variable, it will be automatically added to the last active overlay.
When you remove the overlay, by default, the new changes leave the scope together with the removed overlay.

>>> overlay add spam.nu

>>> def eggs [] { "eggs" }

>>> eggs
eggs

>>> overlay remove spam

>>> eggs
Error: nu::shell::external_command (https://docs.rs/nu-protocol/0.61.1/nu_protocol/enum.ShellError.html#variant.ExternalCommand)

  × External command
   ╭─[entry #13:1:1]
 1 │ eggs
   · ──┬─
   ·   ╰── can't run executable
   ╰────
  help: No such file or directory (os error 2)

Tips

To show the last activated overlay in your prompt, update your PROMPT_COMMAND with

overlay list | last

or

ovelay list | where $it != 'zero' | last

("zero" is the name of the default overlay I currently use).

Tests

Make sure you've run and fixed any issues with these commands:

  • cargo fmt --all -- --check to check standard code formatting (cargo fmt --all applies these changes)
  • cargo clippy --all --all-features -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect to check that you're using the standard code style
  • cargo build; cargo test --all --all-features to check that all the tests pass

kubouch and others added 9 commits May 5, 2022 11:00
* added flags and optional arguments to view-source

* removed redundant code

* removed redundant code

* fmt
* fix bug in shell_integration

* add some comments
* enable cd to work with abbreviations

* add abbreviation example

* fix tests

* make it configurable
Signed-off-by: Yuheng Su <gipsyh.icu@gmail.com>
kubouch added 6 commits May 7, 2022 18:03
In case the origin module updates, the overlay add loads the new module,
makes it overlay's origin and applies the changes. Before, it was
impossible to update the overlay if the module changed.
@kubouch kubouch marked this pull request as ready for review May 7, 2022 16:39
@kubouch kubouch requested a review from sophiajt May 7, 2022 16:41
@sophiajt
Copy link
Copy Markdown
Contributor

sophiajt commented May 7, 2022

Niiice job. Let's do it!

@sophiajt sophiajt merged commit 9b99b2f into nushell:main May 7, 2022
fennewald pushed a commit to fennewald/nushell that referenced this pull request Jun 27, 2022
* WIP: Start laying overlays

* Rename Overlay->Module; Start adding overlay

* Revamp adding overlay

* Add overlay add tests; Disable debug print

* Fix overlay add; Add overlay remove

* Add overlay remove tests

* Add missing overlay remove file

* Add overlay list command

* (WIP?) Enable overlays for env vars

* Move OverlayFrames to ScopeFrames

* (WIP) Move everything to overlays only

ScopeFrame contains nothing but overlays now

* Fix predecls

* Fix wrong overlay id translation and aliases

* Fix broken env lookup logic

* Remove TODOs

* Add overlay add + remove for environment

* Add a few overlay tests; Fix overlay add name

* Some cleanup; Fix overlay add/remove names

* Clippy

* Fmt

* Remove walls of comments

* List overlays from stack; Add debugging flag

Currently, the engine state ordering is somehow broken.

* Fix (?) overlay list test

* Fix tests on Windows

* Fix activated overlay ordering

* Check for active overlays equality in overlay list

This removes the -p flag: Either both parser and engine will have the
same overlays, or the command will fail.

* Add merging on overlay remove

* Change help message and comment

* Add some remove-merge/discard tests

* (WIP) Track removed overlays properly

* Clippy; Fmt

* Fix getting last overlay; Fix predecls in overlays

* Remove merging; Fix re-add overwriting stuff

Also some error message tweaks.

* Fix overlay error in the engine

* Update variable_completions.rs

* Adds flags and optional arguments to view-source (nushell#5446)

* added flags and optional arguments to view-source

* removed redundant code

* removed redundant code

* fmt

* fix bug in shell_integration (nushell#5450)

* fix bug in shell_integration

* add some comments

* enable cd to work with directory abbreviations (nushell#5452)

* enable cd to work with abbreviations

* add abbreviation example

* fix tests

* make it configurable

* make cd recornize symblic link (nushell#5454)

* implement seq char command to generate single character sequence (nushell#5453)

* add tmp code

* add seq char command

* Add split number flag in `split row` (nushell#5434)

Signed-off-by: Yuheng Su <gipsyh.icu@gmail.com>

* Add two more overlay tests

* Add ModuleId to OverlayFrame

* Fix env conversion accidentally activating overlay

It activated overlay from permanent state prematurely which would
cause `overlay add` to misbehave.

* Remove unused parameter; Add overlay list test

* Remove added traces

* Add overlay commands examples

* Modify TODO

* Fix $nu.scope iteration

* Disallow removing default overlay

* Refactor some parser errors

* Remove last overlay if no argument

* Diversify overlay examples

* Make it possible to update overlay's module

In case the origin module updates, the overlay add loads the new module,
makes it overlay's origin and applies the changes. Before, it was
impossible to update the overlay if the module changed.

Co-authored-by: JT <547158+jntrnr@users.noreply.github.com>
Co-authored-by: pwygab <88221256+merelymyself@users.noreply.github.com>
Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com>
Co-authored-by: WindSoilder <WindSoilder@outlook.com>
Co-authored-by: Yuheng Su <gipsyh.icu@gmail.com>
@kubouch kubouch deleted the overlays branch July 14, 2022 14:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants