Class: PuppetLanguageServer::SessionState::DocumentStore

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet-languageserver/session_state/document_store.rb

Instance Method Summary collapse

Constructor Details

#initialize(_options = {}) ⇒ DocumentStore

Returns a new instance of DocumentStore.

Parameters:

  • options

    :workspace Path to the workspace



49
50
51
52
53
54
# File 'lib/puppet-languageserver/session_state/document_store.rb', line 49

def initialize(_options = {})
  @documents = {}
  @doc_mutex = Mutex.new

  initialize_store
end

Instance Method Details

#clearObject



71
72
73
# File 'lib/puppet-languageserver/session_state/document_store.rb', line 71

def clear
  @doc_mutex.synchronize { @documents.clear }
end

#document(uri, doc_version = nil) ⇒ Object



75
76
77
78
79
80
81
82
# File 'lib/puppet-languageserver/session_state/document_store.rb', line 75

def document(uri, doc_version = nil)
  @doc_mutex.synchronize do
    return nil if @documents[uri].nil?
    return nil unless doc_version.nil? || @documents[uri].version == doc_version

    @documents[uri]
  end
end

#document_content(uri, doc_version = nil) ⇒ Object



84
85
86
87
# File 'lib/puppet-languageserver/session_state/document_store.rb', line 84

def document_content(uri, doc_version = nil)
  doc = document(uri, doc_version)
  doc.nil? ? nil : doc.content.clone
end

#document_tokens(uri, doc_version = nil) ⇒ Object



94
95
96
97
98
99
100
101
102
# File 'lib/puppet-languageserver/session_state/document_store.rb', line 94

def document_tokens(uri, doc_version = nil)
  @doc_mutex.synchronize do
    return nil if @documents[uri].nil?
    return nil unless doc_version.nil? || @documents[uri].version == doc_version
    return @documents[uri].tokens unless @documents[uri].tokens.nil?

    return @documents[uri].calculate_tokens!
  end
end

#document_type(uri) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/puppet-languageserver/session_state/document_store.rb', line 113

def document_type(uri)
  case uri
  when %r{/Puppetfile$}i
    :puppetfile
  when /\.pp$/i
    :manifest
  when /\.epp$/i
    :epp
  else
    :unknown
  end
end

#document_urisObject



109
110
111
# File 'lib/puppet-languageserver/session_state/document_store.rb', line 109

def document_uris
  @doc_mutex.synchronize { @documents.keys.dup }
end

#document_version(uri) ⇒ Object



104
105
106
107
# File 'lib/puppet-languageserver/session_state/document_store.rb', line 104

def document_version(uri)
  doc = document(uri)
  doc.nil? ? nil : doc.version
end

#expire_store_informationObject



160
161
162
163
164
# File 'lib/puppet-languageserver/session_state/document_store.rb', line 160

def expire_store_information
  @doc_mutex.synchronize do
    @workspace_info_cache[:expires] = Time.new - 120
  end
end

#get_document(uri) ⇒ Object



89
90
91
92
# File 'lib/puppet-languageserver/session_state/document_store.rb', line 89

def get_document(uri)
  doc = @documents[uri]
  doc.nil? ? nil : doc.content.clone
end

#initialize_store(options = {}) ⇒ Object



153
154
155
156
157
158
# File 'lib/puppet-languageserver/session_state/document_store.rb', line 153

def initialize_store(options = {})
  @workspace_path = options[:workspace]
  @workspace_info_cache = {
    expires: Time.new - 120
  }
end

#plan_file?(uri) ⇒ Boolean

Plan files puppet.com/docs/bolt/1.x/writing_plans.html#concept-4485 can exist in many places The current best detection method is as follows: “Given the full path to the .pp file, if it contains a directory called plans, AND that plans is not a sub-directory of manifests, then it is a plan file”

See github.com/lingua-pupuli/puppet-editor-services/issues/129 for the full discussion

Returns:

  • (Boolean)


131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/puppet-languageserver/session_state/document_store.rb', line 131

def plan_file?(uri)
  uri_path = PuppetLanguageServer::UriHelper.uri_path(uri)
  return false if uri_path.nil?

  # For the text searching below we need a leading slash. That way
  # we don't need to use regexes which is slower
  uri_path = "/#{uri_path}" unless uri_path.start_with?('/')
  if windows?
    plans_index = uri_path.upcase.index('/PLANS/')
    manifests_index = uri_path.upcase.index('/MANIFESTS/')
  else
    plans_index = uri_path.index('/plans/')
    manifests_index = uri_path.index('/manifests/')
  end
  return false if plans_index.nil?
  return true if manifests_index.nil?

  plans_index < manifests_index
end

#remove_document(uri) ⇒ Object



66
67
68
69
# File 'lib/puppet-languageserver/session_state/document_store.rb', line 66

def remove_document(uri)
  @doc_mutex.synchronize { @documents.delete(uri) }
  nil
end

#set_document(uri, content, doc_version) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/puppet-languageserver/session_state/document_store.rb', line 56

def set_document(uri, content, doc_version)
  @doc_mutex.synchronize do
    if @documents[uri].nil?
      @documents[uri] = create_document(uri, content, doc_version)
    else
      @documents[uri].update(content, doc_version)
    end
  end
end

#store_has_environmentconf?Boolean

Returns:

  • (Boolean)


174
175
176
# File 'lib/puppet-languageserver/session_state/document_store.rb', line 174

def store_has_environmentconf?
  store_details[:has_environmentconf]
end

#store_has_module_metadata?Boolean

Returns:

  • (Boolean)


170
171
172
# File 'lib/puppet-languageserver/session_state/document_store.rb', line 170

def store_has_module_metadata?
  store_details[:has_metadatajson]
end

#store_root_pathObject



166
167
168
# File 'lib/puppet-languageserver/session_state/document_store.rb', line 166

def store_root_path
  store_details[:root_path]
end