solargraph 0.46.0 → 0.54.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (279) hide show
  1. checksums.yaml +4 -4
  2. data/.github/FUNDING.yml +1 -0
  3. data/.github/workflows/plugins.yml +40 -0
  4. data/.github/workflows/rspec.yml +37 -41
  5. data/.github/workflows/typecheck.yml +34 -0
  6. data/.gitignore +9 -9
  7. data/.rspec +2 -2
  8. data/.yardopts +2 -2
  9. data/CHANGELOG.md +1338 -1115
  10. data/Gemfile +0 -0
  11. data/LICENSE +1 -1
  12. data/README.md +131 -128
  13. data/Rakefile +0 -0
  14. data/SPONSORS.md +10 -18
  15. data/bin/solargraph +0 -0
  16. data/lib/solargraph/api_map/cache.rb +109 -70
  17. data/lib/solargraph/api_map/index.rb +167 -0
  18. data/lib/solargraph/api_map/source_to_yard.rb +88 -81
  19. data/lib/solargraph/api_map/store.rb +260 -256
  20. data/lib/solargraph/api_map.rb +870 -686
  21. data/lib/solargraph/bench.rb +44 -27
  22. data/lib/solargraph/cache.rb +77 -0
  23. data/lib/solargraph/complex_type/type_methods.rb +217 -130
  24. data/lib/solargraph/complex_type/unique_type.rb +386 -75
  25. data/lib/solargraph/complex_type.rb +394 -221
  26. data/lib/solargraph/convention/base.rb +33 -33
  27. data/lib/solargraph/convention/gemfile.rb +15 -15
  28. data/lib/solargraph/convention/gemspec.rb +22 -22
  29. data/lib/solargraph/convention/rakefile.rb +17 -0
  30. data/lib/solargraph/convention.rb +47 -47
  31. data/lib/solargraph/converters/dd.rb +17 -12
  32. data/lib/solargraph/converters/dl.rb +15 -12
  33. data/lib/solargraph/converters/dt.rb +15 -12
  34. data/lib/solargraph/converters/misc.rb +1 -1
  35. data/lib/solargraph/diagnostics/base.rb +29 -29
  36. data/lib/solargraph/diagnostics/require_not_found.rb +53 -53
  37. data/lib/solargraph/diagnostics/rubocop.rb +113 -98
  38. data/lib/solargraph/diagnostics/rubocop_helpers.rb +66 -63
  39. data/lib/solargraph/diagnostics/severities.rb +15 -15
  40. data/lib/solargraph/diagnostics/type_check.rb +55 -54
  41. data/lib/solargraph/diagnostics/update_errors.rb +41 -41
  42. data/lib/solargraph/diagnostics.rb +55 -55
  43. data/lib/solargraph/doc_map.rb +188 -0
  44. data/lib/solargraph/environ.rb +45 -45
  45. data/lib/solargraph/equality.rb +33 -0
  46. data/lib/solargraph/gem_pins.rb +72 -0
  47. data/lib/solargraph/language_server/completion_item_kinds.rb +35 -35
  48. data/lib/solargraph/language_server/error_codes.rb +20 -20
  49. data/lib/solargraph/language_server/host/diagnoser.rb +89 -89
  50. data/lib/solargraph/language_server/host/dispatch.rb +128 -111
  51. data/lib/solargraph/language_server/host/message_worker.rb +106 -59
  52. data/lib/solargraph/language_server/host/sources.rb +99 -156
  53. data/lib/solargraph/language_server/host.rb +861 -865
  54. data/lib/solargraph/language_server/message/base.rb +96 -89
  55. data/lib/solargraph/language_server/message/cancel_request.rb +13 -13
  56. data/lib/solargraph/language_server/message/client/register_capability.rb +15 -15
  57. data/lib/solargraph/language_server/message/client.rb +11 -11
  58. data/lib/solargraph/language_server/message/completion_item/resolve.rb +60 -58
  59. data/lib/solargraph/language_server/message/completion_item.rb +11 -11
  60. data/lib/solargraph/language_server/message/exit_notification.rb +13 -13
  61. data/lib/solargraph/language_server/message/extended/check_gem_version.rb +112 -100
  62. data/lib/solargraph/language_server/message/extended/document.rb +20 -20
  63. data/lib/solargraph/language_server/message/extended/document_gems.rb +32 -32
  64. data/lib/solargraph/language_server/message/extended/download_core.rb +19 -23
  65. data/lib/solargraph/language_server/message/extended/environment.rb +25 -25
  66. data/lib/solargraph/language_server/message/extended/search.rb +20 -20
  67. data/lib/solargraph/language_server/message/extended.rb +21 -21
  68. data/lib/solargraph/language_server/message/initialize.rb +191 -162
  69. data/lib/solargraph/language_server/message/initialized.rb +28 -27
  70. data/lib/solargraph/language_server/message/method_not_found.rb +16 -16
  71. data/lib/solargraph/language_server/message/method_not_implemented.rb +14 -14
  72. data/lib/solargraph/language_server/message/shutdown.rb +13 -13
  73. data/lib/solargraph/language_server/message/text_document/base.rb +19 -19
  74. data/lib/solargraph/language_server/message/text_document/code_action.rb +17 -17
  75. data/lib/solargraph/language_server/message/text_document/completion.rb +56 -59
  76. data/lib/solargraph/language_server/message/text_document/definition.rb +38 -38
  77. data/lib/solargraph/language_server/message/text_document/did_change.rb +15 -15
  78. data/lib/solargraph/language_server/message/text_document/did_close.rb +15 -15
  79. data/lib/solargraph/language_server/message/text_document/did_open.rb +15 -15
  80. data/lib/solargraph/language_server/message/text_document/did_save.rb +17 -17
  81. data/lib/solargraph/language_server/message/text_document/document_highlight.rb +16 -16
  82. data/lib/solargraph/language_server/message/text_document/document_symbol.rb +26 -23
  83. data/lib/solargraph/language_server/message/text_document/folding_range.rb +26 -26
  84. data/lib/solargraph/language_server/message/text_document/formatting.rb +131 -126
  85. data/lib/solargraph/language_server/message/text_document/hover.rb +58 -54
  86. data/lib/solargraph/language_server/message/text_document/on_type_formatting.rb +34 -34
  87. data/lib/solargraph/language_server/message/text_document/prepare_rename.rb +11 -11
  88. data/lib/solargraph/language_server/message/text_document/references.rb +16 -16
  89. data/lib/solargraph/language_server/message/text_document/rename.rb +19 -19
  90. data/lib/solargraph/language_server/message/text_document/signature_help.rb +24 -29
  91. data/lib/solargraph/language_server/message/text_document/type_definition.rb +24 -0
  92. data/lib/solargraph/language_server/message/text_document.rb +28 -28
  93. data/lib/solargraph/language_server/message/workspace/did_change_configuration.rb +35 -30
  94. data/lib/solargraph/language_server/message/workspace/did_change_watched_files.rb +40 -33
  95. data/lib/solargraph/language_server/message/workspace/did_change_workspace_folders.rb +24 -24
  96. data/lib/solargraph/language_server/message/workspace/workspace_symbol.rb +23 -23
  97. data/lib/solargraph/language_server/message/workspace.rb +14 -14
  98. data/lib/solargraph/language_server/message.rb +94 -93
  99. data/lib/solargraph/language_server/message_types.rb +14 -14
  100. data/lib/solargraph/language_server/progress.rb +135 -0
  101. data/lib/solargraph/language_server/request.rb +24 -24
  102. data/lib/solargraph/language_server/symbol_kinds.rb +36 -36
  103. data/lib/solargraph/language_server/transport/adapter.rb +68 -53
  104. data/lib/solargraph/language_server/transport/data_reader.rb +74 -72
  105. data/lib/solargraph/language_server/transport.rb +13 -13
  106. data/lib/solargraph/language_server/uri_helpers.rb +49 -49
  107. data/lib/solargraph/language_server.rb +20 -19
  108. data/lib/solargraph/library.rb +663 -546
  109. data/lib/solargraph/location.rb +58 -37
  110. data/lib/solargraph/logging.rb +27 -27
  111. data/lib/solargraph/page.rb +89 -83
  112. data/lib/solargraph/parser/comment_ripper.rb +56 -52
  113. data/lib/solargraph/parser/node_methods.rb +83 -43
  114. data/lib/solargraph/parser/node_processor/base.rb +87 -77
  115. data/lib/solargraph/parser/node_processor.rb +45 -43
  116. data/lib/solargraph/parser/{legacy → parser_gem}/class_methods.rb +153 -135
  117. data/lib/solargraph/parser/{legacy → parser_gem}/flawed_builder.rb +18 -16
  118. data/lib/solargraph/parser/{legacy → parser_gem}/node_chainer.rb +164 -148
  119. data/lib/solargraph/parser/parser_gem/node_methods.rb +495 -0
  120. data/lib/solargraph/parser/{rubyvm → parser_gem}/node_processors/alias_node.rb +23 -23
  121. data/lib/solargraph/parser/parser_gem/node_processors/args_node.rb +57 -0
  122. data/lib/solargraph/parser/{rubyvm → parser_gem}/node_processors/begin_node.rb +15 -15
  123. data/lib/solargraph/parser/{legacy → parser_gem}/node_processors/block_node.rb +43 -42
  124. data/lib/solargraph/parser/{legacy → parser_gem}/node_processors/casgn_node.rb +35 -25
  125. data/lib/solargraph/parser/{legacy → parser_gem}/node_processors/cvasgn_node.rb +23 -23
  126. data/lib/solargraph/parser/{rubyvm → parser_gem}/node_processors/def_node.rb +50 -63
  127. data/lib/solargraph/parser/{legacy → parser_gem}/node_processors/defs_node.rb +36 -36
  128. data/lib/solargraph/parser/{legacy → parser_gem}/node_processors/gvasgn_node.rb +23 -23
  129. data/lib/solargraph/parser/{legacy → parser_gem}/node_processors/ivasgn_node.rb +38 -38
  130. data/lib/solargraph/parser/{legacy → parser_gem}/node_processors/lvasgn_node.rb +28 -28
  131. data/lib/solargraph/parser/parser_gem/node_processors/masgn_node.rb +53 -0
  132. data/lib/solargraph/parser/{legacy → parser_gem}/node_processors/namespace_node.rb +39 -39
  133. data/lib/solargraph/parser/{legacy → parser_gem}/node_processors/orasgn_node.rb +16 -16
  134. data/lib/solargraph/parser/{legacy → parser_gem}/node_processors/resbody_node.rb +36 -36
  135. data/lib/solargraph/parser/parser_gem/node_processors/sclass_node.rb +42 -0
  136. data/lib/solargraph/parser/{legacy → parser_gem}/node_processors/send_node.rb +259 -257
  137. data/lib/solargraph/parser/{legacy → parser_gem}/node_processors/sym_node.rb +18 -18
  138. data/lib/solargraph/parser/parser_gem/node_processors.rb +56 -0
  139. data/lib/solargraph/parser/parser_gem.rb +12 -0
  140. data/lib/solargraph/parser/region.rb +66 -66
  141. data/lib/solargraph/parser/snippet.rb +15 -13
  142. data/lib/solargraph/parser.rb +22 -26
  143. data/lib/solargraph/pin/base.rb +378 -296
  144. data/lib/solargraph/pin/base_variable.rb +118 -84
  145. data/lib/solargraph/pin/block.rb +101 -72
  146. data/lib/solargraph/pin/callable.rb +147 -0
  147. data/lib/solargraph/pin/class_variable.rb +8 -8
  148. data/lib/solargraph/pin/closure.rb +57 -37
  149. data/lib/solargraph/pin/common.rb +70 -70
  150. data/lib/solargraph/pin/constant.rb +43 -43
  151. data/lib/solargraph/pin/conversions.rb +123 -96
  152. data/lib/solargraph/pin/delegated_method.rb +101 -0
  153. data/lib/solargraph/pin/documenting.rb +98 -105
  154. data/lib/solargraph/pin/duck_method.rb +16 -16
  155. data/lib/solargraph/pin/global_variable.rb +8 -8
  156. data/lib/solargraph/pin/instance_variable.rb +34 -30
  157. data/lib/solargraph/pin/keyword.rb +15 -15
  158. data/lib/solargraph/pin/keyword_param.rb +8 -8
  159. data/lib/solargraph/pin/local_variable.rb +67 -55
  160. data/lib/solargraph/pin/method.rb +527 -245
  161. data/lib/solargraph/pin/method_alias.rb +31 -31
  162. data/lib/solargraph/pin/namespace.rb +107 -91
  163. data/lib/solargraph/pin/parameter.rb +212 -201
  164. data/lib/solargraph/pin/proxy_type.rb +29 -29
  165. data/lib/solargraph/pin/reference/extend.rb +10 -10
  166. data/lib/solargraph/pin/reference/include.rb +10 -10
  167. data/lib/solargraph/pin/reference/override.rb +29 -29
  168. data/lib/solargraph/pin/reference/prepend.rb +10 -10
  169. data/lib/solargraph/pin/reference/require.rb +14 -14
  170. data/lib/solargraph/pin/reference/superclass.rb +10 -10
  171. data/lib/solargraph/pin/reference.rb +22 -14
  172. data/lib/solargraph/pin/search.rb +56 -56
  173. data/lib/solargraph/pin/signature.rb +17 -0
  174. data/lib/solargraph/pin/singleton.rb +11 -11
  175. data/lib/solargraph/pin/symbol.rb +47 -47
  176. data/lib/solargraph/pin.rb +41 -37
  177. data/lib/solargraph/position.rb +107 -100
  178. data/lib/solargraph/range.rb +98 -95
  179. data/lib/solargraph/rbs_map/conversions.rb +646 -0
  180. data/lib/solargraph/rbs_map/core_fills.rb +50 -0
  181. data/lib/solargraph/rbs_map/core_map.rb +28 -0
  182. data/lib/solargraph/rbs_map/stdlib_map.rb +33 -0
  183. data/lib/solargraph/rbs_map.rb +93 -0
  184. data/lib/solargraph/server_methods.rb +16 -16
  185. data/lib/solargraph/shell.rb +269 -226
  186. data/lib/solargraph/source/chain/array.rb +33 -0
  187. data/lib/solargraph/source/chain/block_symbol.rb +13 -0
  188. data/lib/solargraph/source/chain/block_variable.rb +13 -13
  189. data/lib/solargraph/source/chain/call.rb +303 -204
  190. data/lib/solargraph/source/chain/class_variable.rb +13 -13
  191. data/lib/solargraph/source/chain/constant.rb +89 -75
  192. data/lib/solargraph/source/chain/global_variable.rb +13 -13
  193. data/lib/solargraph/source/chain/hash.rb +33 -28
  194. data/lib/solargraph/source/chain/head.rb +19 -19
  195. data/lib/solargraph/source/chain/if.rb +28 -0
  196. data/lib/solargraph/source/chain/instance_variable.rb +13 -13
  197. data/lib/solargraph/source/chain/link.rb +98 -71
  198. data/lib/solargraph/source/chain/literal.rb +28 -23
  199. data/lib/solargraph/source/chain/or.rb +23 -23
  200. data/lib/solargraph/source/chain/q_call.rb +11 -11
  201. data/lib/solargraph/source/chain/variable.rb +13 -13
  202. data/lib/solargraph/source/chain/z_super.rb +30 -30
  203. data/lib/solargraph/source/chain.rb +252 -164
  204. data/lib/solargraph/source/change.rb +82 -79
  205. data/lib/solargraph/source/cursor.rb +167 -164
  206. data/lib/solargraph/source/source_chainer.rb +194 -191
  207. data/lib/solargraph/source/updater.rb +55 -54
  208. data/lib/solargraph/source.rb +495 -522
  209. data/lib/solargraph/source_map/clip.rb +232 -224
  210. data/lib/solargraph/source_map/completion.rb +23 -23
  211. data/lib/solargraph/source_map/data.rb +30 -0
  212. data/lib/solargraph/source_map/mapper.rb +255 -212
  213. data/lib/solargraph/source_map.rb +217 -180
  214. data/lib/solargraph/type_checker/checks.rb +120 -99
  215. data/lib/solargraph/type_checker/param_def.rb +35 -35
  216. data/lib/solargraph/type_checker/problem.rb +32 -32
  217. data/lib/solargraph/type_checker/rules.rb +62 -57
  218. data/lib/solargraph/type_checker.rb +672 -543
  219. data/lib/solargraph/version.rb +5 -5
  220. data/lib/solargraph/views/environment.erb +56 -58
  221. data/lib/solargraph/workspace/config.rb +239 -231
  222. data/lib/solargraph/workspace.rb +239 -215
  223. data/lib/solargraph/yard_map/cache.rb +25 -19
  224. data/lib/solargraph/yard_map/helpers.rb +16 -16
  225. data/lib/solargraph/yard_map/mapper/to_constant.rb +26 -25
  226. data/lib/solargraph/yard_map/mapper/to_method.rb +94 -78
  227. data/lib/solargraph/yard_map/mapper/to_namespace.rb +28 -27
  228. data/lib/solargraph/yard_map/mapper.rb +78 -77
  229. data/lib/solargraph/yard_map/to_method.rb +86 -79
  230. data/lib/solargraph/yard_map.rb +18 -460
  231. data/lib/solargraph/yard_tags.rb +20 -0
  232. data/lib/solargraph/yardoc.rb +52 -0
  233. data/lib/solargraph.rb +72 -69
  234. data/solargraph.gemspec +21 -10
  235. metadata +184 -115
  236. data/.travis.yml +0 -19
  237. data/lib/solargraph/api_map/bundler_methods.rb +0 -22
  238. data/lib/solargraph/compat.rb +0 -37
  239. data/lib/solargraph/convention/rspec.rb +0 -30
  240. data/lib/solargraph/documentor.rb +0 -76
  241. data/lib/solargraph/language_server/host/cataloger.rb +0 -56
  242. data/lib/solargraph/parser/legacy/node_methods.rb +0 -325
  243. data/lib/solargraph/parser/legacy/node_processors/alias_node.rb +0 -23
  244. data/lib/solargraph/parser/legacy/node_processors/args_node.rb +0 -35
  245. data/lib/solargraph/parser/legacy/node_processors/begin_node.rb +0 -15
  246. data/lib/solargraph/parser/legacy/node_processors/def_node.rb +0 -63
  247. data/lib/solargraph/parser/legacy/node_processors/sclass_node.rb +0 -21
  248. data/lib/solargraph/parser/legacy/node_processors.rb +0 -54
  249. data/lib/solargraph/parser/legacy.rb +0 -12
  250. data/lib/solargraph/parser/rubyvm/class_methods.rb +0 -144
  251. data/lib/solargraph/parser/rubyvm/node_chainer.rb +0 -160
  252. data/lib/solargraph/parser/rubyvm/node_methods.rb +0 -315
  253. data/lib/solargraph/parser/rubyvm/node_processors/args_node.rb +0 -85
  254. data/lib/solargraph/parser/rubyvm/node_processors/block_node.rb +0 -42
  255. data/lib/solargraph/parser/rubyvm/node_processors/casgn_node.rb +0 -22
  256. data/lib/solargraph/parser/rubyvm/node_processors/cvasgn_node.rb +0 -23
  257. data/lib/solargraph/parser/rubyvm/node_processors/defs_node.rb +0 -57
  258. data/lib/solargraph/parser/rubyvm/node_processors/gvasgn_node.rb +0 -23
  259. data/lib/solargraph/parser/rubyvm/node_processors/ivasgn_node.rb +0 -38
  260. data/lib/solargraph/parser/rubyvm/node_processors/kw_arg_node.rb +0 -39
  261. data/lib/solargraph/parser/rubyvm/node_processors/lit_node.rb +0 -20
  262. data/lib/solargraph/parser/rubyvm/node_processors/lvasgn_node.rb +0 -27
  263. data/lib/solargraph/parser/rubyvm/node_processors/namespace_node.rb +0 -39
  264. data/lib/solargraph/parser/rubyvm/node_processors/opt_arg_node.rb +0 -26
  265. data/lib/solargraph/parser/rubyvm/node_processors/orasgn_node.rb +0 -15
  266. data/lib/solargraph/parser/rubyvm/node_processors/resbody_node.rb +0 -45
  267. data/lib/solargraph/parser/rubyvm/node_processors/sclass_node.rb +0 -21
  268. data/lib/solargraph/parser/rubyvm/node_processors/scope_node.rb +0 -15
  269. data/lib/solargraph/parser/rubyvm/node_processors/send_node.rb +0 -277
  270. data/lib/solargraph/parser/rubyvm/node_processors/sym_node.rb +0 -18
  271. data/lib/solargraph/parser/rubyvm/node_processors.rb +0 -63
  272. data/lib/solargraph/parser/rubyvm.rb +0 -40
  273. data/lib/solargraph/yard_map/core_docs.rb +0 -170
  274. data/lib/solargraph/yard_map/core_fills.rb +0 -208
  275. data/lib/solargraph/yard_map/core_gen.rb +0 -76
  276. data/lib/solargraph/yard_map/rdoc_to_yard.rb +0 -140
  277. data/lib/solargraph/yard_map/stdlib_fills.rb +0 -43
  278. data/lib/yard-solargraph.rb +0 -33
  279. data/yardoc/2.2.2.tar.gz +0 -0
