Class: Kward::NativeShell::Completion
- Inherits:
-
Object
- Object
- Kward::NativeShell::Completion
- Defined in:
- lib/kward/shell/native_completion.rb
Overview
Complete literal words without expanding the draft or executing shell code.
Defined Under Namespace
Classes: Result
Constant Summary collapse
- COMMAND_BOUNDARIES =
%w[; && || | & ( ) { } ;; ;&].freeze
- COMMAND_PREFIXES =
%w[! if then elif else while until do].freeze
- REDIRECTIONS =
%w[< > << <<- >> <> >| <& >&].freeze
Instance Method Summary collapse
- #complete(input, cursor, &programmed) ⇒ Object
-
#initialize(directory:, variables:, commands:) ⇒ Completion
constructor
A new instance of Completion.
Constructor Details
#initialize(directory:, variables:, commands:) ⇒ Completion
Returns a new instance of Completion.
12 13 14 |
# File 'lib/kward/shell/native_completion.rb', line 12 def initialize(directory:, variables:, commands:) @directory, @variables, @commands = directory, variables, commands end |
Instance Method Details
#complete(input, cursor, &programmed) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/kward/shell/native_completion.rb', line 16 def complete(input, cursor, &programmed) source = input[0...cursor] tokens = lex(source) current = tokens.pop if tokens.last&.value.is_a?(Word) && tokens.last.finish >= cursor start = current ? current.start : cursor raw = source[start..] prefix = literal_text(current) if current return if current && !prefix return unless current || source[tokens.last&.finish || 0..].match?(/\A[ \t\r]*\z/) prefix ||= "" words, redirect = command_context(tokens) command = words.first || ("" unless words.empty?) command_position = command.nil? && !redirect tilde = raw.start_with?("~/") names = programmed.call(words + [prefix]) if programmed && words.first && !redirect && words.none?(&:nil?) if names names = names.select { |name| candidate?(name, prefix) }.uniq.sort tilde = false elsif command_position && !prefix.include?("/") names = command_candidates(prefix) else names = path_candidates(prefix, tilde: tilde, directories_only: command == "cd" && !redirect, executable: command_position) end return if names.empty? match = names.one? ? names.first : common_prefix(names) finish = word_end(input, current, cursor) format = ->(name) { candidate_text(name, tilde: tilde, command: command_position && !prefix.include?("/")) } choices = names.map do |name| text = format.call(name) text += " " if finish == input.length && !name.end_with?("/") text end extend_word = names.one? || match.length > prefix.length Result.new(range: start...(extend_word ? finish : cursor), replacement: names.one? ? choices.first : (extend_word ? format.call(match) : raw), candidates: names, choices: choices, choice_range: start...finish) rescue SyntaxError, ArgumentError, EncodingError, SystemCallError nil end |