has_state_machine 1.3.1 → 1.3.2
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 +4 -4
- data/README.md +4 -3
- data/lib/has_state_machine/state.rb +5 -2
- data/lib/has_state_machine/version.rb +1 -1
- data/lib/ruby_lsp/has_state_machine/addon.rb +14 -52
- data/lib/ruby_lsp/has_state_machine/definition.rb +71 -127
- data/lib/ruby_lsp/has_state_machine/index_adapter.rb +118 -0
- data/lib/ruby_lsp/has_state_machine/indexing_enhancement.rb +116 -0
- data/lib/ruby_lsp/has_state_machine/namespace_registry.rb +74 -0
- data/lib/ruby_lsp/has_state_machine/workflow_resolver.rb +11 -6
- metadata +4 -2
- data/lib/ruby_lsp/has_state_machine/rails_server_addon.rb +0 -73
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 397b3442af2830ad23f8d12c0f2514906dd588a1577cdec10893813bb4734c29
|
|
4
|
+
data.tar.gz: f6604e68a56cab15157edba950e3aea0808815a30fad3fefb9148c07cc4f93a0
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 493f968d60c1d9c8bfc8a1f2fda23873c85ff458a5f2c424f30090d2aeda94fbf67909d479b40ab16c856a37a5f1d99df1e6783f9cce6214402fe8fb15c07ca2
|
|
7
|
+
data.tar.gz: 07de400274920577a31aa7bb358ffa13bfc38437469d287d4ba97782c542e695fb2493ddeafc5c35ab789a80f0701ac0071d706035488a86de218b06498241c2
|
data/README.md
CHANGED
|
@@ -92,9 +92,10 @@ module Workflow
|
|
|
92
92
|
after_transition do
|
|
93
93
|
Rails.logger.info "== Post has been archived ==\n"
|
|
94
94
|
|
|
95
|
-
#
|
|
96
|
-
#
|
|
97
|
-
|
|
95
|
+
# `state` and `previous_state` return ActiveSupport::StringInquirer
|
|
96
|
+
# (or nil for `previous_state` when absent), so both string comparisons
|
|
97
|
+
# and predicate inquiries like `previous_state&.published?` work.
|
|
98
|
+
Rails.logger.info "== Transitioned from #{previous_state} ==\n" if previous_state&.published?
|
|
98
99
|
end
|
|
99
100
|
|
|
100
101
|
# after_transition_commit runs only once the transition has been
|
|
@@ -152,7 +152,10 @@ module HasStateMachine
|
|
|
152
152
|
|
|
153
153
|
# Available in after_transition and after_transition_commit callbacks.
|
|
154
154
|
def previous_state
|
|
155
|
-
@previous_state
|
|
155
|
+
value = @previous_state || object.previous_changes[state_attribute]&.first
|
|
156
|
+
return if value.nil?
|
|
157
|
+
|
|
158
|
+
value.to_s.inquiry
|
|
156
159
|
end
|
|
157
160
|
|
|
158
161
|
def state_instance(desired_state, transient_values)
|
|
@@ -184,7 +187,7 @@ module HasStateMachine
|
|
|
184
187
|
end
|
|
185
188
|
|
|
186
189
|
def state
|
|
187
|
-
to_s.demodulize.underscore
|
|
190
|
+
to_s.demodulize.underscore.inquiry
|
|
188
191
|
end
|
|
189
192
|
|
|
190
193
|
def transactional?
|
|
@@ -2,35 +2,25 @@
|
|
|
2
2
|
|
|
3
3
|
require "ruby_lsp/addon"
|
|
4
4
|
|
|
5
|
+
# Ruby LSP 0.27 replaces RubyIndexer with Rubydex; revisit once Rubydex DSL indexing lands.
|
|
6
|
+
::RubyLsp::Addon.depend_on_ruby_lsp!(">= 0.23.0", "< 0.27.0") if ::RubyLsp::Addon.respond_to?(:depend_on_ruby_lsp!)
|
|
7
|
+
|
|
5
8
|
require "has_state_machine/version"
|
|
6
9
|
require_relative "definition"
|
|
7
|
-
|
|
8
|
-
::RubyLsp::Addon.depend_on_ruby_lsp!(">= 0.18.0", "< 1.0") if ::RubyLsp::Addon.respond_to?(:depend_on_ruby_lsp!)
|
|
10
|
+
require_relative "indexing_enhancement"
|
|
9
11
|
|
|
10
12
|
module RubyLsp
|
|
11
13
|
module HasStateMachine
|
|
12
14
|
class Addon < ::RubyLsp::Addon
|
|
13
15
|
def activate(global_state, outgoing_queue)
|
|
14
16
|
@global_state = global_state
|
|
15
|
-
@model_name_cache = {}
|
|
16
|
-
@rails_client = register_rails_server_addon(outgoing_queue)
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
"registered"
|
|
20
|
-
else
|
|
21
|
-
"unavailable, using naming convention only"
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
log(
|
|
25
|
-
outgoing_queue,
|
|
26
|
-
"Activating Has State Machine Ruby LSP add-on v#{version} (Rails integration: #{rails_status})"
|
|
27
|
-
)
|
|
18
|
+
log(outgoing_queue, "Activating Has State Machine Ruby LSP add-on v#{version}")
|
|
28
19
|
end
|
|
29
20
|
|
|
30
21
|
def deactivate
|
|
31
22
|
@global_state = nil
|
|
32
|
-
|
|
33
|
-
@model_name_cache = nil
|
|
23
|
+
NamespaceRegistry.clear
|
|
34
24
|
end
|
|
35
25
|
|
|
36
26
|
def name
|
|
@@ -47,51 +37,23 @@ module RubyLsp
|
|
|
47
37
|
uri,
|
|
48
38
|
node_context,
|
|
49
39
|
dispatcher,
|
|
50
|
-
index: @global_state&.index
|
|
51
|
-
rails_client: @rails_client,
|
|
52
|
-
model_name_cache: @model_name_cache
|
|
40
|
+
index: @global_state&.index
|
|
53
41
|
)
|
|
54
42
|
end
|
|
55
43
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
def
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
end
|
|
63
|
-
|
|
64
|
-
def register_rails_runner_client
|
|
65
|
-
rails_addon = ::RubyLsp::Addon.get("Ruby LSP Rails", ">= 0") if ::RubyLsp::Addon.respond_to?(:get)
|
|
66
|
-
return unless rails_addon&.respond_to?(:rails_runner_client)
|
|
67
|
-
|
|
68
|
-
client = rails_addon.rails_runner_client
|
|
69
|
-
return unless client.respond_to?(:register_server_addon)
|
|
70
|
-
return if client.respond_to?(:connected?) && !client.connected?
|
|
71
|
-
|
|
72
|
-
client.register_server_addon(File.expand_path("rails_server_addon.rb", __dir__))
|
|
73
|
-
client
|
|
74
|
-
end
|
|
75
|
-
|
|
76
|
-
def handle_rails_registration_error(outgoing_queue, error)
|
|
77
|
-
unless addon_not_found?(error)
|
|
78
|
-
log(outgoing_queue, "Has State Machine Ruby LSP Rails integration unavailable: #{error.message}")
|
|
44
|
+
# Ruby LSP re-indexes CHANGED files before invoking file-watcher add-ons,
|
|
45
|
+
# so only DELETED files need explicit eviction here.
|
|
46
|
+
def workspace_did_change_watched_files(changes)
|
|
47
|
+
deleted = ::RubyLsp::Constant::FileChangeType::DELETED
|
|
48
|
+
changes.each do |change|
|
|
49
|
+
NamespaceRegistry.instance.clear_uri(change[:uri]) if change[:type] == deleted
|
|
79
50
|
end
|
|
80
|
-
|
|
81
|
-
nil
|
|
82
51
|
end
|
|
83
52
|
|
|
84
|
-
|
|
85
|
-
if defined?(::RubyLsp::Addon::AddonNotFoundError)
|
|
86
|
-
return true if error.is_a?(::RubyLsp::Addon::AddonNotFoundError)
|
|
87
|
-
end
|
|
88
|
-
|
|
89
|
-
error.class.name&.end_with?("AddonNotFoundError")
|
|
90
|
-
end
|
|
53
|
+
private
|
|
91
54
|
|
|
92
55
|
def log(outgoing_queue, message)
|
|
93
56
|
return if outgoing_queue.nil? || outgoing_queue.closed?
|
|
94
|
-
return unless defined?(::RubyLsp::Notification)
|
|
95
57
|
|
|
96
58
|
outgoing_queue << ::RubyLsp::Notification.window_log_message(message)
|
|
97
59
|
end
|
|
@@ -1,23 +1,24 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require_relative "index_adapter"
|
|
4
|
+
require_relative "namespace_registry"
|
|
3
5
|
require_relative "workflow_resolver"
|
|
4
6
|
|
|
5
7
|
module RubyLsp
|
|
6
8
|
module HasStateMachine
|
|
7
9
|
class Definition
|
|
8
|
-
|
|
10
|
+
STATE_SUPERCLASS = "HasStateMachine::State"
|
|
9
11
|
|
|
10
|
-
#
|
|
12
|
+
# ActiveRecord methods that return self, so `object.reload.foo` targets the model
|
|
11
13
|
# just like `object.foo`.
|
|
12
|
-
SELF_RETURNING_METHODS = [
|
|
14
|
+
SELF_RETURNING_METHODS = %w[reload lock!].freeze
|
|
13
15
|
|
|
14
|
-
def initialize(response_builder, _uri, node_context, dispatcher, index: nil,
|
|
15
|
-
|
|
16
|
+
def initialize(response_builder, _uri, node_context, dispatcher, index: nil,
|
|
17
|
+
namespace_registry: NamespaceRegistry.instance)
|
|
16
18
|
@response_builder = response_builder
|
|
17
19
|
@node_context = node_context
|
|
18
|
-
@index = index
|
|
19
|
-
@
|
|
20
|
-
@model_name_cache = model_name_cache
|
|
20
|
+
@index = IndexAdapter.new(index)
|
|
21
|
+
@namespace_registry = namespace_registry
|
|
21
22
|
|
|
22
23
|
dispatcher.register(self, :on_call_node_enter)
|
|
23
24
|
end
|
|
@@ -25,170 +26,104 @@ module RubyLsp
|
|
|
25
26
|
def on_call_node_enter(node)
|
|
26
27
|
return unless current_target?(node)
|
|
27
28
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
29
|
+
is_object_call = object_call?(node)
|
|
30
|
+
is_object_method = !is_object_call && object_method_call?(node)
|
|
31
|
+
return unless is_object_call || is_object_method
|
|
32
|
+
return unless workflow_context?
|
|
32
33
|
|
|
33
|
-
|
|
34
|
-
elsif object_method_call?(node)
|
|
35
|
-
return unless resolved_model_name
|
|
34
|
+
return if model_entries.empty?
|
|
36
35
|
|
|
37
|
-
|
|
38
|
-
entries = method_entries(resolved_model_name, method_name)
|
|
39
|
-
entries = association_entries(resolved_model_name, method_name) if entries.empty?
|
|
40
|
-
entries = constant_entries(resolved_model_name) if entries.empty?
|
|
41
|
-
|
|
42
|
-
push_entries(entries)
|
|
43
|
-
end
|
|
36
|
+
replace_response_entries(is_object_call ? model_entries : resolve_object_method_entries(node))
|
|
44
37
|
end
|
|
45
38
|
|
|
46
39
|
private
|
|
47
40
|
|
|
48
|
-
attr_reader :index, :
|
|
41
|
+
attr_reader :index, :namespace_registry, :node_context, :response_builder
|
|
49
42
|
|
|
50
43
|
def current_target?(node)
|
|
51
|
-
|
|
52
|
-
call_node = node_context.call_node if node_context.respond_to?(:call_node)
|
|
53
|
-
|
|
54
|
-
node.equal?(target) || node.equal?(call_node)
|
|
55
|
-
end
|
|
56
|
-
|
|
57
|
-
def resolved_model_name
|
|
58
|
-
return @resolved_model_name if defined?(@resolved_model_name)
|
|
59
|
-
return @resolved_model_name = nil unless current_class_name
|
|
60
|
-
|
|
61
|
-
@resolved_model_name = model_name_from_rails(workflow_namespace) || convention_model_name
|
|
62
|
-
end
|
|
63
|
-
|
|
64
|
-
def convention_model_name
|
|
65
|
-
return @convention_model_name if defined?(@convention_model_name)
|
|
66
|
-
|
|
67
|
-
@convention_model_name = current_class_name && WorkflowResolver.model_name_for(current_class_name)
|
|
44
|
+
node.equal?(node_context.node) || node.equal?(node_context.call_node)
|
|
68
45
|
end
|
|
69
46
|
|
|
70
|
-
def
|
|
71
|
-
return
|
|
72
|
-
|
|
73
|
-
@workflow_namespace = current_class_name && WorkflowResolver.workflow_namespace_for(current_class_name)
|
|
74
|
-
end
|
|
75
|
-
|
|
76
|
-
def current_class_name
|
|
77
|
-
return @current_class_name if defined?(@current_class_name)
|
|
78
|
-
return @current_class_name = nil unless node_context.respond_to?(:nesting)
|
|
79
|
-
|
|
80
|
-
@current_class_name = class_name_from_nesting(node_context.nesting)
|
|
81
|
-
end
|
|
82
|
-
|
|
83
|
-
def class_name_from_nesting(nesting)
|
|
84
|
-
parts = nesting.map(&:to_s).reject(&:empty?)
|
|
85
|
-
return if parts.empty?
|
|
47
|
+
def workflow_context?
|
|
48
|
+
return false unless current_class_name
|
|
86
49
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
50
|
+
unless index.ready_for_ancestry?(STATE_SUPERCLASS) && index.indexed?(current_class_name)
|
|
51
|
+
return workflow_namespace_known?
|
|
52
|
+
end
|
|
90
53
|
|
|
91
|
-
|
|
92
|
-
return unless workflow_namespace && rails_client_available?
|
|
54
|
+
return true if index.subclass_of?(current_class_name, STATE_SUPERCLASS)
|
|
93
55
|
|
|
94
|
-
#
|
|
95
|
-
#
|
|
96
|
-
|
|
97
|
-
cache = model_name_cache.to_h
|
|
98
|
-
cache.fetch(workflow_namespace) do
|
|
99
|
-
cache[workflow_namespace] = request_model_name(workflow_namespace)
|
|
100
|
-
end
|
|
101
|
-
rescue
|
|
102
|
-
nil
|
|
56
|
+
# Allow model-scoped workflow concern modules (e.g. Workflow::Video::BroadcastsStatus),
|
|
57
|
+
# while rejecting non-state classes (e.g. serializers) that happen to call `object`.
|
|
58
|
+
index.module_only?(current_class_name) && workflow_namespace_known?
|
|
103
59
|
end
|
|
104
60
|
|
|
105
|
-
def
|
|
106
|
-
|
|
107
|
-
server_addon_name: SERVER_ADDON_NAME,
|
|
108
|
-
request_name: "model_for_workflow_namespace",
|
|
109
|
-
workflow_namespace: workflow_namespace
|
|
110
|
-
)
|
|
61
|
+
def workflow_namespace_known?
|
|
62
|
+
return false unless workflow_namespace
|
|
111
63
|
|
|
112
|
-
|
|
64
|
+
namespace_registry.model_for(workflow_namespace) ||
|
|
65
|
+
workflow_namespace.start_with?(WorkflowResolver::WORKFLOW_PREFIX)
|
|
113
66
|
end
|
|
114
67
|
|
|
115
|
-
def
|
|
116
|
-
|
|
117
|
-
return [] unless association_model_name
|
|
68
|
+
def model_name
|
|
69
|
+
return @model_name if defined?(@model_name)
|
|
118
70
|
|
|
119
|
-
|
|
71
|
+
@model_name = namespace_registry.model_for(workflow_namespace) || convention_model_name
|
|
120
72
|
end
|
|
121
73
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
# convention fallback would never run.
|
|
125
|
-
def rails_client_available?
|
|
126
|
-
return false unless rails_client
|
|
127
|
-
|
|
128
|
-
!rails_client.respond_to?(:connected?) || rails_client.connected?
|
|
74
|
+
def model_entries
|
|
75
|
+
@model_entries ||= index.class_entries(model_name)
|
|
129
76
|
end
|
|
130
77
|
|
|
131
|
-
def
|
|
132
|
-
return
|
|
78
|
+
def convention_model_name
|
|
79
|
+
return @convention_model_name if defined?(@convention_model_name)
|
|
133
80
|
|
|
134
|
-
|
|
135
|
-
response_name(result)
|
|
136
|
-
rescue
|
|
137
|
-
nil
|
|
81
|
+
@convention_model_name = current_class_name && WorkflowResolver.model_name_for(current_class_name)
|
|
138
82
|
end
|
|
139
83
|
|
|
140
|
-
def
|
|
141
|
-
return
|
|
84
|
+
def workflow_namespace
|
|
85
|
+
return @workflow_namespace if defined?(@workflow_namespace)
|
|
142
86
|
|
|
143
|
-
|
|
87
|
+
@workflow_namespace = current_class_name && WorkflowResolver.workflow_namespace_for(current_class_name)
|
|
144
88
|
end
|
|
145
89
|
|
|
146
|
-
def
|
|
147
|
-
return
|
|
90
|
+
def current_class_name
|
|
91
|
+
return @current_class_name if defined?(@current_class_name)
|
|
148
92
|
|
|
149
|
-
|
|
93
|
+
@current_class_name = WorkflowResolver.class_name_from_nesting(node_context.nesting)
|
|
150
94
|
end
|
|
151
95
|
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
entries
|
|
157
|
-
return Array(entries)
|
|
96
|
+
# Setters resolve by `name` (`title=`), falling back to the reader (`title`) when only that is indexed.
|
|
97
|
+
def resolve_object_method_entries(node)
|
|
98
|
+
[call_name(node), message(node)].uniq.each do |method_name|
|
|
99
|
+
entries = index.method_entries(model_name, method_name)
|
|
100
|
+
return entries unless entries.empty?
|
|
158
101
|
end
|
|
159
102
|
|
|
160
|
-
|
|
103
|
+
model_entries
|
|
161
104
|
end
|
|
162
105
|
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
106
|
+
# Ruby LSP's own definition listener runs first and pushes State#object and same-name
|
|
107
|
+
# guesses. Replacing them keeps the picker to the model and avoids mixing Location with
|
|
108
|
+
# LocationLink, which strict clients fail to deserialize.
|
|
109
|
+
def replace_response_entries(entries)
|
|
110
|
+
response_builder.response.clear if response_builder.respond_to?(:response)
|
|
167
111
|
|
|
168
|
-
def push_entries(entries)
|
|
169
112
|
entries.each do |entry|
|
|
170
113
|
response_builder << location_for(entry)
|
|
171
114
|
end
|
|
172
115
|
end
|
|
173
116
|
|
|
174
117
|
def location_for(entry)
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
uri: entry.uri.to_s,
|
|
182
|
-
range: range_for(location)
|
|
118
|
+
# Only namespace and method entries carry a name_location.
|
|
119
|
+
selection = (entry.name_location if entry.respond_to?(:name_location)) || entry.location
|
|
120
|
+
::RubyLsp::Interface::LocationLink.new(
|
|
121
|
+
target_uri: entry.uri.to_s,
|
|
122
|
+
target_range: range_for(entry.location),
|
|
123
|
+
target_selection_range: range_for(selection)
|
|
183
124
|
)
|
|
184
125
|
end
|
|
185
126
|
|
|
186
|
-
def entry_location(entry)
|
|
187
|
-
return entry.location if entry.respond_to?(:location)
|
|
188
|
-
|
|
189
|
-
entry.name_location if entry.respond_to?(:name_location)
|
|
190
|
-
end
|
|
191
|
-
|
|
192
127
|
def range_for(location)
|
|
193
128
|
::RubyLsp::Interface::Range.new(
|
|
194
129
|
start: ::RubyLsp::Interface::Position.new(
|
|
@@ -214,13 +149,22 @@ module RubyLsp
|
|
|
214
149
|
end
|
|
215
150
|
|
|
216
151
|
def object_call?(node)
|
|
217
|
-
node &&
|
|
152
|
+
node && self_receiver?(node) && message(node) == "object"
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
def self_receiver?(node)
|
|
156
|
+
recv = receiver(node)
|
|
157
|
+
recv.nil? || recv.is_a?(::Prism::SelfNode)
|
|
218
158
|
end
|
|
219
159
|
|
|
220
160
|
def receiver(node)
|
|
221
161
|
node.receiver if node.respond_to?(:receiver)
|
|
222
162
|
end
|
|
223
163
|
|
|
164
|
+
def call_name(node)
|
|
165
|
+
node.name&.to_s || message(node)
|
|
166
|
+
end
|
|
167
|
+
|
|
224
168
|
def message(node)
|
|
225
169
|
node.message.to_s if node.respond_to?(:message) && node.message
|
|
226
170
|
end
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RubyLsp
|
|
4
|
+
module HasStateMachine
|
|
5
|
+
# Isolates RubyIndexer::Index queries so a future Rubydex backend is a localized swap.
|
|
6
|
+
class IndexAdapter
|
|
7
|
+
def initialize(index)
|
|
8
|
+
@index = index
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def ready_for_ancestry?(superclass_name)
|
|
12
|
+
return false if index.respond_to?(:initial_indexing_completed) && !index.initial_indexing_completed
|
|
13
|
+
|
|
14
|
+
indexed?(superclass_name)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def indexed?(name)
|
|
18
|
+
return false unless name && index&.respond_to?(:indexed?)
|
|
19
|
+
|
|
20
|
+
index.indexed?(name)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# Walks Entry::Class#parent_class directly instead of calling
|
|
24
|
+
# RubyIndexer::Index#linearized_ancestors_of: HasStateMachine::State inherits
|
|
25
|
+
# from String, and any gem monkeypatching String with a non-module constant
|
|
26
|
+
# mixin causes linearized_ancestors_of to raise and cache a truncated ancestor list.
|
|
27
|
+
def subclass_of?(class_name, target_ancestor)
|
|
28
|
+
return false unless class_name && index
|
|
29
|
+
|
|
30
|
+
current = class_name
|
|
31
|
+
seen = {}
|
|
32
|
+
|
|
33
|
+
while current && !seen[current]
|
|
34
|
+
return true if current == target_ancestor
|
|
35
|
+
|
|
36
|
+
seen[current] = true
|
|
37
|
+
entry = superclass_entry(current)
|
|
38
|
+
return false unless entry
|
|
39
|
+
|
|
40
|
+
parent = entry.parent_class
|
|
41
|
+
return false if parent.nil? || parent == "::Object"
|
|
42
|
+
|
|
43
|
+
current = resolve_parent_name(parent, entry.nesting)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
false
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def module_only?(name)
|
|
50
|
+
return false unless name && index
|
|
51
|
+
|
|
52
|
+
entries = resolve_entries(name)
|
|
53
|
+
entries.any? && entries.all?(::RubyIndexer::Entry::Module)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def class_entries(name)
|
|
57
|
+
return [] unless name && index
|
|
58
|
+
|
|
59
|
+
entries = resolve_entries(name)
|
|
60
|
+
entries.select { |entry| class_entry?(entry) }
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def method_entries(owner_name, method_name)
|
|
64
|
+
return [] unless index && owner_name && method_name
|
|
65
|
+
|
|
66
|
+
Array(index.resolve_method(method_name, owner_name))
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
private
|
|
70
|
+
|
|
71
|
+
attr_reader :index
|
|
72
|
+
|
|
73
|
+
def resolve_entries(name)
|
|
74
|
+
entries = Array(index[name])
|
|
75
|
+
entries.any?(::RubyIndexer::Entry::UnresolvedConstantAlias) ? Array(index.resolve(name, [])) : entries
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def superclass_entry(name)
|
|
79
|
+
entries = resolve_entries(name).grep(::RubyIndexer::Entry::Class)
|
|
80
|
+
entries.find { |entry| entry.parent_class && entry.parent_class != "::Object" } || entries.first
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def resolve_parent_name(parent_name, nesting)
|
|
84
|
+
entry = index.resolve(parent_name, nesting[0...-1])&.first
|
|
85
|
+
resolve_constant_name(entry.name) if entry
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def resolve_constant_name(name, seen = {})
|
|
89
|
+
return if seen[name]
|
|
90
|
+
|
|
91
|
+
entries = resolve_entries(name)
|
|
92
|
+
return if entries.empty?
|
|
93
|
+
|
|
94
|
+
entry = entries.first
|
|
95
|
+
if entry.is_a?(::RubyIndexer::Entry::ConstantAlias)
|
|
96
|
+
seen[name] = true
|
|
97
|
+
resolve_constant_name(entry.target, seen) || entry.target
|
|
98
|
+
else
|
|
99
|
+
entry.name
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def class_entry?(entry, seen = [])
|
|
104
|
+
case entry
|
|
105
|
+
when ::RubyIndexer::Entry::Class then true
|
|
106
|
+
when ::RubyIndexer::Entry::ConstantAlias
|
|
107
|
+
target = entry.target
|
|
108
|
+
return false if seen.include?(target)
|
|
109
|
+
|
|
110
|
+
seen << target
|
|
111
|
+
resolve_entries(target).any? { |target_entry| class_entry?(target_entry, seen) }
|
|
112
|
+
when ::RubyIndexer::Entry then false
|
|
113
|
+
else true # test doubles
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
end
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "namespace_registry"
|
|
4
|
+
|
|
5
|
+
module RubyLsp
|
|
6
|
+
module HasStateMachine
|
|
7
|
+
class IndexingEnhancement < ::RubyIndexer::Enhancement
|
|
8
|
+
def initialize(listener)
|
|
9
|
+
super
|
|
10
|
+
NamespaceRegistry.instance.clear_uri(listener.instance_variable_get(:@uri))
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def on_call_node_enter(call_node)
|
|
14
|
+
return unless call_node.name == :has_state_machine
|
|
15
|
+
return unless call_node.receiver.nil? || call_node.receiver.is_a?(::Prism::SelfNode)
|
|
16
|
+
|
|
17
|
+
owner = @listener.current_owner
|
|
18
|
+
return unless owner
|
|
19
|
+
|
|
20
|
+
options = extract_options(call_node)
|
|
21
|
+
state_attr, attr_location = extract_state_attribute(options, call_node)
|
|
22
|
+
|
|
23
|
+
add_reader(state_attr, attr_location)
|
|
24
|
+
register_workflow_namespace(owner, options["workflow_namespace"])
|
|
25
|
+
index_state_predicates(options, state_attr)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
private
|
|
29
|
+
|
|
30
|
+
def extract_options(call_node)
|
|
31
|
+
arguments = call_node.arguments&.arguments
|
|
32
|
+
return {} unless arguments
|
|
33
|
+
|
|
34
|
+
arguments
|
|
35
|
+
.select { |argument| argument.is_a?(::Prism::KeywordHashNode) || argument.is_a?(::Prism::HashNode) }
|
|
36
|
+
.flat_map(&:elements)
|
|
37
|
+
.grep(::Prism::AssocNode)
|
|
38
|
+
.filter_map { |assoc| (key = extract_string_or_symbol(assoc.key)) && [key, assoc.value] }
|
|
39
|
+
.to_h
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def extract_state_attribute(options, call_node)
|
|
43
|
+
node = options["state_attribute"] || options["attribute"]
|
|
44
|
+
name = extract_string_or_symbol(node)
|
|
45
|
+
return ["status", call_node.location] if name.to_s.empty?
|
|
46
|
+
|
|
47
|
+
[name, node.location]
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def register_workflow_namespace(owner, namespace_node)
|
|
51
|
+
return unless owner.is_a?(::RubyIndexer::Entry::Class)
|
|
52
|
+
|
|
53
|
+
namespace = extract_namespace(namespace_node)
|
|
54
|
+
return if namespace.to_s.strip.empty?
|
|
55
|
+
|
|
56
|
+
NamespaceRegistry.instance.register(
|
|
57
|
+
uri: owner.uri,
|
|
58
|
+
workflow_namespace: namespace,
|
|
59
|
+
model_name: owner.name
|
|
60
|
+
)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def index_state_predicates(options, state_attr)
|
|
64
|
+
elements = literal_states(options["states"])
|
|
65
|
+
return if elements.empty?
|
|
66
|
+
|
|
67
|
+
prefix = extract_affix(options["prefix"], state_attr)
|
|
68
|
+
suffix = extract_affix(options["suffix"], state_attr)
|
|
69
|
+
return if [prefix, suffix].include?(:dynamic)
|
|
70
|
+
|
|
71
|
+
elements.each do |element|
|
|
72
|
+
state_name = extract_string_or_symbol(element)
|
|
73
|
+
next if state_name.to_s.empty?
|
|
74
|
+
|
|
75
|
+
predicate = "#{[prefix, state_name, suffix].compact.join("_")}?"
|
|
76
|
+
add_reader(predicate, element.location)
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def literal_states(node)
|
|
81
|
+
node = node.receiver if node.is_a?(::Prism::CallNode) && node.name == :freeze
|
|
82
|
+
node.is_a?(::Prism::ArrayNode) ? node.elements : []
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def extract_affix(node, state_attr)
|
|
86
|
+
case node
|
|
87
|
+
when nil, ::Prism::FalseNode, ::Prism::NilNode then nil
|
|
88
|
+
when ::Prism::TrueNode then state_attr
|
|
89
|
+
else
|
|
90
|
+
value = extract_string_or_symbol(node)
|
|
91
|
+
value.to_s.empty? ? :dynamic : value
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def extract_namespace(node)
|
|
96
|
+
case node
|
|
97
|
+
when ::Prism::StringNode, ::Prism::SymbolNode
|
|
98
|
+
node.unescaped
|
|
99
|
+
when ::Prism::ConstantReadNode, ::Prism::ConstantPathNode
|
|
100
|
+
::RubyIndexer::Index.constant_name(node)
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def extract_string_or_symbol(node)
|
|
105
|
+
case node
|
|
106
|
+
when ::Prism::StringNode, ::Prism::SymbolNode
|
|
107
|
+
node.unescaped
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def add_reader(name, location)
|
|
112
|
+
@listener.add_method(name, location, [::RubyIndexer::Entry::Signature.new([])])
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
end
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RubyLsp
|
|
4
|
+
module HasStateMachine
|
|
5
|
+
class NamespaceRegistry
|
|
6
|
+
class << self
|
|
7
|
+
def instance
|
|
8
|
+
@instance ||= new
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def clear
|
|
12
|
+
instance.clear
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def initialize
|
|
17
|
+
@mutex = Mutex.new
|
|
18
|
+
@by_uri = {}
|
|
19
|
+
@by_namespace = {}
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def clear_uri(uri)
|
|
23
|
+
key = uri.to_s
|
|
24
|
+
@mutex.synchronize do
|
|
25
|
+
remove_uri_entries(key)
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def register(uri:, workflow_namespace:, model_name:)
|
|
30
|
+
uri_key = uri.to_s
|
|
31
|
+
namespace = workflow_namespace.to_s.delete_prefix("::")
|
|
32
|
+
model = model_name.to_s.delete_prefix("::")
|
|
33
|
+
return if namespace.empty? || model.empty?
|
|
34
|
+
|
|
35
|
+
@mutex.synchronize do
|
|
36
|
+
(@by_uri[uri_key] ||= []) << [namespace, model]
|
|
37
|
+
@by_namespace[namespace] = model
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def model_for(workflow_namespace)
|
|
42
|
+
namespace = workflow_namespace.to_s.delete_prefix("::")
|
|
43
|
+
return if namespace.empty?
|
|
44
|
+
|
|
45
|
+
@mutex.synchronize do
|
|
46
|
+
@by_namespace[namespace]
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def clear
|
|
51
|
+
@mutex.synchronize do
|
|
52
|
+
@by_uri.clear
|
|
53
|
+
@by_namespace.clear
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
private
|
|
58
|
+
|
|
59
|
+
def remove_uri_entries(uri_key)
|
|
60
|
+
removed = @by_uri.delete(uri_key)
|
|
61
|
+
return unless removed
|
|
62
|
+
|
|
63
|
+
removed.each do |namespace, model|
|
|
64
|
+
@by_namespace[namespace] = find_model_for_namespace(namespace) if @by_namespace[namespace] == model
|
|
65
|
+
end
|
|
66
|
+
@by_namespace.compact!
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def find_model_for_namespace(namespace)
|
|
70
|
+
@by_uri.values.flatten(1).assoc(namespace)&.last
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
@@ -12,16 +12,21 @@ module RubyLsp
|
|
|
12
12
|
return unless namespace&.start_with?(WORKFLOW_PREFIX)
|
|
13
13
|
|
|
14
14
|
name = namespace.delete_prefix(WORKFLOW_PREFIX)
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
name
|
|
15
|
+
name unless name.empty?
|
|
18
16
|
end
|
|
19
17
|
|
|
20
18
|
def workflow_namespace_for(workflow_class_name)
|
|
21
|
-
namespace = workflow_class_name.to_s.delete_prefix("::").
|
|
22
|
-
|
|
19
|
+
namespace = workflow_class_name.to_s.delete_prefix("::").rpartition("::").first
|
|
20
|
+
namespace unless namespace.empty?
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def class_name_from_nesting(nesting)
|
|
24
|
+
parts = Array(nesting).map(&:to_s).reject(&:empty?)
|
|
25
|
+
return if parts.empty? || parts.last.start_with?("<Class:")
|
|
23
26
|
|
|
24
|
-
|
|
27
|
+
# A `::`-anchored segment resets the lexical scope, so outer segments are dropped.
|
|
28
|
+
anchor = parts.rindex { |part| part.start_with?("::") } || 0
|
|
29
|
+
parts[anchor..].join("::").delete_prefix("::")
|
|
25
30
|
end
|
|
26
31
|
end
|
|
27
32
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: has_state_machine
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.3.
|
|
4
|
+
version: 1.3.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Benjamin Hargett
|
|
@@ -286,7 +286,9 @@ files:
|
|
|
286
286
|
- lib/has_state_machine/version.rb
|
|
287
287
|
- lib/ruby_lsp/has_state_machine/addon.rb
|
|
288
288
|
- lib/ruby_lsp/has_state_machine/definition.rb
|
|
289
|
-
- lib/ruby_lsp/has_state_machine/
|
|
289
|
+
- lib/ruby_lsp/has_state_machine/index_adapter.rb
|
|
290
|
+
- lib/ruby_lsp/has_state_machine/indexing_enhancement.rb
|
|
291
|
+
- lib/ruby_lsp/has_state_machine/namespace_registry.rb
|
|
290
292
|
- lib/ruby_lsp/has_state_machine/workflow_resolver.rb
|
|
291
293
|
homepage: https://www.github.com/encampment/has_state_machine
|
|
292
294
|
licenses:
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require "ruby_lsp/ruby_lsp_rails/server" unless defined?(::RubyLsp::Rails::ServerAddon)
|
|
4
|
-
|
|
5
|
-
module RubyLsp
|
|
6
|
-
module HasStateMachine
|
|
7
|
-
class RailsServerAddon < ::RubyLsp::Rails::ServerAddon
|
|
8
|
-
def name
|
|
9
|
-
"has_state_machine"
|
|
10
|
-
end
|
|
11
|
-
|
|
12
|
-
def execute(request, params)
|
|
13
|
-
with_request_error_handling(request) do
|
|
14
|
-
case request
|
|
15
|
-
when "model_for_workflow_namespace"
|
|
16
|
-
send_result(model_for_workflow_namespace(params[:workflow_namespace] || params.fetch("workflow_namespace")))
|
|
17
|
-
else
|
|
18
|
-
raise NotImplementedError, "Unknown request: #{request}"
|
|
19
|
-
end
|
|
20
|
-
end
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
private
|
|
24
|
-
|
|
25
|
-
def model_for_workflow_namespace(workflow_namespace)
|
|
26
|
-
model = conventional_model_for(workflow_namespace) || models_by_workflow_namespace[workflow_namespace]
|
|
27
|
-
return unless model
|
|
28
|
-
|
|
29
|
-
{name: model.name}
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
# Resolve conventional namespaces without eager-loading the application.
|
|
33
|
-
def conventional_model_for(workflow_namespace)
|
|
34
|
-
workflow_namespace = workflow_namespace.to_s
|
|
35
|
-
return unless workflow_namespace.start_with?("Workflow::")
|
|
36
|
-
|
|
37
|
-
model = workflow_namespace.delete_prefix("Workflow::").safe_constantize
|
|
38
|
-
model if model.try(:workflow_namespace) == workflow_namespace
|
|
39
|
-
end
|
|
40
|
-
|
|
41
|
-
def models_by_workflow_namespace
|
|
42
|
-
@models_by_workflow_namespace ||= active_record_models.each_with_object({}) do |model, index|
|
|
43
|
-
workflow_namespaces_for(model).each { |namespace| index[namespace] = model }
|
|
44
|
-
end
|
|
45
|
-
end
|
|
46
|
-
|
|
47
|
-
# Keep inherited namespaces mapped to the model that declared them.
|
|
48
|
-
def workflow_namespaces_for(model)
|
|
49
|
-
return [] if model.name.nil?
|
|
50
|
-
|
|
51
|
-
definitions = model.try(:state_machine_definitions)
|
|
52
|
-
return Array(model.try(:workflow_namespace)) unless definitions
|
|
53
|
-
|
|
54
|
-
definitions.each_value.filter_map do |machine|
|
|
55
|
-
namespace = machine.workflow_namespace_for(model).to_s
|
|
56
|
-
namespace unless inherited_namespace?(model.superclass, machine, namespace)
|
|
57
|
-
end
|
|
58
|
-
end
|
|
59
|
-
|
|
60
|
-
def inherited_namespace?(parent, machine, namespace)
|
|
61
|
-
parent.try(:state_machine_definitions)&.value?(machine) &&
|
|
62
|
-
machine.workflow_namespace_for(parent).to_s == namespace
|
|
63
|
-
end
|
|
64
|
-
|
|
65
|
-
def active_record_models
|
|
66
|
-
@active_record_models ||= begin
|
|
67
|
-
::Rails.application&.eager_load!
|
|
68
|
-
::ActiveRecord::Base.descendants.reject(&:abstract_class?)
|
|
69
|
-
end
|
|
70
|
-
end
|
|
71
|
-
end
|
|
72
|
-
end
|
|
73
|
-
end
|