-
Notifications
You must be signed in to change notification settings - Fork 19
SuccessorML line comments #102
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
base: main
Are you sure you want to change the base?
Conversation
src/lex/Lexer.sml
Outdated
| loop_inComment (s + 1) {commentStart = s - 1, nesting = 1} | ||
| loop_inComment (s + 1) | ||
| {commentStart = s - 1, nesting = 1, lastCommentStart = s - 1} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here I wonder if it would be simpler to just immediately check for is #")" at (s+1) ? I think then you could avoid needing lastCommentStart.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would work, but only for top-level line comments, because loop_afterOpenParen is only called at top level.
This check alone would not detect line comments that happen to be enclosed in a block comment. Another check would need to be added in loop_inComment.
Since both checks are related to comments, I found a way to have them both in loop_inComment.
|
This is great! Thanks for working on it. I do think it would be preferable to avoid |
|
I did some quick testing and noticed something going wrong with indentation. An extra space is being prepended before Input: Output: Throughout smlfmt/src/base/PrettyTabbedDoc.sml Line 664 in 5c297d2
The bug might be due to how tokens are split into multiple line pieces. See this function: smlfmt/src/prettier-print/TabbedTokenDoc.sml Line 105 in 5c297d2
Perhaps the line comment is being split into @TwoF1nger could you look into this? |
This reverts commit 0cee20d.
I'll leave this for another day, looks difficult. Meanwhile, let me know if you have any further remarks on the lexing changes. |
|
I seem to have stumbled on a fix for the extra space. If Not sure if this is the real fix, or just covers up wrong behavior in the places you mentioned. I still don't quite understand the code. Also, another problem disappeared - an empty line after a line comment used to be filled with a bunch of spaces. |
|
Gotcha -- this fixes the issue with the extra space! Testing now and it looks like layout is not able to automatically re-insert the newline. (The layout engine is treating the line-comment as though it were an inline comment.) For example on this input: It's producing this output: |
|
I think I've figured out a fix. Could you try this patch on top of your most recent commit (d0cc5e0)? This seems to fix the above issue. diff --git a/src/lex/Token.sml b/src/lex/Token.sml
index 3d31cf3..6648996 100644
--- a/src/lex/Token.sml
+++ b/src/lex/Token.sml
@@ -117,6 +117,7 @@ sig
val isReserved: token -> bool
val isStringConstant: token -> bool
val isComment: token -> bool
+ val isLineComment: token -> bool
val isWhitespace: token -> bool
val isCommentOrWhitespace: token -> bool
val isComma: token -> bool
@@ -444,6 +445,17 @@ struct
Comment => true
| _ => false
+ fun isLineComment tok =
+ case getClass tok of
+ Comment =>
+ let
+ val src = getSource tok
+ in
+ Source.length src >= 3 andalso Source.nth src 0 = #"("
+ andalso Source.nth src 1 = #"*" andalso Source.nth src 2 = #")"
+ end
+ | _ => false
+
fun isWhitespace tok =
case getClass tok of
Whitespace => true
diff --git a/src/prettier-print/TabbedTokenDoc.sml b/src/prettier-print/TabbedTokenDoc.sml
index 4350aa8..f8d37dc 100644
--- a/src/prettier-print/TabbedTokenDoc.sml
+++ b/src/prettier-print/TabbedTokenDoc.sml
@@ -105,6 +105,12 @@ local
fun tokenToPieces {tabWidth: int} tok =
if not (Token.isComment tok orelse Token.isStringConstant tok) then
OnePiece (SyntaxHighlighter.highlightToken tok)
+ else if Token.isLineComment tok then
+ (* A bit of a hack. "ManyPieces" forces a layout strategy using
+ * a fresh rigid tab which is forcibly activated, effectively creating
+ * a single whole line in the output for this line comment.
+ *)
+ ManyPieces (Seq.singleton (SyntaxHighlighter.highlightToken tok))
else
let
val src = Token.getSource tok |
|
This seems to be sensitive to where the line comment is among the other code tokens. The patch fixes the issue you described, but in the following, slightly different, situation the next code line is still joined with the becomes Probably because the comment shares a line with the code that preceded it. Thanks a lot for helping! |
|
Ah, interesting -- I have a suspicion of what's going wrong here, but I will have to look closer later. (I am currently at a week-long conference and will be a bit busy!) |
The new parameter
lastCommentStartwas needed so line comments can be detected even when nested in block comments, without having to check characters we've already advanced over.If checking
is #"*" at s - 1 andalso is #"(" at s - 2is not a problem, thenlastCommentStartcan be avoided altogether. I just noticed that the rest of the code only compares to non-negative offsets tos, and went along.