steep 0.37.0 → 0.42.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (221) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +1 -1
  3. data/CHANGELOG.md +34 -0
  4. data/Rakefile +5 -2
  5. data/bin/output_rebaseline.rb +34 -0
  6. data/bin/output_test.rb +53 -0
  7. data/lib/steep.rb +95 -14
  8. data/lib/steep/ast/types/bot.rb +1 -1
  9. data/lib/steep/ast/types/class.rb +4 -0
  10. data/lib/steep/ast/types/factory.rb +10 -0
  11. data/lib/steep/ast/types/logic.rb +16 -3
  12. data/lib/steep/ast/types/top.rb +1 -1
  13. data/lib/steep/cli.rb +31 -7
  14. data/lib/steep/diagnostic/helper.rb +17 -0
  15. data/lib/steep/diagnostic/lsp_formatter.rb +16 -0
  16. data/lib/steep/diagnostic/ruby.rb +619 -0
  17. data/lib/steep/diagnostic/signature.rb +357 -0
  18. data/lib/steep/drivers/annotations.rb +19 -28
  19. data/lib/steep/drivers/check.rb +182 -60
  20. data/lib/steep/drivers/diagnostic_printer.rb +99 -0
  21. data/lib/steep/drivers/langserver.rb +3 -8
  22. data/lib/steep/drivers/print_project.rb +10 -9
  23. data/lib/steep/drivers/stats.rb +124 -32
  24. data/lib/steep/drivers/trace_printer.rb +5 -1
  25. data/lib/steep/drivers/utils/jobs_count.rb +9 -0
  26. data/lib/steep/drivers/validate.rb +31 -13
  27. data/lib/steep/drivers/watch.rb +69 -48
  28. data/lib/steep/drivers/worker.rb +16 -8
  29. data/lib/steep/expectations.rb +159 -0
  30. data/lib/steep/index/rbs_index.rb +334 -0
  31. data/lib/steep/index/signature_symbol_provider.rb +162 -0
  32. data/lib/steep/index/source_index.rb +100 -0
  33. data/lib/steep/project.rb +0 -30
  34. data/lib/steep/project/dsl.rb +5 -3
  35. data/lib/steep/project/options.rb +4 -4
  36. data/lib/steep/project/pattern.rb +56 -0
  37. data/lib/steep/project/target.rb +9 -214
  38. data/lib/steep/range_extension.rb +29 -0
  39. data/lib/steep/server/base_worker.rb +43 -7
  40. data/lib/steep/server/change_buffer.rb +63 -0
  41. data/lib/steep/server/interaction_worker.rb +73 -56
  42. data/lib/steep/server/master.rb +245 -109
  43. data/lib/steep/server/type_check_worker.rb +122 -0
  44. data/lib/steep/server/worker_process.rb +17 -15
  45. data/lib/steep/{project → services}/completion_provider.rb +3 -3
  46. data/lib/steep/services/content_change.rb +61 -0
  47. data/lib/steep/services/file_loader.rb +48 -0
  48. data/lib/steep/{project → services}/hover_content.rb +14 -16
  49. data/lib/steep/services/path_assignment.rb +29 -0
  50. data/lib/steep/services/signature_service.rb +369 -0
  51. data/lib/steep/services/stats_calculator.rb +69 -0
  52. data/lib/steep/services/type_check_service.rb +342 -0
  53. data/lib/steep/signature/validator.rb +174 -32
  54. data/lib/steep/subtyping/check.rb +248 -47
  55. data/lib/steep/subtyping/constraints.rb +2 -2
  56. data/lib/steep/type_construction.rb +565 -295
  57. data/lib/steep/type_inference/constant_env.rb +5 -1
  58. data/lib/steep/type_inference/local_variable_type_env.rb +26 -12
  59. data/lib/steep/type_inference/logic_type_interpreter.rb +99 -26
  60. data/lib/steep/type_inference/type_env.rb +43 -17
  61. data/lib/steep/typing.rb +8 -2
  62. data/lib/steep/version.rb +1 -1
  63. data/smoke/alias/a.rb +0 -3
  64. data/smoke/alias/b.rb +0 -1
  65. data/smoke/alias/c.rb +0 -2
  66. data/smoke/alias/test_expectations.yml +96 -0
  67. data/smoke/and/a.rb +0 -3
  68. data/smoke/and/test_expectations.yml +31 -0
  69. data/smoke/array/a.rb +0 -3
  70. data/smoke/array/b.rb +0 -2
  71. data/smoke/array/c.rb +0 -1
  72. data/smoke/array/test_expectations.yml +103 -0
  73. data/smoke/block/a.rb +0 -2
  74. data/smoke/block/b.rb +0 -2
  75. data/smoke/block/d.rb +0 -4
  76. data/smoke/block/test_expectations.yml +125 -0
  77. data/smoke/case/a.rb +0 -3
  78. data/smoke/case/test_expectations.yml +47 -0
  79. data/smoke/class/a.rb +0 -3
  80. data/smoke/class/c.rb +0 -1
  81. data/smoke/class/f.rb +0 -1
  82. data/smoke/class/g.rb +0 -2
  83. data/smoke/class/i.rb +0 -2
  84. data/smoke/class/test_expectations.yml +120 -0
  85. data/smoke/const/a.rb +0 -3
  86. data/smoke/const/b.rb +7 -0
  87. data/smoke/const/b.rbs +5 -0
  88. data/smoke/const/test_expectations.yml +139 -0
  89. data/smoke/diagnostics-rbs-duplicated/Steepfile +5 -0
  90. data/smoke/diagnostics-rbs-duplicated/a.rbs +5 -0
  91. data/smoke/diagnostics-rbs-duplicated/test_expectations.yml +13 -0
  92. data/smoke/diagnostics-rbs/Steepfile +8 -0
  93. data/smoke/diagnostics-rbs/duplicated-method-definition.rbs +20 -0
  94. data/smoke/diagnostics-rbs/generic-parameter-mismatch.rbs +7 -0
  95. data/smoke/diagnostics-rbs/invalid-method-overload.rbs +3 -0
  96. data/smoke/diagnostics-rbs/invalid-type-application.rbs +7 -0
  97. data/smoke/diagnostics-rbs/invalid_variance_annotation.rbs +3 -0
  98. data/smoke/diagnostics-rbs/recursive-alias.rbs +5 -0
  99. data/smoke/diagnostics-rbs/recursive-class.rbs +8 -0
  100. data/smoke/diagnostics-rbs/superclass-mismatch.rbs +7 -0
  101. data/smoke/diagnostics-rbs/test_expectations.yml +231 -0
  102. data/smoke/diagnostics-rbs/unknown-method-alias.rbs +3 -0
  103. data/smoke/diagnostics-rbs/unknown-type-name-2.rbs +5 -0
  104. data/smoke/diagnostics-rbs/unknown-type-name.rbs +13 -0
  105. data/smoke/diagnostics/Steepfile +5 -0
  106. data/smoke/diagnostics/a.rbs +26 -0
  107. data/smoke/diagnostics/argument_type_mismatch.rb +1 -0
  108. data/smoke/diagnostics/block_body_type_mismatch.rb +1 -0
  109. data/smoke/diagnostics/block_type_mismatch.rb +3 -0
  110. data/smoke/diagnostics/break_type_mismatch.rb +1 -0
  111. data/smoke/diagnostics/else_on_exhaustive_case.rb +12 -0
  112. data/smoke/diagnostics/incompatible_annotation.rb +6 -0
  113. data/smoke/diagnostics/incompatible_argument.rb +1 -0
  114. data/smoke/diagnostics/incompatible_assignment.rb +8 -0
  115. data/smoke/diagnostics/method_arity_mismatch.rb +11 -0
  116. data/smoke/diagnostics/method_body_type_mismatch.rb +6 -0
  117. data/smoke/diagnostics/method_definition_missing.rb +2 -0
  118. data/smoke/diagnostics/method_return_type_annotation_mismatch.rb +7 -0
  119. data/smoke/diagnostics/missing_keyword.rb +1 -0
  120. data/smoke/diagnostics/no_method.rb +1 -0
  121. data/smoke/diagnostics/required_block_missing.rb +1 -0
  122. data/smoke/diagnostics/return_type_mismatch.rb +6 -0
  123. data/smoke/diagnostics/test_expectations.yml +477 -0
  124. data/smoke/diagnostics/unexpected_block_given.rb +1 -0
  125. data/smoke/diagnostics/unexpected_dynamic_method.rb +3 -0
  126. data/smoke/diagnostics/unexpected_jump.rb +4 -0
  127. data/smoke/diagnostics/unexpected_jump_value.rb +3 -0
  128. data/smoke/diagnostics/unexpected_keyword.rb +1 -0
  129. data/smoke/diagnostics/unexpected_splat.rb +1 -0
  130. data/smoke/diagnostics/unexpected_yield.rb +6 -0
  131. data/smoke/diagnostics/unknown_constant_assigned.rb +7 -0
  132. data/smoke/diagnostics/unresolved_overloading.rb +1 -0
  133. data/smoke/diagnostics/unsatisfiable_constraint.rb +7 -0
  134. data/smoke/diagnostics/unsupported_syntax.rb +2 -0
  135. data/smoke/dstr/a.rb +0 -1
  136. data/smoke/dstr/test_expectations.yml +13 -0
  137. data/smoke/ensure/a.rb +0 -4
  138. data/smoke/ensure/test_expectations.yml +62 -0
  139. data/smoke/enumerator/a.rb +0 -6
  140. data/smoke/enumerator/b.rb +0 -3
  141. data/smoke/enumerator/test_expectations.yml +135 -0
  142. data/smoke/extension/a.rb +0 -1
  143. data/smoke/extension/b.rb +0 -2
  144. data/smoke/extension/c.rb +0 -1
  145. data/smoke/extension/f.rb +2 -0
  146. data/smoke/extension/f.rbs +3 -0
  147. data/smoke/extension/test_expectations.yml +73 -0
  148. data/smoke/hash/b.rb +0 -1
  149. data/smoke/hash/c.rb +0 -3
  150. data/smoke/hash/d.rb +0 -1
  151. data/smoke/hash/e.rb +0 -1
  152. data/smoke/hash/test_expectations.yml +81 -0
  153. data/smoke/hello/hello.rb +0 -2
  154. data/smoke/hello/test_expectations.yml +25 -0
  155. data/smoke/if/a.rb +0 -2
  156. data/smoke/if/test_expectations.yml +34 -0
  157. data/smoke/implements/a.rb +0 -2
  158. data/smoke/implements/test_expectations.yml +23 -0
  159. data/smoke/initialize/test_expectations.yml +1 -0
  160. data/smoke/integer/a.rb +0 -7
  161. data/smoke/integer/test_expectations.yml +101 -0
  162. data/smoke/interface/a.rb +0 -2
  163. data/smoke/interface/test_expectations.yml +23 -0
  164. data/smoke/kwbegin/a.rb +0 -1
  165. data/smoke/kwbegin/test_expectations.yml +17 -0
  166. data/smoke/lambda/a.rb +1 -4
  167. data/smoke/lambda/test_expectations.yml +39 -0
  168. data/smoke/literal/a.rb +0 -5
  169. data/smoke/literal/b.rb +0 -2
  170. data/smoke/literal/test_expectations.yml +106 -0
  171. data/smoke/map/test_expectations.yml +1 -0
  172. data/smoke/method/a.rb +0 -5
  173. data/smoke/method/b.rb +0 -1
  174. data/smoke/method/test_expectations.yml +90 -0
  175. data/smoke/module/a.rb +0 -2
  176. data/smoke/module/b.rb +0 -2
  177. data/smoke/module/c.rb +0 -1
  178. data/smoke/module/d.rb +0 -1
  179. data/smoke/module/f.rb +0 -2
  180. data/smoke/module/test_expectations.yml +75 -0
  181. data/smoke/regexp/a.rb +0 -38
  182. data/smoke/regexp/b.rb +0 -26
  183. data/smoke/regexp/test_expectations.yml +615 -0
  184. data/smoke/regression/set_divide.rb +0 -4
  185. data/smoke/regression/test_expectations.yml +43 -0
  186. data/smoke/rescue/a.rb +0 -5
  187. data/smoke/rescue/test_expectations.yml +79 -0
  188. data/smoke/self/a.rb +0 -2
  189. data/smoke/self/test_expectations.yml +23 -0
  190. data/smoke/skip/skip.rb +0 -2
  191. data/smoke/skip/test_expectations.yml +23 -0
  192. data/smoke/stdout/test_expectations.yml +1 -0
  193. data/smoke/super/a.rb +0 -4
  194. data/smoke/super/test_expectations.yml +79 -0
  195. data/smoke/toplevel/a.rb +0 -1
  196. data/smoke/toplevel/test_expectations.yml +15 -0
  197. data/smoke/tsort/Steepfile +2 -0
  198. data/smoke/tsort/a.rb +0 -3
  199. data/smoke/tsort/test_expectations.yml +63 -0
  200. data/smoke/type_case/a.rb +0 -4
  201. data/smoke/type_case/test_expectations.yml +48 -0
  202. data/smoke/unexpected/Steepfile +5 -0
  203. data/smoke/unexpected/test_expectations.yml +25 -0
  204. data/smoke/unexpected/unexpected.rb +1 -0
  205. data/smoke/unexpected/unexpected.rbs +3 -0
  206. data/smoke/yield/a.rb +0 -3
  207. data/smoke/yield/b.rb +6 -0
  208. data/smoke/yield/test_expectations.yml +68 -0
  209. data/steep.gemspec +4 -3
  210. metadata +144 -29
  211. data/bin/smoke_runner.rb +0 -139
  212. data/lib/steep/drivers/signature_error_printer.rb +0 -25
  213. data/lib/steep/errors.rb +0 -565
  214. data/lib/steep/project/file_loader.rb +0 -68
  215. data/lib/steep/project/signature_file.rb +0 -33
  216. data/lib/steep/project/source_file.rb +0 -129
  217. data/lib/steep/server/code_worker.rb +0 -137
  218. data/lib/steep/server/signature_worker.rb +0 -152
  219. data/lib/steep/server/utils.rb +0 -69
  220. data/lib/steep/signature/errors.rb +0 -82
  221. data/lib/steep/type_assignability.rb +0 -367
