ruby-lsp-view_component 0.2.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9589a9b55e32d2f09a38148a16195ac80f4ba55332581c19f58aba78fc373e8b
4
- data.tar.gz: '0209cd47b52990d2fd12f1fc952d27f92db8df310df861f8b360de00f057ef17'
3
+ metadata.gz: 311ae0434d3d08918eab99a1817e1d2448d737e5088bb627c5f58e0bb1347c9c
4
+ data.tar.gz: 11b21df73cdf7d6e5a3c515bd5849cf85eb79982e064dc13d999cf8f5b18fd9d
5
5
  SHA512:
6
- metadata.gz: 8882a227aca9f8781ec3cfc05d63eb56bbfa8b0b3223c5ed15a4e79909fc49209b29be9aa02ab08dc5bfc75069f89cd253613078179d32a873d9168dacda7851
7
- data.tar.gz: d45745ee5df206124ae85ae32f534f5c9d597fd4324cc196402304d9c209e2887905b43a4bc751792c25c69a350175c32877bb47f018aebf1a68dae718a0f75a
6
+ metadata.gz: 33462a033067359f7f4ca3f27b0c29126c680a9fedb0ee068882cf10b4996a4f742fa58f1007fd0217bed0d65f9206be1224375fb231a5810dddcc20addb716d
7
+ data.tar.gz: c64141716d8f327e77e3c2989ad895a860c031c464358d8e1499a7c04611ae4312fb090125c68aad2308d24b2bd8c5fb7db32e44438de53b460749f27b4451d7
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.3.0]
4
+
5
+ - `Jump to template` opens the inherited templates of a component without its own: those of the nearest ancestor that has them, unless a class in between defines `call`.
6
+
3
7
  ## [0.2.1]
4
8
 
5
9
  - Drop the gemspec dependency on `ruby-lsp`. The add-on still declares `~> 0.26.0` with `depend_on_ruby_lsp!`, so a newer Ruby LSP no longer blocks `bundle update`: the add-on skips activation with a warning until it supports that version.
data/README.md CHANGED
@@ -25,9 +25,13 @@ For `app/components/foo_component.rb` with all of these templates, the lens list
25
25
 
26
26
  ERB, Haml and Slim templates are supported. Templates of nested components in the subdirectory, such as `foo_component/item_component.html.erb` next to `foo_component/item_component.rb`, belong to that component and are left out.
27
27
 
28
+ ### Inherited template
29
+
30
+ A component without templates of its own [inherits](https://viewcomponent.org/guide/templates.html#inherited) them from its parent class. The lens opens the templates of the nearest ancestor that has them, unless a class in between defines `call`.
31
+
28
32
  ### Missing template
29
33
 
30
- When a component needs a template and has none, the lens opens `foo_component.html.erb` next to the class. VS Code reports that the file does not exist and offers a **Create File** button. Once the file exists, the same lens opens it.
34
+ When a component needs a template and has none, not even an inherited one, the lens opens `foo_component.html.erb` next to the class. VS Code reports that the file does not exist and offers a **Create File** button. Once the file exists, the same lens opens it.
31
35
 
32
36
  ![Create a missing template](.github/assets/create_template.gif)
33
37
 
@@ -35,7 +39,6 @@ A component counts as needing a template only when it renders through a template
35
39
 
36
40
  - define [`call`](https://viewcomponent.org/guide/templates.html#call), themselves or in a parent class
37
41
  - declare an [inline template](https://viewcomponent.org/guide/templates.html#inline) with `erb_template`, `haml_template` or `slim_template`
38
- - [inherit](https://viewcomponent.org/guide/templates.html#inherited) a template from a parent class
39
42
  - are marked `abstract!`
40
43
 
41
44
  A base class that is never rendered on its own still gets the lens. Mark it `abstract!` to hide it.
@@ -41,7 +41,7 @@ module RubyLsp
41
41
 
42
42
  path = @path #: as !nil
43
43
  paths = templates(path)
44
- paths = ["#{path.delete_suffix(".rb")}.html.erb"] if paths.empty? && needs_template?(node)
44
+ paths = fallback_templates(node, path) if paths.empty?
45
45
  return if paths.empty?
46
46
 
47
47
  @response_builder << create_code_lens(
@@ -55,25 +55,35 @@ module RubyLsp
55
55
 
56
56
  private
57
57
 
58
- #: (Prism::ClassNode) -> bool
59
- def needs_template?(node)
58
+ #: (Prism::ClassNode, String) -> Array[String]
59
+ def fallback_templates(node, path)
60
+ return [] if declares_no_template?(node)
61
+
60
62
  name = [*@nesting, node.constant_path.slice].join("::").delete_prefix("::")
61
63
  ancestors = @index.linearized_ancestors_of(name)
62
64
  base = ancestors.index("ViewComponent::Base")
63
- return false unless base
64
- return false if @index.resolve_method("call", name)
65
- return false if (ancestors.take(base) - [name]).any? { |ancestor| inherited_template?(ancestor) }
65
+ return [] unless base
66
66
 
67
- Array(node.body&.child_nodes).none? do |child|
68
- child.is_a?(Prism::CallNode) && child.receiver.nil? && NO_TEMPLATE_CALLS.include?(child.name)
67
+ call_owner = @index.resolve_method("call", name)&.first&.owner&.name
68
+ ancestors.take(base).each do |ancestor|
69
+ return [] if ancestor == call_owner
70
+
71
+ files = (@index[ancestor] || []).filter_map(&:file_path).uniq
72
+ inherited = files.flat_map { |file| templates(file) }
73
+ return inherited if inherited.any?
69
74
  end
75
+ return [] if call_owner
76
+
77
+ ["#{path.delete_suffix(".rb")}.html.erb"]
70
78
  rescue RubyIndexer::Index::NonExistingNamespaceError
71
- false
79
+ []
72
80
  end
73
81
 
74
- #: (String) -> bool
75
- def inherited_template?(ancestor)
76
- @index[ancestor]&.any? { |entry| entry.file_path && templates(entry.file_path).any? } || false
82
+ #: (Prism::ClassNode) -> bool
83
+ def declares_no_template?(node)
84
+ Array(node.body&.child_nodes).any? do |child|
85
+ child.is_a?(Prism::CallNode) && child.receiver.nil? && NO_TEMPLATE_CALLS.include?(child.name)
86
+ end
77
87
  end
78
88
 
79
89
  #: (String) -> Array[String]
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RubyLsp
4
4
  module RubyLspViewComponent
5
- VERSION = "0.2.1"
5
+ VERSION = "0.3.0"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-lsp-view_component
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Retamal