Class: NvimMcpServer::GetProjectBuffersTool

Inherits:
BaseTool
  • Object
show all
Defined in:
lib/nvim-mcp-server/tools/get_project_buffers_tool.rb

Instance Method Summary collapse

Instance Method Details

#call(project_name:) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/nvim-mcp-server/tools/get_project_buffers_tool.rb', line 11

def call(project_name:)
  socket_path = "/tmp/nvim-#{project_name}.sock"
  log(:info, "Getting buffers for project #{project_name} using socket #{socket_path}")

  result = {
    status: "success",
    project_name: project_name,
    socket_path: socket_path,
    files: []
  }

  begin
    # Check if socket exists
    unless File.exist?(socket_path)
      result[:status] = "error"
      result[:message] = "Neovim socket not found: #{socket_path}"
      return result
    end

    # Connect to Neovim
    client = Neovim.attach_unix(socket_path)

    # Get all buffers
    all_buffers = client.list_bufs
    project_files = []

    all_buffers.each do |buffer|
      # Get buffer name (file path)
      buffer_name = buffer.name

      # Skip unnamed buffers
      next if buffer_name.nil? || buffer_name.empty?

      # Split the path into components
      path_parts = buffer_name.split(File::SEPARATOR)

      # Check if project_name appears in any part of the path
      if path_parts.include?(project_name)
        # Add the complete path to the results
        project_files << buffer_name
      end
    rescue => e
      # Log error but continue processing other buffers
      log(:error, "Error processing buffer: #{e.message}")
      next
    end

    # Sort the files for consistent output
    result[:files] = project_files.sort

    # Close the connection
    client.shutdown
  rescue => e
    log(:error, "Error connecting to Neovim using socket #{socket_path}: #{e.message}")

    # Handle connection and other errors gracefully
    result[:status] = "error"
    result[:message] = e.message
    result[:files] = []
  end

  result
end