@@ -0,0 +1,29 @@
1
+ class RBS::Location
2
+ def as_lsp_range
3
+ {
4
+ start: {
5
+ line: start_line - 1,
6
+ character: start_column
7
+ },
8
+ end: {
9
+ line: end_line - 1,
10
+ character: end_column
11
+ }
12
+ }
13
+ end
14
+ end
15
+
16
+ class Parser::Source::Range
17
+ def as_lsp_range
18
+ {
19
+ start: {
20
+ line: line - 1,
21
+ character: column
22
+ },
23
+ end: {
24
+ line: last_line - 1,
25
+ character: last_column
26
+ }
27
+ }
28
+ end
29
+ end
@@ -3,18 +3,31 @@ module Steep
3
3
  class BaseWorker
4
4
  LSP = LanguageServer::Protocol
5
5
 
6
- include Utils
7
-
8
6
  attr_reader :project
9
- attr_reader :reader, :writer
7
+ attr_reader :reader, :writer, :queue
8
+
9
+ ShutdownJob = Struct.new(:id, keyword_init: true)
10
10
 
11
11
  def initialize(project:, reader:, writer:)
12
12
  @project = project
13
13
  @reader = reader
14
14
  @writer = writer
15
+ @skip_job = false
15
16
  @shutdown = false
