-
-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support tab completion for variables.
This commit introduces a `Gitsh::TabCompletion::VariableCompleter` object. This is completely separate from the NFA-based completion we use for most things, because: - we immediately know we need to use variable completion based on lexical analysis of the input without having to walk through the NFA's states, and - the context in which the variable appears doesn't inform the completions we present the user with. The `Gitsh::TabCompletion::Facade` object gains the responsibility of deciding which type of completion to invoke, based on information from the `Gitsh::TabCompletion::Context` object. To load the config variables, we try to use the `git status --list --name-only` command. However, this command was only introduced in Git 2.6, and versions back to 2.4 are still officially supported by the Git team. If running the command with `--name-only` fails we fall back to parsing the output of `git status --list`. We don't use the backward compatible version everywhere, because the parsing won't be perfect for multi-line config values (`--name-only` was introduced to address this problem).
- Loading branch information
1 parent
1b673e5
commit 33014f7
Showing
14 changed files
with
354 additions
and
10 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
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,51 @@ | ||
module Gitsh | ||
module TabCompletion | ||
class VariableCompleter | ||
def initialize(line_editor, input, env) | ||
@line_editor = line_editor | ||
@input = input | ||
@env = env | ||
end | ||
|
||
def call | ||
line_editor.completion_append_character = completion_append_character | ||
line_editor.completion_suppress_quote = true | ||
|
||
matches | ||
end | ||
|
||
private | ||
|
||
attr_reader :line_editor, :input, :env | ||
|
||
def completion_append_character | ||
if prefix.end_with?('{') | ||
'}' | ||
else | ||
nil | ||
end | ||
end | ||
|
||
def matches | ||
env.available_variables. | ||
select { |name| name.to_s.start_with?(partial_name) }. | ||
map { |name| "#{prefix}#{name}" } | ||
end | ||
|
||
def prefix | ||
parse_input.first | ||
end | ||
|
||
def partial_name | ||
parse_input.last | ||
end | ||
|
||
def parse_input | ||
@parse_input ||= ( | ||
parts = input.rpartition(/\$\{?/) | ||
[parts[0...-1].join, parts.last] | ||
) | ||
end | ||
end | ||
end | ||
end |
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
Oops, something went wrong.