Class: Solargraph::Shell

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

Instance Method Summary collapse

Methods included from ServerMethods

#available_port

Instance Method Details

#available_cores ⇒ Object



109
110
111
# File 'lib/solargraph/shell.rb', line 109

def available_cores
  puts Solargraph::YardMap::CoreDocs.available.join("\n")
end

#clear_cores ⇒ Object



114
115
116
# File 'lib/solargraph/shell.rb', line 114

def clear_cores
  Solargraph::YardMap::CoreDocs.clear
end

#config(directory = '.') ⇒ Object



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

def config(directory = '.')
  matches = []
  if options[:extensions]
    Gem::Specification.each do |g|
      puts g.name
      if g.name.match(/^solargraph\-[A-Za-z0-9_\-]*?\-ext/)
        require g.name
        matches.push g.name
      end
    end
  end
  conf = Solargraph::ApiMap::Config.new.raw_data
  unless matches.empty?
    matches.each do |m|
      conf['extensions'].push m
    end
  end
  File.open(File.join(directory, '.solargraph.yml'), 'w') do |file|
    file.puts conf.to_yaml
  end
  STDOUT.puts "Configuration file initialized."
end

#download_core(version = nil) ⇒ Object



97
98
99
100
101
# File 'lib/solargraph/shell.rb', line 97

def download_core version = nil
  ver = version || Solargraph::YardMap::CoreDocs.best_download
  puts "Downloading docs for #{ver}..."
  Solargraph::YardMap::CoreDocs.download ver
end

#list_cores ⇒ Object



104
105
106
# File 'lib/solargraph/shell.rb', line 104

def list_cores
  puts Solargraph::YardMap::CoreDocs.versions.join("\n")
end

#server ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/solargraph/shell.rb', line 22

def server
  port = options[:port]
  # This line should not be necessary with WEBrick
  #port = available_port if port.zero?
  Solargraph::Server.set :port, port
  Solargraph::Server.set :views, options[:views] unless options[:views].nil?
  Solargraph::Server.set :public_folder, options[:files] unless options[:files].nil?
  my_pid = nil
  Solargraph::Server.run! do
    # This line should not be necessary with WEBrick
    #STDERR.puts "Solargraph server pid=#{Process.pid} port=#{port}"
    my_pid = Process.pid
    Signal.trap("INT") do
      Solargraph::Server.stop!
    end
    Signal.trap("TERM") do
      Solargraph::Server.stop!
    end
  end
end

#suggest(*filenames) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/solargraph/shell.rb', line 50

def suggest(*filenames)
  STDERR.puts "WARNING: The `solargraph suggest` command is a candidate for deprecation. It will either change drastically or not exist in a future version."
  # 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 + "\n" + e.backtrace.join("\n") }.to_json
    STDOUT.puts result
  end
end

#version ⇒ Object



14
15
16
# File 'lib/solargraph/shell.rb', line 14

def version
  puts Solargraph::VERSION
end