16
17
  end
17
18
 
19
+ def skip_jobs_after_shutdown!(flag = true)
20
+ @skip_jobs_after_shutdown = flag
21
+ end
22
+
23
+ def skip_jobs_after_shutdown?
24
+ @skip_jobs_after_shutdown
25
+ end
26
+
27
+ def skip_job?
28
+ @skip_job
29
+ end
30
+
18
31
  def handle_request(request)
19
32
  # process request
20
33
  end
@@ -29,7 +42,29 @@ module Steep
29
42
  Steep.logger.formatter.push_tags(*tags)
30
43
  Steep.logger.tagged "background" do
31
44
  while job = queue.pop
32
- handle_job(job) unless @shutdown
45
+ case job
46
+ when ShutdownJob
47
+ writer.write(id: job.id, result: nil)
48
+ else
49
+ if skip_job?
50
+ Steep.logger.info "Skipping job..."
51
+ else
52
+ begin
53
+ handle_job(job)
54
+ rescue => exn
55
+ Steep.log_error exn
56
+ writer.write(
57
+ {
58
+ method: "window/showMessage",
59
+ params: {
60
+ type: LSP::Constant::MessageType::ERROR,
61
+ message: "Unexpected error: #{exn.message} (#{exn.class})"
62
+ }
63
+ }
64
+ )
65
+ end
66
+ end
67
+ end
33
68
  end
