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_coresObject



117
118
119
# File 'lib/solargraph/shell.rb', line 117

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

#clear_coresObject



122
123
124
# File 'lib/solargraph/shell.rb', line 122

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

#config(directory = '.') ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/solargraph/shell.rb', line 82

def config(directory = '.')
  matches = []
  if options[:extensions]
    Gem::Specification.each do |g|
      if g.name.match(/^solargraph\-[A-Za-z0-9_\-]*?\-ext/)
        require g.name
        matches.push g.name
      end
    end
  end
  conf = Solargraph::Workspace::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



105
106
107
108
109
# File 'lib/solargraph/shell.rb', line 105

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

#list_coresObject



112
113
114
# File 'lib/solargraph/shell.rb', line 112

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

#reportersObject



127
128
129
# File 'lib/solargraph/shell.rb', line 127

def reporters
  puts Solargraph::Diagnostics.reporters
end

#socketObject



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

def socket
  port = options[:port]
  port = available_port if port.zero?
  EventMachine.run do
    Signal.trap("INT") do
      EventMachine.stop
    end
    Signal.trap("TERM") do
      EventMachine.stop
    end
    EventMachine.start_server options[:host], port, Solargraph::LanguageServer::Transport::Socket
    # Emitted for the benefit of clients that start the process on port 0
    STDERR.puts "Solargraph is listening PORT=#{port} PID=#{Process.pid}"
  end
end

#stdioObject



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/solargraph/shell.rb', line 39

def stdio
  EventMachine.run do
    Signal.trap("INT") do
      EventMachine.stop
    end
    Signal.trap("TERM") do
      EventMachine.stop
    end
    Solargraph::LanguageServer::Transport::Stdio.run
    STDERR.puts "Solargraph is listening on stdio PID=#{Process.pid}"
  end
end

#suggest(*filenames) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/solargraph/shell.rb', line 59

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, 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

#versionObject



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

def version
  puts Solargraph::VERSION
end