Skip to content

Commit

Permalink
Merge pull request #98 from TheFriendlyCoder/tab_expansion_fix2
Browse files Browse the repository at this point in the history
Fixes #97 Fixed command completion with params with similar content
  • Loading branch information
TheFriendlyCoder committed Nov 15, 2018
2 parents f297f6d + 6849839 commit dddbd86
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/friendlyshell/command_complete_mixin.py
Expand Up @@ -176,8 +176,9 @@ def _get_callback_param_index(self, parser, original_line, token):
original_line[-1] == " " and not contains_quotes:
return len(parser.params)

partial_line = parser.command
for i in range(len(parser.params)):
offset = original_line.find(parser.params[i], len(parser.command))
offset = original_line.find(parser.params[i], len(partial_line))
self.debug(
"\tSeeing if token %s is the one to match", parser.params[i])
self.debug("\t\tMatching offset is %s", begidx)
Expand All @@ -198,6 +199,8 @@ def _get_callback_param_index(self, parser, original_line, token):
param_index = i
break

partial_line = original_line[:offset + len(parser.params[i])]

# Sanity check
# By the time we get here we should have deduced the token location
# within the input line
Expand Down
31 changes: 31 additions & 0 deletions tests/test_command_complete_mixin.py
Expand Up @@ -232,6 +232,37 @@ def complete_something(self, params, index):
assert matches == expected_matches[0]


@patch("friendlyshell.command_complete_mixin.readline")
def test_complete_repeated_parameter(mock_readline):
expected_matches = ["FuBar2"]

class MyShell(BaseShell, CommandCompleteMixin, BasicLoggerMixin):
completion_called = False
def do_something(self):
pass

def complete_something(self, params, index):
MyShell.completion_called = True
assert len(params) == 2
assert index == 1
assert params[0] == "FuBar1"
assert params[1] == "FuBar"

return expected_matches

input_line = "something FuBar1 FuBar"
mock_readline.get_line_buffer.return_value = input_line
mock_readline.get_begidx.return_value = len("something FuBar1 ")
mock_readline.get_endidx.return_value = len(input_line)

obj = MyShell()

matches = obj._complete_callback("", 0)
assert MyShell.completion_called
assert matches == expected_matches[0]



@patch("friendlyshell.command_complete_mixin.readline")
def test_complete_single_quote(mock_readline):
matches = ["'My Van'"]
Expand Down

0 comments on commit dddbd86

Please sign in to comment.