34
69
  end
35
70
  end
@@ -37,10 +72,12 @@ module Steep
37
72
  Steep.logger.tagged "frontend" do
38
73
  begin
39
74
  reader.read do |request|
75
+ Steep.logger.info "Received message from master: #{request[:method]}(#{request[:id]})"
40
76
  case request[:method]
41
77
  when "shutdown"
42
- @shutdown = true
43
- writer.write(id: request[:id], result: nil)
78
+ queue << ShutdownJob.new(id: request[:id])
79
+ @skip_job = skip_jobs_after_shutdown?
80
+ queue.close
44
81
  when "exit"
45
82
  break
46
83
  else
@@ -48,7 +85,6 @@ module Steep
48
85
  end
49
86
  end
50
87
  ensure
51
- queue << nil
52
88
  thread.join
53
89
  end
54
90
  end
@@ -0,0 +1,63 @@
1
+ module Steep
2
+ module Server
3
+ module ChangeBuffer
4
+ attr_reader :mutex
5
+ attr_reader :buffered_changes
6
+
7
+ def push_buffer
8
+ @mutex.synchronize do
9
+ yield buffered_changes
10
+ end
11
+ end
12
+
13
+ def pop_buffer
14
+ changes = {}
15
+ @mutex.synchronize do
16
+ changes.merge!(buffered_changes)
17
+ buffered_changes.clear
18
+ end
19
+ if block_given?
20
+ yield changes
21
+ else
22
+ changes
23
+ end
24
+ end
25
+
26
+ def load_files(project:, commandline_args:)
27
+ Steep.logger.tagged "#load_files" do
28
+ push_buffer do |changes|
29
+ loader = Services::FileLoader.new(base_dir: project.base_dir)
30
+
31
+ Steep.measure "load changes from disk" do
32
+ project.targets.each do |target|
33
+ loader.load_changes(target.source_pattern, commandline_args, changes: changes)
34
+ loader.load_changes(target.signature_pattern, changes: changes)
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+
41
+ def collect_changes(request)
42
+ push_buffer do |changes|
43
+ path = project.relative_path(Pathname(URI.parse(request[:params][:textDocument][:uri]).path))
44
+ version = request[:params][:textDocument][:version]
45
+ Steep.logger.info { "Updating source: path=#{path}, version=#{version}..." }
46
+
47
+ changes[path] ||= []
48
+ request[:params][:contentChanges].each do |change|
49
+ changes[path] << Services::ContentChange.new(
50
+ range: change[:range]&.yield_self {|range|
51
+ [
52
+ range[:start].yield_self {|pos| Services::ContentChange::Position.new(line: pos[:line] + 1, column: pos[:character]) },
53
+ range[:end].yield_self {|pos| Services::ContentChange::Position.new(line: pos[:line] + 1, column: pos[:character]) }
54
+ ]
55
+ },
56
+ text: change[:text]
57
+ )
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
@@ -1,38 +1,63 @@
1
1
  module Steep
2
2
  module Server
3
3
  class InteractionWorker < BaseWorker
4
- attr_reader :queue
4
+ include ChangeBuffer
5
+
6
+ ApplyChangeJob = Class.new()
7
+ HoverJob = Struct.new(:id, :path, :line, :column, keyword_init: true)
8
+ CompletionJob = Struct.new(:id, :path, :line, :column, :trigger, keyword_init: true)
9
+
10
+ attr_reader :service
5
11
 
6
12
  def initialize(project:, reader:, writer:, queue: Queue.new)
7
13
  super(project: project, reader: reader, writer: writer)
8
14
  @queue = queue
15
+ @service = Services::TypeCheckService.new(project: project, assignment: Services::PathAssignment.all)
16
+ service.no_type_checking!
17
+ @mutex = Mutex.new
18
+ @buffered_changes = {}
9
19
  end
10
20
 
11
21
  def handle_job(job)
12
- Steep.logger.debug "Handling job: id=#{job[:id]}, result=#{job[:result]&.to_hash}"
13
- writer.write(job)
22
+ Steep.logger.tagged "#handle_job" do
23
+ changes = pop_buffer()
24
+
25
+ unless changes.empty?
26
+ Steep.logger.debug { "Applying changes for #{changes.size} files..." }
27
+ service.update(changes: changes) {}
28
+ end
29
+
30
+ case job
31
+ when ApplyChangeJob
32
+ # nop
33
+ when HoverJob
34
+ writer.write({ id: job.id, result: process_hover(job) })
35
+ when CompletionJob
36
+ writer.write({ id: job.id, result: process_completion(job) })
37
+ end
38
+ end
14
39
  end
15
40
 
16
41
  def handle_request(request)
17
42
  case request[:method]
18
43
  when "initialize"
19
- # nop
44
+ load_files(project: project, commandline_args: [])
45
+ queue << ApplyChangeJob.new
46
+ writer.write({ id: request[:id], result: nil })
20
47
 
21
48
  when "textDocument/didChange"
22
- update_source(request)
49
+ collect_changes(request)
50
+ queue << ApplyChangeJob.new
23
51
 
24
52
  when "textDocument/hover"
25
53
  id = request[:id]
26
54
 
