Skip to content

Commit

Permalink
Don't double semantic highlight some comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
gdotdesign committed Oct 22, 2024
1 parent a577ab4 commit de50c27
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 7 deletions.
49 changes: 49 additions & 0 deletions spec/compilers/directives/highlight-comments-multiple
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
component Main {
fun render : Html {
@highlight {
// Comment1
// Comment2
""
}[1]
}
}
--------------------------------------------------------------------------------
import {
createElement as B,
fragment as C
} from "./runtime.js";

export const A = () => {
return [
``,
B(C, {}, [
B("span", {
className: "line"
}, [
B("span", {
className: "comment"
}, [`// Comment1`]),
`
`
]),
B("span", {
className: "line"
}, [
``,
B("span", {
className: "comment"
}, [`// Comment2`]),
`
`
]),
B("span", {
className: "line"
}, [
``,
B("span", {
className: "string"
}, [`""`])
])
])
][1]
};
15 changes: 15 additions & 0 deletions spec/formatters/comment_multiple_3
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Test {
fun test : String {
// Comment E
// Comment F
""
}
}
--------------------------------------------------------------------------------
module Test {
fun test : String {
// Comment E
// Comment F
""
}
}
4 changes: 3 additions & 1 deletion src/formatter/processor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ module Mint
# TODO: Check if all of the conditions are still needed.
if last
space_separated =
last && Ast.space_separated?(last, item)
last &&
!last.is_a?(Ast::Comment) &&
Ast.space_separated?(last, item)

comments_and_one_is_block =
last.is_a?(Ast::Comment) &&
Expand Down
15 changes: 9 additions & 6 deletions src/parsers/comment.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Mint
class Parser
def comment : Ast::Comment?
parse do |start_position|
content, type, next_comment =
content, type, next_comment, to =
if word! "/*"
consumed =
gather { consume { !word?("*/") && !eof? } }.to_s
Expand All @@ -12,21 +12,24 @@ module Mint
snippet self
end unless word! "*/"

{consumed, Ast::Comment::Type::Block, nil}
{consumed, Ast::Comment::Type::Block, nil, position}
elsif word! "//"
consumed =
gather { consume { char != '\n' && !eof? } }.to_s

to_position =
position

comment =
parse do
spaces = gather { whitespace }.to_s
next unless word?("//") && spaces.count('\n') == 1
self.comment
end

{consumed, Ast::Comment::Type::Inline, comment}
{consumed, Ast::Comment::Type::Inline, comment, to_position}
else
{nil, Ast::Comment::Type::Block, nil}
{nil, Ast::Comment::Type::Block, nil, position}
end

next unless content
Expand All @@ -35,9 +38,9 @@ module Mint
next_comment: next_comment,
from: start_position,
content: content,
to: position,
type: type,
file: file)
file: file,
to: to)
end
end
end
Expand Down

0 comments on commit de50c27

Please sign in to comment.