-
Notifications
You must be signed in to change notification settings - Fork 182
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
WIP: experiment with line directives #2426
Conversation
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.
I think that's a good idea :) Though, the formatting is not reliable due to the current implementation of Fmt
.
I have no solution for the newline problem.
| Docstring _ -> None | ||
| Comment {txt; _} -> ( | ||
try | ||
let pattern = "<LINE_DIRECTIVE:([0-9]+)>(.*)" in |
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.
We don't need to encode this as a string, we could have a new constructor.
@@ -684,7 +695,11 @@ let fmt_cmts t conf ~fmt_code ?pro ?epi ?(eol = Fmt.fmt "@\n") ?(adj = eol) | |||
let adj_cmt = eol_cmt && Location.line_difference last_loc loc = 1 in | |||
fmt_or_k eol_cmt (fmt_or_k adj_cmt adj eol) (fmt_opt epi) | |||
in | |||
fmt_opt pro $ fmt_cmts_aux t conf cmts ~fmt_code pos $ epi | |||
( match List.hd cmts with | |||
| Some cmt when Option.is_some @@ Cmt.is_line_directive cmt -> fmt "\n" |
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.
Unfortunately, this doesn't work if the line directive follows a break. (as seen in the tests)
We would need a new formatting primitive to remove the indentation that was added by the previous break: fmt "@\n" $ remove_indentation
. Though, I'm not sure that's possible with Format's current implementation.
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.
I tried a few things like setting max indent to zero but didn't find any fix to this issue.
| ([' ' '\t']* (['0'-'9']+ as _num) [' ' '\t']* | ||
("\"" ([^ '\010' '\013' '\"' ] * as _name) "\"") as directive) | ||
| ([' ' '\t']* (['0'-'9']+ as line) [' ' '\t']* | ||
("\"" ([^ '\010' '\013' '\"' ] * as name) "\"")) |
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.
We might accept #if
, etc.. from cppo, though that wouldn't format cppo files in most cases.
We could also accept any line starting with #
by redesigning the parsing of toplevel directive.
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.
I don't think cppo directives can be handled in the same way. I was thinking that we can encode the conditions as regular if expressions with an attribute indicating they are cppo conditions, but didn't give it more thought than that.
562767b
to
430ce3c
Compare
Can you explain the expected workflow for formatting files containing line directive ? It's weird to try to preserve location with line directive and also shuffle location while formatting. |
It gives the users the choice to format or not their file, they can still list their files in |
The only workflow I know for line directive is inside automatically generated files in which case the source files are not meant to be edited directly. Changing the shape of a file with line directive means that locations updated by the line directive are now incorrect and useless. |
I had a vague idea on how to use that to format cppo-processed files, at least some of them:
This is just the beginning of an idea and that might be a bad one, there might be simpler ways to format cppo files, but that's what triggered my interest in formatting line directives (which, compared to cppo directives such as |
This seems to be a very convoluted workflow that gives many opportunities for failures. I use a custom ppx in js_of_ocaml that uses the same principals. I suspect efforts would be better spent extending (or replacing) ppx_optcomp. |
No description provided.