-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- v1.9.4
- v1.9.3
- v1.9.2
- v1.9.1
- v1.9.0
- v1.9.0.dev.2
- v1.9.0.dev.1
- v1.8.3
- v1.8.2
- v1.8.1
- v1.8.0
- v1.8.0.pre.2
- v1.8.0.pre.1
- v1.8.0.dev.2
- v1.8.0.dev.1
- v1.7.1
- v1.7.0
- v1.7.0.dev.4
- v1.7.0.dev.3
- v1.7.0.dev.2
- v1.7.0.dev.1
- v1.6.0
- v1.6.0.pre.4
- v1.6.0.pre.3
- v1.6.0.pre.2
- v1.6.0.pre.1
- v1.5.3
- v1.5.2
- v1.5.1
- v1.5.0
- v1.5.0.pre.6
- v1.5.0.pre.5
- v1.5.0.pre.4
- v1.5.0.pre.3
- v1.5.0.pre.2
- v1.5.0.pre.1
- v1.4.0
- v1.4.0.dev.5
- v1.4.0.dev.4
- v1.4.0.dev.3
- v1.4.0.dev.2
- v1.4.0.dev.1
- v1.3.2
- v1.3.1
- v1.3.0
- v1.3.0.pre.2
- v1.3.0.pre.1
- v1.2.1
- v1.2.0
- v1.2.0.pre.1
- v1.1.1
- v1.1.0
- v1.1.0.pre.1
- v1.0.2
- v1.0.1
- v1.0.0
- v0.52.2
- v0.52.1
- v0.52.0
- v0.51.0
- v0.50.0
- v0.49.1
- v0.49.0
- v0.48.0
- v0.47.1
- v0.47.0
- v0.46.0
- v0.45.0
- v0.44.1
- v0.44.0
- v0.43.1
- v0.43.0
- v0.42.0
- v0.41.0
- v0.40.0
- v0.39.0
- v0.38.0
- v0.37.0
- v0.36.0
- v0.35.0
- v0.34.0
- v0.33.0
- v0.32.0
- v0.31.1
- v0.31.0
- v0.30.0
- v0.29.0
- v0.28.0
- v0.27.0
- v0.25.0
- v0.24.0
- v0.23.0
- v0.22.0
- v0.21.0
- v0.20.0
- v0.19.0
- v0.18.0
- v0.17.1
- v0.17.0
- v0.16.3
- v0.16.2
- v0.16.1
- v0.16.0
- v0.15.0
- v0.14.0
- v0.13.0
- v0.12.0
- v0.11.1
- v0.11.0
Showing
5 changed files
with
279 additions
and
1 deletion.
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,134 @@ | ||
module Steep | ||
module Drivers | ||
class Langserver | ||
attr_reader :source_dirs | ||
attr_reader :signature_dirs | ||
attr_reader :options | ||
attr_reader :subscribers | ||
|
||
include Utils::EachSignature | ||
|
||
def initialize(source_dirs:, signature_dirs:) | ||
@source_dirs = source_dirs | ||
@signature_dirs = signature_dirs | ||
@options = Project::Options.new | ||
@subscribers = {} | ||
|
||
subscribe :initialize do |request:, notifier:| | ||
LanguageServer::Protocol::Interface::InitializeResult.new( | ||
capabilities: LanguageServer::Protocol::Interface::ServerCapabilities.new( | ||
text_document_sync: LanguageServer::Protocol::Interface::TextDocumentSyncOptions.new( | ||
open_close: true, | ||
change: LanguageServer::Protocol::Constant::TextDocumentSyncKind::FULL, | ||
), | ||
), | ||
) | ||
end | ||
|
||
subscribe :shutdown do |request:, notifier:| | ||
Steep.logger.warn "Shutting down the server..." | ||
exit | ||
end | ||
|
||
subscribe :"textDocument/didOpen" do |request:, notifier:| | ||
uri = URI.parse(request[:params][:textDocument][:uri]) | ||
text = request[:params][:textDocument][:text] | ||
synchronize_project(uri: uri, text: text, notifier: notifier) | ||
end | ||
|
||
subscribe :"textDocument/didChange" do |request:, notifier:| | ||
uri = URI.parse(request[:params][:textDocument][:uri]) | ||
text = request[:params][:contentChanges][0][:text] | ||
synchronize_project(uri: uri, text: text, notifier: notifier) | ||
end | ||
end | ||
|
||
def subscribe(method, &callback) | ||
@subscribers[method] = callback | ||
end | ||
|
||
def project | ||
@project ||= Project.new.tap do |project| | ||
source_dirs.each do |path| | ||
each_file_in_path(".rb", path) do |file_path| | ||
file = Project::SourceFile.new(path: file_path, options: options) | ||
file.content = file_path.read | ||
project.source_files[file_path] = file | ||
end | ||
end | ||
|
||
signature_dirs.each do |path| | ||
each_file_in_path(".rbi", path) do |file_path| | ||
file = Project::SignatureFile.new(path: file_path) | ||
file.content = file_path.read | ||
project.signature_files[file_path] = file | ||
end | ||
end | ||
end | ||
end | ||
|
||
def run | ||
writer = LanguageServer::Protocol::Transport::Stdio::Writer.new | ||
reader = LanguageServer::Protocol::Transport::Stdio::Reader.new | ||
notifier = Proc.new { |method:, params: {}| writer.write(method: method, params: params) } | ||
|
||
reader.read do |request| | ||
id = request[:id] | ||
method = request[:method].to_sym | ||
Steep.logger.warn "Received event: #{method}" | ||
subscriber = subscribers[method] | ||
if subscriber | ||
result = subscriber.call(request: request, notifier: notifier) | ||
if id | ||
writer.write(id: id, result: result) | ||
end | ||
else | ||
Steep.logger.warn "Ignored event: #{method}" | ||
end | ||
end | ||
end | ||
|
||
def synchronize_project(uri:, text:, notifier:) | ||
path = Pathname(uri.path).relative_path_from(Pathname.pwd) | ||
|
||
case path.extname | ||
when ".rb" | ||
file = project.source_files[path] || Project::SourceFile.new(path: path, options: options) | ||
file.content = text | ||
project.source_files[path] = file | ||
project.type_check | ||
|
||
diags = (file.errors || []).map do |error| | ||
LanguageServer::Protocol::Interface::Diagnostic.new( | ||
message: error.to_s, | ||
severity: LanguageServer::Protocol::Constant::DiagnosticSeverity::ERROR, | ||
range: LanguageServer::Protocol::Interface::Range.new( | ||
start: LanguageServer::Protocol::Interface::Position.new( | ||
line: error.node.loc.line - 1, | ||
character: error.node.loc.column, | ||
), | ||
end: LanguageServer::Protocol::Interface::Position.new( | ||
line: error.node.loc.last_line - 1, | ||
character: error.node.loc.last_column, | ||
), | ||
) | ||
) | ||
end | ||
|
||
notifier.call( | ||
method: :"textDocument/publishDiagnostics", | ||
params: LanguageServer::Protocol::Interface::PublishDiagnosticsParams.new( | ||
uri: uri, | ||
diagnostics: diags, | ||
), | ||
) | ||
when ".rbi" | ||
file = project.signature_files[path] || Project::SignatureFile.new(path: path) | ||
file.content = text | ||
project.signature_files[path] = file | ||
project.type_check | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
require_relative "test_helper" | ||
|
||
class LangserverTest < Minitest::Test | ||
include ShellHelper | ||
|
||
def dirs | ||
@dirs ||= [] | ||
end | ||
|
||
def langserver_command | ||
"#{__dir__}/../exe/steep langserver #{current_dir}" | ||
end | ||
|
||
def jsonrpc(hash) | ||
hash_str = hash.to_json | ||
"Content-Length: #{hash_str.bytesize}\r\n" + "\r\n" + hash_str | ||
end | ||
|
||
def test_initialize | ||
in_tmpdir do | ||
Open3.popen3(langserver_command) do |stdin, stdout, stderr, wait_thr| | ||
stdin.puts jsonrpc( | ||
id: 0, | ||
method: "initialize", | ||
params: {}, | ||
jsonrpc: "2.0", | ||
) | ||
stdin.close | ||
wait_thr.join | ||
|
||
assert_equal jsonrpc( | ||
id: 0, | ||
result: { | ||
capabilities: { | ||
textDocumentSync: { openClose: true, change: 1 } | ||
} | ||
}, | ||
jsonrpc: "2.0", | ||
), stdout.read | ||
end | ||
end | ||
end | ||
|
||
def test_did_open | ||
in_tmpdir do | ||
Open3.popen3(langserver_command) do |stdin, stdout, stderr, wait_thr| | ||
stdin.puts jsonrpc( | ||
method: "textDocument/didOpen", | ||
params: { | ||
textDocument: { | ||
uri: "file:///root/workdir/example.rb", | ||
languageId: "ruby", | ||
version: 1, | ||
text: "[\"foo\"].join(\",\").map {|str| puts str}", | ||
} | ||
} | ||
) | ||
stdin.close | ||
wait_thr.join | ||
|
||
assert_equal jsonrpc( | ||
method: "textDocument/publishDiagnostics", | ||
params: { | ||
uri: "file:///root/workdir/example.rb", | ||
diagnostics: [{ | ||
range: { | ||
start: { line: 0, character: 0 }, | ||
end: { line: 0, character: 38 }, | ||
}, | ||
severity: 1, | ||
message: "#{Pathname("/root/workdir/example.rb").relative_path_from(Pathname.pwd)}:1:0: NoMethodError: type=::String, method=map" | ||
}] | ||
}, | ||
jsonrpc: "2.0", | ||
), stdout.read | ||
end | ||
end | ||
end | ||
|
||
def test_did_change | ||
in_tmpdir do | ||
Open3.popen3(langserver_command) do |stdin, stdout, stderr, wait_thr| | ||
stdin.puts jsonrpc( | ||
method: "textDocument/didChange", | ||
params: { | ||
textDocument: { | ||
uri: "file:///root/workdir/example.rb", | ||
version: 2, | ||
}, | ||
contentChanges: [ | ||
{ text: "[\"foo\"].join(\",\").map {|str| puts str}" } | ||
] | ||
} | ||
) | ||
stdin.close | ||
wait_thr.join | ||
|
||
assert_equal jsonrpc( | ||
method: "textDocument/publishDiagnostics", | ||
params: { | ||
uri: "file:///root/workdir/example.rb", | ||
diagnostics: [{ | ||
range: { | ||
start: { line: 0, character: 0 }, | ||
end: { line: 0, character: 38 }, | ||
}, | ||
severity: 1, | ||
message: "#{Pathname("/root/workdir/example.rb").relative_path_from(Pathname.pwd)}:1:0: NoMethodError: type=::String, method=map" | ||
}] | ||
}, | ||
jsonrpc: "2.0", | ||
), stdout.read | ||
end | ||
end | ||
end | ||
end |