Skip to content

Conversation

@vitallium
Copy link
Collaborator

Description

Adds constants assignments to the symbols outline.
According to the Ruby specification almost everything is a constant:

class ApplicationController # ApplicationController is a constant
Bar = :bar # Bar is a constant
BAR = 1 # BAR is a constant
Bar = Class.new # Bar is a constant

The challenge is to show only the most necessary symbols in the symbols outline. Pulling all constants results in an output where class declarations are shown as well, and this is not what we want. The solution is to pull only assigned constants. We are fortunate here because, otherwise, the Ruby interpreter would produce an uninitialized constant error. Consider the following code:

# frozen_string_literal: true

ABC = 1
Goo = 2
Foo = 3

foo = 1

class Category < ApplicationRecord
  validates :name, presence: true, length: { maximum: 255 }, uniqueness: true
  validates :hidden, inclusion: { in: [ true, false ] }
end

class RubyBlog
  URL    = "rubyguides.com"
  AUTHOR = "Jesus Castello"
  SomeError = "Jesus Castello"
end

Opening the symbols outline panel produces the following:

image

Closes #50

@cla-bot cla-bot bot added the cla-signed label Apr 15, 2025
@vitallium vitallium marked this pull request as ready for review April 15, 2025 18:41
@vitallium vitallium merged commit 7044d0c into main Apr 15, 2025
1 check passed
@vitallium vitallium deleted the vs/add-constants-outline branch April 15, 2025 18:52

(assignment
(((constant) @constant
(#match? @constant "^[A-Z\\d_]")) @name
Copy link
Contributor

Choose a reason for hiding this comment

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

🤔 Why match on digits and underscores here? They're valid inside the constant name, but not at the beginning.

Copy link
Collaborator Author

@vitallium vitallium Apr 15, 2025

Choose a reason for hiding this comment

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

That was a bold copy-and-paste from highlights.scm, I shouldn't have done it that way. I also found another case where this query matches the incorrect node in an assignment::

Category = Kernel

Such code produces two constants in the outline panel:

CleanShot 2025-04-15 at 21 04 47@2x

I think we should target places where the code assigns values to constants only:

(assignment left: (constant) @name) @item

vitallium added a commit that referenced this pull request Apr 15, 2025
[Previous version](#60)
captures constant assignments to constants:

```ruby
Left = Right
```

In the snippet above, we capture both constants `Left` and `Right` but
we need `Left` only. This commit improves the TS query to capture only
the left side of the assignment.

|Before|After|
|----|----|
|![CleanShot 2025-04-15 at 21 32
00@2x](https://github.com/user-attachments/assets/173a24fe-34ed-41a2-b5b4-7e8cc8b8d1ea)|![CleanShot
2025-04-15 at 21 32
21@2x](https://github.com/user-attachments/assets/9893b631-bd44-4b38-abaf-8ec19a2c3392)|
notpeter pushed a commit to zed-industries/extensions that referenced this pull request Apr 30, 2025
Hi, this pull request updates the Ruby extension to v0.5.6, which
includes many goodies and fixes. You can find the full list of changes
on the release page
[here](https://github.com/zed-extensions/ruby/releases/tag/v0.5.6). I'm
also copying it below for your convenience:

## What's Changed

```
1. Remove default tasks.json by @vitallium in zed-extensions/ruby#36
2. Remove else from outdent triggers by @vitallium in zed-extensions/ruby#42
3. Add completion_query_characters by @smitbarmase in zed-extensions/ruby#49
4. Add minimal README by @andyw8 in zed-extensions/ruby#54
5. Specify ? and ! as word characters by @andyw8 in zed-extensions/ruby#58
6. Add some basic tasks by @andyw8 in zed-extensions/ruby#57
7. Add constants to outline view by @vitallium in zed-extensions/ruby#60
8. Capture constants with assignments only for outline panel by @vitallium in zed-extensions/ruby#61
9. Include singleton class in outline by @joeldrapper in zed-extensions/ruby#67
10. Fix missing Tailwind completions by @I-Info in zed-extensions/ruby#62
11. Fix visibility keywords in outline by @joeldrapper in zed-extensions/ruby#65
12. Include method modifiers in outline by @joeldrapper in zed-extensions/ruby#66
13. Improve Ruby outlines with support for macros by @joeldrapper in zed-extensions/ruby#70
14. Mark instance & class variables as "@variable.special" by @asok in zed-extensions/ruby#63
15. Add task to evaluate selected text as Ruby by @andyw8 in zed-extensions/ruby#73
```

Thanks!
lucascompython pushed a commit to lucascompython/extensions that referenced this pull request Oct 3, 2025
Hi, this pull request updates the Ruby extension to v0.5.6, which
includes many goodies and fixes. You can find the full list of changes
on the release page
[here](https://github.com/zed-extensions/ruby/releases/tag/v0.5.6). I'm
also copying it below for your convenience:

## What's Changed

```
1. Remove default tasks.json by @vitallium in zed-extensions/ruby#36
2. Remove else from outdent triggers by @vitallium in zed-extensions/ruby#42
3. Add completion_query_characters by @smitbarmase in zed-extensions/ruby#49
4. Add minimal README by @andyw8 in zed-extensions/ruby#54
5. Specify ? and ! as word characters by @andyw8 in zed-extensions/ruby#58
6. Add some basic tasks by @andyw8 in zed-extensions/ruby#57
7. Add constants to outline view by @vitallium in zed-extensions/ruby#60
8. Capture constants with assignments only for outline panel by @vitallium in zed-extensions/ruby#61
9. Include singleton class in outline by @joeldrapper in zed-extensions/ruby#67
10. Fix missing Tailwind completions by @I-Info in zed-extensions/ruby#62
11. Fix visibility keywords in outline by @joeldrapper in zed-extensions/ruby#65
12. Include method modifiers in outline by @joeldrapper in zed-extensions/ruby#66
13. Improve Ruby outlines with support for macros by @joeldrapper in zed-extensions/ruby#70
14. Mark instance & class variables as "@variable.special" by @asok in zed-extensions/ruby#63
15. Add task to evaluate selected text as Ruby by @andyw8 in zed-extensions/ruby#73
```

Thanks!
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.

Include constants in outline

3 participants