Skip to content

Commit

Permalink
Highlight single character errors. (#721)
Browse files Browse the repository at this point in the history
  • Loading branch information
gdotdesign authored Nov 28, 2024
1 parent 16577e2 commit dd23533
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 19 deletions.
14 changes: 14 additions & 0 deletions spec/errors/access_expected_field_2
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
component Main {
fun render : String {
"". word
--------------------------------------------------------------------------------
░ ERROR (ACCESS_EXPECTED_FIELD) ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░

I was expecting the name of the accessed entity but I found "a space" instead:

┌ errors/access_expected_field_2:3:8
├───────────────────────────────────
1│ component Main {
2│ fun render : String {
3│ "". word
│ ⌃
5 changes: 4 additions & 1 deletion src/errorable.cr
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,11 @@ module Mint
target =
case value
in Parser
min =
value.char == '\0' ? 0 : 1

SnippetData.new(
to: value.position.offset + value.word.to_s.size,
to: value.position.offset + [min, value.word.to_s.size].max,
filename: value.file.relative_path,
from: value.position.offset,
input: value.file.contents)
Expand Down
34 changes: 19 additions & 15 deletions src/parsers/connect_variable.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,27 @@ module Mint
next unless name = variable_constant(track: false) ||
variable(track: false)

whitespace
if keyword! "as"
whitespace
target =
parse do
whitespace
next unless keyword! "as"
whitespace

next error :connect_variable_expected_as do
block do
text "The"
bold "exposed name"
text "of a connection"
bold "must be specified, here is an example:"
end
next error :connect_variable_expected_as do
block do
text "The"
bold "exposed name"
text "of a connection"
bold "must be specified, here is an example:"
end

snippet "connect Store exposing { item as name }"
expected "the exposed name", word
snippet self
end unless target = variable
end
snippet "connect Store exposing { item as name }"
expected "the exposed name", word
snippet self
end unless variable = self.variable

variable
end

Ast::ConnectVariable.new(
from: start_position,
Expand Down
6 changes: 3 additions & 3 deletions src/utils/terminal_snippet.cr
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@ module Mint
self[0, diff_from]

center =
self[diff_from, diff_to].colorize.on(:white).fore(:red).to_s
self[diff_from, diff_to]

right =
self[diff_to, contents.size]

highlighted =
left + center + right
left + center.colorize.on(:white).fore(:red).to_s + right

arrows =
(" " * left.size) + ("" * center.uncolorize.size)
(" " * left.size) + ("" * center.size)

{highlighted, arrows}
end
Expand Down

0 comments on commit dd23533

Please sign in to comment.