lib/spoom/sorbet/lsp/structures.rb in spoom-1.1.8 vs lib/spoom/sorbet/lsp/structures.rb in spoom-1.1.9

- old
+ new

@@ -1,6 +1,6 @@ -# typed: true +# typed: strict # frozen_string_literal: true require_relative "../../printer" require "set" @@ -21,10 +21,11 @@ include PrintableSymbol const :contents, String const :range, T.nilable(Range) + sig { params(json: T::Hash[T.untyped, T.untyped]).returns(Hover) } def self.from_json(json) Hover.new( contents: json['contents']['value'], range: json['range'] ? Range.from_json(json['range']) : nil ) @@ -34,10 +35,11 @@ def accept_printer(printer) printer.print("#{contents}\n") printer.print_object(range) if range end + sig { returns(String) } def to_s "#{contents} (#{range})." end end @@ -46,10 +48,11 @@ include PrintableSymbol const :line, Integer const :char, Integer + sig { params(json: T::Hash[T.untyped, T.untyped]).returns(Position) } def self.from_json(json) Position.new( line: json['line'].to_i, char: json['character'].to_i ) @@ -58,10 +61,11 @@ sig { override.params(printer: SymbolPrinter).void } def accept_printer(printer) printer.print_colored("#{line}:#{char}", Color::LIGHT_BLACK) end + sig { returns(String) } def to_s "#{line}:#{char}" end end @@ -70,10 +74,11 @@ include PrintableSymbol const :start, Position const :end, Position + sig { params(json: T::Hash[T.untyped, T.untyped]).returns(Range) } def self.from_json(json) Range.new( start: Position.from_json(json['start']), end: Position.from_json(json['end']) ) @@ -84,10 +89,11 @@ printer.print_object(start) printer.print_colored("-", Color::LIGHT_BLACK) printer.print_object(self.end) end + sig { returns(String) } def to_s "#{start}-#{self.end}" end end @@ -96,10 +102,11 @@ include PrintableSymbol const :uri, String const :range, LSP::Range + sig { params(json: T::Hash[T.untyped, T.untyped]).returns(Location) } def self.from_json(json) Location.new( uri: json['uri'], range: Range.from_json(json['range']) ) @@ -109,10 +116,11 @@ def accept_printer(printer) printer.print_colored("#{printer.clean_uri(uri)}:", Color::LIGHT_BLACK) printer.print_object(range) end + sig { returns(String) } def to_s "#{uri}:#{range}" end end @@ -122,10 +130,11 @@ const :label, T.nilable(String) const :doc, Object # TODO const :params, T::Array[T.untyped] # TODO + sig { params(json: T::Hash[T.untyped, T.untyped]).returns(SignatureHelp) } def self.from_json(json) SignatureHelp.new( label: json['label'], doc: json['documentation'], params: json['parameters'], @@ -138,10 +147,11 @@ printer.print("(") printer.print(params.map { |l| "#{l['label']}: #{l['documentation']}" }.join(", ")) printer.print(")") end + sig { returns(String) } def to_s "#{label}(#{params})." end end @@ -152,10 +162,11 @@ const :range, LSP::Range const :code, Integer const :message, String const :informations, Object + sig { params(json: T::Hash[T.untyped, T.untyped]).returns(Diagnostic) } def self.from_json(json) Diagnostic.new( range: Range.from_json(json['range']), code: json['code'].to_i, message: json['message'], @@ -166,10 +177,11 @@ sig { override.params(printer: SymbolPrinter).void } def accept_printer(printer) printer.print(to_s) end + sig { returns(String) } def to_s "Error: #{message} (#{code})." end end @@ -182,10 +194,11 @@ const :kind, Integer const :location, T.nilable(Location) const :range, T.nilable(Range) const :children, T::Array[DocumentSymbol] + sig { params(json: T::Hash[T.untyped, T.untyped]).returns(DocumentSymbol) } def self.from_json(json) DocumentSymbol.new( name: json['name'], detail: json['detail'], kind: json['kind'], @@ -219,20 +232,21 @@ printer.dedent end # TODO: also display details? end + sig { returns(String) } def to_s "#{name} (#{range})" end + sig { returns(String) } def kind_string - return "<unknown:#{kind}>" unless SYMBOL_KINDS.key?(kind) - SYMBOL_KINDS[kind] + SYMBOL_KINDS[kind] || "<unknown:#{kind}>" end - SYMBOL_KINDS = { + SYMBOL_KINDS = T.let({ 1 => "file", 2 => "module", 3 => "namespace", 4 => "package", 5 => "class", @@ -255,29 +269,33 @@ 22 => "enum_member", 23 => "struct", 24 => "event", 25 => "operator", 26 => "type_parameter", - } + }, T::Hash[Integer, String]) end class SymbolPrinter < Printer extend T::Sig - attr_accessor :seen, :prefix + sig { returns(T::Set[Integer]) } + attr_accessor :seen + sig { returns(T.nilable(String)) } + attr_accessor :prefix + sig do params( out: T.any(IO, StringIO), colors: T::Boolean, indent_level: Integer, prefix: T.nilable(String) ).void end def initialize(out: $stdout, colors: true, indent_level: 0, prefix: nil) super(out: out, colors: colors, indent_level: indent_level) - @seen = Set.new + @seen = T.let(Set.new, T::Set[Integer]) @out = out @colors = colors @indent_level = indent_level @prefix = prefix end @@ -293,9 +311,10 @@ objects.each { |object| print_object(object) } end sig { params(uri: String).returns(String) } def clean_uri(uri) + prefix = self.prefix return uri unless prefix uri.delete_prefix(prefix) end sig { params(objects: T::Array[PrintableSymbol]).void }