Module: Jade::Signature

Extended by:
Signature
Included in:
Signature
Defined in:
lib/jade/signature.rb

Overview

One-line rendering of a name and its type, shared by hover and q api.

Constant Summary collapse

LETTERS =
('a'..'z').to_a.freeze

Instance Method Summary collapse

Instance Method Details

#constraint_prefix(constraints) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/jade/signature.rb', line 33

def constraint_prefix(constraints)
  return '' if constraints.empty?

  constraints
    .map { short_constraint(it) }
    .uniq
    .join(', ')
    .then { "#{it} => " }
end

#display_name(var, taken) ⇒ Object



27
28
29
30
31
# File 'lib/jade/signature.rb', line 27

def display_name(var, taken)
  return var.name if var.name && !taken.include?(var.name)

  (LETTERS - taken).first || "#{var.name}#{taken.size}"
end

#naming(type, constraints) ⇒ Object

Vars compare by id but print as their name, which nothing keeps unique.



17
18
19
20
21
22
23
24
25
# File 'lib/jade/signature.rb', line 17

def naming(type, constraints)
  (type.unbound_vars + constraints.flat_map(&:unbound_vars))
    .uniq(&:id)
    .reduce([{}, []]) do |(mappings, taken), var|
      display_name(var, taken)
        .then { [mappings.merge(var.id => Type.var(var.id, it)), taken + [it]] }
    end
    .then { |(mappings, _)| Frontend::TypeChecking::Substitution[mappings] }
end

#render(name, type, constraints) ⇒ Object



10
11
12
13
14
# File 'lib/jade/signature.rb', line 10

def render(name, type, constraints)
  naming(type, constraints)
    .then { |sub| [sub.apply(type), constraints.map { sub.apply(it) }] }
    .then { |(renamed, cs)| "#{name} : #{constraint_prefix(cs)}#{renamed}" }
end

#short_constraint(constraint) ⇒ Object



43
44
45
# File 'lib/jade/signature.rb', line 43

def short_constraint(constraint)
  "#{constraint.interface.split('.').last} #{constraint.type}"
end