klenod-lsp 0.0.17 → 0.0.19

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8177f2a77a4e5e7b60ae1c83dc1d5106ad639b1357291987260e1b7915e4bd6f
4
- data.tar.gz: 3a84fbf499a587a1592188606bc6be5d354b85f64d8acb8b03fe4eabc77add25
3
+ metadata.gz: f030e25f4d8f0b9a3a9bb2c06290cb9fc0dfc76d06edd31d1711cbb2ea8f6b06
4
+ data.tar.gz: 2af008e5e1194f19215feb073aa83f53bef621c33978c619a400a962b30263da
5
5
  SHA512:
6
- metadata.gz: 98dc6548b480686f3b577787663feac6a6a4d3e0315eb71a96962986bdb60de975e4078df05369d670a6455733d1028dc89098983e73a4264fcb6ad43089ba53
7
- data.tar.gz: 1ae98c5232a88076529ed9828a0317dc35a532f65ce850abe88c7b2af016270c6f85c87ee9864b49fb0735065addcc6041de155fa485905794f19b2267333b5e
6
+ metadata.gz: 3be7cbd795174e6d9406e0b5973bf251228ac161df739ab607d1d9b22765e4205fdcc7b7c0e30e5e72c2c99b3b05c72a8336fc1c5fe4e3964b2d259b39c6c261
7
+ data.tar.gz: 4893cd09895f929310af2d945703d70b4197236c9e670f5ff431c7351278e5dc4eb122b63da68a5bb51f1c76b2ff3630112ed45c20ca9cfe78a7f6315571cfad
@@ -8,7 +8,7 @@ require_relative "text"
8
8
  module Klenod
9
9
  module LSP
10
10
  # Turns the errors in an Analysis into LSP diagnostics on the original
11
- # document. Every diagnostic is an error: Klenod has no warnings yet.
11
+ # document.
12
12
  module Diagnostics
13
13
  Interface = LanguageServer::Protocol::Interface
14
14
  Constant = LanguageServer::Protocol::Constant
@@ -125,9 +125,17 @@ module Klenod
125
125
  end
126
126
 
127
127
  def diagnostic(span, message)
128
+ diagnostic_with_severity(span, message, Constant::DiagnosticSeverity::ERROR)
129
+ end
130
+
131
+ def warning(span, message)
132
+ diagnostic_with_severity(span, message, Constant::DiagnosticSeverity::WARNING)
133
+ end
134
+
135
+ def diagnostic_with_severity(span, message, severity)
128
136
  Interface::Diagnostic.new(
129
137
  range: span.to_range,
130
- severity: Constant::DiagnosticSeverity::ERROR,
138
+ severity: severity,
131
139
  source: SOURCE,
132
140
  message: message
133
141
  )
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Klenod
4
+ module LSP
5
+ module Languages
6
+ class Haml
7
+ # Detects scripts whose nested Haml will not produce output. Haml's
8
+ # parser retains the body as children of the script node, which avoids
9
+ # guessing from indentation or warning about ordinary Ruby source in
10
+ # a filter.
11
+ module Iteration
12
+ module_function
13
+
14
+ ITERATOR = /\.(?<method>each|map)\b(?:\s*\([^\n]*\))?\s+do\b/
15
+
16
+ def diagnostics(analysis)
17
+ root = Klenod::Build::Plugins::HamlPlugin.parse_haml(analysis.source, module_id: analysis.module_id)
18
+ nodes = root.children.dup
19
+ diagnostics = []
20
+
21
+ until nodes.empty?
22
+ node = nodes.shift
23
+ nodes.concat(node.children)
24
+ case node.type
25
+ when :script
26
+ match = ITERATOR.match(node.value.fetch(:text))
27
+ diagnostics << each_diagnostic(node, match, analysis.lines) if match&.[](:method) == "each" && rendered_content?(node)
28
+ when :silent_script
29
+ diagnostics << silent_script_diagnostic(node, analysis.lines) if rendered_content?(node)
30
+ end
31
+ end
32
+
33
+ diagnostics
34
+ rescue Klenod::Build::Plugins::HamlPlugin::ParseError
35
+ []
36
+ end
37
+
38
+ def rendered_content?(node)
39
+ node.children.any? do |child|
40
+ [:plain, :script, :tag].include?(child.type) || (child.type == :filter && child.value.fetch(:name) != "ruby") || rendered_content?(child)
41
+ end
42
+ end
43
+
44
+ def each_diagnostic(node, match, lines)
45
+ line = node.line - 1
46
+ line_text = lines.fetch(line, "")
47
+ method_start = line_text.index(".#{match[:method]}") || 0
48
+ span = Text::Span.new(line, method_start, method_start + match[:method].length + 1)
49
+
50
+ Diagnostics.warning(span, "`= ...each do` returns the original collection, not the rendered Haml children; use `map` instead")
51
+ end
52
+
53
+ def silent_script_diagnostic(node, lines)
54
+ line = node.line - 1
55
+ line_text = lines.fetch(line, "")
56
+ start_character = line_text.index("-") || 0
57
+ span = Text::Span.new(line, start_character, start_character + 1)
58
+
59
+ Diagnostics.warning(span, "A silent `-` script discards its nested Haml content; use `=` when it should render")
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -6,6 +6,7 @@ require_relative "../text"
6
6
  require_relative "imports"
7
7
  require_relative "haml/classes"
8
8
  require_relative "haml/completion"
9
+ require_relative "haml/iteration"
9
10
  require_relative "haml/props"
10
11
  require_relative "haml/rename"
11
12
 
@@ -27,6 +28,8 @@ module Klenod
27
28
  diagnostics.concat(Classes.diagnostics(analysis, workspace, index)) if workspace && index
28
29
  diagnostics.concat(Props.diagnostics(analysis, workspace, index)) if workspace && index
29
30
  diagnostics.concat(unused_binding_diagnostics(analysis))
31
+ diagnostics.concat(Iteration.diagnostics(analysis)) unless analysis.build_error
32
+ diagnostics
30
33
  end
31
34
 
32
35
  def completion(analysis, position, workspace, index = nil)
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Klenod
4
4
  module LSP
5
- VERSION = "0.0.17"
5
+ VERSION = "0.0.19"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: klenod-lsp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.17
4
+ version: 0.0.19
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrés Alin
@@ -15,14 +15,14 @@ dependencies:
15
15
  requirements:
16
16
  - - '='
17
17
  - !ruby/object:Gem::Version
18
- version: 0.0.17
18
+ version: 0.0.19
19
19
  type: :runtime
20
20
  prerelease: false
21
21
  version_requirements: !ruby/object:Gem::Requirement
22
22
  requirements:
23
23
  - - '='
24
24
  - !ruby/object:Gem::Version
25
- version: 0.0.17
25
+ version: 0.0.19
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: language_server-protocol
28
28
  requirement: !ruby/object:Gem::Requirement
@@ -57,6 +57,7 @@ files:
57
57
  - lib/klenod/lsp/languages/haml.rb
58
58
  - lib/klenod/lsp/languages/haml/classes.rb
59
59
  - lib/klenod/lsp/languages/haml/completion.rb
60
+ - lib/klenod/lsp/languages/haml/iteration.rb
60
61
  - lib/klenod/lsp/languages/haml/props.rb
61
62
  - lib/klenod/lsp/languages/haml/rename.rb
62
63
  - lib/klenod/lsp/languages/haml/ruby_regions.rb