Skip to content

Conversation

@vitallium
Copy link
Collaborator

Change all argument captures from @name to @context.extra in both call patterns to properly categorize these elements as contextual information rather than names to avoid capturing them for ZED_SYMBOL variable. This variable is used for running tests.

Sample code:

class CategoryTest < ActiveSupport::TestCase
  class Foo
    include Bar
    belongs_to :foo
    has_many :whatever
    alias_method :a, :b
  end

  class Foo
    include Bar
    belongs_to :foo
    has_many :whatever
    alias_method :a, :b
  end

  module Foo
    include Bar
    belongs_to :foo
    has_many :whatever
    alias_method :a, :b
  end

  class Foo
    private
    important!
  end

  module Foo
    private
    important!
  end

  test "the truth" do
    assert true
  end
end

The outline is identical to the current behavior:

CategoryTest
└── Foo (class)
    ├── include Bar
    ├── belongs_to :foo
    ├── has_many :whatever
    └── alias_method :a, :b
└── Foo (class)
    ├── include Bar
    ├── belongs_to :foo
    ├── has_many :whatever
    └── alias_method :a, :b
└── Foo (module)
    ├── include Bar
    ├── belongs_to :foo
    ├── has_many :whatever
    └── alias_method :a, :b
└── Foo (class)
    ├── private
    └── important!
└── Foo (module)
    ├── private
    └── important!
└── test "the truth"

Screenshots

before after
before after

Fixes #92

Change all argument captures from `@name` to `@context.extra` in both
call patterns to properly categorize these elements as contextual
information rather than names to avoid capturing them for `ZED_SYMBOL`
variable. This variable is used for running tests.

Sample code:

```ruby
class CategoryTest < ActiveSupport::TestCase
  class Foo
    include Bar
    belongs_to :foo
    has_many :whatever
    alias_method :a, :b
  end

  class Foo
    include Bar
    belongs_to :foo
    has_many :whatever
    alias_method :a, :b
  end

  module Foo
    include Bar
    belongs_to :foo
    has_many :whatever
    alias_method :a, :b
  end

  class Foo
    private
    important!
  end

  module Foo
    private
    important!
  end

  test "the truth" do
    assert true
  end
end
```

The outline is identical to the current behavior:

```
CategoryTest
└── Foo (class)
    ├── include Bar
    ├── belongs_to :foo
    ├── has_many :whatever
    └── alias_method :a, :b
└── Foo (class)
    ├── include Bar
    ├── belongs_to :foo
    ├── has_many :whatever
    └── alias_method :a, :b
└── Foo (module)
    ├── include Bar
    ├── belongs_to :foo
    ├── has_many :whatever
    └── alias_method :a, :b
└── Foo (class)
    ├── private
    └── important!
└── Foo (module)
    ├── private
    └── important!
└── test "the truth"
```
@cla-bot cla-bot bot added the cla-signed label May 9, 2025
@vitallium
Copy link
Collaborator Author

We probably could use context.extra in other places as well but I would like to focus on resolving the linked issue first.

(simple_symbol) @context.extra
(scope_resolution) @context.extra
(constant) @context.extra
"," @context
Copy link
Collaborator Author

@vitallium vitallium May 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this capture should be @context.extra as well?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't use context.extra here and everywhere else because we will lose ability to search through symbols:

CleanShot.2025-05-10.at.06.57.58.mp4

@duduribeiro
Copy link

hey @vitallium 👋

I tested this branch locally and there is something odd now:

calling it from the class works fine:

⏵ Task `test test/models/cnae_test.rb -n /CnaeTest/` finished successfully
⏵ Command: /bin/zsh -i -c 'bin/rails test test/models/cnae_test.rb -n /CnaeTest/'

but calling from the test is not getting the right test name. it uses the word test instead of the test name itself:

class CnaeTest < ActiveSupport::TestCase
  test "formats a CNAE code" do # <<< running here
    assert_equal "6209-1/00", Cnae.format("6209100")
  end
⏵ Task `test test/models/cnae_test.rb -n /test/` finished successfully
⏵ Command: /bin/zsh -i -c 'bin/rails test test/models/cnae_test.rb -n /test/'

See it is using test instead of formats a CNAE code?

@vitallium
Copy link
Collaborator Author

vitallium commented May 10, 2025

I think the fix here is not correct. I think losing ability to search through additional information of methods like include Bar (Bar) is important. Instead we can rely on captures from the Runnables query because Zed supports custom captures as variables in task templates. Consider the following query

(
    (call
        method: (identifier) @run (#eq? @run "test")
        arguments: (argument_list (string (string_content) @name))
    ) @_ruby-test
    (#set! tag ruby-test)
)

This query has 3 captures: @run, @name, and @_ruby-test. The @run capture is ZED_RUNNABLE_SYMBOL, which is used to determine the position of the Run task button. All other captures are mapped to ZED_CUSTOM_(capture_name) and can be used in task templates:

  • @name -> ZED_CUSTOM_name
  • @_ruby-test -> N/A. Captures prefixed with an underscore (_) are not mapped.

The task template in such a scenario will be like this:

[
  {
    "label": "test $ZED_RELATIVE_FILE -n /$ZED_CUSTOM_name/",
    "command": "bin/rails",
    "args": ["test", "$ZED_RELATIVE_FILE", "-n", "/$ZED_CUSTOM_name/"],
    "tags": ["ruby-test"]
  }
]

This isn't the best option, but it should work. I'll open another PR using this approach.

@vitallium
Copy link
Collaborator Author

hey @vitallium 👋

I tested this branch locally and there is something odd now:

calling it from the class works fine:

⏵ Task `test test/models/cnae_test.rb -n /CnaeTest/` finished successfully
⏵ Command: /bin/zsh -i -c 'bin/rails test test/models/cnae_test.rb -n /CnaeTest/'

but calling from the test is not getting the right test name. it uses the word test instead of the test name itself:

class CnaeTest < ActiveSupport::TestCase
  test "formats a CNAE code" do # <<< running here
    assert_equal "6209-1/00", Cnae.format("6209100")
  end
⏵ Task `test test/models/cnae_test.rb -n /test/` finished successfully
⏵ Command: /bin/zsh -i -c 'bin/rails test test/models/cnae_test.rb -n /test/'

See it is using test instead of formats a CNAE code?

Yeah, I noticed that as well. Please see my comment above about the incorrect fix in this pull request. Thanks for testing it too!

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

New outline queries produce invalid $ZED_SYMBOL for runnables

3 participants