diff --git a/Commands/Complete.tmCommand b/Commands/Complete.tmCommand new file mode 100644 index 0000000..971af67 --- /dev/null +++ b/Commands/Complete.tmCommand @@ -0,0 +1,91 @@ + + + + + beforeRunningCommand + saveActiveFile + command + #!/usr/bin/env ruby18 +require ENV['TM_SUPPORT_PATH'] + '/lib/ui.rb' +require ENV['TM_SUPPORT_PATH'] + "/lib/escape.rb" +require ENV['TM_SUPPORT_PATH'] + "/lib/tm/require_cmd.rb" +require ENV['TM_SUPPORT_PATH'] + "/lib/tm/htmloutput.rb" +require ENV['TM_SUPPORT_PATH'] + "/lib/web_preview" + +# current document +document = [] +File.open(ENV['TM_FILEPATH'], "r+") do |file| + document = file.readlines +end + +# byte offset of cursor position from the beginning of file +# cursor = document[ 0, ENV['TM_LINE_NUMBER'].to_i - 1].join().length + ENV['TM_LINE_INDEX'].to_i +# line = document[ENV['TM_LINE_NUMBER'].to_i - 1] +# word = line.split(' ').last + +line_number = ENV['TM_LINE_NUMBER'] +line_index = ENV['TM_LINE_INDEX'] +file_path = ENV['TM_FILEPATH'] +output = `"$TM_RACER" complete #{line_number} #{line_index} #{file_path}` + +def make_completion_hash(line) + comp = line.split(",") + match = comp[0].split(" ").last + image = comp[4] + display = comp[5] + out = "%s\t%s" % [match.ljust(40), display] + return { 'match' => match, 'display' => out, 'insert' => '', 'image' => '' } +end + +hashes = output.split("\n").select{ |line| line.start_with? "MATCH" }.collect{ |line| make_completion_hash(line) } +options = { :extra_chars => "_", :case_insensitive => false } + +# quit if no completions found +TextMate.exit_show_tool_tip("No completions found.") if hashes.length == 0 + +if hashes.length == 1 + word = ENV['TM_CURRENT_WORD'] || "" + snippet = hashes[0]["match"].gsub(/^#{Regexp.escape(word)}/, "") + hashes[0]["insert"] + TextMate.exit_insert_snippet( snippet ) +else + TextMate::UI.complete( hashes , options ) +end + + + fallbackInput + word + hideFromUser + true + input + none + inputFormat + text + keyEquivalent + ~ + name + Complete + outputCaret + afterOutput + outputFormat + text + outputLocation + atCaret + requiredCommands + + + command + racer + moreInfoURL + https://github.com/phildawes/racer + variable + TM_RACER + + + scope + source.rust + uuid + FE908865-7729-4926-9FAC-2D54895BEA48 + version + 2 + +