Class: PlatformosCheck::LanguageServer::Bridge

Inherits:
Object
  • Object
show all
Defined in:
lib/platformos_check/language_server/bridge.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(messenger) ⇒ Bridge

Returns a new instance of Bridge.



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/platformos_check/language_server/bridge.rb', line 27

def initialize(messenger)
  # The messenger is responsible for IO.
  # Could be STDIO or WebSockets or Mock.
  @messenger = messenger

  # Whether the client supports work done progress notifications
  @supports_work_done_progress = false

  @work_done_progress_mutex = Mutex.new
  @work_done_progress_token = 1
end

Instance Attribute Details

#supports_work_done_progress=(value) ⇒ Object (writeonly)

Sets the attribute supports_work_done_progress

Parameters:

  • value

    the value to set the attribute supports_work_done_progress to.



25
26
27
# File 'lib/platformos_check/language_server/bridge.rb', line 25

def supports_work_done_progress=(value)
  @supports_work_done_progress = value
end

Instance Method Details

#log(message) ⇒ Object



39
40
41
# File 'lib/platformos_check/language_server/bridge.rb', line 39

def log(message)
  @messenger.log(message)
end

#read_messageObject



43
44
45
46
47
48
# File 'lib/platformos_check/language_server/bridge.rb', line 43

def read_message
  message_body = @messenger.read_message
  message_json = JSON.parse(message_body, symbolize_names: true)
  @messenger.log(JSON.pretty_generate(message_json)) if PlatformosCheck.debug?
  message_json
end

#receive_response(id, result) ⇒ Object



69
70
71
# File 'lib/platformos_check/language_server/bridge.rb', line 69

def receive_response(id, result)
  Channel.by_id(id) << result
end

#send_create_work_done_progress_requestObject



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/platformos_check/language_server/bridge.rb', line 116

def send_create_work_done_progress_request
  # This isn't necessary, but it kind of is to make it obvious
  # that this variable is not thread safe. Don't try to refactor
  # this with @work_done_progress_token because you're going to
  # have a hard time.
  token = @work_done_progress_mutex.synchronize do
    @work_done_progress_token += 1
  end

  return token unless supports_work_done_progress?

  # We're going to wait for a response here...
  send_request("window/workDoneProgress/create", {
                 token:
               })

  token
end

#send_internal_error(id, e) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/platformos_check/language_server/bridge.rb', line 85

def send_internal_error(id, e)
  # For a reason I can't comprehend, sometimes
  # e.full_message _hangs_ and brings your CPU to 100%.
  # It's wrapped in here because it prints anyway...
  # This shit is weird, yo.
  Timeout.timeout(1) do
    warn e.full_message
  end
ensure
  send_response(id, nil, {
                  code: ErrorCodes::INTERNAL_ERROR,
                  message: "A platformos-check-language-server has occured, inspect OUTPUT logs for details."
                })
end

#send_message(message_hash) ⇒ Object



50
51
52
53
54
55
# File 'lib/platformos_check/language_server/bridge.rb', line 50

def send_message(message_hash)
  message_hash[:jsonrpc] = '2.0'
  message_body = JSON.dump(message_hash)
  @messenger.log(JSON.pretty_generate(message_hash)) if PlatformosCheck.debug?
  @messenger.send_message(message_body)
end

#send_notification(method, params) ⇒ Object



101
102
103
104
105
# File 'lib/platformos_check/language_server/bridge.rb', line 101

def send_notification(method, params)
  message = { method: }
  message[:params] = params
  send_message(message)
end

#send_progress(token, value) ⇒ Object



108
109
110
# File 'lib/platformos_check/language_server/bridge.rb', line 108

def send_progress(token, value)
  send_notification("$/progress", token:, value:)
end

#send_request(method, params = nil) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/platformos_check/language_server/bridge.rb', line 58

def send_request(method, params = nil)
  channel = Channel.create
  message = { id: channel.id }
  message[:method] = method
  message[:params] = params if params
  send_message(message)
  channel.pop
ensure
  channel.close
end

#send_response(id, result = nil, error = nil) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/platformos_check/language_server/bridge.rb', line 74

def send_response(id, result = nil, error = nil)
  message = { id: }
  if error
    message[:error] = error
  else
    message[:result] = result
  end
  send_message(message)
end

#send_work_done_progress_begin(token, title) ⇒ Object



135
136
137
138
139
140
141
142
143
144
# File 'lib/platformos_check/language_server/bridge.rb', line 135

def send_work_done_progress_begin(token, title)
  return unless supports_work_done_progress?

  send_progress(token, {
                  kind: 'begin',
                  title:,
                  cancellable: false,
                  percentage: 0
                })
end

#send_work_done_progress_end(token, message) ⇒ Object



157
158
159
160
161
162
163
164
# File 'lib/platformos_check/language_server/bridge.rb', line 157

def send_work_done_progress_end(token, message)
  return unless supports_work_done_progress?

  send_progress(token, {
                  kind: 'end',
                  message:
                })
end

#send_work_done_progress_report(token, message, percentage) ⇒ Object



146
147
148
149
150
151
152
153
154
155
# File 'lib/platformos_check/language_server/bridge.rb', line 146

def send_work_done_progress_report(token, message, percentage)
  return unless supports_work_done_progress?

  send_progress(token, {
                  kind: 'report',
                  message:,
                  cancellable: false,
                  percentage:
                })
end

#supports_work_done_progress?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/platformos_check/language_server/bridge.rb', line 112

def supports_work_done_progress?
  @supports_work_done_progress
end