Module: Steep::Server::LSPFormatter

Includes:
Steep::Services
Defined in:
lib/steep/server/lsp_formatter.rb

Constant Summary collapse

LSP =
LanguageServer::Protocol

Class Method Summary collapse

Class Method Details

.declaration_summary(decl) ⇒ Object



419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
# File 'lib/steep/server/lsp_formatter.rb', line 419

def declaration_summary(decl)
  # Note that all names in the declarations is absolute
  case decl
  when RBS::AST::Declarations::Class
    super_class = if super_class = decl.super_class
                    " < #{name_and_args(super_class.name, super_class.args)}"
                  end
    "class #{name_and_params(decl.name.relative!, decl.type_params)}#{super_class}"
  when RBS::AST::Declarations::Module
    self_type = unless decl.self_types.empty?
                  " : #{decl.self_types.map {|s| name_and_args(s.name, s.args) }.join(", ")}"
                end
    "module #{name_and_params(decl.name.relative!, decl.type_params)}#{self_type}"
  when RBS::AST::Declarations::TypeAlias
    "type #{name_and_params(decl.name.relative!, decl.type_params)} = #{decl.type}"
  when RBS::AST::Declarations::Interface
    "interface #{name_and_params(decl.name.relative!, decl.type_params)}"
  when RBS::AST::Declarations::ClassAlias
    "class #{decl.new_name.relative!} = #{decl.old_name}"
  when RBS::AST::Declarations::ModuleAlias
    "module #{decl.new_name.relative!} = #{decl.old_name}"
  when RBS::AST::Declarations::Global
    "#{decl.name}: #{decl.type}"
  when RBS::AST::Declarations::Constant
    "#{decl.name.relative!}: #{decl.type}"
  when RBS::AST::Ruby::Declarations::ClassDecl
    "class #{decl.class_name.relative!}"
  when RBS::AST::Ruby::Declarations::ModuleDecl
    "module #{decl.module_name.relative!}"
  when RBS::AST::Ruby::Declarations::ConstantDecl
    "#{decl.constant_name.relative!}: #{decl.type}"
  when RBS::AST::Ruby::Declarations::ClassModuleAliasDecl
    keyword =
      if decl.annotation.is_a?(RBS::AST::Ruby::Annotations::ClassAliasAnnotation)
        "class"
      else
        "module"
      end
    "#{keyword} #{decl.new_name} = #{decl.old_name}"
  end
end

.format_comment(comment, header: nil, &block) ⇒ Object



352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# File 'lib/steep/server/lsp_formatter.rb', line 352

def format_comment(comment, header: nil, &block)
  return unless comment

  io = StringIO.new
  if header
    io.puts "### 📚 #{header.gsub("_", "\\_")}"
    io.puts
  end
  io.puts comment.string.rstrip.gsub(/^[ \t]*<!--(?~-->)-->\n/, "").gsub(/\A([ \t]*\n)+/, "")

  if block
    yield io.string
  else
    io.string
  end
end

.format_comments(comments) ⇒ Object



318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/steep/server/lsp_formatter.rb', line 318

def format_comments(comments)
  io = StringIO.new

  with_docs = [] #: Array[[String, RBS::AST::Comment]]
  without_docs = [] #: Array[String]

  comments.each do |title, comment|
    if comment
      with_docs << [title, comment]
    else
      without_docs << title
    end
  end

  unless with_docs.empty?
    with_docs.each do |title, comment|
      io.puts format_comment(comment, header: title)
      io.puts
    end

    unless without_docs.empty?
      io.puts
      io.puts "----"
      if without_docs.size == 1
        io.puts "🔍 One more definition without docs"
      else
        io.puts "🔍 #{without_docs.size} more definitions without docs"
      end
    end
  end

  io.string
end

.format_completion_docs(item) ⇒ Object



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/steep/server/lsp_formatter.rb', line 220

