Module: Ibex::LSP::DocumentStoreValidation

Included in:
DocumentStore
Defined in:
lib/ibex/lsp/document_store_validation.rb,
sig/ibex/lsp/document_store_validation.rbs

Overview

Validates versions, source bounds, and prospective multi-file workspace edits.

Instance Method Summary collapse

Instance Method Details

#disk_path_for_close(path, uri) ⇒ String?

Resolve disk independently from overlay aliases before removing an open buffer.

RBS:

  • (String path, String uri) -> String?

Parameters:

  • path (String)
  • uri (String)

Returns:

  • (String, nil)


48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ibex/lsp/document_store_validation.rb', line 48

def disk_path_for_close(path, uri)
  # @type self: DocumentStore
  canonical = loader.disk_path(path)
  return canonical if workspace.root_for(canonical)

  raise ProtocolError.new("document disk target is outside workspace roots: #{uri}", code: -32_602)
rescue Errno::ENOENT, Errno::ENOTDIR
  nil
rescue SystemCallError => e
  raise ProtocolError.new("cannot resolve document disk target #{uri.inspect}: #{e.message}", code: -32_602)
end

#parse_candidate(path, source) ⇒ void

This method returns an undefined value.

RBS:

  • (String path, String source) -> void

Parameters:

  • path (String)
  • source (String)


42
43
44
# File 'lib/ibex/lsp/document_store_validation.rb', line 42

def parse_candidate(path, source)
  Frontend::Parser.new(source, file: path, mode: :extended).parse_source_document
end

#valid_replacements?(replacements) ⇒ Boolean

RBS:

  • (Hash[String, String] replacements) -> bool

Parameters:

  • replacements (Hash[String, String])

Returns:

  • (Boolean)


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

def valid_replacements?(replacements)
  # @type self: DocumentStore
  candidate = Frontend::SourceLoader.new
  @snapshots.each_value do |entry|
    candidate.set_overlay(entry.fetch(:path), entry.fetch(:source)) if entry.fetch(:open)
  end
  replacements.each do |path, source|
    candidate.set_overlay(path, source)
    parse_candidate(path, source)
  end
  roots = replacements.keys.flat_map { |path| affected_roots(path) }.uniq
  roots.each { |root| Frontend::Resolver.new(root, mode: :extended, loader: candidate).resolve }
  true
rescue Ibex::Error, SystemCallError
  false
end

#validate_source(source) ⇒ void

This method returns an undefined value.

RBS:

  • (String source) -> void

Parameters:

  • source (String)


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

def validate_source(source)
  raise ProtocolError.new("document text must be a string", code: -32_602) unless source.is_a?(String)
  return if source.bytesize <= Limits::MAX_DOCUMENT_BYTES

  raise ProtocolError.new("document exceeds #{Limits::MAX_DOCUMENT_BYTES} bytes", code: -32_602)
end

#validate_version(version) ⇒ void

This method returns an undefined value.

RBS:

  • (Integer version) -> void

Parameters:

  • version (Integer)


28
29
30
31
# File 'lib/ibex/lsp/document_store_validation.rb', line 28

def validate_version(version)
  raise ProtocolError.new("document version must be a non-negative integer", code: -32_602) unless
    version.is_a?(Integer) && !version.negative?
end