Class: Diakonos::Lsp::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/diakonos/lsp/session.rb

Constant Summary collapse

INITIAL_VERSION =
1

Instance Method Summary collapse

Constructor Details

#initialize(on_diagnostics: nil, server:) ⇒ Session

Returns a new instance of Session.



6
7
8
9
10
11
12
# File 'lib/diakonos/lsp/session.rb', line 6

def initialize(on_diagnostics: nil, server:)
  @diagnostics = {}
  @document_versions = {}
  @on_diagnostics = on_diagnostics
  @pending_requests = {}
  @server = server
end

Instance Method Details

#diagnostics_for_line(uri:, line:) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/diakonos/lsp/session.rb', line 46

def diagnostics_for_line(uri:, line:)
  Array(
    @diagnostics[uri]&.select { |d|
      d.start_line <= line && line <= d.end_line
    }
  )
end

#go_to_definition(buffer:, on_result:) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/diakonos/lsp/session.rb', line 30

def go_to_definition(buffer:, on_result:)
  send_request(
    method: 'textDocument/definition',
    on_response: on_result,
    params: {
      position: {
        character: buffer.last_col,
        line: buffer.last_row,
      },
      textDocument: {
        uri: buffer.lsp_uri,
      },
    },
  )
end

#hover(buffer:, on_result:) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/diakonos/lsp/session.rb', line 14

def hover(buffer:, on_result:)
  send_request(
    method: 'textDocument/hover',
    on_response: on_result,
    params: {
      position: {
        character: buffer.last_col,
        line: buffer.last_row,
      },
      textDocument: {
        uri: buffer.lsp_uri,
      },
    },
  )
end

#notify_did_change(buffer:) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/diakonos/lsp/session.rb', line 54

def notify_did_change(buffer:)
  uri = buffer.lsp_uri
  if @document_versions.key?(uri)
    @document_versions[uri] += 1
    send_notification(
      method: 'textDocument/didChange',
      params: {
        textDocument: {
          uri:,
          version: @document_versions[uri],
        },
        contentChanges: [
          { text: buffer.lines.join("\n") },
        ],
      },
    )
  end
end

#notify_did_close(buffer:) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/diakonos/lsp/session.rb', line 73

def notify_did_close(buffer:)
  uri = buffer.lsp_uri
  if @document_versions.key?(uri)
    @document_versions.delete(uri)
    send_notification(
      method: 'textDocument/didClose',
      params: {
        textDocument: { uri: },
      },
    )
  end
end

#notify_did_open(buffer:) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/diakonos/lsp/session.rb', line 86

def notify_did_open(buffer:)
  uri = buffer.lsp_uri
  if uri
    @document_versions[uri] = INITIAL_VERSION
    send_notification(
      method: 'textDocument/didOpen',
      params: {
        textDocument: {
          languageId: buffer.original_language,
          text: buffer.lines.join("\n"),
          uri:,
          version: INITIAL_VERSION,
        },
      },
    )
  end
end

#process_queueObject



104
105
106
107
108
109
110
111
# File 'lib/diakonos/lsp/session.rb', line 104

def process_queue
  loop do
    message = @server.queue.pop(true)
    handle_message(message:)
  rescue ThreadError
    break
  end
end

#send_notification(method:, params: {}) ⇒ Object



113
114
115
116
117
118
119
120
# File 'lib/diakonos/lsp/session.rb', line 113

def send_notification(method:, params: {})
  @server.write(
    message: {
      method:,
      params:,
    },
  )
end

#send_request(method:, on_response: nil, params: {}) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/diakonos/lsp/session.rb', line 122

def send_request(method:, on_response: nil, params: {})
  request_id = @server.next_request_id
  @pending_requests[request_id] = {
    id: request_id,
    method:,
    on_response:,
    sent_at: Time.now,
  }
  @server.write(
    message: {
      id: request_id,
      method:,
      params:,
    },
  )

  request_id
end