Class: Orbacle::LangServer

Inherits:
Object
  • Object
show all
Includes:
Lsp::LanguageServer
Defined in:
lib/orbacle/lang_server.rb

Defined Under Namespace

Modules: Errors

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger, engine) ⇒ LangServer

Returns a new instance of LangServer.



14
15
16
17
18
# File 'lib/orbacle/lang_server.rb', line 14

def initialize(logger, engine)
  @logger = logger
  @engine = engine
  @file_contents = {}
end

Instance Attribute Details

#engineObject (readonly)

Returns the value of attribute engine.



20
21
22
# File 'lib/orbacle/lang_server.rb', line 20

def engine
  @engine
end

#loggerObject (readonly)

Returns the value of attribute logger.



20
21
22
# File 'lib/orbacle/lang_server.rb', line 20

def logger
  @logger
end

Instance Method Details

#get_file_content(uri) ⇒ Object



75
76
77
# File 'lib/orbacle/lang_server.rb', line 75

def get_file_content(uri)
  @file_contents[uri.path] || File.read(uri.path)
end

#handle_initialize(request) ⇒ Object



22
23
24
25
26
27
# File 'lib/orbacle/lang_server.rb', line 22

def handle_initialize(request)
  root_path = request.root_uri.path
  logger.info("Initializing at #{root_path.inspect}")
  engine.index(root_path)
  Lsp::ResponseMessage.successful(nil)
end

#handle_text_document_completion(request) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/orbacle/lang_server.rb', line 52

def handle_text_document_completion(request)
  log_errors do
    file_content = get_file_content(request.text_document.uri)
    completions = engine.completions_for_call_under_position(file_content, Position.new(request.position.line, request.position.character - 1))
    Lsp::ResponseMessage.successful(completions.map {|c| Lsp::CompletionItem.new(c) })
  end
end

#handle_text_document_definition(request) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/orbacle/lang_server.rb', line 39

def handle_text_document_definition(request)
  log_errors do
    file_path = request.text_document.uri.path
    file_content = File.read(file_path)
    locations = engine.locations_for_definition_under_position(file_path, file_content, Position.new(request.position.line, request.position.character))
    if locations
      Lsp::ResponseMessage.successful(locations.map(&method(:location_to_lsp_location)))
    else
      Lsp::ResponseMessage.new(nil, Errors::NoDefinitionFound)
    end
  end
end

#handle_text_document_did_change(request) ⇒ Object



60
61
62
63
64
# File 'lib/orbacle/lang_server.rb', line 60

def handle_text_document_did_change(request)
  log_errors do
    @file_contents[request.text_document.uri.path] = request.content_changes[0].text
  end
end

#handle_text_document_hover(request) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/orbacle/lang_server.rb', line 29

def handle_text_document_hover(request)
  log_errors do
    filepath = request.text_document.uri.path
    pretty_type = engine.get_type_information(filepath, Position.new(request.position.line, request.position.character))
    Lsp::ResponseMessage.successful(
      Lsp::TextDocumentHoverResult.new(
        "Type of that expression: #{pretty_type}"))
  end
end

#location_to_lsp_location(location) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/orbacle/lang_server.rb', line 79

def location_to_lsp_location(location)
  Lsp::Location.new(
    URI("file://#{location.uri}"),
    Lsp::Range.new(
      Lsp::Position.new(location.start.line, location.start.character),
      Lsp::Position.new(location.end.line, location.end.character)))
end

#log_errorsObject



66
67
68
69
70
71
72
73
# File 'lib/orbacle/lang_server.rb', line 66

def log_errors
  begin
    yield
  rescue => e
    logger.error(e)
    raise
  end
end