Module: Lsp::LanguageServer

Defined in:
lib/lsp.rb

Constant Summary collapse

LanguageServerError =
Class.new(StandardError)
NotImplementedError =
Class.new(LanguageServerError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#language_server=(value) ⇒ Object (writeonly)

Sets the attribute language_server

Parameters:

  • value

    the value to set the attribute language_server to.



160
161
162
# File 'lib/lsp.rb', line 160

def language_server=(value)
  @language_server = value
end

Instance Method Details

#handle_initialize(request) ⇒ Object



170
171
172
# File 'lib/lsp.rb', line 170

def handle_initialize(request)
  raise NotImplementedError
end

#handle_text_document_definition(request) ⇒ Object



166
167
168
# File 'lib/lsp.rb', line 166

def handle_text_document_definition(request)
  raise NotImplementedError
end

#handle_text_document_did_openObject



174
175
176
# File 'lib/lsp.rb', line 174

def handle_text_document_did_open
  raise NotImplementedError
end

#handle_text_document_hover(request) ⇒ Object



162
163
164
# File 'lib/lsp.rb', line 162

def handle_text_document_hover(request)
  raise NotImplementedError
end

#notify(method_name, params) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/lsp.rb', line 130

def notify(method_name, params)
  case method_name
  when "textDocument/didOpen"
    handle_text_document_did_open
  when "textDocument/didChange"
    handle_text_document_did_change(
      DidChangeTextDocumentParams.new(
        VersionedTextDocumentIdentifier.new(
          URI(params.fetch(:textDocument).fetch(:uri)),
          params.fetch(:textDocument).fetch(:version)),
        params.fetch(:contentChanges).map do |contentChange|
          next if contentChange[:range]
          TextDocumentContentChangeEvent.new(
            contentChange.fetch(:text))
        end.compact))
  end
rescue NotImplementedError
end

#request(id, method_name, params) ⇒ Object



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
# File 'lib/lsp.rb', line 95

def request(id, method_name, params)
  response = case method_name
  when "textDocument/hover"
    handle_text_document_hover(
      TextDocumentPositionParams.new(
        TextDocumentIdentifier.new(
          URI(params.fetch(:textDocument).fetch(:uri))),
        Position.from_hash(params.fetch(:position))))
  when "textDocument/definition"
    handle_text_document_definition(
      TextDocumentPositionParams.new(
        TextDocumentIdentifier.new(
          URI(params.fetch(:textDocument).fetch(:uri))),
        Position.from_hash(params.fetch(:position))))
  when "initialize"
    handle_initialize(
      InitializeRequest.new(
        URI(params.fetch(:rootUri))))
  when "textDocument/completion"
    handle_text_document_completion(
      TextDocumentPositionParams.new(
        TextDocumentIdentifier.new(
          URI(params.fetch(:textDocument).fetch(:uri))),
        Position.from_hash(params.fetch(:position))))
  else
    ResponseMessage.new(nil, ResponseError::MethodNotFound.new)
  end
  @language_server.response(
    id,
    to_result(response.result),
    to_result(response.error))
rescue NotImplementedError
  ResponseMessage.new(nil, ResponseError::MethodNotFound.new)
end

#to_result(obj) ⇒ Object



149
150
151
152
153
154
155
156
157
158
# File 'lib/lsp.rb', line 149

def to_result(obj)
  case obj
  when Array
    obj.map {|elem| to_result(elem) }
  when NilClass
    nil
  else
    obj.to_h
  end
end