Class: Solargraph::Shell

Inherits:
Thor
  • Object
show all
Defined in:
lib/solargraph/shell.rb

Instance Method Summary collapse

Instance Method Details

#bundledObject



46
47
48
49
50
# File 'lib/solargraph/shell.rb', line 46

def bundled
  Bundler.load.specs.each { |s|
    puts s.name
  }
end

#prepareObject



12
13
14
15
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
# File 'lib/solargraph/shell.rb', line 12

def prepare
  cache_dir = File.join(Dir.home, '.solargraph', 'cache')
  version_dir = File.join(cache_dir, '2.0.0')
  unless File.exist?(version_dir) or options[:force]
    FileUtils.mkdir_p cache_dir
    require 'net/http'
    puts 'Downloading 2.0.0...'
    Net::HTTP.start(options[:host]) do |http|
        resp = http.get("/2.0.0.tar.gz")
        open(File.join(cache_dir, '2.0.0.tar.gz'), "wb") do |file|
            file.write(resp.body)
        end
        puts 'Uncompressing archives...'
        FileUtils.rm_rf version_dir if File.exist?(version_dir)
        tar_extract = Gem::Package::TarReader.new(Zlib::GzipReader.open(File.join(cache_dir, '2.0.0.tar.gz')))
        tar_extract.rewind
        tar_extract.each do |entry|
          if entry.directory?
            FileUtils.mkdir_p File.join(cache_dir, entry.full_name)
          else
            FileUtils.mkdir_p File.join(cache_dir, File.dirname(entry.full_name))
            File.open(File.join(cache_dir, entry.full_name), 'wb') do |f|
              f << entry.read
            end
          end
        end
        tar_extract.close
        FileUtils.rm File.join(cache_dir, '2.0.0.tar.gz')
        puts 'Done.'
    end
  end
end

#serialize(directory) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/solargraph/shell.rb', line 58

def serialize directory
  api_map = ApiMap.new directory
  files = Dir[File.join directory, 'lib', '**', '*.rb'] + Dir[File.join directory, 'app', '**', '*.rb']
  files.each { |f|
    api_map.append_file f
  }
  File.open(File.join(directory, '.solargraph.ser'), 'wb') { |file|
    file << Marshal.dump(api_map)
  }
end

#serverObject



53
54
55
# File 'lib/solargraph/shell.rb', line 53

def server
  Solargraph::Server.run!
end

#suggest(*filenames) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/solargraph/shell.rb', line 76

def suggest(*filenames)
  # HACK: The ARGV array needs to be manipulated for ARGF.read to work
  ARGV.clear
  ARGV.concat filenames
  text = ARGF.read
  filename = options[:filename] || filenames[0]
  begin
    code_map = CodeMap.new(code: text, filename: filename)
    offset = code_map.get_offset(options[:line], options[:column])
    sugg = code_map.suggest_at(offset, with_snippets: true, filtered: true)
    result = { "status" => "ok", "suggestions" => sugg }.to_json
    STDOUT.puts result
  rescue Exception => e
    STDERR.puts e
    STDERR.puts e.backtrace.join("\n")
    #result = { "status" => "err", "message" => e.message }.to_json
    result = { "status" => "err", "message" => e.message + "\n" + e.backtrace.join("\n") }.to_json
    STDOUT.puts result
  end
end