Class: Steep::Drivers::Langserver
- Inherits:
-
Object
- Object
- Steep::Drivers::Langserver
- Includes:
- Utils::DriverHelper
- Defined in:
- lib/steep/drivers/langserver.rb
Defined Under Namespace
Classes: TypeCheckRequest
Instance Attribute Summary collapse
-
#latest_update_version ⇒ Object
readonly
Returns the value of attribute latest_update_version.
-
#stderr ⇒ Object
readonly
Returns the value of attribute stderr.
-
#stdin ⇒ Object
readonly
Returns the value of attribute stdin.
-
#stdout ⇒ Object
readonly
Returns the value of attribute stdout.
-
#type_check_queue ⇒ Object
readonly
Returns the value of attribute type_check_queue.
-
#type_check_thread ⇒ Object
readonly
Returns the value of attribute type_check_thread.
-
#write_mutex ⇒ Object
readonly
Returns the value of attribute write_mutex.
Attributes included from Utils::DriverHelper
Instance Method Summary collapse
- #diagnostic_for_type_error(error) ⇒ Object
- #diagnostic_for_validation_error(error) ⇒ Object
- #diagnostics_raw(message, loc) ⇒ Object
- #enqueue_type_check(version) ⇒ Object
- #format_hover(content) ⇒ Object
- #handle_request(request) ⇒ Object
-
#initialize(stdout:, stderr:, stdin:) ⇒ Langserver
constructor
A new instance of Langserver.
- #project ⇒ Object
- #reader ⇒ Object
- #report_diagnostics(path, diagnostics) ⇒ Object
- #response_to_hover(path:, line:, column:) ⇒ Object
- #run ⇒ Object
- #run_type_check ⇒ Object
- #start_type_check ⇒ Object
- #write(method:, params:) ⇒ Object
- #writer ⇒ Object
Methods included from Utils::DriverHelper
Constructor Details
#initialize(stdout:, stderr:, stdin:) ⇒ Langserver
Returns a new instance of Langserver.
16 17 18 19 20 21 22 |
# File 'lib/steep/drivers/langserver.rb', line 16 def initialize(stdout:, stderr:, stdin:) @stdout = stdout @stderr = stderr @stdin = stdin @write_mutex = Mutex.new @type_check_queue = Queue.new end |
Instance Attribute Details
#latest_update_version ⇒ Object (readonly)
Returns the value of attribute latest_update_version.
7 8 9 |
# File 'lib/steep/drivers/langserver.rb', line 7 def latest_update_version @latest_update_version end |
#stderr ⇒ Object (readonly)
Returns the value of attribute stderr.
5 6 7 |
# File 'lib/steep/drivers/langserver.rb', line 5 def stderr @stderr end |
#stdin ⇒ Object (readonly)
Returns the value of attribute stdin.
6 7 8 |
# File 'lib/steep/drivers/langserver.rb', line 6 def stdin @stdin end |
#stdout ⇒ Object (readonly)
Returns the value of attribute stdout.
4 5 6 |
# File 'lib/steep/drivers/langserver.rb', line 4 def stdout @stdout end |
#type_check_queue ⇒ Object (readonly)
Returns the value of attribute type_check_queue.
9 10 11 |
# File 'lib/steep/drivers/langserver.rb', line 9 def type_check_queue @type_check_queue end |
#type_check_thread ⇒ Object (readonly)
Returns the value of attribute type_check_thread.
10 11 12 |
# File 'lib/steep/drivers/langserver.rb', line 10 def type_check_thread @type_check_thread end |
#write_mutex ⇒ Object (readonly)
Returns the value of attribute write_mutex.
8 9 10 |
# File 'lib/steep/drivers/langserver.rb', line 8 def write_mutex @write_mutex end |
Instance Method Details
#diagnostic_for_type_error(error) ⇒ Object
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/steep/drivers/langserver.rb', line 249 def diagnostic_for_type_error(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 |
#diagnostic_for_validation_error(error) ⇒ Object
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/steep/drivers/langserver.rb', line 215 def diagnostic_for_validation_error(error) LanguageServer::Protocol::Interface::Diagnostic.new( message: StringIO.new("").tap {|io| error.puts(io) }.string, severity: LanguageServer::Protocol::Constant::DiagnosticSeverity::ERROR, range: LanguageServer::Protocol::Interface::Range.new( start: LanguageServer::Protocol::Interface::Position.new( line: error.location.start_line - 1, character: error.location.start_column, ), end: LanguageServer::Protocol::Interface::Position.new( line: error.location.end_line - 1, character: error.location.end_column, ), ) ) end |
#diagnostics_raw(message, loc) ⇒ Object
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/steep/drivers/langserver.rb', line 232 def diagnostics_raw(, loc) LanguageServer::Protocol::Interface::Diagnostic.new( message: , severity: LanguageServer::Protocol::Constant::DiagnosticSeverity::ERROR, range: LanguageServer::Protocol::Interface::Range.new( start: LanguageServer::Protocol::Interface::Position.new( line: loc.start_line - 1, character: loc.start_column, ), end: LanguageServer::Protocol::Interface::Position.new( line: loc.end_line - 1, character: loc.end_column, ), ) ) end |
#enqueue_type_check(version) ⇒ Object
36 37 38 39 |
# File 'lib/steep/drivers/langserver.rb', line 36 def enqueue_type_check(version) @latest_update_version = version type_check_queue << TypeCheckRequest.new(version: version) end |
#format_hover(content) ⇒ Object
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/steep/drivers/langserver.rb', line 285 def format_hover(content) case content when Project::HoverContent::VariableContent "`#{content.name}`: `#{content.type.to_s}`" when Project::HoverContent::MethodCallContent method_name = case content.method_name when Project::HoverContent::InstanceMethodName "#{content.method_name.class_name}##{content.method_name.method_name}" when Project::HoverContent::SingletonMethodName "#{content.method_name.class_name}.#{content.method_name.method_name}" else nil end if method_name string = <<HOVER ``` #{method_name} ~> #{content.type} ``` HOVER if content.definition if content.definition.comment string << "\n----\n\n#{content.definition.comment.string}" end string << "\n----\n\n#{content.definition.method_types.map {|x| "- `#{x}`\n" }.join()}" end else "`#{content.type}`" end when Project::HoverContent::DefinitionContent string = <<HOVER ``` def #{content.method_name}: #{content.method_type} ``` HOVER if (comment = content.definition.comment) string << "\n----\n\n#{comment.string}\n" end if content.definition.method_types.size > 1 string << "\n----\n\n#{content.definition.method_types.map {|x| "- `#{x}`\n" }.join()}" end string when Project::HoverContent::TypeContent "`#{content.type}`" end end |
#handle_request(request) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/steep/drivers/langserver.rb', line 74 def handle_request(request) id = request[:id] method = request[:method].to_sym Steep.logger.tagged "id=#{id}, method=#{method}" do case method when :initialize yield id, LanguageServer::Protocol::Interface::InitializeResult.new( capabilities: LanguageServer::Protocol::Interface::ServerCapabilities.new( text_document_sync: LanguageServer::Protocol::Interface::TextDocumentSyncOptions.new( change: LanguageServer::Protocol::Constant::TextDocumentSyncKind::FULL ), hover_provider: true, ) ) enqueue_type_check nil when :"textDocument/didChange" uri = URI.parse(request[:params][:textDocument][:uri]) path = project.relative_path(Pathname(uri.path)) text = request[:params][:contentChanges][0][:text] Steep.logger.debug { "path=#{path}, content=#{text.lines.first&.chomp}..." } project.targets.each do |target| Steep.logger.tagged "target=#{target.name}" do case when target.source_file?(path) if text.empty? && !path.file? Steep.logger.info { "Deleting source file: #{path}..." } target.remove_source(path) report_diagnostics path, [] else Steep.logger.info { "Updating source file: #{path}..." } target.update_source(path, text) end when target.possible_source_file?(path) Steep.logger.info { "Adding source file: #{path}..." } target.add_source(path, text) when target.signature_file?(path) if text.empty? && !path.file? Steep.logger.info { "Deleting signature file: #{path}..." } target.remove_signature(path) report_diagnostics path, [] else Steep.logger.info { "Updating signature file: #{path}..." } target.update_signature(path, text) end when target.possible_signature_file?(path) Steep.logger.info { "Adding signature file: #{path}..." } target.add_signature(path, text) end end end version = request[:params][:textDocument][:version] enqueue_type_check version when :"textDocument/hover" uri = URI.parse(request[:params][:textDocument][:uri]) path = project.relative_path(Pathname(uri.path)) line = request[:params][:position][:line] column = request[:params][:position][:character] yield id, response_to_hover(path: path, line: line, column: column) when :shutdown yield id, nil when :exit type_check_queue << nil type_check_thread.join exit end end end |
#project ⇒ Object
32 33 34 |
# File 'lib/steep/drivers/langserver.rb', line 32 def project @project or raise "Empty #project" end |
#reader ⇒ Object
28 29 30 |
# File 'lib/steep/drivers/langserver.rb', line 28 def reader @reader ||= LanguageServer::Protocol::Transport::Io::Reader.new(stdin) end |
#report_diagnostics(path, diagnostics) ⇒ Object
204 205 206 207 208 209 210 211 212 213 |
# File 'lib/steep/drivers/langserver.rb', line 204 def report_diagnostics(path, diagnostics) Steep.logger.info { "Reporting #{diagnostics.size} diagnostics for #{path}..." } write( method: :"textDocument/publishDiagnostics", params: LanguageServer::Protocol::Interface::PublishDiagnosticsParams.new( uri: URI.parse(project.absolute_path(path).to_s).tap {|uri| uri.scheme = "file"}, diagnostics: diagnostics, ) ) end |
#response_to_hover(path:, line:, column:) ⇒ Object
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/steep/drivers/langserver.rb', line 266 def response_to_hover(path:, line:, column:) Steep.logger.info { "path=#{path}, line=#{line}, column=#{column}" } hover = Project::HoverContent.new(project: project) content = hover.content_for(path: path, line: line+1, column: column+1) if content range = content.location.yield_self do |location| start_position = { line: location.line - 1, character: location.column } end_position = { line: location.last_line - 1, character: location.last_column } { start: start_position, end: end_position } end LanguageServer::Protocol::Interface::Hover.new( contents: { kind: "markdown", value: format_hover(content) }, range: range ) end end |
#run ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/steep/drivers/langserver.rb', line 41 def run @project = load_config() loader = Project::FileLoader.new(project: project) loader.load_sources([]) loader.load_signatures() start_type_check() reader.read do |request| Steep.logger.tagged "lsp" do Steep.logger.debug { "Received a request: request=#{request.to_json}" } handle_request(request) do |id, result| if id write_mutex.synchronize do Steep.logger.debug { "Writing response to #{id}: #{result.to_json}" } writer.write(id: id, result: result) end end end end end 0 end |
#run_type_check ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/steep/drivers/langserver.rb', line 160 def run_type_check() Steep.logger.tagged "#run_type_check" do Steep.logger.info { "Running type check..." } type_check project Steep.logger.info { "Sending diagnostics..." } project.targets.each do |target| Steep.logger.tagged "target=#{target.name}, status=#{target.status.class}" do Steep.logger.info { "Clearing signature diagnostics..." } target.signature_files.each_value do |file| report_diagnostics file.path, [] end case (status = target.status) when Project::Target::SignatureValidationErrorStatus Steep.logger.info { "Signature validation error" } status.errors.group_by(&:path).each do |path, errors| diagnostics = errors.map {|error| diagnostic_for_validation_error(error) } report_diagnostics path, diagnostics end when Project::Target::TypeCheckStatus Steep.logger.info { "Type check" } status.type_check_sources.each do |source| diagnostics = case source.status when Project::SourceFile::TypeCheckStatus source.errors.map {|error| diagnostic_for_type_error(error) } when Project::SourceFile::AnnotationSyntaxErrorStatus [diagnostics_raw(source.status.error., source.status.location)] when Project::SourceFile::ParseErrorStatus [] when Project::SourceFile::TypeCheckErrorStatus [] end report_diagnostics source.path, diagnostics end when Project::Target::SignatureSyntaxErrorStatus Steep.logger.info { "Signature syntax error" } end end end end end |
#start_type_check ⇒ Object
150 151 152 153 154 155 156 157 158 |
# File 'lib/steep/drivers/langserver.rb', line 150 def start_type_check @type_check_thread = Thread.start do while request = type_check_queue.deq if @latest_update_version == nil || @latest_update_version == request.version run_type_check() end end end end |
#write(method:, params:) ⇒ Object
67 68 69 70 71 72 |
# File 'lib/steep/drivers/langserver.rb', line 67 def write(method:, params:) write_mutex.synchronize do Steep.logger.debug { "Sending request: method=#{method}, params=#{params.to_json}"} writer.write(method: method, params: params) end end |
#writer ⇒ Object
24 25 26 |
# File 'lib/steep/drivers/langserver.rb', line 24 def writer @writer ||= LanguageServer::Protocol::Transport::Io::Writer.new(stdout) end |