Module: Ibex::LSP::RequestSupport

Included in:
DocumentHandlers, InitializationHandlers, NavigationHandlers, RequestHandlers
Defined in:
lib/ibex/lsp/request_support.rb,
sig/ibex/lsp/request_support.rbs

Overview

Shared publication, state, and parameter validation for request handlers.

Instance Method Summary collapse

Instance Method Details

#hash_member(value, name) ⇒ Hash[String, untyped]

RBS:

  • (Hash[String, untyped] value, String name) -> Hash[String, untyped]

Parameters:

  • value (Hash[String, untyped])
  • name (String)

Returns:

  • (Hash[String, untyped])


41
42
43
44
45
46
# File 'lib/ibex/lsp/request_support.rb', line 41

def hash_member(value, name)
  member = value[name]
  return member if member.is_a?(Hash)

  raise ProtocolError.new("#{name} must be an object", code: -32_602)
end

#integer_member(value, name) ⇒ Integer

RBS:

  • (Hash[String, untyped] value, String name) -> Integer

Parameters:

  • value (Hash[String, untyped])
  • name (String)

Returns:

  • (Integer)


57
58
59
60
61
62
# File 'lib/ibex/lsp/request_support.rb', line 57

def integer_member(value, name)
  member = value[name]
  return member if member.is_a?(Integer)

  raise ProtocolError.new("#{name} must be an integer", code: -32_602)
end

#params_hash(value) ⇒ Hash[String, untyped]

RBS:

  • (untyped value) -> Hash[String, untyped]

Parameters:

  • value (Object)

Returns:

  • (Hash[String, untyped])


33
34
35
36
37
38
# File 'lib/ibex/lsp/request_support.rb', line 33

def params_hash(value)
  return {} if value.nil?
  return value if value.is_a?(Hash)

  raise ProtocolError.new("params must be an object", code: -32_602)
end

#publish(publications) ⇒ void

This method returns an undefined value.

RBS:

  • (Array[DocumentStore::publication] publications) -> void

Parameters:

  • publications (Array[DocumentStore::publication])


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/ibex/lsp/request_support.rb', line 10

def publish(publications)
  publications.each do |publication|
    version = publication[:version]
    next if version && stale_publication?(publication.fetch(:uri), version)

    params = {
      "uri" => publication.fetch(:uri),
      "diagnostics" => publication.fetch(:diagnostics)
    } #: Hash[String, untyped]
    params["version"] = version if version
    @transport.write_message(
      "jsonrpc" => "2.0", "method" => "textDocument/publishDiagnostics", "params" => params
    )
  end
end

#require_running!void

This method returns an undefined value.

RBS:

  • () -> void



72
73
74
# File 'lib/ibex/lsp/request_support.rb', line 72

def require_running!
  require_state!(:running)
end

#require_state!(state) ⇒ void

This method returns an undefined value.

RBS:

  • (Symbol state) -> void

Parameters:

  • state (Symbol)


65
66
67
68
69
# File 'lib/ibex/lsp/request_support.rb', line 65

def require_state!(state)
  return if @state == state

  raise ProtocolError.new("request requires #{state} server state", code: -32_600)
end

#stale_publication?(uri, version) ⇒ Boolean

RBS:

  • (String uri, Integer version) -> bool

Parameters:

  • uri (String)
  • version (Integer)

Returns:

  • (Boolean)


27
28
29
30
# File 'lib/ibex/lsp/request_support.rb', line 27

def stale_publication?(uri, version)
  path = workspace.path(uri)
  store.snapshot_for(path)&.fetch(:version) != version
end

#storeDocumentStore

RBS:

  • () -> DocumentStore

Returns:



77
78
79
# File 'lib/ibex/lsp/request_support.rb', line 77

def store
  @store || raise(ProtocolError.new("server is not initialized", code: -32_002))
end

#string_member(value, name) ⇒ String

RBS:

  • (Hash[String, untyped] value, String name) -> String

Parameters:

  • value (Hash[String, untyped])
  • name (String)

Returns:

  • (String)


49
50
51
52
53
54
# File 'lib/ibex/lsp/request_support.rb', line 49

def string_member(value, name)
  member = value[name]
  return member if member.is_a?(String)

  raise ProtocolError.new("#{name} must be a string", code: -32_602)
end

#workspaceWorkspace

RBS:

  • () -> Workspace

Returns:



82
83
84
# File 'lib/ibex/lsp/request_support.rb', line 82

def workspace
  @workspace || raise(ProtocolError.new("server is not initialized", code: -32_002))
end