def format_completion_docs(item)
  case item
  when Services::CompletionProvider::LocalVariableItem
    local_variable(item.identifier, item.type)
  when Services::CompletionProvider::ConstantItem
    io = StringIO.new

    io.puts <<~MD
      ```rbs
      #{declaration_summary(item.decl)}
      ```
    MD

    unless item.comments.all?(&:nil?)
      io.puts "----"
      io.puts format_comments(
        item.comments.map {|comment|
          [item.full_name.relative!.to_s, comment] #: [String, RBS::AST::Comment?]
        }
      )
    end

    io.string
  when Services::CompletionProvider::InstanceVariableItem
    instance_variable(item.identifier, item.type)
  when Services::CompletionProvider::SimpleMethodNameItem
    item_comment =
      case item.method_member
      when RBS::AST::Members::Base
        item.method_member.comment
      when RBS::AST::Ruby::Members::Base
        nil
      end
    format_method_item_doc(item.method_types, [], { item.method_name => item_comment })
  when Services::CompletionProvider::ComplexMethodNameItem
    method_names = item.method_names.map(&:relative).uniq
    comments = item.method_definitions.transform_values do |member|
      case member
      when RBS::AST::Members::Base
        member.comment
      when RBS::AST::Ruby::Members::Base
        nil
      end
    end
    format_method_item_doc(item.method_types, method_names, comments)
  when Services::CompletionProvider::GeneratedMethodNameItem
    format_method_item_doc(item.method_types, [], {}, "🤖 Generated method for receiver type")
  when Services::CompletionProvider::TypeNameItem
    io = StringIO.new

    io.puts <<~MD
      ```rbs
      #{declaration_summary(item.decl)}
      ```
    MD

    unless item.comments.empty?
      io.puts "----"
      io.puts format_comments(
        item.comments.map {|comment|
          [item.absolute_type_name.relative!.to_s, comment] #: [String, RBS::AST::Comment?]
        }
      )
    end

    io.string
  when Services::CompletionProvider::KeywordArgumentItem
    <<~MD
      **Keyword argument**: `#{item.identifier}`
    MD
  else
    raise
  end
end

.format_hover_content(content) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/steep/server/lsp_formatter.rb', line 20

def format_hover_content(content)
  case content
  when HoverProvider::VariableContent
    local_variable(content.name, content.type)

  when HoverProvider::MethodCallContent
    io = StringIO.new
    call = content.method_call

    case call
    when TypeInference::MethodCall::Typed
      io.puts <<~MD
        ```rbs
        #{call.actual_method_type.type.return_type}
        ```

        ----
      MD

      method_decls = call.method_decls.sort_by {|decl| decl.method_name.to_s }
      method_types = method_decls.map(&:method_type)

      if call.is_a?(TypeInference::MethodCall::Special)
        method_types = [
          call.actual_method_type.with(
            type: call.actual_method_type.type.with(return_type: call.return_type)
          )
        ]

        header = <<~MD
          **💡 Custom typing rule applies**

          ----
        MD
      end
    when TypeInference::MethodCall::Error
      method_decls = call.method_decls.sort_by {|decl| decl.method_name.to_s }
      method_types = method_decls.map {|decl| decl.method_type }

      header = <<~MD
        **🚨 No compatible method type found**

        ----
      MD
    end

    method_names = method_decls.map {|decl| decl.method_name.relative }
    docs = method_decls.map {|decl| [decl.method_name, decl.method_def.comment] }.to_h

    if header
      io.puts header
    end

    io.puts(
      format_method_item_doc(method_types, method_names, docs)
    )

    io.string

  when HoverProvider::DefinitionContent
    io = StringIO.new

    method_name =
      if content.method_name.is_a?(SingletonMethodName)
        "self.#{content.method_name.method_name}"
      else
        content.method_name.method_name
      end

    prefix_size = "def ".size + method_name.size
    method_types = content.definition.method_types

    io.puts <<~MD
      ```rbs
      def #{method_name}: #{method_types.join("\n" + " "*prefix_size + "| ") }
      ```

      ----
    MD

    if content.definition.method_types.size > 1
      io.puts "**Internal method type**"
      io.puts <<~MD
        ```rbs
        #{content.method_type}
        ```

        ----
      MD
    end

    io.puts format_comments(
      content.definition.comments.map {|comment|
        [content.method_name.relative.to_s, comment] #: [String, RBS::AST::Comment?]
      }
    )

    io.string
  when HoverProvider::ConstantContent
    io = StringIO.new

    decl_summary =
      case
      when decl = content.class_decl
        declaration_summary(decl.primary_decl)
      when decl = content.constant_decl
        declaration_summary(decl.decl)
      when decl = content.class_alias
        declaration_summary(decl.decl)
      end

    io.puts <<~MD
      ```rbs
      #{decl_summary}
      ```
    MD

    comments = content.comments.map {|comment|
      [content.full_name.relative!.to_s, comment] #: [String, RBS::AST::Comment?]
    }

    unless comments.all?(&:nil?)
      io.puts "----"
      io.puts format_comments(comments)
    end

    io.string
  when HoverProvider::TypeContent
    <<~MD
      ```rbs
      #{content.type}
      ```
    MD

  when HoverProvider::TypeAssertionContent
    <<~MD
      ```rbs
      #{content.asserted_type}
      ```

      ↑ Converted from `#{content.original_type.to_s}`
    MD

  when HoverProvider::TypeAliasContent, HoverProvider::InterfaceTypeContent
    io = StringIO.new()

    io.puts <<~MD
      ```rbs
      #{declaration_summary(content.decl)}
      ```
    MD

    if comment = content.decl.comment
      io.puts
      io.puts "----"

      io.puts format_comment(comment, header: content.decl.name.relative!.to_s)
    end

    io.string

  when HoverProvider::ClassTypeContent
    io = StringIO.new

    io << <<~MD
    ```rbs
    #{declaration_summary(content.decl)}
    ```
    MD

    comment =
      case content.decl
      when RBS::AST::Declarations::Base
        content.decl.comment
      when RBS::AST::Ruby::Declarations::Base
        nil
      end

    if comment
      io.puts "----"

      class_name =
        case content.decl
        when RBS::AST::Declarations::ModuleAlias, RBS::AST::Declarations::ClassAlias
          content.decl.new_name
        when RBS::AST::Declarations::Class, RBS::AST::Declarations::Module
          content.decl.name
        else
          raise
        end

      io << format_comments([[class_name.relative!.to_s, content.decl.comment]])
    end

    io.string
  else
    raise content.class.to_s
  end
