Class: Kapusta::LSP

Inherits:
Object
  • Object
show all
Defined in:
lib/kapusta/lsp.rb,
lib/kapusta/lsp/rename.rb,
lib/kapusta/lsp/definition.rb,
lib/kapusta/lsp/formatting.rb,
lib/kapusta/lsp/identifier.rb,
lib/kapusta/lsp/diagnostics.rb,
lib/kapusta/lsp/scope_walker.rb,
lib/kapusta/lsp/workspace_index.rb

Defined Under Namespace

Modules: Definition, Diagnostics, Formatting, Identifier, Rename Classes: ScopeWalker, WorkspaceIndex

Constant Summary collapse

NOT_INITIALIZED =
-32_002
METHOD_NOT_FOUND =
-32_601
FULL_SYNC =
1

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input:, output:, log:) ⇒ LSP

Returns a new instance of LSP.



63
64
65
66
67
68
69
70
71
72
# File 'lib/kapusta/lsp.rb', line 63

def initialize(input:, output:, log:)
  @input = input.binmode
  @output = output.binmode
  @log = log
  @debug = LSP.debug?
  @sources = {}
  @workspace_index = WorkspaceIndex.new
  @initialized = false
  @shutdown = false
end

Class Method Details

.debug?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/kapusta/lsp.rb', line 18

def self.debug?
  %w[1 true yes on].include?(ENV['KAPUSTA_LS_DEBUG'].to_s.downcase)
end

.debug_log(log, message) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/kapusta/lsp.rb', line 43

def self.debug_log(log, message)
  return unless debug?

  log.write("kapusta-ls[debug pid=#{Process.pid}]: #{message}\n")
  log.flush
rescue StandardError
  nil
end

.install_signal_handlers(log) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/kapusta/lsp.rb', line 30

def self.install_signal_handlers(log)
  %w[TERM INT HUP].each do |sig|
    Signal.trap(sig) do
      debug_log(log, "signal #{sig} received, exiting")
      exit!(0)
    rescue StandardError
      exit!(0)
    end
  rescue ArgumentError
    nil
  end
end

.start(input: $stdin, output: $stdout, log: $stderr) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/kapusta/lsp.rb', line 22

def self.start(input: $stdin, output: $stdout, log: $stderr)
  server = new(input:, output:, log:)
  install_signal_handlers(log)
  server.run
  debug_log(log, 'run returned, calling exit!(0)')
  exit!(0)
end

.uri_to_path(uri) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/kapusta/lsp.rb', line 52

def self.uri_to_path(uri)
  return unless uri.is_a?(String)

  parsed = URI.parse(uri)
  return URI::DEFAULT_PARSER.unescape(parsed.path) if parsed.scheme == 'file'

  uri
rescue URI::InvalidURIError
  nil
end

Instance Method Details

#runObject



74
75
76
77
78
79
80
# File 'lib/kapusta/lsp.rb', line 74

def run
  debug('run loop start')
  until (message = read_message).nil?
    handle(message)
  end
  debug('stdin EOF, run loop exiting')
end