Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Highlight single character errors. #721

Merged
merged 1 commit into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uncolorize removes the whitespace, which resulted in cases where nothing was highlighted.

(" " * left.size) + ("⌃" * center.size)

{highlighted, arrows}
end
Expand Down