Class: NREPL::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/nrepl-lazuli/connection.rb

Constant Summary collapse

@@debug_counter =
0

Instance Method Summary collapse

Constructor Details

#initialize(input, server:, debug: false, out: input, binding: nil, bindings: {}, bindings_by_id: {}) ⇒ Connection

Returns a new instance of Connection.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/nrepl-lazuli/connection.rb', line 5

def initialize(
  input,
  server:,
  debug: false,
  out: input,
  binding: nil,
  bindings: {},
  bindings_by_id: {}
)
  @debug = debug
  @server = server
  @in = input
  @out = out
  @pending_evals = {}
  @counter = 0
  @binding = binding
  @bindings = bindings
  @bindings_by_id = bindings_by_id
  @retries = 0
end

Instance Method Details

#send_exception(msg, e) ⇒ Object



464
465
466
467
# File 'lib/nrepl-lazuli/connection.rb', line 464

def send_exception(msg, e)
  error = e.inspect.sub(/\>$/, ' stack=' + e.backtrace.inspect+'>')
  send_msg(response_for(msg, { 'ex' => error, 'status' => ['done', 'error'] }))
end

#send_msg(msg) ⇒ Object



469
470
471
472
473
474
475
476
# File 'lib/nrepl-lazuli/connection.rb', line 469

def send_msg(msg)
  debug "Sending", msg
  @out.write_nonblock(BEncode.encode(msg))
  @out.flush
rescue IO::EAGAINWaitWritable
  @retries += 1
  @out.close if @retries > 50
end

#treat_messages!Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/nrepl-lazuli/connection.rb', line 26

def treat_messages!
  bencode = BEncode.new(@in)
  loop do
    break if @in.eof?
    msg = bencode.parse!
    debug "Received", msg
    next unless msg
    treat_msg(msg)
  end
  @pending_evals.each { |(i, _)| clear_eval!(i) }
rescue Errno::ECONNRESET
  @pending_evals.each { |(i, _)| clear_eval!(i) }
rescue => e
  STDOUT.puts "CRASH! #{e}"
end

#treat_msg(msg) ⇒ Object



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
# File 'lib/nrepl-lazuli/connection.rb', line 42

def treat_msg(msg)
  case msg['op']
  when 'clone'
    register_session(msg)
  when 'complete'
    get_completions(msg)
  when 'find_definition'
    find_definition(msg)
  when 'describe'
    describe_msg(msg)
  when 'eval'
    eval_op(msg, false)
  when 'eval_pause'
    eval_op(msg, true)
  when 'last_exception'
    ex = Server.class_variable_get(:@@last_exception)
    send_msg(response_for(msg, { 'result' => ex.inspect, 'status' => ['done'] }))
  when 'eval_resume'
    msg['id'] ||= "eval_#{@counter += 1}"
    stop_id = msg['stop_id']
    clear_eval!(stop_id)

    send_msg(response_for(msg, {
      'status' => ['done'],
      'op' => msg['op']
    }))
  when 'unwatch'
    msg['id'] ||= "eval_#{@counter += 1}"
    watch_id = msg['watch_id']
    info = @bindings_by_id.delete(watch_id)
    if(info)
      file_info = @bindings.fetch(info[:file], {})
      content = file_info.fetch(info[:row], {})
      content.delete(watch_id)
      file_info.delete(info[:row]) if(content.empty?)
      @bindings.delete(info[:file]) if(file_info.empty?)
    end

    send_msg(response_for(msg, {
      'status' => ['done'],
      'op' => msg['op']
    }))
  when 'interrupt'
    id = if(msg['interrupt-id'])
      msg['interrupt-id']
    else
      @pending_evals.keys.first
    end
    pending = @pending_evals[id] || {}
    thread = pending[:thread]
    msg['id'] ||= (id || 'unknown')

    if(thread)
      thread.kill
      clear_eval!(id)
      send_msg(response_for(msg, {
        'status' => ['done', 'interrupted'],
        'op' => msg['op']
      }))
    else
      send_msg(response_for(msg, {
        'status' => ['done'],
        'op' => msg['op']
      }))
    end
  when 'update_watch_lines'
    msg['id'] ||= "eval_#{@counter += 1}"
    file = msg['file']
    initial_line = msg['line']
    delta = msg['delta']
    rows_bindings = @bindings[file] || {}
    rows_bindings.keys.each do |row|
      if row > initial_line
        watch = rows_bindings.delete(row)
        new_row = row + delta
        old_id = "#{file}:#{row+1}"
        new_id = "#{file}:#{new_row+1}"

        watch.keys.each do |id|
          if id == old_id
            watch_per_id = @bindings_by_id.delete(id)
            @bindings_by_id[new_id] = watch_per_id.update(row: new_row)
          else
            @bindings_by_id[id][:row] = new_row
          end
        end
        rows_bindings[new_row] = watch
        old_binding = watch.delete(old_id)
        watch[new_id] = old_binding if(old_binding)
      end
    end

    get_watches(msg)
  when 'get_watches'
    get_watches(msg)
  when 'watches_for_file'
    msg['id'] ||= "eval_#{@counter += 1}"
    file = msg['file']
    rows_bindings = @bindings[file] || {}
    send_msg(response_for(msg, {
      'status' => ['done'],
      'rows' => rows_bindings.keys.sort,
      'op' => msg['op']
    }))
  when 'set_trace'
    disable = msg['trace'] == 0
    disable ? @server.call_trace.disable : @server.call_trace.enable
    send_msg(response_for(msg, { 'op' => msg['op'], 'status' => ['done'] }))
  else
    send_msg(response_for(msg, {
      'op' => msg['op'],
      'status' => ['done', 'error'],
      'error' => "unknown operation: #{msg['op'].inspect}"
    }))
  end
end