Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions lib/samovar/completion/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,21 @@ class Context
# @parameter environment [Hash] The environment for completion callbacks.
# @returns [Context] The completion context.
def self.for(command_class, arguments, environment: ENV)
arguments = arguments.dup
current = arguments.pop || ""

return self.new(
command_class.table.merged,
arguments,
arguments.last || "",
current,
environment: environment,
)
end

# Initialize a new completion context.
#
# @parameter table [Table] The command table to complete.
# @parameter arguments [Array(String)] The truncated command-line arguments.
# @parameter arguments [Array(String)] The completed words before the current token.
# @parameter current [String] The token being completed.
# @parameter row [Object | Nil] The parser row whose value is being completed.
# @parameter environment [Hash] The environment for completion callbacks.
Expand All @@ -42,7 +45,7 @@ def initialize(table, arguments, current, row = nil, environment: ENV)
# @attribute [Table] The command table to complete.
attr :table

# @attribute [Array(String)] The truncated command-line arguments.
# @attribute [Array(String)] The completed words before the current token.
attr :arguments

# @attribute [String] The token being completed.
Expand All @@ -68,18 +71,11 @@ def with_row(row)
)
end

# The completed words before the current token.
#
# @returns [Array(String)] The arguments before the token being completed.
def words
@arguments.take(@arguments.size - 1)
end

# Complete the current command class.
#
# @returns [Result] The completion result.
def complete
complete_rows(@table, words)
complete_rows(@table, @arguments.dup)
end

# Complete the given command class with completed words.
Expand Down
2 changes: 1 addition & 1 deletion lib/samovar/split.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def complete(input, context, collected)
input.first,
description: "Delegate completion",
type: :delegate,
index: context.words.index(@marker) + 1,
index: context.arguments.index(@marker) + 1,
),
])
end
Expand Down
20 changes: 20 additions & 0 deletions test/samovar/completion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -284,4 +284,24 @@ def complete(input, **options)
expect(output.string).to be(:include?, "command\tleaf\tLeaf command.\n")
expect(output.string).to be(:include?, "option\t--verbose\tEnable verbose output.\n")
end

it "splits completed arguments from the current token" do
arguments = ["leaf", "--ver"]

context = Samovar::Completion::Context.for(CompletionTop, arguments)

expect(context.arguments).to be == ["leaf"]
expect(context.current).to be == "--ver"
expect(arguments).to be == ["leaf", "--ver"]
end

it "completes with no arguments" do
output = StringIO.new

result = CompletionTop.complete([], output: output)

expect(values(result)).to be(:include?, "leaf")
expect(output.string).to be(:include?, "command\tleaf\tLeaf command.\n")
expect(output.string).to be(:include?, "option\t--verbose\tEnable verbose output.\n")
end
end
Loading