27
55
  uri = URI.parse(request[:params][:textDocument][:uri])
28
56
  path = project.relative_path(Pathname(uri.path))
29
- line = request[:params][:position][:line]
57
+ line = request[:params][:position][:line]+1
30
58
  column = request[:params][:position][:character]
31
59
 
32
- queue << {
33
- id: id,
34
- result: response_to_hover(path: path, line: line, column: column)
35
- }
60
+ queue << HoverJob.new(id: id, path: path, line: line, column: column)
36
61
 
37
62
  when "textDocument/completion"
38
63
  id = request[:id]
@@ -43,20 +68,17 @@ module Steep
43
68
  line, column = params[:position].yield_self {|hash| [hash[:line]+1, hash[:character]] }
44
69
  trigger = params[:context][:triggerCharacter]
45
70
 
46
- queue << {
47
- id: id,
48
- result: response_to_completion(path: path, line: line, column: column, trigger: trigger)
49
- }
71
+ queue << CompletionJob.new(id: id, path: path, line: line, column: column, trigger: trigger)
50
72
  end
51
73
  end
52
74
 
53
- def response_to_hover(path:, line:, column:)
54
- Steep.logger.tagged "#response_to_hover" do
55
- Steep.measure "Generating response" do
56
- Steep.logger.info { "path=#{path}, line=#{line}, column=#{column}" }
75
+ def process_hover(job)
76
+ Steep.logger.tagged "#process_hover" do
77
+ Steep.measure "Generating hover response" do
78
+ Steep.logger.info { "path=#{job.path}, line=#{job.line}, column=#{job.column}" }
57
79
 
58
- hover = Project::HoverContent.new(project: project)
59
- content = hover.content_for(path: path, line: line+1, column: column+1)
80
+ hover = Services::HoverContent.new(service: service)
81
+ content = hover.content_for(path: job.path, line: job.line, column: job.column+1)
60
82
  if content
61
83
  range = content.location.yield_self do |location|
62
84
  start_position = { line: location.line - 1, character: location.column }
@@ -69,22 +91,22 @@ module Steep
69
91
  range: range
70
92
  )
71
93
  end
94
+ rescue Typing::UnknownNodeError => exn
95
+ Steep.log_error exn, message: "Failed to compute hover: #{exn.inspect}"
96
+ nil
72
97
  end
73
- rescue Typing::UnknownNodeError => exn
74
- Steep.log_error exn, message: "Failed to compute hover: #{exn.inspect}"
75
- nil
76
98
  end
77
99
  end
78
100
 
79
101
  def format_hover(content)
80
102
  case content
81
- when Project::HoverContent::VariableContent
103
+ when Services::HoverContent::VariableContent
82
104
  "`#{content.name}`: `#{content.type.to_s}`"
83
- when Project::HoverContent::MethodCallContent
105
+ when Services::HoverContent::MethodCallContent
84
106
  method_name = case content.method_name
85
- when Project::HoverContent::InstanceMethodName
107
+ when Services::HoverContent::InstanceMethodName
86
108
  "#{content.method_name.class_name}##{content.method_name.method_name}"
87
- when Project::HoverContent::SingletonMethodName
109
+ when Services::HoverContent::SingletonMethodName
88
110
  "#{content.method_name.class_name}.#{content.method_name.method_name}"
89
111
  else
90
112
  nil
@@ -106,7 +128,7 @@ HOVER
106
128
  else
107
129
  "`#{content.type}`"
108
130
  end
109
- when Project::HoverContent::DefinitionContent
131
+ when Services::HoverContent::DefinitionContent
110
132
  string = <<HOVER
111
133
  ```
112
134
  def #{content.method_name}: #{content.method_type}
@@ -121,42 +143,37 @@ HOVER
121
143
  end
122
144
 
123
145
  string
124
- when Project::HoverContent::TypeContent
146
+ when Services::HoverContent::TypeContent
125
147
  "`#{content.type}`"
126
148
  end
127
149
  end
128
150
 
129
- def response_to_completion(path:, line:, column:, trigger:)
151
+ def process_completion(job)
130
152
  Steep.logger.tagged("#response_to_completion") do
131
153
  Steep.measure "Generating response" do
132
- Steep.logger.info "path: #{path}, line: #{line}, column: #{column}, trigger: #{trigger}"
133
-
134
- target = project.target_for_source_path(path) or return
135
- target.type_check(target_sources: [], validate_signatures: false)
154
+ Steep.logger.info "path: #{job.path}, line: #{job.line}, column: #{job.column}, trigger: #{job.trigger}"
136
155
 
137
- case (status = target&.status)
138
- when Project::Target::TypeCheckStatus
139
- subtyping = status.subtyping
140
- source = target.source_files[path]
156
+ target = project.target_for_source_path(job.path) or return
157
+ file = service.source_files[job.path] or return
158
+ subtyping = service.signature_services[target.name].current_subtyping or return
141
159
 
142
- provider = Project::CompletionProvider.new(source_text: source.content, path: path, subtyping: subtyping)
143
- items = begin
144
- provider.run(line: line, column: column)
145
- rescue Parser::SyntaxError
146
- []
147
- end
160
+ provider = Services::CompletionProvider.new(source_text: file.content, path: job.path, subtyping: subtyping)
161
+ items = begin
162
+ provider.run(line: job.line, column: job.column)
163
+ rescue Parser::SyntaxError
164
+ []
165
+ end
148
166
 
149
- completion_items = items.map do |item|
150
- format_completion_item(item)
151
- end
167
+ completion_items = items.map do |item|
168
+ format_completion_item(item)
169
+ end
152
170
 
