Skip to content

Commit c512044

Browse files
committed
Rewrite LSP structs as a bare Ruby classes
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
1 parent 081ddea commit c512044

File tree

2 files changed

+127
-35
lines changed

2 files changed

+127
-35
lines changed

lib/spoom/sorbet/lsp/base.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
module Spoom
55
module LSP
66
# Base messaging
7-
# We don't use T::Struct for those so we can subclass them
87

98
# A general message as defined by JSON-RPC.
109
#

lib/spoom/sorbet/lsp/structures.rb

Lines changed: 127 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,20 @@ module PrintableSymbol
1313
def accept_printer(printer) = raise NotImplementedError, "Abstract method called"
1414
end
1515

16-
class Hover < T::Struct
16+
class Hover
1717
include PrintableSymbol
1818

19-
const :contents, String
20-
const :range, T.nilable(Range)
19+
#: String
20+
attr_reader :contents
21+
22+
#: Range?
23+
attr_reader :range
24+
25+
#: (contents: String, ?range: Range?) -> void
26+
def initialize(contents:, range: nil)
27+
@contents = contents
28+
@range = range
29+
end
2130

2231
class << self
2332
#: (Hash[untyped, untyped] json) -> Hover
@@ -42,11 +51,20 @@ def to_s
4251
end
4352
end
4453

45-
class Position < T::Struct
54+
class Position
4655
include PrintableSymbol
4756

48-
const :line, Integer
49-
const :char, Integer
57+
#: Integer
58+
attr_reader :line
59+
60+
#: Integer
61+
attr_reader :char
62+
63+
#: (line: Integer, char: Integer) -> void
64+
def initialize(line:, char:)
65+
@line = line
66+
@char = char
67+
end
5068

5169
class << self
5270
#: (Hash[untyped, untyped] json) -> Position
@@ -70,41 +88,59 @@ def to_s
7088
end
7189
end
7290

73-
class Range < T::Struct
91+
class Range
7492
include PrintableSymbol
7593

76-
const :start, Position
77-
const :end, Position
94+
#: Position
95+
attr_reader :start_pos
96+
97+
#: Position
98+
attr_reader :end_pos
99+
100+
#: (start_pos: Position, end_pos: Position) -> void
101+
def initialize(start_pos:, end_pos:)
102+
@start_pos = start_pos
103+
@end_pos = end_pos
104+
end
78105

79106
class << self
80107
#: (Hash[untyped, untyped] json) -> Range
81108
def from_json(json)
82109
Range.new(
83-
start: Position.from_json(json["start"]),
84-
end: Position.from_json(json["end"]),
110+
start_pos: Position.from_json(json["start"]),
111+
end_pos: Position.from_json(json["end"]),
85112
)
86113
end
87114
end
88115

89116
# @override
90117
#: (SymbolPrinter printer) -> void
91118
def accept_printer(printer)
92-
printer.print_object(start)
119+
printer.print_object(start_pos)
93120
printer.print_colored("-", Color::LIGHT_BLACK)
94-
printer.print_object(self.end)
121+
printer.print_object(end_pos)
95122
end
96123

97124
#: -> String
98125
def to_s
99-
"#{start}-#{self.end}"
126+
"#{start_pos}-#{end_pos}"
100127
end
101128
end
102129

103-
class Location < T::Struct
130+
class Location
104131
include PrintableSymbol
105132

106-
const :uri, String
107-
const :range, LSP::Range
133+
#: String
134+
attr_reader :uri
135+
136+
#: Range
137+
attr_reader :range
138+
139+
#: (uri: String, range: Range) -> void
140+
def initialize(uri:, range:)
141+
@uri = uri
142+
@range = range
143+
end
108144

109145
class << self
110146
#: (Hash[untyped, untyped] json) -> Location
@@ -129,12 +165,26 @@ def to_s
129165
end
130166
end
131167

132-
class SignatureHelp < T::Struct
168+
class SignatureHelp
133169
include PrintableSymbol
134170

135-
const :label, T.nilable(String)
136-
const :doc, Object # TODO
137-
const :params, T::Array[T.untyped] # TODO
171+
#: String?
172+
attr_reader :label
173+
174+
# TODO
175+
#: Object
176+
attr_reader :doc
177+
178+
# TODO
179+
#: Array[untyped]
180+
attr_reader :params
181+
182+
#: (doc: Object, params: Array[untyped], ?label: String?) -> void
183+
def initialize(doc:, params:, label: nil)
184+
@label = label
185+
@doc = doc
186+
@params = params
187+
end
138188

139189
class << self
140190
#: (Hash[untyped, untyped] json) -> SignatureHelp
@@ -162,13 +212,28 @@ def to_s
162212
end
163213
end
164214

165-
class Diagnostic < T::Struct
215+
class Diagnostic
166216
include PrintableSymbol
167217

168-
const :range, LSP::Range
169-
const :code, Integer
170-
const :message, String
171-
const :information, Object
218+
#: Range
219+
attr_reader :range
220+
221+
#: Integer
222+
attr_reader :code
223+
224+
#: String
225+
attr_reader :message
226+
227+
#: Object
228+
attr_reader :information
229+
230+
#: (range: Range, code: Integer, message: String, information: Object) -> void
231+
def initialize(range:, code:, message:, information:)
232+
@range = range
233+
@code = code
234+
@message = message
235+
@information = information
236+
end
172237

173238
class << self
174239
#: (Hash[untyped, untyped] json) -> Diagnostic
@@ -194,15 +259,43 @@ def to_s
194259
end
195260
end
196261

197-
class DocumentSymbol < T::Struct
262+
class DocumentSymbol
198263
include PrintableSymbol
199264

200-
const :name, String
201-
const :detail, T.nilable(String)
202-
const :kind, Integer
203-
const :location, T.nilable(Location)
204-
const :range, T.nilable(Range)
205-
const :children, T::Array[DocumentSymbol]
265+
#: String
266+
attr_reader :name
267+
268+
#: String?
269+
attr_reader :detail
270+
271+
#: Integer
272+
attr_reader :kind
273+
274+
#: Location?
275+
attr_reader :location
276+
277+
#: Range?
278+
attr_reader :range
279+
280+
#: Array[DocumentSymbol]
281+
attr_reader :children
282+
283+
#: (
284+
#| name: String,
285+
#| kind: Integer,
286+
#| children: Array[DocumentSymbol],
287+
#| ?detail: String?,
288+
#| ?location: Location?,
289+
#| ?range: Range?
290+
#| ) -> void
291+
def initialize(name:, kind:, children:, detail: nil, location: nil, range: nil)
292+
@name = name
293+
@detail = detail
294+
@kind = kind
295+
@location = location
296+
@range = range
297+
@children = children
298+
end
206299

207300
class << self
208301
#: (Hash[untyped, untyped] json) -> DocumentSymbol
@@ -221,7 +314,7 @@ def from_json(json)
221314
# @override
222315
#: (SymbolPrinter printer) -> void
223316
def accept_printer(printer)
224-
h = serialize.hash
317+
h = hash
225318
return if printer.seen.include?(h)
226319

227320
printer.seen.add(h)

0 commit comments

Comments
 (0)