-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #521 from testdouble/enhance-lsp
Enhance LSP
- Loading branch information
Showing
8 changed files
with
345 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module Standard | ||
module Lsp | ||
class Logger | ||
def puts(message) | ||
warn("[server] #{message}") | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
module Standard | ||
module Lsp | ||
class Routes | ||
def initialize(writer, logger, standardizer) | ||
@writer = writer | ||
@logger = logger | ||
@standardizer = standardizer | ||
|
||
@text_cache = {} | ||
end | ||
|
||
def self.handle(name, &block) | ||
define_method("handle_#{name}", &block) | ||
end | ||
|
||
def for(name) | ||
name = "handle_#{name}" | ||
if respond_to?(name) | ||
method(name) | ||
end | ||
end | ||
|
||
handle "initialize" do |request| | ||
@writer.write(id: request[:id], result: Proto::Interface::InitializeResult.new( | ||
capabilities: Proto::Interface::ServerCapabilities.new( | ||
document_formatting_provider: true, | ||
diagnostic_provider: true, | ||
text_document_sync: Proto::Interface::TextDocumentSyncOptions.new( | ||
change: Proto::Constant::TextDocumentSyncKind::FULL | ||
) | ||
) | ||
)) | ||
end | ||
|
||
handle "initialized" do |request| | ||
@logger.puts "Standard Ruby v#{Standard::VERSION} LSP server initialized, pid #{Process.pid}" | ||
end | ||
|
||
handle "shutdown" do |request| | ||
@logger.puts "Client asked to shutdown Standard LSP server. Exiting..." | ||
at_exit { | ||
@writer.write(id: request[:id], result: nil) | ||
} | ||
exit 0 | ||
end | ||
|
||
handle "textDocument/diagnostic" do |request| | ||
doc = request[:params][:textDocument] | ||
result = diagnostic(doc[:uri], doc[:text]) | ||
@writer.write(result) | ||
end | ||
|
||
handle "textDocument/didChange" do |request| | ||
params = request[:params] | ||
result = diagnostic(params[:textDocument][:uri], params[:contentChanges][0][:text]) | ||
@writer.write(result) | ||
end | ||
|
||
handle "textDocument/didOpen" do |request| | ||
doc = request[:params][:textDocument] | ||
result = diagnostic(doc[:uri], doc[:text]) | ||
@writer.write(result) | ||
end | ||
|
||
handle "textDocument/didClose" do |request| | ||
@text_cache.delete(request.dig(:params, :textDocument, :uri)) | ||
end | ||
|
||
handle "textDocument/formatting" do |request| | ||
uri = request[:params][:textDocument][:uri] | ||
@writer.write({id: request[:id], result: format_file(uri)}) | ||
end | ||
|
||
handle "textDocument/didSave" do |_request| | ||
# No-op | ||
end | ||
|
||
handle "$/cancelRequest" do |_request| | ||
# No-op | ||
end | ||
|
||
handle "$/setTrace" do |_request| | ||
# No-op | ||
end | ||
|
||
private | ||
|
||
def format_file(file_uri) | ||
text = @text_cache[file_uri] | ||
if text.nil? | ||
@logger.puts "Format request arrived before text synchonized; skipping: `#{file_uri}'" | ||
[] | ||
else | ||
new_text = @standardizer.format(text) | ||
if new_text == text | ||
[] | ||
else | ||
[{ | ||
newText: new_text, | ||
range: { | ||
start: {line: 0, character: 0}, | ||
end: {line: text.count("\n") + 1, character: 0} | ||
} | ||
}] | ||
end | ||
end | ||
end | ||
|
||
def diagnostic(file_uri, text) | ||
@text_cache[file_uri] = text | ||
offenses = @standardizer.offenses(text) | ||
|
||
lsp_diagnostics = offenses.map { |o| | ||
code = o[:cop_name] | ||
|
||
msg = o[:message].delete_prefix(code) | ||
loc = o[:location] | ||
|
||
severity = case o[:severity] | ||
when "error", "fatal" | ||
SEV::ERROR | ||
when "warning" | ||
SEV::WARNING | ||
when "convention" | ||
SEV::INFORMATION | ||
when "refactor", "info" | ||
SEV::HINT | ||
else # the above cases fully cover what RuboCop sends at this time | ||
logger.puts "Unknown severity: #{severity.inspect}" | ||
SEV::HINT | ||
end | ||
|
||
{ | ||
code: code, | ||
message: msg, | ||
range: { | ||
start: {character: loc[:start_column] - 1, line: loc[:start_line] - 1}, | ||
end: {character: loc[:last_column] - 1, line: loc[:last_line] - 1} | ||
}, | ||
severity: severity, | ||
source: "standard" | ||
} | ||
} | ||
|
||
{ | ||
method: "textDocument/publishDiagnostics", | ||
params: { | ||
uri: file_uri, | ||
diagnostics: lsp_diagnostics | ||
} | ||
} | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,144 +1,43 @@ | ||
require "language_server-protocol" | ||
require_relative "standardizer" | ||
require_relative "routes" | ||
require_relative "logger" | ||
|
||
module Standard | ||
module LSP | ||
class Server | ||
Proto = LanguageServer::Protocol | ||
SEV = Proto::Constant::DiagnosticSeverity | ||
module Lsp | ||
Proto = LanguageServer::Protocol | ||
SEV = Proto::Constant::DiagnosticSeverity | ||
|
||
class Server | ||
def self.start(standardizer) | ||
new(standardizer).start | ||
end | ||
|
||
attr_accessor :standardizer, :writer, :reader, :logger, :text_cache, :subscribers | ||
|
||
def initialize(standardizer) | ||
self.standardizer = standardizer | ||
self.writer = Proto::Transport::Io::Writer.new($stdout) | ||
self.reader = Proto::Transport::Io::Reader.new($stdin) | ||
self.logger = $stderr | ||
self.text_cache = {} | ||
|
||
self.subscribers = { | ||
"initialize" => ->(request) { | ||
init_result = Proto::Interface::InitializeResult.new( | ||
capabilities: Proto::Interface::ServerCapabilities.new( | ||
document_formatting_provider: true, | ||
diagnostic_provider: true, | ||
text_document_sync: Proto::Constant::TextDocumentSyncKind::FULL | ||
) | ||
) | ||
writer.write(id: request[:id], result: init_result) | ||
}, | ||
|
||
"initialized" => ->(request) { logger.puts "standard v#{Standard::VERSION} initialized, pid #{Process.pid}" }, | ||
|
||
"shutdown" => ->(request) { | ||
logger.puts "asked to shutdown, exiting..." | ||
exit | ||
}, | ||
|
||
"textDocument/didChange" => ->(request) { | ||
params = request[:params] | ||
result = diagnostic(params[:textDocument][:uri], params[:contentChanges][0][:text]) | ||
writer.write(result) | ||
}, | ||
|
||
"textDocument/didOpen" => ->(request) { | ||
td = request[:params][:textDocument] | ||
result = diagnostic(td[:uri], td[:text]) | ||
writer.write(result) | ||
}, | ||
|
||
"textDocument/didClose" => ->(request) { | ||
text_cache.delete(request.dig(:params, :textDocument, :uri)) | ||
}, | ||
|
||
"textDocument/formatting" => ->(request) { | ||
uri = request[:params][:textDocument][:uri] | ||
writer.write({id: request[:id], result: format_file(uri)}) | ||
}, | ||
|
||
"textDocument/didSave" => ->(request) {} | ||
} | ||
@standardizer = standardizer | ||
@writer = Proto::Transport::Io::Writer.new($stdout) | ||
@reader = Proto::Transport::Io::Reader.new($stdin) | ||
@logger = Logger.new | ||
@routes = Routes.new(@writer, @logger, @standardizer) | ||
end | ||
|
||
def start | ||
reader.read do |request| | ||
@reader.read do |request| | ||
method = request[:method] | ||
if (subscriber = subscribers[method]) | ||
subscriber.call(request) | ||
if (route = @routes.for(method)) | ||
route.call(request) | ||
else | ||
logger.puts "unknown method: #{method}" | ||
@writer.write({id: request[:id], error: Proto::Interface::ResponseError.new( | ||
code: Proto::Constant::ErrorCodes::METHOD_NOT_FOUND, | ||
message: "Unsupported Method: #{method}" | ||
)}) | ||
@logger.puts "Unsupported Method: #{method}" | ||
end | ||
rescue => e | ||
logger.puts "error #{e.class} #{e.message[0..100]}" | ||
logger.puts e.backtrace.inspect | ||
@logger.puts "Error #{e.class} #{e.message[0..100]}" | ||
@logger.puts e.backtrace.inspect | ||
end | ||
end | ||
|
||
def format_file(file_uri) | ||
text = text_cache[file_uri] | ||
new_text = standardizer.format(text) | ||
|
||
if new_text == text | ||
[] | ||
else | ||
[{ | ||
newText: new_text, | ||
range: { | ||
start: {line: 0, character: 0}, | ||
end: {line: text.count("\n") + 1, character: 0} | ||
} | ||
}] | ||
end | ||
end | ||
|
||
def diagnostic(file_uri, text) | ||
text_cache[file_uri] = text | ||
offenses = standardizer.offenses(text) | ||
|
||
lsp_diagnostics = offenses.map { |o| | ||
code = o[:cop_name] | ||
|
||
msg = o[:message].delete_prefix(code) | ||
loc = o[:location] | ||
|
||
severity = case o[:severity] | ||
when "error", "fatal" | ||
SEV::ERROR | ||
when "warning" | ||
SEV::WARNING | ||
when "convention" | ||
SEV::INFORMATION | ||
when "refactor", "info" | ||
SEV::HINT | ||
else # the above cases fully cover what RuboCop sends at this time | ||
logger.puts "unknown severity: #{severity.inspect}" | ||
SEV::HINT | ||
end | ||
|
||
{ | ||
code: code, | ||
message: msg, | ||
range: { | ||
start: {character: loc[:start_column] - 1, line: loc[:start_line] - 1}, | ||
end: {character: loc[:last_column] - 1, line: loc[:last_line] - 1} | ||
}, | ||
severity: severity, | ||
source: "standard" | ||
} | ||
} | ||
|
||
{ | ||
method: "textDocument/publishDiagnostics", | ||
params: { | ||
diagnostics: lsp_diagnostics, | ||
uri: file_uri | ||
} | ||
} | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.