153
- Steep.logger.debug "items = #{completion_items.inspect}"
171
+ Steep.logger.debug "items = #{completion_items.inspect}"
154
172
 
155
- LSP::Interface::CompletionList.new(
156
- is_incomplete: false,
157
- items: completion_items
158
- )
159
- end
173
+ LSP::Interface::CompletionList.new(
174
+ is_incomplete: false,
175
+ items: completion_items
176
+ )
160
177
  end
161
178
  end
162
179
  end
@@ -174,7 +191,7 @@ HOVER
174
191
  )
175
192
 
176
193
  case item
177
- when Project::CompletionProvider::LocalVariableItem
194
+ when Services::CompletionProvider::LocalVariableItem
178
195
  LanguageServer::Protocol::Interface::CompletionItem.new(
179
196
  label: item.identifier,
180
197
  kind: LanguageServer::Protocol::Constant::CompletionItemKind::VARIABLE,
@@ -184,7 +201,7 @@ HOVER
184
201
  new_text: "#{item.identifier}"
185
202
  )
186
203
  )
187
- when Project::CompletionProvider::MethodNameItem
204
+ when Services::CompletionProvider::MethodNameItem
188
205
  label = "def #{item.identifier}: #{item.method_type}"
189
206
  method_type_snippet = method_type_to_snippet(item.method_type)
190
207
  LanguageServer::Protocol::Interface::CompletionItem.new(
@@ -198,7 +215,7 @@ HOVER
198
215
  insert_text_format: LanguageServer::Protocol::Constant::InsertTextFormat::SNIPPET,
199
216
  sort_text: item.inherited? ? 'z' : 'a' # Ensure language server puts non-inherited methods before inherited methods
200
217
  )
201
- when Project::CompletionProvider::InstanceVariableItem
218
+ when Services::CompletionProvider::InstanceVariableItem
202
219
  label = "#{item.identifier}: #{item.type}"