end

.format_method_item_doc(method_types, method_names, comments, footer = "") ⇒ Object



461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
# File 'lib/steep/server/lsp_formatter.rb', line 461

def format_method_item_doc(method_types, method_names, comments, footer = "")
  io = StringIO.new

  io.puts "**Method type**:"
  io.puts "```rbs"
  if method_types.size == 1
    io.puts method_types[0].to_s
  else
    io.puts "  #{method_types.join("\n| ")}"
  end
  io.puts "```"

  if method_names.size > 1
    io.puts "**Possible methods**: #{method_names.map {|type| "`#{type.to_s}`" }.join(", ")}"
    io.puts
  end

  unless comments.each_value.all?(&:nil?)
    io.puts "----"
    io.puts format_comments(comments.transform_keys {|name| name.relative.to_s }.entries)
  end

  unless footer.empty?
    io.puts footer.rstrip
  end

  io.string
end

.format_rbs_completion_docs(type_name, decl, comments) ⇒ Object



295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/steep/server/lsp_formatter.rb', line 295

def format_rbs_completion_docs(type_name, decl, comments)
  io = StringIO.new

  io.puts <<~MD
  ```rbs
  #{declaration_summary(decl)}
  ```
  MD

  unless comments.empty?
    io.puts
    io.puts "----"

    io.puts format_comments(
      comments.map {|comment|
        [type_name.relative!.to_s, comment] #: [String, RBS::AST::Comment?]
      }
    )
  end

  io.string
end

.instance_variable(name, type) ⇒ Object



375
376
377
378
379
# File 'lib/steep/server/lsp_formatter.rb', line 375

def instance_variable(name, type)
  <<~MD
    **Instance variable** `#{name}: #{type}`
  MD
end

.local_variable(name, type) ⇒ Object



369
370
371
372
373
# File 'lib/steep/server/lsp_formatter.rb', line 369

def local_variable(name, type)
  <<~MD
    **Local variable** `#{name}: #{type}`
  MD
end

.markup_content(string = nil, &block) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/steep/server/lsp_formatter.rb', line 10

def markup_content(string = nil, &block)
  if block
    string = yield()
  end

  if string
    LSP::Interface::MarkupContent.new(kind: LSP::Constant::MarkupKind::MARKDOWN, value: string)
  end
end

.name_and_args(name, args) ⇒ Object



411
412
413
414
415
416
417
# File 'lib/steep/server/lsp_formatter.rb', line 411

def name_and_args(name, args)
  if args.empty?
    "#{name}"
  else
    "#{name}[#{args.map(&:to_s).join(", ")}]"
  end
end

.name_and_params(name, params) ⇒ Object



381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
# File 'lib/steep/server/lsp_formatter.rb', line 381

def name_and_params(name, params)
  if params.empty?
    "#{name}"
  else
    ps = params.each.map do |param|
      s = +""
      if param.unchecked?
        s << "unchecked "
      end
      case param.variance
      when :invariant
        # nop
      when :covariant
        s << "out "
      when :contravariant
        s << "in "
      end
      s << param.name.to_s

      if param.upper_bound_type
        s << " < #{param.upper_bound_type.to_s}"
      end

      s
    end

    "#{name}[#{ps.join(", ")}]"
  end
end