Module: Steep::Server::ChangeBuffer

Included in:
InteractionWorker, TypeCheckWorker
Defined in:
lib/steep/server/change_buffer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#buffered_changesObject (readonly)

Returns the value of attribute buffered_changes.



5
6
7
# File 'lib/steep/server/change_buffer.rb', line 5

def buffered_changes
  @buffered_changes
end

#mutexObject (readonly)

Returns the value of attribute mutex.



4
5
6
# File 'lib/steep/server/change_buffer.rb', line 4

def mutex
  @mutex
end

Instance Method Details

#collect_changes(request) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/steep/server/change_buffer.rb', line 41

def collect_changes(request)
  push_buffer do |changes|
    if path = Steep::PathHelper.to_pathname(request[:params][:textDocument][:uri])
      path = project.relative_path(path)
      version = request[:params][:textDocument][:version]
      Steep.logger.info { "Updating source: path=#{path}, version=#{version}..." }

      changes[path] ||= []
      request[:params][:contentChanges].each do |change|
        changes.fetch(path) << Services::ContentChange.new(
          range: change[:range]&.yield_self {|range|
            [
              range[:start].yield_self {|pos| Services::ContentChange::Position.new(line: pos[:line] + 1, column: pos[:character]) },
              range[:end].yield_self {|pos| Services::ContentChange::Position.new(line: pos[:line] + 1, column: pos[:character]) }
            ]
          },
          text: change[:text]
        )
      end
    end
  end
end

#load_files(input) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/steep/server/change_buffer.rb', line 27

def load_files(input)
  Steep.logger.tagged "#load_files" do
    push_buffer do |changes|
      input.each do |filename, content|
        if content.is_a?(Hash)
          base64_decoded = content[:text].unpack1("m") #: String
          content = base64_decoded.force_encoding(Encoding::UTF_8)
        end
        changes[Pathname(filename.to_s)] = [Services::ContentChange.new(text: content)]
      end
    end
  end
end

#pop_bufferObject



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/steep/server/change_buffer.rb', line 13

def pop_buffer
  changes = mutex.synchronize do
    copy = buffered_changes.dup
    buffered_changes.clear
    copy
  end

  if block_given?
    yield changes
  else
    changes
  end
end

#push_bufferObject



7
8
9
10
11
# File 'lib/steep/server/change_buffer.rb', line 7

def push_buffer
  mutex.synchronize do
    yield buffered_changes
  end
end

#reset_change(uri:, text:) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/steep/server/change_buffer.rb', line 64

def reset_change(uri:, text:)
  push_buffer do |changes|
    if path = Steep::PathHelper.to_pathname(uri)
      path = project.relative_path(path)
      changes[path] = [Services::ContentChange.new(text: text)]
    end
  end
end