ruby_language_server 0.4.2 → 0.9.9

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.
@@ -17,6 +17,9 @@ module RubyLanguageServer
17
17
  scope :method_scopes, -> { where(class_type: TYPE_METHOD) }
18
18
  scope :for_line, ->(line) { where('top_line <= ? AND bottom_line >= ?', line, line).or(where(parent_id: nil)) }
19
19
  scope :by_path_length, -> { order(Arel.sql('length(path) DESC')) }
20
+
21
+ # The separators between a scope's path and its children's (see JoinHash).
22
+ DESCENDANT_SEPARATORS = JoinHash.values_at(TYPE_MODULE, TYPE_METHOD, TYPE_BLOCK).uniq.freeze
20
23
  # attr_accessor :top_line # first line
21
24
  # attr_accessor :bottom_line # last line
22
25
  # attr_accessor :parent # parent scope
@@ -24,12 +27,13 @@ module RubyLanguageServer
24
27
  # attr_accessor :name # method
25
28
  # attr_accessor :superclass_name # superclass name
26
29
 
27
- def self.build(parent = nil, type = TYPE_ROOT, name = '', top_line = 1, column = 1)
30
+ def self.build(parent = nil, type = TYPE_ROOT, name = '', top_line = 1, column = 1, bottom_line = nil) # rubocop:disable Metrics/ParameterLists
28
31
  full_name = [parent&.full_name, name].compact.join(JoinHash[type])
29
32
  create!(
30
33
  parent:,
31
34
  top_line:,
32
35
  column:,
36
+ bottom_line:,
33
37
  name:,
34
38
  path: full_name,
35
39
  class_type: type
@@ -46,26 +50,64 @@ module RubyLanguageServer
46
50
  scope_parts.count
47
51
  end
48
52
 
49
- # Self and all descendents flattened into array
53
+ # Self and all descendents, in the same file.
50
54
  def self_and_descendants
51
- return Scope.all if root_scope?
52
-
53
- Scope.where('path like ?', "#{path}%")
55
+ Scope.where(id:).or(descendants)
54
56
  end
55
57
 
58
+ # Every scope nested inside this one, in the same file. A descendant's path is this
59
+ # path followed by a separator (::, #, >), so Foo's descendants include Foo::Bar and
60
+ # Foo#baz but not FooBar. % and _ in the path match literally.
56
61
  def descendants
57
- Scope.where('path like ?', "#{path}_%")
62
+ same_file = Scope.where(code_file_id:)
63
+ return same_file.where.not(id:) if root_scope?
64
+
65
+ prefix = self.class.sanitize_sql_like(path)
66
+ patterns = DESCENDANT_SEPARATORS.map { |separator| "#{prefix}#{self.class.sanitize_sql_like(separator)}%" }
67
+ same_file.where((["path LIKE ? ESCAPE '\\'"] * patterns.length).join(' OR '), *patterns)
58
68
  end
59
69
 
60
70
  def set_superclass_name(partial)
61
71
  if partial.start_with?('::')
62
72
  self.superclass_name = partial.gsub(/^::/, '')
63
73
  else
64
- self.superclass_name = [parent ? parent.full_name : nil, partial].compact.join(JoinHash[class_type])
74
+ self.superclass_name = [parent&.full_name, partial].compact.join(JoinHash[class_type])
65
75
  end
66
76
  save!
67
77
  end
68
78
 
79
+ # The scopes of this class's superclass, one per file that defines it, or [].
80
+ # superclass_name was qualified with the enclosing namespace when parsed
81
+ # (Foo::Bar for `class Nar < Bar` inside Foo), so if that path doesn't exist, drop
82
+ # leading namespaces until one does, the way Ruby falls back to outer constants.
83
+ def superclass_scopes
84
+ return [] if superclass_name.blank?
85
+
86
+ parts = superclass_name.split('::')
87
+ parts.each_index do |index|
88
+ scopes = Scope.where(path: parts[index..].join('::'), class_type: TYPE_CLASS).where.not(id:).to_a
89
+ return scopes if scopes.any?
90
+ end
91
+ []
92
+ end
93
+
94
+ # Superclass scopes, nearest first, stopping at a cycle or after max_depth classes.
95
+ def ancestor_class_scopes(max_depth: 20)
96
+ ancestors = []
97
+ frontier = [self]
98
+ max_depth.times do
99
+ frontier = frontier.flat_map(&:superclass_scopes).uniq - ancestors - [self]
100
+ break if frontier.empty?
101
+
102
+ ancestors.concat(frontier)
103
+ end
104
+ ancestors
105
+ end
106
+
107
+ def class_scope?
108
+ class_type == TYPE_CLASS
109
+ end
110
+
69
111
  def root_scope?
70
112
  class_type == TYPE_ROOT
71
113
  end
@@ -79,14 +121,23 @@ module RubyLanguageServer
79
121
  [TYPE_MODULE, TYPE_CLASS, TYPE_METHOD, TYPE_VARIABLE].include?(class_type)
80
122
  end
81
123
 
82
- # Called from ScopeParser when a peer of this block starts - because we don't have an end notifier.
83
- # So we do some reasonable cleanup here.
84
- def close(line)
85
- return destroy! if block_scope? && variables.none?
124
+ # Get parameters as an array of hashes
125
+ def parsed_parameters
126
+ return [] unless parameters.present?
86
127
 
87
- self.top_line ||= variables.map(&:top_line).min
88
- self.bottom_line = [bottom_line, line].compact.min
89
- save!
128
+ JSON.parse(parameters)
129
+ rescue JSON::ParserError
130
+ []
131
+ end
132
+
133
+ # Set parameters from an array
134
+ def set_parameters(params_array)
135
+ self.parameters = params_array.to_json if params_array.present?
136
+ end
137
+
138
+ # Called from ScopeParser to cleanup empty blocks.
139
+ def close
140
+ destroy! if block_scope? && variables.none?
90
141
  end
91
142
 
92
143
  private