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 Method Summary collapse

Constructor Details

#initialize(server_name) ⇒ 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 defaults to using the current working directory as the server identifier.

Parameters:

  • server_name (String, nil)

    The name of the Vim server to connect to. If nil or empty, defaults to the current working directory path in uppercase



20
21
22
23
# File 'lib/ollama_chat/vim.rb', line 20

def initialize(server_name)
  server_name.full? or server_name = default_server_name
  @server_name = server_name
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.



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

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

#default_server_nameObject

The default server name is derived from the current working directory It converts the absolute path to uppercase for consistent identification This approach ensures each working directory gets a unique server identifier The server name format makes it easy to distinguish different Vim sessions



29
30
31
# File 'lib/ollama_chat/vim.rb', line 29

def default_server_name
  Pathname.pwd.to_s.upcase
end

#insert(text) ⇒ Object

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



40
41
42
43
44
45
46
47
48
# File 'lib/ollama_chat/vim.rb', line 40

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