Permalink
Browse files

Change /format to /diagnostics and return LSP-compatible results.

  • Loading branch information...
castwide committed Feb 12, 2018
1 parent a404927 commit 6c35f7c96676249da79f5e4f7304464902a1cca7
Showing with 36 additions and 5 deletions.
  1. +0 −3 Gemfile
  2. +35 −2 lib/solargraph/server.rb
  3. +1 −0 solargraph.gemspec
@@ -1,6 +1,3 @@
source 'https://rubygems.org'

gemspec name: 'solargraph'

# @todo Determine whether this should be core to Solargraph or a separate LSP gem
gem 'rubocop'
@@ -22,13 +22,46 @@ def self.wait
@@semaphore.unlock
end

post '/format' do
post '/diagnostics' do
content_type :json
severities = {
'refactor' => 4,
'convention' => 3,
'warning' => 2,
'error' => 1,
'fatal' => 1
}
begin
filename = params['filename']
text = params['text']
o, e, s = Open3.capture3("bundle exec rubocop -f j -s #{Shellwords.escape(filename)}", stdin_data: text)
{ "status" => "ok", "data" => JSON.parse(o) }.to_json
STDERR.puts e unless e.nil? or e.empty?
resp = JSON.parse(o)
diagnostics = []
if resp['summary']['offense_count'] > 0
resp['files'].each do |file|
file['offenses'].each do |off|
diag = {
range: {
start: {
line: off['location']['start_line'] - 1,
character: off['location']['start_column'] - 1
},
end: {
line: off['location']['last_line'] - 1,
character: off['location']['last_column']
}
},
# 1 = Error, 2 = Warning, 3 = Information, 4 = Hint
severity: severities[off['severity']],
source: off['cop_name'],
message: off['message'].gsub(/^#{off['cop_name']}\:/, '')
}
diagnostics.push diag
end
end
end
{ "status" => "ok", "data" => diagnostics }.to_json
rescue Exception => e
send_exception e
end
@@ -21,6 +21,7 @@ Gem::Specification.new do |s|
s.add_runtime_dependency 'sinatra', '~> 2'
s.add_runtime_dependency 'yard', '~> 0.9'
s.add_runtime_dependency 'bundler', '~> 1.14'
s.add_runtime_dependency 'rubocop', '~> 0.52'

s.add_development_dependency 'rspec', '~> 3.5', '>= 3.5.0'
s.add_development_dependency 'rack-test', '~> 0.7'

0 comments on commit 6c35f7c

Please sign in to comment.