Class: OllamaChat::Vim

Inherits:
Object
  • Object
show all
Defined in:
lib/ollama_chat/vim.rb

Overview

A class that provides functionality for inserting text into Vim buffers via remote communication.

Examples:

vim = OllamaChat::Vim.new("MY_SERVER")
vim.insert("Hello, Vim!")

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server_name = nil, clientserver: nil) ⇒ OllamaChat::Vim

Initializes a new Vim server connection

Creates a new OllamaChat::Vim instance for interacting with a specific Vim server. If no server name is provided, it derives a standardized server name based on the current working directory using the default_server_name method.

Parameters:

  • server_name (String, nil) (defaults to: nil)

    The name of the Vim server to connect to. If nil or empty, defaults to a server name derived from the current working directory using default_server_name

  • clientserver (String) (defaults to: nil)

    The clientserver protocol to use, defaults to ‘socket’



25
26
27
28
29
# File 'lib/ollama_chat/vim.rb', line 25

def initialize(server_name = nil, clientserver: nil)
  server_name.full? or server_name = self.class.default_server_name
  @server_name  = server_name
  @clientserver = clientserver || 'socket'
end

Instance Attribute Details

#clientserverString (readonly)

The clientserver attribute reader returns the clientserver protocol to be used.

Returns:

  • (String)

    the clientserver protocol identifier



41
42
43
# File 'lib/ollama_chat/vim.rb', line 41

def clientserver
  @clientserver
end

#server_nameString (readonly)

The server_name attribute reader returns the name of the Vim server to connect to.

Returns:

  • (String)

    the name of the Vim server



35
36
37
# File 'lib/ollama_chat/vim.rb', line 35

def server_name
  @server_name
end

Class Method Details

.default_server_name(name = Dir.pwd) ⇒ String

The default_server_name method generates a standardized server name based on a given name or the current working directory.

This method creates a unique server identifier by combining the basename of the provided name (or current working directory) with a truncated MD5 hash digest of the full path. The resulting name is converted to uppercase for consistent formatting.

Parameters:

  • name (String) (defaults to: Dir.pwd)

    the base name to use for server identification defaults to the current working directory

Returns:

  • (String)

    a formatted server name suitable for use with Vim server connections



56
57
58
59
60
61
# File 'lib/ollama_chat/vim.rb', line 56

def self.default_server_name(name = Dir.pwd)
  prefix = File.basename(name)
  suffix = Digest::MD5.hexdigest(name)[0, 8]
  name = [ prefix, suffix ] * ?-
  name.upcase
end

Instance Method Details

#colObject

Returns the current column position of the cursor in the Vim server

This method queries the specified Vim server for the current cursor position using Vim’s remote expression feature. It executes a Vim command that returns the result of ‘col(’.‘)`, which represents the current column number (1-indexed) of the cursor position.



91
92
93
# File 'lib/ollama_chat/vim.rb', line 91

def col
  `vim --clientserver "#@clientserver" --servername "#@server_name" --remote-expr "col('.')"`.chomp.to_i
end

#insert(text) ⇒ OllamaChat::Vim?

Inserts text at the current cursor position in Vim

This method writes the provided text to a temporary file and uses Vim’s remote-send functionality to insert it at the current cursor position. The text is automatically indented to match the current column position.

Parameters:

  • text (String)

    The text to be inserted into the Vim buffer

Returns:



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ollama_chat/vim.rb', line 71

def insert(text)
  spaces = (col - 1).clamp(0..)
  text   = text.gsub(/^/, ' ' * spaces)
  Tempfile.open do |tmp|
    tmp.write(text)
    tmp.flush
    result = system %{
      vim --clientserver "#@clientserver" --servername "#@server_name" --remote-send "<ESC>:r #{tmp.path}<CR>"
    }
    report_error(result) and return
  end
  self
end

#open_file(file_path, start_line = nil, end_line = nil) ⇒ OllamaChat::Vim?

The open_file method opens a file in Vim at a specified line and optionally marks a range.

This method sends a command to a running Vim server to open a file at a given line number. If an end line is provided, it also marks the range between start and end lines. The method ensures the Vim server is running before attempting to send commands.

Parameters:

  • file_path (String)

    the path to the file to be opened in Vim

  • start_line (Integer) (defaults to: nil)

    the line number to start at (defaults to 1)

  • end_line (Integer, nil) (defaults to: nil)

    the line number to end at, if marking a range

Returns:

  • (OllamaChat::Vim, nil)

    returns self if successful, nil if failed

  • (nil)

    returns nil if the Vim server is not running

  • (nil)

    returns nil if the system command fails



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/ollama_chat/vim.rb', line 123

def open_file(file_path, start_line = nil, end_line = nil)
  start_line ||= 1
  unless server_running?
    STDERR.puts <<~EOT
        Failed! Vim has to be running with server name "#@server_name"!
    EOT
    return
  end
  cmd = %{
    vim --clientserver "#@clientserver" --servername "#@server_name" --remote +#{start_line} "#{file_path}"
  }
  result = system(cmd)
  report_error(result) and return
  if end_line
    mark_range = "<ESC>:normal #{start_line}GV#{end_line}G<CR>"
    cmd = %{
      vim --clientserver "#@clientserver" --servername "#@server_name" --remote-send #{mark_range.inspect}
    }
    result = system(cmd)
    report_error(result) and return
  else
    center = "<ESC>zz"
    cmd = %{
      vim --clientserver "#@clientserver" --servername "#@server_name" --remote-send #{center.inspect}
    }
    result = system(cmd)
    report_error(result) and return
  end
  self
end

#report_error(result) ⇒ true, false

The report_error method handles error reporting for Vim server operations.

This method checks if a system command result indicates failure and outputs an appropriate error message to standard error when the command fails.

Parameters:

  • result (true, false)

    the result of a system command execution

Returns:

  • (true, false)

    returns true if the command failed, false otherwise

  • (true, false)

    returns false if the command succeeded



163
164
165
166
167
168
169
170
171
# File 'lib/ollama_chat/vim.rb', line 163

def report_error(result)
  unless result
    STDERR.puts <<~EOT
        Failed! vim is required in path and running with server name "#@server_name".
    EOT
    true
  end
  false
end

#server_running?true, false

The server_running? method checks if a Vim server is currently running and accessible.

This method determines whether a Vim server with the configured server name is active by attempting to retrieve the current cursor column position. If the column position is greater than zero, it indicates that Vim is running and the server is accessible.

Returns:

  • (true, false)

    true if the Vim server is running and accessible, false otherwise



104
105
106
# File 'lib/ollama_chat/vim.rb', line 104

def server_running?
  col > 0
end