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,522 +1,495 @@
1
- # frozen_string_literal: true
2
-
3
- require 'yard'
4
-
5
- module Solargraph
6
- # A Ruby file that has been parsed into an AST.
7
- #
8
- class Source
9
- autoload :Updater, 'solargraph/source/updater'
10
- autoload :Change, 'solargraph/source/change'
11
- autoload :Mapper, 'solargraph/source/mapper'
12
- autoload :EncodingFixes, 'solargraph/source/encoding_fixes'
13
- autoload :Cursor, 'solargraph/source/cursor'
14
- autoload :Chain, 'solargraph/source/chain'
15
- autoload :SourceChainer, 'solargraph/source/source_chainer'
16
-
17
- include EncodingFixes
18
-
19
- # @return [String]
20
- attr_reader :filename
21
-
22
- # @return [String]
23
- attr_reader :code
24
-
25
- # @return [Parser::AST::Node]
26
- attr_reader :node
27
-
28
- # @return [Hash{Integer => Array<String>}]
29
- attr_reader :comments
30
-
31
- # @todo Deprecate?
32
- # @return [Integer]
33
- attr_reader :version
34
-
35
- # @param code [String]
36
- # @param filename [String]
37
- # @param version [Integer]
38
- def initialize code, filename = nil, version = 0
39
- @code = normalize(code)
40
- @repaired = code
41
- @filename = filename
42
- @version = version
43
- @domains = []
44
- begin
45
- @node, @comments = Solargraph::Parser.parse_with_comments(@code, filename)
46
- @parsed = true
47
- rescue Parser::SyntaxError, EncodingError => e
48
- @node = nil
49
- @comments = {}
50
- @parsed = false
51
- ensure
52
- @code.freeze
53
- end
54
- end
55
-
56
- # @param range [Solargraph::Range]
57
- # @return [String]
58
- def at range
59
- from_to range.start.line, range.start.character, range.ending.line, range.ending.character
60
- end
61
-
62
- # @param l1 [Integer]
63
- # @param c1 [Integer]
64
- # @param l2 [Integer]
65
- # @param c2 [Integer]
66
- # @return [String]
67
- def from_to l1, c1, l2, c2
68
- b = Solargraph::Position.line_char_to_offset(@code, l1, c1)
69
- e = Solargraph::Position.line_char_to_offset(@code, l2, c2)
70
- @code[b..e-1]
71
- end
72
-
73
- # Get the nearest node that contains the specified index.
74
- #
75
- # @param line [Integer]
76
- # @param column [Integer]
77
- # @return [AST::Node]
78
- def node_at(line, column)
79
- tree_at(line, column).first
80
- end
81
-
82
- # Get an array of nodes containing the specified index, starting with the
83
- # nearest node and ending with the root.
84
- #
85
- # @param line [Integer]
86
- # @param column [Integer]
87
- # @return [Array<AST::Node>]
88
- def tree_at(line, column)
89
- # offset = Position.line_char_to_offset(@code, line, column)
90
- position = Position.new(line, column)
91
- stack = []
92
- inner_tree_at @node, position, stack
93
- stack
94
- end
95
-
96
- # Start synchronizing the source. This method updates the code without
97
- # parsing a new AST. The resulting Source object will be marked not
98
- # synchronized (#synchronized? == false).
99
- #
100
- # @param updater [Source::Updater]
101
- # @return [Source]
102
- def start_synchronize updater
103
- raise 'Invalid synchronization' unless updater.filename == filename
104
- real_code = updater.write(@code)
105
- src = Source.allocate
106
- src.filename = filename
107
- src.code = real_code
108
- src.version = updater.version
109
- src.parsed = parsed?
110
- src.repaired = updater.repair(@repaired)
111
- src.synchronized = false
112
- src.node = @node
113
- src.comments = @comments
114
- src.error_ranges = error_ranges
115
- src.last_updater = updater
116
- return src.finish_synchronize unless real_code.lines.length == @code.lines.length
117
- src
118
- end
119
-
120
- # Finish synchronizing a source that was updated via #start_synchronize.
121
- # This method returns self if the source is already synchronized. Otherwise
122
- # it parses the AST and returns a new synchronized Source.
123
- #
124
- # @return [Source]
125
- def finish_synchronize
126
- return self if synchronized?
127
- synced = Source.new(@code, filename)
128
- if synced.parsed?
129
- synced.version = version
130
- return synced
131
- end
132
- synced = Source.new(@repaired, filename)
133
- synced.error_ranges.concat (error_ranges + last_updater.changes.map(&:range))
134
- synced.code = @code
135
- synced.synchronized = true
136
- synced.version = version
137
- synced
138
- end
139
-
140
- # Synchronize the Source with an update. This method applies changes to the
141
- # code, parses the new code's AST, and returns the resulting Source object.
142
- #
143
- # @param updater [Source::Updater]
144
- # @return [Source]
145
- def synchronize updater
146
- raise 'Invalid synchronization' unless updater.filename == filename
147
- real_code = updater.write(@code)
148
- if real_code == @code
149
- @version = updater.version
150
- return self
151
- end
152
- synced = Source.new(real_code, filename)
153
- if synced.parsed?
154
- synced.version = updater.version
155
- return synced
156
- end
157
- incr_code = updater.repair(@repaired)
158
- synced = Source.new(incr_code, filename)
159
- synced.error_ranges.concat (error_ranges + updater.changes.map(&:range))
160
- synced.code = real_code
161
- synced.version = updater.version
162
- synced
163
- end
164
-
165
- # @param position [Position, Array(Integer, Integer)]
166
- # @return [Source::Cursor]
167
- def cursor_at position
168
- Cursor.new(self, position)
169
- end
170
-
171
- # @return [Boolean]
172
- def parsed?
173
- @parsed
174
- end
175
-
176
- def repaired?
177
- @is_repaired ||= (@code != @repaired)
178
- end
179
-
180
- # @param position [Position]
181
- # @return [Boolean]
182
- def string_at? position
183
- if Parser.rubyvm?
184
- string_ranges.each do |range|
185
- if synchronized?
186
- return true if range.include?(position) || range.ending == position
187
- else
188
- return true if last_updater && last_updater.changes.one? && range.contain?(last_updater.changes.first.range.start)
189
- end
190
- end
191
- false
192
- else
193
- return false if Position.to_offset(code, position) >= code.length
194
- string_nodes.each do |node|
195
- range = Range.from_node(node)
196
- next if range.ending.line < position.line
197
- break if range.ending.line > position.line
198
- return true if node.type == :str && range.include?(position) && range.start != position
199
- return true if [:STR, :str].include?(node.type) && range.include?(position) && range.start != position
200
- if node.type == :dstr
201
- inner = node_at(position.line, position.column)
202
- next if inner.nil?
203
- inner_range = Range.from_node(inner)
204
- next unless range.include?(inner_range.ending)
205
- return true if inner.type == :str
206
- inner_code = at(Solargraph::Range.new(inner_range.start, position))
207
- return true if (inner.type == :dstr && inner_range.ending.character <= position.character) && !inner_code.end_with?('}') ||
208
- (inner.type != :dstr && inner_range.ending.line == position.line && position.character <= inner_range.ending.character && inner_code.end_with?('}'))
209
- end
210
- break if range.ending.line > position.line
211
- end
212
- false
213
- end
214
- end
215
-
216
- def string_ranges
217
- @string_ranges ||= Parser.string_ranges(node)
218
- end
219
-
220
- # @param position [Position]
221
- # @return [Boolean]
222
- def comment_at? position
223
- comment_ranges.each do |range|
224
- return true if range.include?(position) ||
225
- (range.ending.line == position.line && range.ending.column < position.column)
226
- break if range.ending.line > position.line
227
- end
228
- false
229
- end
230
-
231
- # @param name [String]
232
- # @return [Array<Location>]
233
- def references name
234
- Parser.references self, name
235
- end
236
-
237
- # @return [Array<Range>]
238
- def error_ranges
239
- @error_ranges ||= []
240
- end
241
-
242
- # @param node [Parser::AST::Node]
243
- # @return [String]
244
- def code_for(node)
245
- rng = Range.from_node(node)
246
- b = Position.line_char_to_offset(@code, rng.start.line, rng.start.column)
247
- e = Position.line_char_to_offset(@code, rng.ending.line, rng.ending.column)
248
- frag = code[b..e-1].to_s
249
- frag.strip.gsub(/,$/, '')
250
- end
251
-
252
- # @param node [Parser::AST::Node]
253
- # @return [String]
254
- def comments_for node
255
- rng = Range.from_node(node)
256
- stringified_comments[rng.start.line] ||= begin
257
- buff = associated_comments[rng.start.line]
258
- buff ? stringify_comment_array(buff) : nil
259
- end
260
- end
261
-
262
- # A location representing the file in its entirety.
263
- #
264
- # @return [Location]
265
- def location
266
- st = Position.new(0, 0)
267
- en = Position.from_offset(code, code.length)
268
- range = Range.new(st, en)
269
- Location.new(filename, range)
270
- end
271
-
272
- FOLDING_NODE_TYPES = if Parser.rubyvm?
273
- %i[
274
- CLASS SCLASS MODULE DEFN DEFS IF WHILE UNLESS ITER STR HASH ARRAY LIST
275
- ].freeze
276
- else
277
- %i[
278
- class sclass module def defs if str dstr array while unless kwbegin hash block
279
- ].freeze
280
- end
281
-
282
- # Get an array of ranges that can be folded, e.g., the range of a class
283
- # definition or an if condition.
284
- #
285
- # See FOLDING_NODE_TYPES for the list of node types that can be folded.
286
- #
287
- # @return [Array<Range>]
288
- def folding_ranges
289
- @folding_ranges ||= begin
290
- result = []
291
- inner_folding_ranges node, result
292
- result.concat foldable_comment_block_ranges
293
- result
294
- end
295
- end
296
-
297
- def synchronized?
298
- @synchronized = true if @synchronized.nil?
299
- @synchronized
300
- end
301
-
302
- # Get a hash of comments grouped by the line numbers of the associated code.
303
- #
304
- # @return [Hash{Integer => Array<Parser::Source::Comment>}]
305
- def associated_comments
306
- @associated_comments ||= begin
307
- result = {}
308
- buffer = String.new('')
309
- last = nil
310
- @comments.each_pair do |num, snip|
311
- if !last || num == last + 1
312
- buffer.concat "#{snip.text}\n"
313
- else
314
- result[first_not_empty_from(last + 1)] = buffer.clone
315
- buffer.replace "#{snip.text}\n"
316
- end
317
- last = num
318
- end
319
- result[first_not_empty_from(last + 1)] = buffer unless buffer.empty? || last.nil?
320
- result
321
- end
322
- end
323
-
324
- private
325
-
326
- def first_not_empty_from line
327
- cursor = line
328
- cursor += 1 while cursor < code_lines.length && code_lines[cursor].strip.empty?
329
- cursor = line if cursor > code_lines.length - 1
330
- cursor
331
- end
332
-
333
- # @param top [Parser::AST::Node]
334
- # @param result [Array<Range>]
335
- # @param parent [Symbol]
336
- # @return [void]
337
- def inner_folding_ranges top, result = [], parent = nil
338
- return unless Parser.is_ast_node?(top)
339
- if FOLDING_NODE_TYPES.include?(top.type)
340
- # @todo Smelly exception for hash's first-level array in RubyVM
341
- unless [:ARRAY, :LIST].include?(top.type) && parent == :HASH
342
- range = Range.from_node(top)
343
- if result.empty? || range.start.line > result.last.start.line
344
- result.push range unless range.ending.line - range.start.line < 2
345
- end
346
- end
347
- end
348
- top.children.each do |child|
349
- inner_folding_ranges(child, result, top.type)
350
- end
351
- end
352
-
353
- # Get a string representation of an array of comments.
354
- #
355
- # @param comments [String]
356
- # @return [String]
357
- def stringify_comment_array comments
358
- ctxt = String.new('')
359
- started = false
360
- skip = nil
361
- comments.lines.each { |l|
362
- # Trim the comment and minimum leading whitespace
363
- p = l.encode('UTF-8', invalid: :replace, replace: '?').gsub(/^#+/, '')
364
- if p.strip.empty?
365
- next unless started
366
- ctxt.concat p
367
- else
368
- here = p.index(/[^ \t]/)
369
- skip = here if skip.nil? || here < skip
370
- ctxt.concat p[skip..-1]
371
- end
372
- started = true
373
- }
374
- ctxt
375
- end
376
-
377
- # A hash of line numbers and their associated comments.
378
- #
379
- # @return [Hash{Integer => Array<String>}]
380
- def stringified_comments
381
- @stringified_comments ||= {}
382
- end
383
-
384
- # @return [Array<Parser::AST::Node>]
385
- def string_nodes
386
- @string_nodes ||= string_nodes_in(@node)
387
- end
388
-
389
- # @return [Array<Range>]
390
- def comment_ranges
391
- @comment_ranges ||= @comments.values.map(&:range)
392
- end
393
-
394
- # Get an array of foldable comment block ranges. Blocks are excluded if
395
- # they are less than 3 lines long.
396
- #
397
- # @return [Array<Range>]
398
- def foldable_comment_block_ranges
399
- return [] unless synchronized?
400
- result = []
401
- grouped = []
402
- comments.keys.each do |l|
403
- if grouped.empty? || l == grouped.last + 1
404
- grouped.push l
405
- else
406
- result.push Range.from_to(grouped.first, 0, grouped.last, 0) unless grouped.length < 3
407
- grouped = [l]
408
- end
409
- end
410
- result.push Range.from_to(grouped.first, 0, grouped.last, 0) unless grouped.length < 3
411
- result
412
- end
413
-
414
- # @param n [Parser::AST::Node]
415
- # @return [Array<Parser::AST::Node>]
416
- def string_nodes_in n
417
- result = []
418
- if Parser.is_ast_node?(n)
419
- if n.type == :str || n.type == :dstr || n.type == :STR || n.type == :DSTR
420
- result.push n
421
- else
422
- n.children.each{ |c| result.concat string_nodes_in(c) }
423
- end
424
- end
425
- result
426
- end
427
-
428
- # @param node [Parser::AST::Node]
429
- # @param position [Position]
430
- # @param stack [Array<Parser::AST::Node>]
431
- # @return [void]
432
- def inner_tree_at node, position, stack
433
- return if node.nil?
434
- here = Range.from_node(node)
435
- if here.contain?(position) || colonized(here, position, node)
436
- stack.unshift node
437
- node.children.each do |c|
438
- next unless Parser.is_ast_node?(c)
439
- next if !Parser.rubyvm? && c.loc.expression.nil?
440
- inner_tree_at(c, position, stack)
441
- end
442
- end
443
- end
444
-
445
- def colonized range, position, node
446
- node.type == :COLON2 &&
447
- range.ending.line == position.line &&
448
- range.ending.character == position.character - 2 &&
449
- code[Position.to_offset(code, Position.new(position.line, position.character - 2)), 2] == '::'
450
- end
451
-
452
- protected
453
-
454
- # @return [String]
455
- attr_writer :filename
456
-
457
- # @return [Integer]
458
- attr_writer :version
459
-
460
- # @param val [String]
461
- # @return [String]
462
- def code=(val)
463
- @code_lines= nil
464
- @code = val
465
- end
466
-
467
- # @return [Parser::AST::Node]
468
- attr_writer :node
469
-
470
- # @return [Array<Range>]
471
- attr_writer :error_ranges
472
-
473
- # @return [String]
474
- attr_accessor :repaired
475
-
476
- # @return [Boolean]
477
- attr_writer :parsed
478
-
479
- # @return [Array<Parser::Source::Comment>]
480
- attr_writer :comments
481
-
482
- # @return [Boolean]
483
- attr_writer :synchronized
484
-
485
- # @return [Source::Updater]
486
- attr_accessor :last_updater
487
-
488
- private
489
-
490
- # @return [Array<String>]
491
- def code_lines
492
- @code_lines ||= code.lines
493
- end
494
-
495
- class << self
496
- # @param filename [String]
497
- # @return [Solargraph::Source]
498
- def load filename
499
- file = File.open(filename)
500
- code = file.read
501
- file.close
502
- Source.load_string(code, filename)
503
- end
504
-
505
- # @param code [String]
506
- # @param filename [String]
507
- # @param version [Integer]
508
- # @return [Solargraph::Source]
509
- def load_string code, filename = nil, version = 0
510
- Source.new code, filename, version
511
- end
512
-
513
- # @param comments [String]
514
- # @return [YARD::DocstringParser]
515
- def parse_docstring comments
516
- # HACK: Pass a dummy code object to the parser for plugins that
517
- # expect it not to be nil
518
- YARD::Docstring.parser.parse(comments, YARD::CodeObjects::Base.new(:root, 'stub'))
519
- end
520
- end
521
- end
522
- end
1
+ # frozen_string_literal: true
2
+
3
+ require 'yard'
4
+
5
+ module Solargraph
6
+ # A Ruby file that has been parsed into an AST.
7
+ #
8
+ class Source
9
+ autoload :Updater, 'solargraph/source/updater'
10
+ autoload :Change, 'solargraph/source/change'
11
+ autoload :EncodingFixes, 'solargraph/source/encoding_fixes'
12
+ autoload :Cursor, 'solargraph/source/cursor'
13
+ autoload :Chain, 'solargraph/source/chain'
14
+ autoload :SourceChainer, 'solargraph/source/source_chainer'
15
+
16
+ include EncodingFixes
17
+
18
+ # @return [String]
19
+ attr_reader :filename
20
+
21
+ # @return [String]
22
+ def code
23
+ finalize
24
+ @code
25
+ end
26
+
27
+ # @return [Parser::AST::Node, nil]
28
+ def node
29
+ finalize
30
+ @node
31
+ end
32
+
33
+ # @return [Hash{Integer => Array<String>}]
34
+ def comments
35
+ finalize
36
+ @comments
37
+ end
38
+
39
+ # @todo Deprecate?
40
+ # @return [Integer]
41
+ attr_reader :version
42
+
43
+ # @param code [String]
44
+ # @param filename [String, nil]
45
+ # @param version [Integer]
46
+ def initialize code, filename = nil, version = 0
47
+ @code = normalize(code)
48
+ @repaired = code
49
+ @filename = filename
50
+ @version = version
51
+ end
52
+
53
+ # @param range [Solargraph::Range]
54
+ # @return [String]
55
+ def at range
56
+ from_to range.start.line, range.start.character, range.ending.line, range.ending.character
57
+ end
58
+
59
+ # @param l1 [Integer]
60
+ # @param c1 [Integer]
61
+ # @param l2 [Integer]
62
+ # @param c2 [Integer]
63
+ # @return [String]
64
+ def from_to l1, c1, l2, c2
65
+ b = Solargraph::Position.line_char_to_offset(code, l1, c1)
66
+ e = Solargraph::Position.line_char_to_offset(code, l2, c2)
67
+ code[b..e-1]
68
+ end
69
+
70
+ # Get the nearest node that contains the specified index.
71
+ #
72
+ # @param line [Integer]
73
+ # @param column [Integer]
74
+ # @return [AST::Node]
75
+ def node_at(line, column)
76
+ tree_at(line, column).first
77
+ end
78
+
79
+ # Get an array of nodes containing the specified index, starting with the
80
+ # nearest node and ending with the root.
81
+ #
82
+ # @param line [Integer]
83
+ # @param column [Integer]
84
+ # @return [Array<AST::Node>]
85
+ def tree_at(line, column)
86
+ position = Position.new(line, column)
87
+ stack = []
88
+ inner_tree_at node, position, stack
89
+ stack
90
+ end
91
+
92
+ # Synchronize the Source with an update. This method applies changes to the
93
+ # code, parses the new code's AST, and returns the resulting Source object.
94
+ #
95
+ # @param updater [Source::Updater]
96
+ # @return [Source]
97
+ def synchronize updater
98
+ raise 'Invalid synchronization' unless updater.filename == filename
99
+ real_code = updater.write(@code)
100
+ if real_code == @code
101
+ @version = updater.version
102
+ return self
103
+ end
104
+ Source.new(@code, filename, updater.version).tap do |src|
105
+ src.repaired = @repaired
106
+ src.error_ranges.concat error_ranges
107
+ src.changes.concat(changes + updater.changes)
108
+ end
109
+ end
110
+
111
+ # @param position [Position, Array(Integer, Integer)]
112
+ # @return [Source::Cursor]
113
+ def cursor_at position
114
+ finalize
115
+ Cursor.new(self, position)
116
+ end
117
+
118
+ # @return [Boolean]
119
+ def parsed?
120
+ finalize
121
+ @parsed
122
+ end
123
+
124
+ def repaired?
125
+ code != @repaired
126
+ end
127
+
128
+ # @param position [Position]
129
+ # @return [Boolean]
130
+ def string_at? position
131
+ return false if Position.to_offset(code, position) >= code.length
132
+ string_nodes.each do |node|
133
+ range = Range.from_node(node)
134
+ next if range.ending.line < position.line
135
+ break if range.ending.line > position.line
136
+ return true if node.type == :str && range.include?(position) && range.start != position
137
+ return true if [:STR, :str].include?(node.type) && range.include?(position) && range.start != position
138
+ if node.type == :dstr
139
+ inner = node_at(position.line, position.column)
140
+ next if inner.nil?
141
+ inner_range = Range.from_node(inner)
142
+ next unless range.include?(inner_range.ending)
143
+ return true if inner.type == :str
144
+ inner_code = at(Solargraph::Range.new(inner_range.start, position))
145
+ return true if (inner.type == :dstr && inner_range.ending.character <= position.character) && !inner_code.end_with?('}') ||
146
+ (inner.type != :dstr && inner_range.ending.line == position.line && position.character <= inner_range.ending.character && inner_code.end_with?('}'))
147
+ end
148
+ break if range.ending.line > position.line
149
+ end
150
+ false
151
+ end
152
+
153
+ # @return [::Array<Range>]
154
+ def string_ranges
155
+ @string_ranges ||= Parser.string_ranges(node)
156
+ end
157
+
158
+ # @param position [Position]
159
+ # @return [Boolean]
160
+ def comment_at? position
161
+ comment_ranges.each do |range|
162
+ return true if range.include?(position) ||
163
+ (range.ending.line == position.line && range.ending.column < position.column)
164
+ break if range.ending.line > position.line
165
+ end
166
+ false
167
+ end
168
+
169
+ # @param name [String]
170
+ # @return [Array<Location>]
171
+ def references name
172
+ Parser.references self, name
173
+ end
174
+
175
+ # @return [Array<Range>]
176
+ def error_ranges
177
+ @error_ranges ||= []
178
+ end
179
+
180
+ # @param node [Parser::AST::Node]
181
+ # @return [String]
182
+ def code_for(node)
183
+ rng = Range.from_node(node)
184
+ b = Position.line_char_to_offset(code, rng.start.line, rng.start.column)
185
+ e = Position.line_char_to_offset(code, rng.ending.line, rng.ending.column)
186
+ frag = code[b..e-1].to_s
187
+ frag.strip.gsub(/,$/, '')
188
+ end
189
+
190
+ # @param node [Parser::AST::Node]
191
+ # @return [String, nil]
192
+ def comments_for node
193
+ rng = Range.from_node(node)
194
+ stringified_comments[rng.start.line] ||= begin
195
+ buff = associated_comments[rng.start.line]
196
+ buff ? stringify_comment_array(buff) : nil
197
+ end
198
+ end
199
+
200
+ # A location representing the file in its entirety.
201
+ #
202
+ # @return [Location]
203
+ def location
204
+ st = Position.new(0, 0)
205
+ en = Position.from_offset(code, code.length)
206
+ range = Range.new(st, en)
207
+ Location.new(filename, range)
208
+ end
209
+
210
+ FOLDING_NODE_TYPES = %i[
211
+ class sclass module def defs if str dstr array while unless kwbegin hash block
212
+ ].freeze
213
+
214
+ # Get an array of ranges that can be folded, e.g., the range of a class
215
+ # definition or an if condition.
216
+ #
217
+ # See FOLDING_NODE_TYPES for the list of node types that can be folded.
218
+ #
219
+ # @return [Array<Range>]
220
+ def folding_ranges
221
+ @folding_ranges ||= begin
222
+ result = []
223
+ inner_folding_ranges node, result
224
+ result.concat foldable_comment_block_ranges
225
+ result
226
+ end
227
+ end
228
+
229
+ def synchronized?
230
+ true
231
+ end
232
+
233
+ # Get a hash of comments grouped by the line numbers of the associated code.
234
+ #
235
+ # @return [Hash{Integer => String}]
236
+ def associated_comments
237
+ @associated_comments ||= begin
238
+ result = {}
239
+ buffer = String.new('')
240
+ # @type [Integer, nil]
241
+ last = nil
242
+ comments.each_pair do |num, snip|
243
+ if !last || num == last + 1
244
+ buffer.concat "#{snip.text}\n"
245
+ else
246
+ result[first_not_empty_from(last + 1)] = buffer.clone
247
+ buffer.replace "#{snip.text}\n"
248
+ end
249
+ last = num
250
+ end
251
+ result[first_not_empty_from(last + 1)] = buffer unless buffer.empty? || last.nil?
252
+ result
253
+ end
254
+ end
255
+
256
+ private
257
+
258
+ # @param line [Integer]
259
+ # @return [Integer]
260
+ def first_not_empty_from line
261
+ cursor = line
262
+ cursor += 1 while cursor < code_lines.length && code_lines[cursor].strip.empty?
263
+ cursor = line if cursor > code_lines.length - 1
264
+ cursor
265
+ end
266
+
267
+ # @param top [Parser::AST::Node]
268
+ # @param result [Array<Range>]
269
+ # @param parent [Symbol, nil]
270
+ # @return [void]
271
+ def inner_folding_ranges top, result = [], parent = nil
272
+ return unless Parser.is_ast_node?(top)
273
+ if FOLDING_NODE_TYPES.include?(top.type)
274
+ range = Range.from_node(top)
275
+ if result.empty? || range.start.line > result.last.start.line
276
+ result.push range unless range.ending.line - range.start.line < 2
277
+ end
278
+ end
279
+ top.children.each do |child|
280
+ inner_folding_ranges(child, result, top.type)
281
+ end
282
+ end
283
+
284
+ # Get a string representation of an array of comments.
285
+ #
286
+ # @param comments [String]
287
+ # @return [String]
288
+ def stringify_comment_array comments
289
+ ctxt = String.new('')
290
+ started = false
291
+ skip = nil
292
+ comments.lines.each { |l|
293
+ # Trim the comment and minimum leading whitespace
294
+ p = l.force_encoding('UTF-8').encode('UTF-8', invalid: :replace, replace: '?').gsub(/^#+/, '')
295
+ if p.strip.empty?
296
+ next unless started
297
+ ctxt.concat p
298
+ else
299
+ here = p.index(/[^ \t]/)
300
+ skip = here if skip.nil? || here < skip
301
+ ctxt.concat p[skip..-1]
302
+ end
303
+ started = true
304
+ }
305
+ ctxt
306
+ end
307
+
308
+ # A hash of line numbers and their associated comments.
309
+ #
310
+ # @return [Hash{Integer => Array<String>, nil}]
311
+ def stringified_comments
312
+ @stringified_comments ||= {}
313
+ end
314
+
315
+ # @return [Array<Parser::AST::Node>]
316
+ def string_nodes
317
+ @string_nodes ||= string_nodes_in(node)
318
+ end
319
+
320
+ # @return [Array<Range>]
321
+ def comment_ranges
322
+ @comment_ranges ||= comments.values.map(&:range)
323
+ end
324
+
325
+ # Get an array of foldable comment block ranges. Blocks are excluded if
326
+ # they are less than 3 lines long.
327
+ #
328
+ # @return [Array<Range>]
329
+ def foldable_comment_block_ranges
330
+ return [] unless synchronized?
331
+ result = []
332
+ grouped = []
333
+ comments.keys.each do |l|
334
+ if grouped.empty? || l == grouped.last + 1
335
+ grouped.push l
336
+ else
337
+ result.push Range.from_to(grouped.first, 0, grouped.last, 0) unless grouped.length < 3
338
+ grouped = [l]
339
+ end
340
+ end
341
+ result.push Range.from_to(grouped.first, 0, grouped.last, 0) unless grouped.length < 3
342
+ result
343
+ end
344
+
345
+ # @param n [Parser::AST::Node, nil]
346
+ # @return [Array<Parser::AST::Node>]
347
+ def string_nodes_in n
348
+ result = []
349
+ if Parser.is_ast_node?(n)
350
+ if n.type == :str || n.type == :dstr || n.type == :STR || n.type == :DSTR
351
+ result.push n
352
+ else
353
+ n.children.each{ |c| result.concat string_nodes_in(c) }
354
+ end
355
+ end
356
+ result
357
+ end
358
+
359
+ # @param node [Parser::AST::Node, nil]
360
+ # @param position [Position]
361
+ # @param stack [Array<Parser::AST::Node>]
362
+ # @return [void]
363
+ def inner_tree_at node, position, stack
364
+ return if node.nil?
365
+ here = Range.from_node(node)
366
+ if here.contain?(position)
367
+ stack.unshift node
368
+ node.children.each do |c|
369
+ next unless Parser.is_ast_node?(c)
370
+ next if c.loc.expression.nil?
371
+ inner_tree_at(c, position, stack)
372
+ end
373
+ end
374
+ end
375
+
376
+ protected
377
+
378
+ # @return [Array<Change>]
379
+ def changes
380
+ @changes ||= []
381
+ end
382
+
383
+ # @return [String]
384
+ attr_writer :filename
385
+
386
+ # @return [Integer]
387
+ attr_writer :version
388
+
389
+ def finalize
390
+ return if @finalized && changes.empty?
391
+
392
+ changes.each do |change|
393
+ @code = change.write(@code)
394
+ end
395
+ @finalized = true
396
+ begin
397
+ @node, @comments = Solargraph::Parser.parse_with_comments(@code, filename)
398
+ @parsed = true
399
+ @repaired = @code
400
+ rescue Parser::SyntaxError, EncodingError => e
401
+ @node = nil
402
+ @comments = {}
403
+ @parsed = false
404
+ ensure
405
+ @code.freeze
406
+ end
407
+ if !@parsed && !changes.empty?
408
+ changes.each do |change|
409
+ @repaired = change.repair(@repaired)
410
+ end
411
+ error_ranges.concat(changes.map(&:range))
412
+ begin
413
+ @node, @comments = Solargraph::Parser.parse_with_comments(@repaired, filename)
414
+ @parsed = true
415
+ rescue Parser::SyntaxError, EncodingError => e
416
+ @node = nil
417
+ @comments = {}
418
+ @parsed = false
419
+ end
420
+ elsif @parsed
421
+ error_ranges.clear
422
+ end
423
+ changes.clear
424
+ end
425
+
426
+ # @param val [String]
427
+ # @return [String]
428
+ def code=(val)
429
+ @code_lines = nil
430
+ @finalized = false
431
+ @code = val
432
+ end
433
+
434
+ # @return [Parser::AST::Node, nil]
435
+ attr_writer :node
436
+
437
+ # @return [Array<Range>]
438
+ attr_writer :error_ranges
439
+
440
+ # @return [String]
441
+ attr_writer :repaired
442
+
443
+ def repaired
444
+ finalize
445
+ @repaired
446
+ end
447
+
448
+ # @return [Boolean]
449
+ attr_writer :parsed
450
+
451
+ # @return [Hash{Integer => String}
452
+ attr_writer :comments
453
+
454
+ # @return [Boolean]
455
+ attr_writer :synchronized
456
+
457
+ private
458
+
459
+ # @return [Array<String>]
460
+ def code_lines
461
+ @code_lines ||= code.lines
462
+ end
463
+
464
+ class << self
465
+ # @param filename [String]
466
+ # @return [Solargraph::Source]
467
+ def load filename
468
+ file = File.open(filename)
469
+ code = file.read
470
+ file.close
471
+ Source.load_string(code, filename)
472
+ end
473
+
474
+ # @param code [String]
475
+ # @param filename [String, nil]
476
+ # @param version [Integer]
477
+ # @return [Solargraph::Source]
478
+ def load_string code, filename = nil, version = 0
479
+ Source.new code, filename, version
480
+ end
481
+
482
+ # @param comments [String]
483
+ # @return [YARD::DocstringParser]
484
+ def parse_docstring comments
485
+ # HACK: Pass a dummy code object to the parser for plugins that
486
+ # expect it not to be nil
487
+ YARD::Docstring.parser.parse(comments, YARD::CodeObjects::Base.new(:root, 'stub'))
488
+ rescue StandardError => e
489
+ Solargraph.logger.info "YARD failed to parse docstring: [#{e.class}] #{e.message}"
490
+ Solargraph.logger.debug "Unparsed comment: #{comments}"
491
+ YARD::Docstring.parser
492
+ end
493
+ end
494
+ end
495
+ end