Module: MilkTea::LSP::Server::ServerTypeHierarchy

Included in:
MilkTea::LSP::Server
Defined in:
lib/milk_tea/lsp/server/type_hierarchy.rb

Constant Summary collapse

TYPE_KIND_MAP =
{
  struct: 22,
  enum: 13,
  flags: 13,
  variant: 13,
  union: 22,
  interface: 11,
}.freeze

Instance Method Summary collapse

Instance Method Details

#handle_prepare_type_hierarchy(params) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/milk_tea/lsp/server/type_hierarchy.rb', line 16

def handle_prepare_type_hierarchy(params)
  text_document = params["textDocument"]
  position = params["position"]
  uri = text_document["uri"]
  lsp_line = position["line"]
  lsp_char = position["character"]

  facts = @workspace.get_facts(uri)
  return nil unless facts

  token = @workspace.find_token_at(uri, lsp_line, lsp_char)
  return nil unless token
  return nil unless token.type == :identifier

  type_name = token.lexeme

  type_entry = type_hierarchy_entry(facts, type_name, uri)
  return nil unless type_entry

  [type_entry]
end

#handle_subtypes(params) ⇒ Object



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
# File 'lib/milk_tea/lsp/server/type_hierarchy.rb', line 64

def handle_subtypes(params)
  item = params["item"] || params[:item]
  data = item["data"] || item[:data]
  return [] unless data.is_a?(Hash) || data.respond_to?(:[])

  type_name = data["name"] || data[:name]
  return [] if type_name.to_s.empty?

  results = []
  seen = Set.new

  @workspace.all_documents.each do |doc_uri|
    facts = @workspace.get_facts(doc_uri)
    next unless facts

    results_for = collect_subtypes(facts, type_name, doc_uri)
    results_for.each do |entry|
      key = [entry[:name], entry[:uri]]
      next if seen.include?(key)

      seen << key
      results << entry
    end
  end

  results
end

#handle_supertypes(params) ⇒ Object



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
# File 'lib/milk_tea/lsp/server/type_hierarchy.rb', line 38

def handle_supertypes(params)
  item = params["item"] || params[:item]
  data = item["data"] || item[:data]
  return [] unless data.is_a?(Hash) || data.respond_to?(:[])

  type_name = data["name"] || data[:name]
  uri = data["uri"] || data[:uri]
  return [] if type_name.to_s.empty?

  facts = @workspace.get_facts(uri)
  return [] unless facts

  results = []
  type_obj = facts.types[type_name]

  if type_obj && facts.implemented_interfaces.key?(type_obj)
    iface_names = facts.implemented_interfaces[type_obj]
    iface_names.each do |iface|
      entry = type_hierarchy_entry(facts, iface.name, uri)
      results << entry if entry
    end
  end

  results
end