@@ -1,865 +1,861 @@
1
- # frozen_string_literal: true
2
-
3
- require 'diff/lcs'
4
- require 'observer'
5
- require 'securerandom'
6
- require 'set'
7
-
8
- module Solargraph
9
- module LanguageServer
10
- # The language server protocol's data provider. Hosts are responsible for
11
- # querying the library and processing messages. They also provide thread
12
- # safety for multi-threaded transports.
13
- #
14
- class Host
15
- autoload :Diagnoser, 'solargraph/language_server/host/diagnoser'
16
- autoload :Cataloger, 'solargraph/language_server/host/cataloger'
17
- autoload :Sources, 'solargraph/language_server/host/sources'
18
- autoload :Dispatch, 'solargraph/language_server/host/dispatch'
19
- autoload :MessageWorker, 'solargraph/language_server/host/message_worker'
20
-
21
- include UriHelpers
22
- include Logging
23
- include Dispatch
24
- include Observable
25
-
26
- attr_writer :client_capabilities
27
-
28
- def initialize
29
- @cancel_semaphore = Mutex.new
30
- @buffer_semaphore = Mutex.new
31
- @request_mutex = Mutex.new
32
- @cancel = []
33
- @buffer = String.new
34
- @stopped = true
35
- @next_request_id = 1
36
- @dynamic_capabilities = Set.new
37
- @registered_capabilities = Set.new
38
- end
39
-
40
- # Start asynchronous process handling.
41
- #
42
- # @return [void]
43
- def start
44
- return unless stopped?
45
- @stopped = false
46
- diagnoser.start
47
- cataloger.start
48
- sources.start
49
- message_worker.start
50
- end
51
-
52
- # Update the configuration options with the provided hash.
53
- #
54
- # @param update [Hash]
55
- # @return [void]
56
- def configure update
57
- return if update.nil?
58
- options.merge! update
59
- logger.level = LOG_LEVELS[options['logLevel']] || DEFAULT_LOG_LEVEL
60
- end
61
-
62
- # @return [Hash]
63
- def options
64
- @options ||= default_configuration
65
- end
66
-
67
- # Cancel the method with the specified ID.
68
- #
69
- # @param id [Integer]
70
- # @return [void]
71
- def cancel id
72
- @cancel_semaphore.synchronize { @cancel.push id }
73
- end
74
-
75
- # True if the host received a request to cancel the method with the
76
- # specified ID.
77
- #
78
- # @param id [Integer]
79
- # @return [Boolean]
80
- def cancel? id
81
- result = false
82
- @cancel_semaphore.synchronize { result = @cancel.include? id }
83
- result
84
- end
85
-
86
- # Delete the specified ID from the list of cancelled IDs if it exists.
87
- #
88
- # @param id [Integer]
89
- # @return [void]
90
- def clear id
91
- @cancel_semaphore.synchronize { @cancel.delete id }
92
- end
93
-
94
- # Called by adapter, to handle the request
95
- # @param request [Hash]
96
- # @return [void]
97
- def process request
98
- message_worker.queue(request)
99
- end
100
-
101
- # Start processing a request from the client. After the message is
102
- # processed, caller is responsible for sending the response.
103
- #
104
- # @param request [Hash] The contents of the message.
105
- # @return [Solargraph::LanguageServer::Message::Base] The message handler.
106
- def receive request
107
- if request['method']
108
- logger.info "Server received #{request['method']}"
109
- logger.debug request
110
- message = Message.select(request['method']).new(self, request)
111
- begin
112
- message.process
113
- rescue StandardError => e
114
- logger.warn "Error processing request: [#{e.class}] #{e.message}"
115
- logger.warn e.backtrace.join("\n")
116
- message.set_error Solargraph::LanguageServer::ErrorCodes::INTERNAL_ERROR, "[#{e.class}] #{e.message}"
117
- end
118
- message
119
- elsif request['id']
120
- if requests[request['id']]
121
- requests[request['id']].process(request['result'])
122
- requests.delete request['id']
123
- else
124
- logger.warn "Discarding client response to unrecognized message #{request['id']}"
125
- nil
126
- end
127
- else
128
- logger.warn "Invalid message received."
129
- logger.debug request
130
- end
131
- end
132
-
133
- # Respond to a notification that a file was created in the workspace.
134
- # The libraries will determine whether the file should be merged; see
135
- # Solargraph::Library#create_from_disk.
136
- #
137
- # @param uri [String] The file uri.
138
- # @return [Boolean] True if a library accepted the file.
139
- def create uri
140
- filename = uri_to_file(uri)
141
- result = false
142
- libraries.each do |lib|
143
- result = true if lib.create_from_disk(filename)
144
- end
145
- diagnoser.schedule uri if open?(uri)
146
- result
147
- end
148
-
149
- # Delete the specified file from the library.
150
- #
151
- # @param uri [String] The file uri.
152
- # @return [void]
153
- def delete uri
154
- filename = uri_to_file(uri)
155
- libraries.each do |lib|
156
- lib.delete(filename)
157
- end
158
- send_notification "textDocument/publishDiagnostics", {
159
- uri: uri,
160
- diagnostics: []
161
- }
162
- end
163
-
164
- # Open the specified file in the library.
165
- #
166
- # @param uri [String] The file uri.
167
- # @param text [String] The contents of the file.
168
- # @param version [Integer] A version number.
169
- # @return [void]
170
- def open uri, text, version
171
- src = sources.open(uri, text, version)
172
- libraries.each do |lib|
173
- lib.merge src
174
- end
175
- diagnoser.schedule uri
176
- end
177
-
178
- # @param uri [String]
179
- # @return [void]
180
- def open_from_disk uri
181
- sources.open_from_disk(uri)
182
- diagnoser.schedule uri
183
- end
184
-
185
- # True if the specified file is currently open in the library.
186
- #
187
- # @param uri [String]
188
- # @return [Boolean]
189
- def open? uri
190
- sources.include? uri
191
- end
192
-
193
- # Close the file specified by the URI.
194
- #
195
- # @param uri [String]
196
- # @return [void]
197
- def close uri
198
- logger.info "Closing #{uri}"
199
- sources.close uri
200
- diagnoser.schedule uri
201
- end
202
-
203
- # @param uri [String]
204
- # @return [void]
205
- def diagnose uri
206
- if sources.include?(uri)
207
- library = library_for(uri)
208
- if library.mapped? && library.synchronized?
209
- logger.info "Diagnosing #{uri}"
210
- begin
211
- results = library.diagnose uri_to_file(uri)
212
- send_notification "textDocument/publishDiagnostics", {
213
- uri: uri,
214
- diagnostics: results
215
- }
216
- rescue DiagnosticsError => e
217
- logger.warn "Error in diagnostics: #{e.message}"
218
- options['diagnostics'] = false
219
- send_notification 'window/showMessage', {
220
- type: LanguageServer::MessageTypes::ERROR,
221
- message: "Error in diagnostics: #{e.message}"
222
- }
223
- rescue FileNotFoundError => e
224
- # @todo This appears to happen when an external file is open and
225
- # scheduled for diagnosis, but the file was closed (i.e., the
226
- # editor moved to a different file) before diagnosis started
227
- logger.warn "Unable to diagnose #{uri} : #{e.message}"
228
- send_notification 'textDocument/publishDiagnostics', {
229
- uri: uri,
230
- diagnostics: []
231
- }
232
- end
233
- else
234
- logger.info "Deferring diagnosis of #{uri}"
235
- diagnoser.schedule uri
236
- end
237
- else
238
- send_notification 'textDocument/publishDiagnostics', {
239
- uri: uri,
240
- diagnostics: []
241
- }
242
- end
243
- end
244
-
245
- # Update a document from the parameters of a textDocument/didChange
246
- # method.
247
- #
248
- # @param params [Hash]
249
- # @return [void]
250
- def change params
251
- updater = generate_updater(params)
252
- sources.async_update params['textDocument']['uri'], updater
253
- diagnoser.schedule params['textDocument']['uri']
254
- end
255
-
256
- # Queue a message to be sent to the client.
257
- #
258
- # @param message [String] The message to send.
259
- # @return [void]
260
- def queue message
261
- @buffer_semaphore.synchronize { @buffer += message }
262
- changed
263
- notify_observers
264
- end
265
-
266
- # Clear the message buffer and return the most recent data.
267
- #
268
- # @return [String] The most recent data or an empty string.
269
- def flush
270
- tmp = ''
271
- @buffer_semaphore.synchronize do
272
- tmp = @buffer.clone
273
- @buffer.clear
274
- end
275
- tmp
276
- end
277
-
278
- # Prepare a library for the specified directory.
279
- #
280
- # @param directory [String]
281
- # @param name [String, nil]
282
- # @return [void]
283
- def prepare directory, name = nil
284
- # No need to create a library without a directory. The generic library
285
- # will handle it.
286
- return if directory.nil?
287
- logger.info "Preparing library for #{directory}"
288
- path = ''
289
- path = normalize_separators(directory) unless directory.nil?
290
- begin
291
- lib = Solargraph::Library.load(path, name)
292
- libraries.push lib
293
- async_library_map lib
294
- rescue WorkspaceTooLargeError => e
295
- send_notification 'window/showMessage', {
296
- 'type' => Solargraph::LanguageServer::MessageTypes::WARNING,
297
- 'message' => e.message
298
- }
299
- end
300
- end
301
-
302
- # Prepare multiple folders.
303
- #
304
- # @param array [Array<Hash{String => String}>]
305
- # @return [void]
306
- def prepare_folders array
307
- return if array.nil?
308
- array.each do |folder|
309
- prepare uri_to_file(folder['uri']), folder['name']
310
- end
311
- end
312
-
313
- # Remove a directory.
314
- #
315
- # @param directory [String]
316
- # @return [void]
317
- def remove directory
318
- logger.info "Removing library for #{directory}"
319
- # @param lib [Library]
320
- libraries.delete_if do |lib|
321
- next false if lib.workspace.directory != directory
322
- true
323
- end
324
- end
325
-
326
- # @param array [Array<Hash>]
327
- # @return [void]
328
- def remove_folders array
329
- array.each do |folder|
330
- remove uri_to_file(folder['uri'])
331
- end
332
- end
333
-
334
- # @return [Array<String>]
335
- def folders
336
- libraries.map { |lib| lib.workspace.directory }
337
- end
338
-
339
- # Send a notification to the client.
340
- #
341
- # @param method [String] The message method
342
- # @param params [Hash] The method parameters
343
- # @return [void]
344
- def send_notification method, params
345
- response = {
346
- jsonrpc: "2.0",
347
- method: method,
348
- params: params
349
- }
350
- json = response.to_json
351
- envelope = "Content-Length: #{json.bytesize}\r\n\r\n#{json}"
352
- queue envelope
353
- logger.info "Server sent #{method}"
354
- logger.debug params
355
- end
356
-
357
- # Send a request to the client and execute the provided block to process
358
- # the response. If an ID is not provided, the host will use an auto-
359
- # incrementing integer.
360
- #
361
- # @param method [String] The message method
362
- # @param params [Hash] The method parameters
363
- # @param block [Proc] The block that processes the response
364
- # @yieldparam [Hash] The result sent by the client
365
- # @return [void]
366
- def send_request method, params, &block
367
- @request_mutex.synchronize do
368
- message = {
369
- jsonrpc: "2.0",
370
- method: method,
371
- params: params,
372
- id: @next_request_id
373
- }
374
- json = message.to_json
375
- requests[@next_request_id] = Request.new(@next_request_id, &block)
376
- envelope = "Content-Length: #{json.bytesize}\r\n\r\n#{json}"
377
- queue envelope
378
- @next_request_id += 1
379
- logger.info "Server sent #{method}"
380
- logger.debug params
381
- end
382
- end
383
-
384
- # Register the methods as capabilities with the client.
385
- # This method will avoid duplicating registrations and ignore methods
386
- # that were not flagged for dynamic registration by the client.
387
- #
388
- # @param methods [Array<String>] The methods to register
389
- # @return [void]
390
- def register_capabilities methods
391
- logger.debug "Registering capabilities: #{methods}"
392
- registrations = methods.select { |m| can_register?(m) and !registered?(m) }.map do |m|
393
- @registered_capabilities.add m
394
- {
395
- id: m,
396
- method: m,
397
- registerOptions: dynamic_capability_options[m]
398
- }
399
- end
400
- return if registrations.empty?
401
- send_request 'client/registerCapability', { registrations: registrations }
402
- end
403
-
404
- # Unregister the methods with the client.
405
- # This method will avoid duplicating unregistrations and ignore methods
406
- # that were not flagged for dynamic registration by the client.
407
- #
408
- # @param methods [Array<String>] The methods to unregister
409
- # @return [void]
410
- def unregister_capabilities methods
411
- logger.debug "Unregistering capabilities: #{methods}"
412
- unregisterations = methods.select{|m| registered?(m)}.map{ |m|
413
- @registered_capabilities.delete m
414
- {
415
- id: m,
416
- method: m
417
- }
418
- }
419
- return if unregisterations.empty?
420
- send_request 'client/unregisterCapability', { unregisterations: unregisterations }
421
- end
422
-
423
- # Flag a method as available for dynamic registration.
424
- #
425
- # @param method [String] The method name, e.g., 'textDocument/completion'
426
- # @return [void]
427
- def allow_registration method
428
- @dynamic_capabilities.add method
429
- end
430
-
431
- # True if the specified LSP method can be dynamically registered.
432
- #
433
- # @param method [String]
434
- # @return [Boolean]
435
- def can_register? method
436
- @dynamic_capabilities.include?(method)
437
- end
438
-
439
- # True if the specified method has been registered.
440
- #
441
- # @param method [String] The method name, e.g., 'textDocument/completion'
442
- # @return [Boolean]
443
- def registered? method
444
- @registered_capabilities.include?(method)
445
- end
446
-
447
- def synchronizing?
448
- !libraries.all?(&:synchronized?)
449
- end
450
-
451
- # @return [void]
452
- def stop
453
- return if @stopped
454
- @stopped = true
455
- message_worker.stop
456
- cataloger.stop
457
- diagnoser.stop
458
- sources.stop
459
- changed
460
- notify_observers
461
- end
462
-
463
- def stopped?
464
- @stopped
465
- end
466
-
467
- # Locate multiple pins that match a completion item. The first match is
468
- # based on the corresponding location in a library source if available.
469
- # Subsequent matches are based on path.
470
- #
471
- # @param params [Hash] A hash representation of a completion item
472
- # @return [Array<Pin::Base>]
473
- def locate_pins params
474
- return [] unless params['data'] && params['data']['uri']
475
- library = library_for(params['data']['uri'])
476
- result = []
477
- if params['data']['location']
478
- location = Location.new(
479
- params['data']['location']['filename'],
480
- Range.from_to(
481
- params['data']['location']['range']['start']['line'],
482
- params['data']['location']['range']['start']['character'],
483
- params['data']['location']['range']['end']['line'],
484
- params['data']['location']['range']['end']['character']
485
- )
486
- )
487
- result.concat library.locate_pins(location).select{ |pin| pin.name == params['label'] }
488
- end
489
- if params['data']['path']
490
- result.concat library.path_pins(params['data']['path'])
491
- end
492
- # Selecting by both location and path can result in duplicate pins
493
- result.uniq { |p| [p.path, p.location] }
494
- end
495
-
496
- # @param uri [String]
497
- # @return [String]
498
- def read_text uri
499
- library = library_for(uri)
500
- filename = uri_to_file(uri)
501
- library.read_text(filename)
502
- end
503
-
504
- def formatter_config uri
505
- library = library_for(uri)
506
- library.workspace.config.formatter
507
- end
508
-
509
- # @param uri [String]
510
- # @param line [Integer]
511
- # @param column [Integer]
512
- # @return [Solargraph::SourceMap::Completion]
513
- def completions_at uri, line, column
514
- library = library_for(uri)
515
- library.completions_at uri_to_file(uri), line, column
516
- end
517
-
518
- # @return [Bool] if has pending completion request
519
- def has_pending_completions?
520
- message_worker.messages.reverse_each.any? { |req| req['method'] == 'textDocument/completion' }
521
- end
522
-
523
- # @param uri [String]
524
- # @param line [Integer]
525
- # @param column [Integer]
526
- # @return [Array<Solargraph::Pin::Base>]
527
- def definitions_at uri, line, column
528
- library = library_for(uri)
529
- library.definitions_at(uri_to_file(uri), line, column)
530
- end
531
-
532
- # @param uri [String]
533
- # @param line [Integer]
534
- # @param column [Integer]
535
- # @return [Array<Solargraph::Pin::Base>]
536
- def signatures_at uri, line, column
537
- library = library_for(uri)
538
- library.signatures_at(uri_to_file(uri), line, column)
539
- end
540
-
541
- # @param uri [String]
542
- # @param line [Integer]
543
- # @param column [Integer]
544
- # @param strip [Boolean] Strip special characters from variable names
545
- # @param only [Boolean] If true, search current file only
546
- # @return [Array<Solargraph::Range>]
547
- def references_from uri, line, column, strip: true, only: false
548
- library = library_for(uri)
549
- library.references_from(uri_to_file(uri), line, column, strip: strip, only: only)
550
- end
551
-
552
- # @param query [String]
553
- # @return [Array<Solargraph::Pin::Base>]
554
- def query_symbols query
555
- result = []
556
- (libraries + [generic_library]).each { |lib| result.concat lib.query_symbols(query) }
557
- result.uniq
558
- end
559
-
560
- # @param query [String]
561
- # @return [Array<String>]
562
- def search query
563
- result = []
564
- libraries.each { |lib| result.concat lib.search(query) }
565
- result
566
- end
567
-
568
- # @param query [String]
569
- # @return [Array]
570
- def document query
571
- result = []
572
- libraries.each { |lib| result.concat lib.document(query) }
573
- result
574
- end
575
-
576
- # @param uri [String]
577
- # @return [Array<Solargraph::Pin::Base>]
578
- def document_symbols uri
579
- library = library_for(uri)
580
- # At this level, document symbols should be unique; e.g., a
581
- # module_function method should return the location for Module.method
582
- # or Module#method, but not both.
583
- library.document_symbols(uri_to_file(uri)).uniq(&:location)
584
- end
585
-
586
- # Send a notification to the client.
587
- #
588
- # @param text [String]
589
- # @param type [Integer] A MessageType constant
590
- # @return [void]
591
- def show_message text, type = LanguageServer::MessageTypes::INFO
592
- send_notification 'window/showMessage', {
593
- type: type,
594
- message: text
595
- }
596
- end
597
-
598
- # Send a notification with optional responses.
599
- #
600
- # @param text [String]
601
- # @param type [Integer] A MessageType constant
602
- # @param actions [Array<String>] Response options for the client
603
- # @param block The block that processes the response
604
- # @yieldparam [String] The action received from the client
605
- # @return [void]
606
- def show_message_request text, type, actions, &block
607
- send_request 'window/showMessageRequest', {
608
- type: type,
609
- message: text,
610
- actions: actions
611
- }, &block
612
- end
613
-
614
- # Get a list of IDs for server requests that are waiting for responses
615
- # from the client.
616
- #
617
- # @return [Array<Integer>]
618
- def pending_requests
619
- requests.keys
620
- end
621
-
622
- # @return [Hash{String => Object}]
623
- def default_configuration
624
- {
625
- 'completion' => true,
626
- 'hover' => true,
627
- 'symbols' => true,
628
- 'definitions' => true,
629
- 'rename' => true,
630
- 'references' => true,
631
- 'autoformat' => false,
632
- 'diagnostics' => false,
633
- 'formatting' => false,
634
- 'folding' => true,
635
- 'highlights' => true,
636
- 'logLevel' => 'warn'
637
- }
638
- end
639
-
640
- # @param uri [String]
641
- # @return [Array<Range>]
642
- def folding_ranges uri
643
- sources.find(uri).folding_ranges
644
- end
645
-
646
- # @return [void]
647
- def catalog
648
- return unless libraries.all?(&:mapped?)
649
- libraries.each(&:catalog)
650
- end
651
-
652
- def client_capabilities
653
- @client_capabilities ||= {}
654
- end
655
-
656
- private
657
-
658
- # @return [MessageWorker]
659
- def message_worker
660
- @message_worker ||= MessageWorker.new(self)
661
- end
662
-
663
- # @return [Diagnoser]
664
- def diagnoser
665
- @diagnoser ||= Diagnoser.new(self)
666
- end
667
-
668
- # @return [Cataloger]
669
- def cataloger
670
- @cataloger ||= Cataloger.new(self)
671
- end
672
-
673
- # A hash of client requests by ID. The host uses this to keep track of
674
- # pending responses.
675
- #
676
- # @return [Hash{Integer => Hash}]
677
- def requests
678
- @requests ||= {}
679
- end
680
-
681
- # @param path [String]
682
- # @return [String]
683
- def normalize_separators path
684
- return path if File::ALT_SEPARATOR.nil?
685
- path.gsub(File::ALT_SEPARATOR, File::SEPARATOR)
686
- end
687
-
688
- # @param params [Hash]
689
- # @return [Source::Updater]
690
- def generate_updater params
691
- changes = []
692
- params['contentChanges'].each do |recvd|
693
- chng = check_diff(params['textDocument']['uri'], recvd)
694
- changes.push Solargraph::Source::Change.new(
695
- (chng['range'].nil? ?
696
- nil :
697
- Solargraph::Range.from_to(chng['range']['start']['line'], chng['range']['start']['character'], chng['range']['end']['line'], chng['range']['end']['character'])
698
- ),
699
- chng['text']
700
- )
701
- end
702
- Solargraph::Source::Updater.new(
703
- uri_to_file(params['textDocument']['uri']),
704
- params['textDocument']['version'],
705
- changes
706
- )
707
- end
708
-
709
- # @param uri [String]
710
- # @param change [Hash]
711
- # @return [Hash]
712
- def check_diff uri, change
713
- return change if change['range']
714
- source = sources.find(uri)
715
- return change if source.code.length + 1 != change['text'].length
716
- diffs = Diff::LCS.diff(source.code, change['text'])
717
- return change if diffs.length.zero? || diffs.length > 1 || diffs.first.length > 1
718
- # @type [Diff::LCS::Change]
719
- diff = diffs.first.first
720
- return change unless diff.adding? && ['.', ':', '(', ',', ' '].include?(diff.element)
721
- position = Solargraph::Position.from_offset(source.code, diff.position)
722
- {
723
- 'range' => {
724
- 'start' => {
725
- 'line' => position.line,
726
- 'character' => position.character
727
- },
728
- 'end' => {
729
- 'line' => position.line,
730
- 'character' => position.character
731
- }
732
- },
733
- 'text' => diff.element
734
- }
735
- rescue Solargraph::FileNotFoundError
736
- change
737
- end
738
-
739
- # @return [Hash]
740
- def dynamic_capability_options
741
- @dynamic_capability_options ||= {
742
- # textDocumentSync: 2, # @todo What should this be?
743
- 'textDocument/completion' => {
744
- resolveProvider: true,
745
- triggerCharacters: ['.', ':', '@']
746
- },
747
- # hoverProvider: true,
748
- # definitionProvider: true,
749
- 'textDocument/signatureHelp' => {
750
- triggerCharacters: ['(', ',', ' ']
751
- },
752
- # documentFormattingProvider: true,
753
- 'textDocument/onTypeFormatting' => {
754
- firstTriggerCharacter: '{',
755
- moreTriggerCharacter: ['(']
756
- },
757
- # documentSymbolProvider: true,
758
- # workspaceSymbolProvider: true,
759
- # workspace: {
760
- # workspaceFolders: {
761
- # supported: true,
762
- # changeNotifications: true
763
- # }
764
- # }
765
- 'textDocument/definition' => {
766
- definitionProvider: true
767
- },
768
- 'textDocument/references' => {
769
- referencesProvider: true
770
- },
771
- 'textDocument/rename' => {
772
- renameProvider: prepare_rename? ? { prepareProvider: true } : true
773
- },
774
- 'textDocument/documentSymbol' => {
775
- documentSymbolProvider: true
776
- },
777
- 'workspace/symbol' => {
778
- workspaceSymbolProvider: true
779
- },
780
- 'textDocument/formatting' => {
781
- formattingProvider: true
782
- },
783
- 'textDocument/foldingRange' => {
784
- foldingRangeProvider: true
785
- },
786
- 'textDocument/codeAction' => {
787
- codeActionProvider: true
788
- },
789
- 'textDocument/documentHighlight' => {
790
- documentHighlightProvider: true
791
- }
792
- }
793
- end
794
-
795
- def prepare_rename?
796
- client_capabilities['rename'] && client_capabilities['rename']['prepareSupport']
797
- end
798
-
799
- def client_supports_progress?
800
- client_capabilities['window'] && client_capabilities['window']['workDoneProgress']
801
- end
802
-
803
- # @param library [Library]
804
- # @return [void]
805
- def async_library_map library
806
- return if library.mapped?
807
- Thread.new do
808
- if client_supports_progress?
809
- uuid = SecureRandom.uuid
810
- send_request 'window/workDoneProgress/create', {
811
- token: uuid
812
- } do |response|
813
- do_async_library_map library, response.nil? ? uuid : nil
814
- end
815
- else
816
- do_async_library_map library
817
- end
818
- end
819
- end
820
-
821
- def do_async_library_map library, uuid = nil
822
- total = library.workspace.sources.length
823
- if uuid
824
- send_notification '$/progress', {
825
- token: uuid,
826
- value: {
827
- kind: 'begin',
828
- title: "Mapping workspace",
829
- message: "0/#{total} files",
830
- cancellable: false,
831
- percentage: 0
832
- }
833
- }
834
- end
835
- pct = 0
836
- mod = 10
837
- while library.next_map
838
- next unless uuid
839
- cur = ((library.source_map_hash.keys.length.to_f / total.to_f) * 100).to_i
840
- if cur > pct && cur % mod == 0
841
- pct = cur
842
- send_notification '$/progress', {
843
- token: uuid,
844
- value: {
845
- kind: 'report',
846
- cancellable: false,
847
- message: "#{library.source_map_hash.keys.length}/#{total} files",
848
- percentage: pct
849
- }
850
- }
851
- end
852
- end
853
- if uuid
854
- send_notification '$/progress', {
855
- token: uuid,
856
- value: {
857
- kind: 'end',
858
- message: 'Mapping complete'
859
- }
860
- }
861
- end
862
- end
863
- end
864
- end
865
- end
1
+ # frozen_string_literal: true
2
+
3
+ require 'diff/lcs'
4
+ require 'observer'
5
+ require 'securerandom'
6
+
7
+ module Solargraph
8
+ module LanguageServer
9
+ # The language server protocol's data provider. Hosts are responsible for
10
+ # querying the library and processing messages. They also provide thread
11
+ # safety for multi-threaded transports.
12
+ #
13
+ class Host
14
+ autoload :Diagnoser, 'solargraph/language_server/host/diagnoser'
15
+ autoload :Sources, 'solargraph/language_server/host/sources'
16
+ autoload :Dispatch, 'solargraph/language_server/host/dispatch'
17
+ autoload :MessageWorker, 'solargraph/language_server/host/message_worker'
18
+
19
+ include UriHelpers
20
+ include Logging
21
+ include Dispatch
22
+ include Observable
23
+
24
+ attr_writer :client_capabilities
25
+
26
+ def initialize
27
+ @buffer_semaphore = Mutex.new
28
+ @request_mutex = Mutex.new
29
+ @buffer = String.new
30
+ @stopped = true
31
+ @next_request_id = 1
32
+ @dynamic_capabilities = Set.new
33
+ @registered_capabilities = Set.new
34
+ end
35
+
36
+ # Start asynchronous process handling.
37
+ #
38
+ # @return [void]
39
+ def start
40
+ return unless stopped?
41
+ @stopped = false
42
+ diagnoser.start
43
+ message_worker.start
44
+ end
45
+
46
+ # Update the configuration options with the provided hash.
47
+ #
48
+ # @param update [Hash]
49
+ # @return [void]
50
+ def configure update
51
+ return if update.nil?
52
+ options.merge! update
53
+ logger.level = LOG_LEVELS[options['logLevel']] || DEFAULT_LOG_LEVEL
54
+ end
55
+
56
+ # @return [Hash{String => [Boolean, String]}]
57
+ def options
58
+ @options ||= default_configuration
59
+ end
60
+
61
+ # Cancel the method with the specified ID.
62
+ #
63
+ # @param id [Integer]
64
+ # @return [void]
65
+ def cancel id
66
+ cancelled.push id
67
+ end
68
+
69
+ # True if the host received a request to cancel the method with the
70
+ # specified ID.
71
+ #
72
+ # @param id [Integer]
73
+ # @return [Boolean]
74
+ def cancel? id
75
+ cancelled.include? id
76
+ end
77
+
78
+ # Delete the specified ID from the list of cancelled IDs if it exists.
79
+ #
80
+ # @param id [Integer]
81
+ # @return [void]
82
+ def clear id
83
+ cancelled.delete id
84
+ end
85
+
86
+ # Called by adapter, to handle the request
87
+ # @param request [Hash]
88
+ # @return [void]
89
+ def process request
90
+ message_worker.queue(request)
91
+ end
92
+
93
+ # Start processing a request from the client. After the message is
94
+ # processed, caller is responsible for sending the response.
95
+ #
96
+ # @param request [Hash{String => unspecified}] The contents of the message.
97
+ # @return [Solargraph::LanguageServer::Message::Base, nil] The message handler.
98
+ def receive request
99
+ if request['method']
100
+ logger.info "Host received ##{request['id']} #{request['method']}"
101
+ logger.debug request
102
+ message = Message.select(request['method']).new(self, request)
103
+ begin
104
+ message.process unless cancel?(request['id'])
105
+ rescue StandardError => e
106
+ logger.warn "Error processing request: [#{e.class}] #{e.message}"
107
+ logger.warn e.backtrace.join("\n")
108
+ message.set_error Solargraph::LanguageServer::ErrorCodes::INTERNAL_ERROR, "[#{e.class}] #{e.message}"
109
+ end
110
+ message
111
+ elsif request['id']
112
+ if requests[request['id']]
113
+ requests[request['id']].process(request['result'])
114
+ requests.delete request['id']
115
+ else
116
+ logger.warn "Discarding client response to unrecognized message #{request['id']}"
117
+ nil
118
+ end
119
+ else
120
+ logger.warn "Invalid message received."
121
+ logger.debug request
122
+ nil
123
+ end
124
+ end
125
+
126
+ # Respond to a notification that files were created in the workspace.
127
+ # The libraries will determine whether the files should be merged; see
128
+ # Solargraph::Library#create_from_disk.
129
+ #
130
+ # @param uris [Array<String>] The URIs of the files.
131
+ # @return [Boolean] True if at least one library accepted at least one file.
132
+ def create *uris
133
+ filenames = uris.map { |uri| uri_to_file(uri) }
134
+ result = false
135
+ libraries.each do |lib|
136
+ result = true if lib.create_from_disk(*filenames)
137
+ end
138
+ uris.each do |uri|
139
+ diagnoser.schedule uri if open?(uri)
140
+ end
141
+ result
142
+ end
143
+
144
+ # Delete the specified files from the library.
145
+ #
146
+ # @param uris [Array<String>] The file uris.
147
+ # @return [void]
148
+ def delete *uris
149
+ filenames = uris.map { |uri| uri_to_file(uri) }
150
+ libraries.each do |lib|
151
+ lib.delete_observer self
152
+ lib.delete(*filenames)
153
+ end
154
+ uris.each do |uri|
155
+ send_notification "textDocument/publishDiagnostics", {
156
+ uri: uri,
157
+ diagnostics: []
158
+ }
159
+ end
160
+ end
161
+
162
+ # Open the specified file in the library.
163
+ #
164
+ # @param uri [String] The file uri.
165
+ # @param text [String] The contents of the file.
166
+ # @param version [Integer] A version number.
167
+ # @return [void]
168
+ def open uri, text, version
169
+ src = sources.open(uri, text, version)
170
+ libraries.each do |lib|
171
+ lib.merge src
172
+ end
173
+ diagnoser.schedule uri
174
+ end
175
+
176
+ # @param uri [String]
177
+ # @return [void]
178
+ def open_from_disk uri
179
+ sources.open_from_disk(uri)
180
+ diagnoser.schedule uri
181
+ end
182
+
183
+ # True if the specified file is currently open in the library.
184
+ #
185
+ # @param uri [String]
186
+ # @return [Boolean]
187
+ def open? uri
188
+ sources.include? uri
189
+ end
190
+
191
+ # Close the file specified by the URI.
192
+ #
193
+ # @param uri [String]
194
+ # @return [void]
195
+ def close uri
196
+ logger.info "Closing #{uri}"
197
+ sources.close uri
198
+ diagnoser.schedule uri
199
+ end
200
+
201
+ # @param uri [String]
202
+ # @return [void]
203
+ def diagnose uri
204
+ if sources.include?(uri)
205
+ library = library_for(uri)
206
+ if library.mapped? && library.synchronized?
207
+ logger.info "Diagnosing #{uri}"
208
+ begin
209
+ results = library.diagnose uri_to_file(uri)
210
+ send_notification "textDocument/publishDiagnostics", {
211
+ uri: uri,
212
+ diagnostics: results
213
+ }
214
+ rescue DiagnosticsError => e
215
+ logger.warn "Error in diagnostics: #{e.message}"
216
+ options['diagnostics'] = false
217
+ send_notification 'window/showMessage', {
218
+ type: LanguageServer::MessageTypes::ERROR,
219
+ message: "Error in diagnostics: #{e.message}"
220
+ }
221
+ rescue FileNotFoundError => e
222
+ # @todo This appears to happen when an external file is open and
223
+ # scheduled for diagnosis, but the file was closed (i.e., the
224
+ # editor moved to a different file) before diagnosis started
225
+ logger.warn "Unable to diagnose #{uri} : #{e.message}"
226
+ send_notification 'textDocument/publishDiagnostics', {
227
+ uri: uri,
228
+ diagnostics: []
229
+ }
230
+ end
231
+ else
232
+ logger.info "Deferring diagnosis of #{uri}"
233
+ diagnoser.schedule uri
234
+ end
235
+ else
236
+ send_notification 'textDocument/publishDiagnostics', {
237
+ uri: uri,
238
+ diagnostics: []
239
+ }
240
+ end
241
+ end
242
+
243
+ # Update a document from the parameters of a textDocument/didChange
244
+ # method.
245
+ #
246
+ # @param params [Hash]
247
+ # @return [void]
248
+ def change params
249
+ updater = generate_updater(params)
250
+ sources.update params['textDocument']['uri'], updater
251
+ diagnoser.schedule params['textDocument']['uri']
252
+ end
253
+
254
+ # Queue a message to be sent to the client.
255
+ #
256
+ # @param message [String] The message to send.
257
+ # @return [void]
258
+ def queue message
259
+ @buffer_semaphore.synchronize { @buffer += message }
260
+ changed
261
+ notify_observers
262
+ end
263
+
264
+ # Clear the message buffer and return the most recent data.
265
+ #
266
+ # @return [String] The most recent data or an empty string.
267
+ def flush
268
+ tmp = ''
269
+ @buffer_semaphore.synchronize do
270
+ tmp = @buffer.clone
271
+ @buffer.clear
272
+ end
273
+ tmp
274
+ end
275
+
276
+ # Prepare a library for the specified directory.
277
+ #
278
+ # @param directory [String]
279
+ # @param name [String, nil]
280
+ # @return [void]
281
+ def prepare directory, name = nil
282
+ # No need to create a library without a directory. The generic library
283
+ # will handle it.
284
+ return if directory.nil?
285
+ logger.info "Preparing library for #{directory}"
286
+ path = ''
287
+ path = normalize_separators(directory) unless directory.nil?
288
+ begin
289
+ workspace = Solargraph::Workspace.new(path, nil, options)
290
+ lib = Solargraph::Library.new(workspace, name)
291
+ lib.add_observer self
292
+ libraries.push lib
293
+ library_map lib
294
+ rescue WorkspaceTooLargeError => e
295
+ send_notification 'window/showMessage', {
296
+ 'type' => Solargraph::LanguageServer::MessageTypes::WARNING,
297
+ 'message' => e.message
298
+ }
299
+ end
300
+ end
301
+
302
+ # Prepare multiple folders.
303
+ #
304
+ # @param array [Array<Hash{String => String}>]
305
+ # @return [void]
306
+ def prepare_folders array
307
+ return if array.nil?
308
+ array.each do |folder|
309
+ prepare uri_to_file(folder['uri']), folder['name']
310
+ end
311
+ end
312
+
313
+ # Remove a directory.
314
+ #
315
+ # @param directory [String]
316
+ # @return [void]
317
+ def remove directory
318
+ logger.info "Removing library for #{directory}"
319
+ # @param lib [Library]
320
+ libraries.delete_if do |lib|
321
+ next false if lib.workspace.directory != directory
322
+ lib.delete_observer self
323
+ true
324
+ end
325
+ end
326
+
327
+ # @param array [Array<Hash>]
328
+ # @return [void]
329
+ def remove_folders array
330
+ array.each do |folder|
331
+ remove uri_to_file(folder['uri'])
332
+ end
333
+ end
334
+
335
+ # @return [Array<String>]
336
+ def folders
337
+ libraries.map { |lib| lib.workspace.directory }
338
+ end
339
+
340
+ # Send a notification to the client.
341
+ #
342
+ # @param method [String] The message method
343
+ # @param params [Hash] The method parameters
344
+ # @return [void]
345
+ def send_notification method, params
346
+ response = {
347
+ jsonrpc: "2.0",
348
+ method: method,
349
+ params: params
350
+ }
351
+ json = response.to_json
352
+ envelope = "Content-Length: #{json.bytesize}\r\n\r\n#{json}"
353
+ queue envelope
354
+ logger.info "Server sent #{method}"
355
+ logger.debug params
356
+ end
357
+
358
+ # Send a request to the client and execute the provided block to process
359
+ # the response. If an ID is not provided, the host will use an auto-
360
+ # incrementing integer.
361
+ #
362
+ # @param method [String] The message method
363
+ # @param params [Hash] The method parameters
364
+ # @param block [Proc] The block that processes the response
365
+ # @yieldparam [Hash] The result sent by the client
366
+ # @return [void]
367
+ def send_request method, params, &block
368
+ @request_mutex.synchronize do
369
+ message = {
370
+ jsonrpc: "2.0",
371
+ method: method,
372
+ params: params,
373
+ id: @next_request_id
374
+ }
375
+ json = message.to_json
376
+ requests[@next_request_id] = Request.new(@next_request_id, &block)
377
+ envelope = "Content-Length: #{json.bytesize}\r\n\r\n#{json}"
378
+ queue envelope
379
+ @next_request_id += 1
380
+ logger.debug params
381
+ end
382
+ end
383
+
384
+ # Register the methods as capabilities with the client.
385
+ # This method will avoid duplicating registrations and ignore methods
386
+ # that were not flagged for dynamic registration by the client.
387
+ #
388
+ # @param methods [Array<String>] The methods to register
389
+ # @return [void]
390
+ def register_capabilities methods
391
+ logger.debug "Registering capabilities: #{methods}"
392
+ registrations = methods.select { |m| can_register?(m) and !registered?(m) }.map do |m|
393
+ @registered_capabilities.add m
394
+ {
395
+ id: m,
396
+ method: m,
397
+ registerOptions: dynamic_capability_options[m]
398
+ }
399
+ end
400
+ return if registrations.empty?
401
+ send_request 'client/registerCapability', { registrations: registrations }
402
+ end
403
+
404
+ # Unregister the methods with the client.
405
+ # This method will avoid duplicating unregistrations and ignore methods
406
+ # that were not flagged for dynamic registration by the client.
407
+ #
408
+ # @param methods [Array<String>] The methods to unregister
409
+ # @return [void]
410
+ def unregister_capabilities methods
411
+ logger.debug "Unregistering capabilities: #{methods}"
412
+ unregisterations = methods.select{|m| registered?(m)}.map{ |m|
413
+ @registered_capabilities.delete m
414
+ {
415
+ id: m,
416
+ method: m
417
+ }
418
+ }
419
+ return if unregisterations.empty?
420
+ send_request 'client/unregisterCapability', { unregisterations: unregisterations }
421
+ end
422
+
423
+ # Flag a method as available for dynamic registration.
424
+ #
425
+ # @param method [String] The method name, e.g., 'textDocument/completion'
426
+ # @return [void]
427
+ def allow_registration method
428
+ @dynamic_capabilities.add method
429
+ end
430
+
431
+ # True if the specified LSP method can be dynamically registered.
432
+ #
433
+ # @param method [String]
434
+ # @return [Boolean]
435
+ def can_register? method
436
+ @dynamic_capabilities.include?(method)
437
+ end
438
+
439
+ # True if the specified method has been registered.
440
+ #
441
+ # @param method [String] The method name, e.g., 'textDocument/completion'
442
+ # @return [Boolean]
443
+ def registered? method
444
+ @registered_capabilities.include?(method)
445
+ end
446
+
447
+ def synchronizing?
448
+ !libraries.all?(&:synchronized?)
449
+ end
450
+
451
+ # @return [void]
452
+ def stop
453
+ return if @stopped
454
+ @stopped = true
455
+ message_worker.stop
456
+ diagnoser.stop
457
+ changed
458
+ notify_observers
459
+ end
460
+
461
+ def stopped?
462
+ @stopped
463
+ end
464
+
465
+ # Locate multiple pins that match a completion item. The first match is
466
+ # based on the corresponding location in a library source if available.
467
+ # Subsequent matches are based on path.
468
+ #
469
+ # @param params [Hash] A hash representation of a completion item
470
+ # @return [Array<Pin::Base>]
471
+ def locate_pins params
472
+ return [] unless params['data'] && params['data']['uri']
473
+ library = library_for(params['data']['uri'])
474
+ # @type [Array<Pin::Base>]
475
+ result = []
476
+ if params['data']['location']
477
+ location = Location.new(
478
+ params['data']['location']['filename'],
479
+ Range.from_to(
480
+ params['data']['location']['range']['start']['line'],
481
+ params['data']['location']['range']['start']['character'],
482
+ params['data']['location']['range']['end']['line'],
483
+ params['data']['location']['range']['end']['character']
484
+ )
485
+ )
486
+ result.concat library.locate_pins(location).select{ |pin| pin.name == params['label'] }
487
+ end
488
+ if params['data']['path']
489
+ result.concat library.path_pins(params['data']['path'])
490
+ # @todo This exception is necessary because `Library#path_pins` does
491
+ # not perform a namespace method query, so the implicit `.new` pin
492
+ # might not exist.
493
+ if result.empty? && params['data']['path'] =~ /\.new$/
494
+ result.concat(library.path_pins(params['data']['path'].sub(/\.new$/, '#initialize')).map do |pin|
495
+ next pin unless pin.name == 'initialize'
496
+
497
+ Pin::Method.new(
498
+ name: 'new',
499
+ scope: :class,
500
+ location: pin.location,
501
+ parameters: pin.parameters,
502
+ return_type: ComplexType.try_parse(params['data']['path']),
503
+ comments: pin.comments,
504
+ closure: pin.closure
505
+ )
506
+ end)
507
+ end
508
+ end
509
+ # Selecting by both location and path can result in duplicate pins
510
+ result.uniq { |p| [p.path, p.location] }
511
+ end
512
+
513
+ # @param uri [String]
514
+ # @return [String]
515
+ def read_text uri
516
+ library = library_for(uri)
517
+ filename = uri_to_file(uri)
518
+ library.read_text(filename)
519
+ end
520
+
521
+ # @param uri [String]
522
+ # @return [Hash]
523
+ def formatter_config uri
524
+ library = library_for(uri)
525
+ library.workspace.config.formatter
526
+ end
527
+
528
+ # @param uri [String]
529
+ # @param line [Integer]
530
+ # @param column [Integer]
531
+ # @return [Solargraph::SourceMap::Completion]
532
+ def completions_at uri, line, column
533
+ library = library_for(uri)
534
+ library.completions_at uri_to_file(uri), line, column
535
+ end
536
+
537
+ # @return [Bool] if has pending completion request
538
+ def has_pending_completions?
539
+ message_worker.messages.reverse_each.any? { |req| req['method'] == 'textDocument/completion' }
540
+ end
541
+
542
+ # @param uri [String]
543
+ # @param line [Integer]
544
+ # @param column [Integer]
545
+ # @return [Array<Solargraph::Pin::Base>]
546
+ def definitions_at uri, line, column
547
+ library = library_for(uri)
548
+ library.definitions_at(uri_to_file(uri), line, column)
549
+ end
550
+
551
+ # @param uri [String]
552
+ # @param line [Integer]
553
+ # @param column [Integer]
554
+ # @return [Array<Solargraph::Pin::Base>]
555
+ def type_definitions_at uri, line, column
556
+ library = library_for(uri)
557
+ library.type_definitions_at(uri_to_file(uri), line, column)
558
+ end
559
+
560
+ # @param uri [String]
561
+ # @param line [Integer]
562
+ # @param column [Integer]
563
+ # @return [Array<Solargraph::Pin::Base>]
564
+ def signatures_at uri, line, column
565
+ library = library_for(uri)
566
+ library.signatures_at(uri_to_file(uri), line, column)
567
+ end
568
+
569
+ # @param uri [String]
570
+ # @param line [Integer]
571
+ # @param column [Integer]
572
+ # @param strip [Boolean] Strip special characters from variable names
573
+ # @param only [Boolean] If true, search current file only
574
+ # @return [Array<Solargraph::Location>]
575
+ def references_from uri, line, column, strip: true, only: false
576
+ library = library_for(uri)
577
+ library.references_from(uri_to_file(uri), line, column, strip: strip, only: only)
578
+ end
579
+
580
+ # @param query [String]
581
+ # @return [Array<Solargraph::Pin::Base>]
582
+ def query_symbols query
583
+ result = []
584
+ (libraries + [generic_library]).each { |lib| result.concat lib.query_symbols(query) }
585
+ result.uniq
586
+ end
587
+
588
+ # @param query [String]
589
+ # @return [Array<String>]
590
+ def search query
591
+ result = []
592
+ libraries.each { |lib| result.concat lib.search(query) }
593
+ result
594
+ end
595
+
596
+ # @param query [String]
597
+ # @return [Array]
598
+ def document query
599
+ result = []
600
+ libraries.each { |lib| result.concat lib.document(query) }
601
+ result
602
+ end
603
+
604
+ # @param uri [String]
605
+ # @return [Array<Solargraph::Pin::Base>]
606
+ def document_symbols uri
607
+ library = library_for(uri)
608
+ # At this level, document symbols should be unique; e.g., a
609
+ # module_function method should return the location for Module.method
610
+ # or Module#method, but not both.
611
+ library.document_symbols(uri_to_file(uri)).uniq(&:location)
612
+ end
613
+
614
+ # Send a notification to the client.
615
+ #
616
+ # @param text [String]
617
+ # @param type [Integer] A MessageType constant
618
+ # @return [void]
619
+ def show_message text, type = LanguageServer::MessageTypes::INFO
620
+ send_notification 'window/showMessage', {
621
+ type: type,
622
+ message: text
623
+ }
624
+ end
625
+
626
+ # Send a notification with optional responses.
627
+ #
628
+ # @param text [String]
629
+ # @param type [Integer] A MessageType constant
630
+ # @param actions [Array<String>] Response options for the client
631
+ # @param block The block that processes the response
632
+ # @yieldparam [String] The action received from the client
633
+ # @return [void]
634
+ def show_message_request text, type, actions, &block
635
+ send_request 'window/showMessageRequest', {
636
+ type: type,
637
+ message: text,
638
+ actions: actions
639
+ }, &block
640
+ end
641
+
642
+ # Get a list of IDs for server requests that are waiting for responses
643
+ # from the client.
644
+ #
645
+ # @return [Array<Integer>]
646
+ def pending_requests
647
+ requests.keys
648
+ end
649
+
650
+ # @return [Hash{String => [Boolean,String]}]
651
+ def default_configuration
652
+ {
653
+ 'completion' => true,
654
+ 'hover' => true,
655
+ 'symbols' => true,
656
+ 'definitions' => true,
657
+ 'typeDefinitions' => true,
658
+ 'rename' => true,
659
+ 'references' => true,
660
+ 'autoformat' => false,
661
+ 'diagnostics' => true,
662
+ 'formatting' => false,
663
+ 'folding' => true,
664
+ 'highlights' => true,
665
+ 'logLevel' => 'warn'
666
+ }
667
+ end
668
+
669
+ # @param uri [String]
670
+ # @return [Array<Range>]
671
+ def folding_ranges uri
672
+ sources.find(uri).folding_ranges
673
+ end
674
+
675
+ # @return [void]
676
+ def catalog
677
+ return unless libraries.all?(&:mapped?)
678
+ libraries.each(&:catalog)
679
+ end
680
+
681
+ # @return [Hash{String => Hash{String => Boolean}}]
682
+ def client_capabilities
683
+ @client_capabilities ||= {}
684
+ end
685
+
686
+ def client_supports_progress?
687
+ client_capabilities['window'] && client_capabilities['window']['workDoneProgress']
688
+ end
689
+
690
+ private
691
+
692
+ # @return [Array<Integer>]
693
+ def cancelled
694
+ @cancelled ||= []
695
+ end
696
+
697
+ # @return [MessageWorker]
698
+ def message_worker
699
+ @message_worker ||= MessageWorker.new(self)
700
+ end
701
+
702
+ # @return [Diagnoser]
703
+ def diagnoser
704
+ @diagnoser ||= Diagnoser.new(self)
705
+ end
706
+
707
+ # A hash of client requests by ID. The host uses this to keep track of
708
+ # pending responses.
709
+ #
710
+ # @return [Hash{Integer => Solargraph::LanguageServer::Host}]
711
+ def requests
712
+ @requests ||= {}
713
+ end
714
+
715
+ # @param path [String]
716
+ # @return [String]
717
+ def normalize_separators path
718
+ return path if File::ALT_SEPARATOR.nil?
719
+ path.gsub(File::ALT_SEPARATOR, File::SEPARATOR)
720
+ end
721
+
722
+ # @param params [Hash]
723
+ # @return [Source::Updater]
724
+ def generate_updater params
725
+ changes = []
726
+ params['contentChanges'].each do |recvd|
727
+ chng = check_diff(params['textDocument']['uri'], recvd)
728
+ changes.push Solargraph::Source::Change.new(
729
+ (chng['range'].nil? ?
730
+ nil :
731
+ Solargraph::Range.from_to(chng['range']['start']['line'], chng['range']['start']['character'], chng['range']['end']['line'], chng['range']['end']['character'])
732
+ ),
733
+ chng['text']
734
+ )
735
+ end
736
+ Solargraph::Source::Updater.new(
737
+ uri_to_file(params['textDocument']['uri']),
738
+ params['textDocument']['version'],
739
+ changes
740
+ )
741
+ end
742
+
743
+ # @param uri [String]
744
+ # @param change [Hash]
745
+ # @return [Hash]
746
+ def check_diff uri, change
747
+ return change if change['range']
748
+ source = sources.find(uri)
749
+ return change if source.code.length + 1 != change['text'].length
750
+ diffs = Diff::LCS.diff(source.code, change['text'])
751
+ return change if diffs.length.zero? || diffs.length > 1 || diffs.first.length > 1
752
+ # @type [Diff::LCS::Change]
753
+ diff = diffs.first.first
754
+ return change unless diff.adding? && ['.', ':', '(', ',', ' '].include?(diff.element)
755
+ position = Solargraph::Position.from_offset(source.code, diff.position)
756
+ {
757
+ 'range' => {
758
+ 'start' => {
759
+ 'line' => position.line,
760
+ 'character' => position.character
761
+ },
762
+ 'end' => {
763
+ 'line' => position.line,
764
+ 'character' => position.character
765
+ }
766
+ },
767
+ 'text' => diff.element
768
+ }
769
+ rescue Solargraph::FileNotFoundError
770
+ change
771
+ end
772
+
773
+ # @return [Hash]
774
+ def dynamic_capability_options
775
+ @dynamic_capability_options ||= {
776
+ # textDocumentSync: 2, # @todo What should this be?
777
+ 'textDocument/completion' => {
778
+ resolveProvider: true,
779
+ triggerCharacters: ['.', ':', '@']
780
+ },
781
+ # hoverProvider: true,
782
+ # definitionProvider: true,
783
+ 'textDocument/signatureHelp' => {
784
+ triggerCharacters: ['(', ',', ' ']
785
+ },
786
+ # documentFormattingProvider: true,
787
+ 'textDocument/onTypeFormatting' => {
788
+ firstTriggerCharacter: '{',
789
+ moreTriggerCharacter: ['(']
790
+ },
791
+ # documentSymbolProvider: true,
792
+ # workspaceSymbolProvider: true,
793
+ # workspace: {
794
+ # workspaceFolders: {
795
+ # supported: true,
796
+ # changeNotifications: true
797
+ # }
798
+ # }
799
+ 'textDocument/definition' => {
800
+ definitionProvider: true
801
+ },
802
+ 'textDocument/typeDefinition' => {
803
+ typeDefinitionProvider: true
804
+ },
805
+ 'textDocument/references' => {
806
+ referencesProvider: true
807
+ },
808
+ 'textDocument/rename' => {
809
+ renameProvider: prepare_rename? ? { prepareProvider: true } : true
810
+ },
811
+ 'textDocument/documentSymbol' => {
812
+ documentSymbolProvider: true
813
+ },
814
+ 'workspace/symbol' => {
815
+ workspaceSymbolProvider: true
816
+ },
817
+ 'textDocument/formatting' => {
818
+ formattingProvider: true
819
+ },
820
+ 'textDocument/foldingRange' => {
821
+ foldingRangeProvider: true
822
+ },
823
+ 'textDocument/codeAction' => {
824
+ codeActionProvider: true
825
+ },
826
+ 'textDocument/documentHighlight' => {
827
+ documentHighlightProvider: true
828
+ }
829
+ }
830
+ end
831
+
832
+ def prepare_rename?
833
+ client_capabilities['rename'] && client_capabilities['rename']['prepareSupport']
834
+ end
835
+
836
+ # @param library [Library]
837
+ # @return [void]
838
+ def library_map library
839
+ return if library.mapped?
840
+ Thread.new { sync_library_map library }
841
+ end
842
+
843
+ # @param library [Library]
844
+ # @param uuid [String, nil]
845
+ # @return [void]
846
+ def sync_library_map library
847
+ total = library.workspace.sources.length
848
+ progress = Progress.new('Mapping workspace')
849
+ progress.begin "0/#{total} files", 0
850
+ progress.send self
851
+ while library.next_map
852
+ pct = ((library.source_map_hash.keys.length.to_f / total) * 100).to_i
853
+ progress.report "#{library.source_map_hash.keys.length}/#{total} files", pct
854
+ progress.send self
855
+ end
856
+ progress.finish 'done'
857
+ progress.send self
858
+ end
859
+ end
860
+ end
861
+ end