Skip to content

Feature request: switch between a Ruby file and its test (spec) file #33

Description

@vittorius

Basically, the subject. It's a pretty common piece of functionality for a Ruby extension IMHO.

Also, there can be a config to map custom relations between files and their specs (e.g. from app/controllers/{file}.rb to spec/requests/api/v1/{file}_spec.rb.

A good example of it could be https://marketplace.visualstudio.com/items?itemName=malt03.rails-rspec-file-toggle. We can replicate its functionality.

WDYT? I'm willing to contribute BTW.

Activity

  1. vitallium commented on Feb 18, 2025

    @vitallium
    Collaborator

    Hi, this would be amazing, but unfortunately, the Zed Extension API is limited, and there is no way to implement this currently.

  2. asok commented on Feb 28, 2025

    @asok
    Contributor

    For anyone looking for some kind of a solution for this I'm able to employ tasks to achieve this.
    Basically I have this in my tasks.json:

      {
        "label": "open_alternative_file",
        "command": "zed \"$ZED_WORKTREE_ROOT\"/$(spec-alternative-file $ZED_RELATIVE_FILE)",
        "hide": "on_success",
        "allow_concurrent_runs": false,
        "use_new_terminal": false,
        "reveal": "never",
        "shell": {
          "with_arguments": {
            "program": "sh",
            "args": ["--noediting", "--norc", "--noprofile"]
          }
        }
      },
    

    and such script in my PATH:

    #!/bin/sh
    
    # Check if a filename is provided
    if [ "$#" -ne 1 ]; then
        echo "Usage: $0 <filename>"
        exit 1
    fi
    
    # Get the provided filename
    filename=$1
    
    # Determine the alternative file
    if [[ "$filename" =~ _spec\.rb$ ]]; then
        # If the file is a spec file, convert it to the corresponding regular Ruby file
        alternative_file=$(echo "$filename" | sed 's|^spec/|app/|; s|_spec\.rb$|.rb|')
    else
        # If the file is a regular Ruby file, convert it to the corresponding spec file
        alternative_file=$(echo "$filename" | sed 's|^app/|spec/|; s|\.rb$|_spec.rb|')
    fi
    
    echo "$alternative_file"
  3. andyw8 commented on Mar 24, 2025

    @andyw8
    Contributor

    This feature was added to Ruby LSP in Shopify/ruby-lsp#2985 and uses a custom LSP request along with integration with VS Code.

    The PR description also shows how it can be used with NeoVim, and I imagine something similar would be possible for Zed once scripting is supported (there's a long discussion here).

    @asok's approach of shelling out to zed is interesting, but I can't think how that would be hooked into the language server.

    cc @jenny-codes

  4. joeldrapper commented on Apr 16, 2025

    @joeldrapper
    Contributor

    I got this working using a task that I assigned to a keyboard shortcut. You just need an executable that can take a file as input and then launch zed to another file by shelling out. If it’s possible to include a task and have it point to an executable in the extension, it could be doable.

  5. andyw8 commented on Apr 16, 2025

    @andyw8
    Contributor

    I suspect it wouldn't be viable since the extension compiles down to WebAssembly, but you could perhaps have the extension auto-install a tiny gem?

  6. MoskitoHero commented on Apr 17, 2025

    @MoskitoHero

    I just started writing that gem yesterday. It works locally, I just need a little time to package it.

  7. MoskitoHero commented on Apr 17, 2025

    @MoskitoHero

    Here you go, it’s quite rough for now, but it does the job (on my projects). Feel free to make PRs to match more settings / frameworks.

    https://github.com/MoskitoHero/zed-test-toggle

  8. andyw8 commented on Apr 17, 2025

    @andyw8
    Contributor

    Nice! Had to make a small fix to the README example:

    MoskitoHero/zed-test-toggle#1

  9. added theissue type on Apr 24, 2025
  10. Tonksthebear commented on Jun 30, 2025

    @Tonksthebear

    I made a rails helper in a similar vain to the zed-test-toggle. It allows jumping between a controller method and the related view if it exists and also a view and the respective controller + action. It takes into account inheritance and what not. It was a major bottleneck for me switching off of vs code forks so hopefully this helps others too until scripting is supported

    https://github.com/Tonksthebear/zed-rails-jumper

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions