Autocomplete for filenames

This commit is contained in:
bleeptrack 2020-09-28 16:52:00 +02:00
parent ec537a3a1f
commit d498c2340b

View file

@ -150,17 +150,32 @@ func sort_by_frequency_desc(a, b):
return a[1] > b[1] return a[1] > b[1]
func generate_completions(command): func generate_completions(command):
var results = []
# Collect git commands.
if command.substr(0, 4) == "git ": if command.substr(0, 4) == "git ":
var rest = command.substr(4) var rest = command.substr(4)
var subcommands = relevant_subcommands() var subcommands = relevant_subcommands()
var results = []
for sc in subcommands: for sc in subcommands:
if sc.substr(0, rest.length()) == rest: if sc.substr(0, rest.length()) == rest:
results.push_back("git "+sc) results.push_back("git "+sc)
return results # Part 1: Only autocomplete after git subcommand.
return [] # Part2: Prevent autocompletion to only show filename at the beginning of a command.
if !(command.substr(0,4) == "git " and command.split(" ").size() <= 2) and command.split(" ").size() > 1:
var last_word = Array(command.split(" ")).pop_back()
var file_string = repository.shell.run("find -type f")
var files = file_string.split("\n")
files = Array(files)
# The last entry is an empty string, remove it.
files.pop_back()
for file_path in files:
file_path = file_path.substr(2)
if file_path.substr(0,4) != ".git" and file_path.substr(0,last_word.length()) == last_word:
results.push_back(command+file_path.substr(last_word.length()))
return results
func _input_changed(new_text): func _input_changed(new_text):
call_deferred("regenerate_completions_menu", new_text) call_deferred("regenerate_completions_menu", new_text)