Class: RubyLsp::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_lsp/store.rb

Defined Under Namespace

Classes: NonExistingDocumentError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(global_state) ⇒ Store

: (GlobalState global_state) -> void



12
13
14
15
16
# File 'lib/ruby_lsp/store.rb', line 12

def initialize(global_state)
  @global_state = global_state
  @state = {} #: Hash[String, Document[untyped]]
  @client_name = "Unknown" #: String
end

Instance Attribute Details

#client_nameObject

: String



9
10
11
# File 'lib/ruby_lsp/store.rb', line 9

def client_name
  @client_name
end

Instance Method Details

#cache_fetch(uri, request_name, &block) ⇒ Object

: [T] (URI::Generic uri, String request_name) { (Document document) -> T } -> T



90
91
92
# File 'lib/ruby_lsp/store.rb', line 90

def cache_fetch(uri, request_name, &block)
  get(uri).cache_fetch(request_name, &block)
end

#clearObject

: -> void



63
64
65
# File 'lib/ruby_lsp/store.rb', line 63

def clear
  @state.clear
end

#delete(uri) ⇒ Object

: (URI::Generic uri) -> void



73
74
75
# File 'lib/ruby_lsp/store.rb', line 73

def delete(uri)
  @state.delete(uri.to_s)
end

#each(&block) ⇒ Object

: { (String uri, Document document) -> void } -> void



83
84
85
86
87
# File 'lib/ruby_lsp/store.rb', line 83

def each(&block)
  @state.each do |uri, document|
    block.call(uri, document)
  end
end

#empty?Boolean

: -> bool

Returns:

  • (Boolean)


68
69
70
# File 'lib/ruby_lsp/store.rb', line 68

def empty?
  @state.empty?
end

#get(uri) ⇒ Object

: (URI::Generic uri) -> Document



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ruby_lsp/store.rb', line 19

def get(uri)
  document = @state[uri.to_s]
  return document unless document.nil?

  # For unsaved files (`untitled:Untitled-1` uris), there's no path to read from. If we don't have the untitled file
  # already present in the store, then we have to raise non existing document error
  path = uri.to_standardized_path
  raise NonExistingDocumentError, uri.to_s unless path

  ext = File.extname(path)
  language_id = case ext
  when ".erb", ".rhtml"
    :erb
  when ".rbs"
    :rbs
  else
    :ruby
  end

  set(uri: uri, source: File.binread(path), version: 0, language_id: language_id)
  @state[uri.to_s] #: as !nil
rescue Errno::ENOENT
  raise NonExistingDocumentError, uri.to_s
end

#key?(uri) ⇒ Boolean

: (URI::Generic uri) -> bool

Returns:

  • (Boolean)


78
79
80
# File 'lib/ruby_lsp/store.rb', line 78

def key?(uri)
  @state.key?(uri.to_s)
end

#push_edits(uri:, edits:, version:) ⇒ Object

: (uri: URI::Generic, edits: Array[Hash[Symbol, untyped]], version: Integer) -> void



57
58
59
60
# File 'lib/ruby_lsp/store.rb', line 57

def push_edits(uri:, edits:, version:)
  @state[uri.to_s] #: as !nil
    .push_edits(edits, version: version)
end

#set(uri:, source:, version:, language_id:) ⇒ Object

: (uri: URI::Generic, source: String, version: Integer, language_id: Symbol) -> Document



45
46
47
48
49
50
51
52
53
54
# File 'lib/ruby_lsp/store.rb', line 45

def set(uri:, source:, version:, language_id:)
  @state[uri.to_s] = case language_id
  when :erb
    ERBDocument.new(source: source, version: version, uri: uri, global_state: @global_state)
  when :rbs
    RBSDocument.new(source: source, version: version, uri: uri, global_state: @global_state)
  else
    RubyDocument.new(source: source, version: version, uri: uri, global_state: @global_state)
  end
end