summaryrefslogtreecommitdiffhomepage
path: root/dragon/autocomplete.rb
diff options
context:
space:
mode:
authorAmir Rajan <[email protected]>2021-01-18 12:08:34 -0600
committerAmir Rajan <[email protected]>2021-01-18 12:08:34 -0600
commita4b9c048a1d751f5226833bb0c527ba1a8ac5d09 (patch)
tree3f2535e7a6272e796d50e7f07c906d4c9eb1b14a /dragon/autocomplete.rb
parenta24a71805b1924ae7f80776c736f94575c171d2c (diff)
downloaddragonruby-game-toolkit-contrib-a4b9c048a1d751f5226833bb0c527ba1a8ac5d09.tar.gz
dragonruby-game-toolkit-contrib-a4b9c048a1d751f5226833bb0c527ba1a8ac5d09.zip
Synced with 2.3.
Diffstat (limited to 'dragon/autocomplete.rb')
-rw-r--r--dragon/autocomplete.rb131
1 files changed, 131 insertions, 0 deletions
diff --git a/dragon/autocomplete.rb b/dragon/autocomplete.rb
new file mode 100644
index 0000000..f5f03b4
--- /dev/null
+++ b/dragon/autocomplete.rb
@@ -0,0 +1,131 @@
+# Copyright 2019 DragonRuby LLC
+# MIT License
+# autocomplete.rb has been released under MIT (*only this file*).
+
+module GTK
+ class Runtime
+ module Autocomplete
+ def autocomplete_parse opts
+ if opts[:file] && !opts[:text]
+ opts[:text] = read_file opts[:file]
+ end
+
+ text = opts[:text]
+ index = opts[:index]
+ sum = 0
+ lines = text.each_line.to_a.map do |l|
+ sum += l.length
+ { line: l, length: l.length, sum: sum }
+ end
+ cursor_line = lines.find { |l| l[:sum] >= index }
+ previous_line = lines.find { |l| l[:sum] < index }
+ previous_line ||= { sum: 0 }
+ if cursor_line
+ sub_index = index - previous_line[:sum]
+ word = (cursor_line[:line][0..sub_index - 1]).strip
+ token = (word.split " ")[-1]
+ dots = (token.split ".")
+ dot = dots[-1]
+ end
+
+ {
+ text: opts[:text],
+ file: opts[:file],
+ index: opts[:index],
+ cursor_line: cursor_line,
+ previous_line: previous_line,
+ word: word,
+ token: token,
+ dots: dots,
+ dot: dot
+ }
+ end
+
+ def autocomplete_filter_methods keys, *ignores
+ ignores ||= []
+ ignores = [ignores].flatten
+ keys = keys.map { |k| k.to_s }
+ others = ["def", "end"] +
+ [ :entity_keys_by_ref,
+ :entity_name,
+ :as_hash,
+ :clear!,
+ :created_at_elapsed,
+ :entity_id,
+ "entity_id=",
+ "tick_count=",
+ :global_created_at_elapsed,
+ :load_entity_data!,
+ :meta,
+ :meta!,
+ :new?,
+ :old?,
+ :original_eq_eq, :set!,
+ :update_entity_keys_by_ref,
+ :with_meta] +
+ ignores + keys.find_all { |k| k.to_s.to_i.to_s == k.to_s }
+
+ final = (keys - (others.map { |m| m.to_s })).uniq
+ final
+ end
+
+ def suggest_autocompletion opts
+ parse_result = autocomplete_parse opts
+ return [] unless parse_result[:cursor_line]
+ text = parse_result[:text]
+ word = parse_result[:word]
+ token = parse_result[:token]
+ dots = parse_result[:dots]
+ dot = parse_result[:dot]
+
+ return [] if word.strip.start_with? "#"
+
+ if word[-1] == "." && token
+ lookup = {
+ 'args' => lambda { $gtk.args },
+ 'inputs' => lambda { $gtk.args.inputs },
+ 'outputs' => lambda { $gtk.args.outputs },
+ 'layout' => lambda { $gtk.args.outputs },
+ 'keyboard' => lambda { $gtk.args.keyboard },
+ 'key_down' => lambda { $gtk.args.keyboard.key_down },
+ 'key_up' => lambda { $gtk.args.keyboard.key_up },
+ 'state' => lambda { $gtk.args.state },
+ '$gtk' => lambda { $gtk }
+ }
+
+ lookup_result = lookup[dot]
+
+ return autocomplete_filter_methods lookup_result.call.autocomplete_methods if lookup_result
+
+ start_collecting = false
+ dots_after_state = dots.find_all do |s|
+ if s == "state"
+ start_collecting = true
+ false
+ else
+ start_collecting
+ end
+ end
+
+ target = $gtk.args.state
+ dots_after_state.each do |k|
+ target = target.as_hash[k.to_sym] if target.respond_to? :as_hash
+ end
+
+ return autocomplete_filter_methods target.as_hash.keys
+ end
+
+
+ text.gsub!("[", " ")
+ text.gsub!("]", " ")
+ text.gsub!("(", " ")
+ text.gsub!(")", " ")
+ text.gsub!(":", "")
+ text.gsub!(".", " ")
+ text.gsub!("=", " ")
+ return (autocomplete_filter_methods (text.split " "),
+ :gtk, :false, :true, :args, :suppress_mailbox, :end)
+ end
+ end # end Autocomplete
+ end # end Runtime
+end # end GTK