-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2194c04
commit f196e3c
Showing
6 changed files
with
198 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
require "json" | ||
require "./arguments" | ||
require "./command-builder" | ||
|
||
class Kakoune::CompletionBuilder | ||
include Arguments | ||
|
||
# Aliases | ||
alias Command = Array(String) | ||
alias Candidate = { String, Array(Command), String } | ||
|
||
# Properties | ||
property name : String | ||
property line : Int32 | ||
property column : Int32 | ||
property length : Int32 | ||
property timestamp : Int32 | ||
|
||
# Constructor | ||
property constructor = [] of Candidate | ||
|
||
# Creates a new builder. | ||
def initialize(@name, @line, @column, @length, @timestamp) | ||
end | ||
|
||
# Creates a new builder, with its configuration specified in the block. | ||
def self.new(name, line, column, length, timestamp, &block) | ||
new(name, line, column, length, timestamp).tap do |builder| | ||
yield builder | ||
end | ||
end | ||
|
||
# Builds command from block. | ||
def self.build(name, line, column, length, timestamp, &block : self ->) | ||
new(name, line, column, length, timestamp, &block).build | ||
end | ||
|
||
# Adds a single candidate. | ||
def add(text : String, command : Array(Command), menu : String) | ||
constructor.push({ text, command, menu }) | ||
end | ||
|
||
# Adds multiple candidates. | ||
def add(candidates : Array(Candidate)) | ||
constructor.concat(candidates) | ||
end | ||
|
||
# Adds candidates from a JSON stream. | ||
def add(io : IO) | ||
add(Array(Candidate).from_json(io)) | ||
end | ||
|
||
# Builds the completion command. | ||
def build | ||
String.build do |string| | ||
string << quote("set-option", "window", name, build_header(line, column, length, timestamp)) << " " | ||
|
||
constructor.each do |text, select_command, menu_text| | ||
string << quote(build_candidate(text, select_command, menu_text)) << " " | ||
end | ||
end | ||
end | ||
|
||
private def build_header(line, column, length, timestamp) | ||
"#{line}.#{column}+#{length}@#{timestamp}" | ||
end | ||
|
||
private def escape_field(field) | ||
field.gsub({ '|' => "\\|", '\\' => "\\\\" }) | ||
end | ||
|
||
private def build_candidate(text, command, menu) | ||
select_command = CommandBuilder.build do |builder| | ||
builder.add(command) | ||
end | ||
|
||
# Escape fields | ||
text = escape_field(text) | ||
select_command = escape_field(select_command) | ||
menu_text = escape_field(menu) | ||
|
||
# Build candidate | ||
candidate = "#{text}|#{select_command}|#{menu_text}" | ||
end | ||
end |