203
220
  LanguageServer::Protocol::Interface::CompletionItem.new(
204
221
  label: label,
@@ -6,182 +6,318 @@ module Steep
6
6
  attr_reader :steepfile
7
7
  attr_reader :project
8
8
  attr_reader :reader, :writer
9
- attr_reader :queue
10
- attr_reader :worker_count
11
- attr_reader :worker_to_paths
12
9
 
13
10
  attr_reader :interaction_worker
14
- attr_reader :signature_worker
15
- attr_reader :code_workers
11
+ attr_reader :typecheck_workers
12
+
13
+ attr_reader :response_handlers
14
+
15
+ # There are four types of threads:
16
+ #
17
+ # 1. Main thread -- Reads messages from client
18
+ # 2. Worker threads -- Reads messages from associated worker
19
+ # 3. Reconciliation thread -- Receives message from worker threads, reconciles, processes, and forwards to write thread
20
+ # 4. Write thread -- Writes messages to client
21
+ #
22
+ # We have two queues:
23
+ #
24
+ # 1. `recon_queue` is to pass messages from worker threads to reconciliation thread
25
+ # 2. `write` thread is to pass messages to write thread
26
+ #
27
+ # Message passing: Client -> Server (Master) -> Worker
28
+ #
29
+ # 1. Client -> Server
30
+ # Master receives messages from the LSP client on main thread.
31
+ #
32
+ # 2. Master -> Worker
33
+ # Master writes messages to workers on main thread.
34
+ #
35
+ # Message passing: Worker -> Server (Master) -> (reconciliation queue) -> (write queue) -> Client
36
+ #
37
+ # 3. Worker -> Master
38
+ # Master receives messages on threads dedicated for each worker.
39
+ # The messages sent from workers are then forwarded to the reconciliation thread through reconciliation queue.
40
+ #
41
+ # 4. Server -> Client
42
+ # The reconciliation thread reads messages from reconciliation queue, does something, and finally sends messages to the client via write queue.
43
+ #
44
+ attr_reader :write_queue
45
+ attr_reader :recon_queue
46
+
47
+ class ResponseHandler
48
+ attr_reader :workers
49
+
50
+ attr_reader :request
51
+ attr_reader :responses
52
+
53
+ attr_reader :on_response_handlers
54
+ attr_reader :on_completion_handlers
55
+
56
+ def initialize(request:, workers:)
57
+ @workers = []
58
+
59
+ @request = request
60
+ @responses = workers.each.with_object({}) do |worker, hash|
61
+ hash[worker] = nil
62
+ end
63
+
64
+ @on_response_handlers = []
65
+ @on_completion_handlers = []
66
+ end
67
+
68
+ def on_response(&block)
69
+ on_response_handlers << block
70
+ end
71
+
72
+ def on_completion(&block)
73
+ on_completion_handlers << block
74
+ end
75
+
76
+ def request_id
77
+ request[:id]
78
+ end
79
+
80
+ def process_response(response, worker)
81
+ responses[worker] = response
82
+
83
+ on_response_handlers.each do |handler|
84
+ handler[worker, response]
85
+ end
16
86
 
17
- include Utils
87
+ if completed?
88
+ on_completion_handlers.each do |handler|
89
+ handler[*responses.values]
90
+ end
91
+ end
92
+ end
18
93
 
19
- def initialize(project:, reader:, writer:, interaction_worker:, signature_worker:, code_workers:, queue: Queue.new)
94
+ def completed?
95
+ responses.each_value.none?(&:nil?)
96
+ end
97
+ end
98
+
99
+ def initialize(project:, reader:, writer:, interaction_worker:, typecheck_workers:, queue: Queue.new)
20
100
  @project = project
21
101
  @reader = reader
22
102
  @writer = writer
23
- @queue = queue
103
+ @write_queue = queue
104
+ @recon_queue = Queue.new
24
105
  @interaction_worker = interaction_worker
25
- @signature_worker = signature_worker
26
- @code_workers = code_workers
27
- @worker_to_paths = {}
106
+ @typecheck_workers = typecheck_workers
28
107
  @shutdown_request_id = nil
108
+ @response_handlers = {}
29
109
  end
30
110
 
31
111
  def start
32
- source_paths = project.all_source_files
33
- bin_size = (source_paths.size / code_workers.size) + 1
34
- source_paths.each_slice(bin_size).with_index do |paths, index|
35
- register_code_to_worker(paths, worker: code_workers[index])
36
- end
112
+ Steep.logger.tagged "master" do
113
+ tags = Steep.logger.formatter.current_tags.dup
37
114
 
38
- tags = Steep.logger.formatter.current_tags.dup
39
- tags << "master"
115
+ worker_threads = []
40
116
 
41
- Thread.new do
42
- Steep.logger.formatter.push_tags(*tags, "from-worker@interaction")
43
- interaction_worker.reader.read do |message|
44
- process_message_from_worker(message)
117
+ if interaction_worker
118
+ worker_threads << Thread.new do
119
+ Steep.logger.formatter.push_tags(*tags, "from-worker@interaction")
120
+ interaction_worker.reader.read do |message|
121
+ process_message_from_worker(message, worker: interaction_worker)
122
+ end
123
+ end
45
124
  end
46
- end
47
125
 
48
- Thread.new do
49
- Steep.logger.formatter.push_tags(*tags, "from-worker@signature")
50
- signature_worker.reader.read do |message|
51
- process_message_from_worker(message)
126
+ typecheck_workers.each do |worker|
127
+ worker_threads << Thread.new do
128
+ Steep.logger.formatter.push_tags(*tags, "from-worker@#{worker.name}")
129
+ worker.reader.read do |message|
130
+ process_message_from_worker(message, worker: worker)
131
+ end
132
+ end
52
133
  end
53
- end
54
134
 
55
- code_workers.each do |worker|
56
- Thread.new do
57
- Steep.logger.formatter.push_tags(*tags, "from-worker@#{worker.name}")
58
- worker.reader.read do |message|
59
- process_message_from_worker(message)
135
+ worker_threads << Thread.new do
136
+ Steep.logger.formatter.push_tags(*tags, "write")
137
+ while message = write_queue.pop
138
+ writer.write(message)
60
139
  end
61
- end
62
- end
63
140
 
64
- Thread.new do
65
- Steep.logger.formatter.push_tags(*tags, "from-client")
66
- reader.read do |request|
67
- process_message_from_client(request)
141
+ writer.io.close
68
142
  end
69
- end
70
143
 
71
- while job = queue.pop
72
- if @shutdown_request_id
73
- if job[:id] == @shutdown_request_id
74
- writer.write(job)
75
- break
144
+ worker_threads << Thread.new do
145
+ Steep.logger.formatter.push_tags(*tags, "reconciliation")
146
+ while (message, worker = recon_queue.pop)
147
+ id = message[:id]
148
+ handler = response_handlers[id] or raise
149
+
150
+ Steep.logger.info "Processing response to #{handler.request[:method]}(#{id}) from #{worker.name}"
151
+
152
+ handler.process_response(message, worker)
153
+
154
+ if handler.completed?
155
+ Steep.logger.info "Response to #{handler.request[:method]}(#{id}) completed"
156
+ response_handlers.delete(id)
157
+ end
76
158
  end
77
- else
78
- writer.write(job)
79
159
  end
80
- end
81
160
 
82
- writer.io.close
161
+ Steep.logger.tagged "main" do
162
+ reader.read do |request|
163
+ process_message_from_client(request) or break
164
+ end
83
165
 
84
- each_worker do |w|
85
- w.shutdown()
166
+ worker_threads.each do |thread|
167
+ thread.join
168
+ end
169
+ end
86
170
  end
87
171
  end
88
172
 
89
173
  def each_worker(&block)
90
174
  if block_given?
91
- yield interaction_worker
92
- yield signature_worker
93
- code_workers.each &block
175
+ yield interaction_worker if interaction_worker
176
+ typecheck_workers.each &block
94
177
  else
95
178
  enum_for :each_worker
96
179
  end
97
180
  end
98
181
 
99
182
  def process_message_from_client(message)
183
+ Steep.logger.info "Received message #{message[:method]}(#{message[:id]})"
100
184
  id = message[:id]
101
185
 
102
186
  case message[:method]
103
187
  when "initialize"
104
- queue << {
105
- id: id,
106
- result: LSP::Interface::InitializeResult.new(
107
- capabilities: LSP::Interface::ServerCapabilities.new(
108
- text_document_sync: LSP::Interface::TextDocumentSyncOptions.new(
109
- change: LSP::Constant::TextDocumentSyncKind::INCREMENTAL
110
- ),
111
- hover_provider: true,
112
- completion_provider: LSP::Interface::CompletionOptions.new(
113
- trigger_characters: [".", "@"]
188
+ broadcast_request(message) do |handler|
189
+ handler.on_completion do
190
+ write_queue << {
191
+ id: id,
192
+ result: LSP::Interface::InitializeResult.new(
193
+ capabilities: LSP::Interface::ServerCapabilities.new(
194
+ text_document_sync: LSP::Interface::TextDocumentSyncOptions.new(
195
+ change: LSP::Constant::TextDocumentSyncKind::INCREMENTAL
196
+ ),
197
+ hover_provider: true,
198
+ completion_provider: LSP::Interface::CompletionOptions.new(
199
+ trigger_characters: [".", "@"]
200
+ ),
201
+ workspace_symbol_provider: true
202
+ )
114
203
  )
115
- )
116
- )
117
- }
118
-
119
- each_worker do |worker|
120
- worker << message
204
+ }
205
+ end
121
206
  end
122
207
 
123
208
  when "textDocument/didChange"
124
- update_source(message)
125
-
126
- uri = URI.parse(message[:params][:textDocument][:uri])
127
- path = project.relative_path(Pathname(uri.path))
128
-
129
- unless registered_path?(path)
130
- register_code_to_worker [path], worker: least_busy_worker()
209
+ broadcast_notification(message)
210
+
211
+ when "textDocument/hover", "textDocument/completion"
212
+ if interaction_worker
213
+ send_request(message, worker: interaction_worker) do |handler|
214
+ handler.on_completion do |response|
215
+ write_queue << response
216
+ end
217
+ end
131
218
  end
132
219
 
133
- each_worker do |worker|
134
- worker << message
135
- end
220
+ when "textDocument/open"
221
+ # Ignores open notification
136
222
 
137
- when "textDocument/hover"
138
- interaction_worker << message
223
+ when "workspace/symbol"
224
+ send_request(message, workers: typecheck_workers) do |handler|
225
+ handler.on_completion do |*responses|
226
+ result = responses.flat_map {|resp| resp[:result] || [] }
139
227
 
140
- when "textDocument/completion"
141
- interaction_worker << message
228
+ write_queue << {
229
+ id: handler.request_id,
230
+ result: result
231
+ }
232
+ end
233
+ end
142
234
 
143
- when "textDocument/open"
144
- # Ignores open notification
235
+ when "workspace/executeCommand"
236
+ case message[:params][:command]
237
+ when "steep/stats"
238
+ send_request(message, workers: typecheck_workers) do |handler|
239
+ handler.on_completion do |*responses|
240
+ stats = responses.flat_map {|resp| resp[:result] }
241
+ write_queue << {
242
+ id: handler.request_id,
243
+ result: stats
244
+ }
245
+ end
246
+ end
247
+ end
145
248
 
146
249
  when "shutdown"
147
- queue << { id: id, result: nil }
148
- @shutdown_request_id = id
250
+ broadcast_request(message) do |handler|
251
+ handler.on_completion do |*_|
252
+ write_queue << { id: id, result: nil}
253
+
254
+ write_queue.close
255
+ recon_queue.close
256
+ end
257
+ end
149
258
 
150
259
  when "exit"
151
- queue << nil
260
+ broadcast_notification(message)
261
+
262
+ return false
152
263
  end
264
+
265
+ true
153
266
  end
154
267
 
155
- def process_message_from_worker(message)
156
- queue << message
268
+ def broadcast_notification(message)
269
+ Steep.logger.info "Broadcasting notification #{message[:method]}"
270
+ each_worker do |worker|
271
+ worker << message
272
+ end
157
273
  end
158
274
 
159
- def paths_for(worker)
160
- worker_to_paths[worker] ||= Set[]
275
+ def send_notification(message, worker:)
276
+ Steep.logger.info "Sending notification #{message[:method]} to #{worker.name}"
277
+ worker << message
161
278
  end
162
279
 
163
- def least_busy_worker
164
- code_workers.min_by do |w|
165
- paths_for(w).size
280
+ def send_request(message, worker: nil, workers: [])
281
+ workers << worker if worker
282
+
283
+ Steep.logger.info "Sending request #{message[:method]}(#{message[:id]}) to #{workers.map(&:name).join(", ")}"
284
+ handler = ResponseHandler.new(request: message, workers: workers)
285
+ yield(handler) if block_given?
286
+ response_handlers[handler.request_id] = handler
287
+
288
+ workers.each do |w|
289
+ w << message
166
290
  end
167
291
  end
168
292
 
169
- def registered_path?(path)
170
- worker_to_paths.each_value.any? {|set| set.include?(path) }
293
+ def broadcast_request(message)
294
+ Steep.logger.info "Broadcasting request #{message[:method]}(#{message[:id]})"
295
+ handler = ResponseHandler.new(request: message, workers: each_worker.to_a)
296
+ yield(handler) if block_given?
297
+ response_handlers[handler.request_id] = handler
298
+
299
+ each_worker do |worker|
300
+ worker << message
301
+ end
171
302
  end
172
303
 
173
- def register_code_to_worker(paths, worker:)
174
- paths_for(worker).merge(paths)
304
+ def process_message_from_worker(message, worker:)
305
+ case
306
+ when message.key?(:id) && !message.key?(:method)
307
+ # Response from worker
308
+ Steep.logger.debug { "Received response #{message[:id]} from worker" }
309
+ recon_queue << [message, worker]
310
+ when message.key?(:method) && !message.key?(:id)
311
+ # Notification from worker
312
+ Steep.logger.debug { "Received notification #{message[:method]} from worker" }
313
+ write_queue << message
314
+ end
315
+ end
175
316
 
176
- worker << {
177
- method: "workspace/executeCommand",
178
- params: LSP::Interface::ExecuteCommandParams.new(
179
- command: "steep/registerSourceToWorker",
180
- arguments: paths.map do |path|
181
- "file://#{project.absolute_path(path)}"
182
- end
183
- )
184
- }
317
+ def kill
318
+ each_worker do |worker|
319
+ worker.kill
320
+ end
185
321
  end
186
322
  end
187
323
  end