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

Active patterns with defined with otherwise invalid name is incorrectly re-formatted #3126

Open
3 of 4 tasks
josh-degraw opened this issue Sep 19, 2024 · 2 comments
Open
3 of 4 tasks

Comments

@josh-degraw
Copy link
Contributor

Issue created from fantomas-online

Code

let (|``Custom Prefix``|_|) (p: string) (s: string) =
  if s.StartsWith(p) then
    Some(s.Substring(p.Length))
  else
    None

Result

let (|Custom Prefix|_|) (p: string) (s: string) =
    if s.StartsWith(p) then
        Some(s.Substring(p.Length))
    else
        None

Problem description

Please describe here the Fantomas problem you encountered.
Check out our Contribution Guidelines.

Extra information

  • The formatted result breaks my code.
  • The formatted result gives compiler warnings.
  • I or my company would be willing to help fix this.
  • I would like a release if this problem is solved.

Options

Fantomas main branch at 1/1/1990

Default Fantomas configuration

Did you know that you can ignore files when formatting by using a .fantomasignore file?
PS: It's unlikely that someone else will solve your specific issue, as it's something that you have a personal stake in.

@nojaf
Copy link
Contributor

nojaf commented Sep 19, 2024

You should snatch the original ident text from source code in

| Some(IdentTrivia.HasParenthesis _) -> stn $"(%s{ident.idText})" ident.idRange

using creationAide.TextFromSource fallback range.

@nojaf
Copy link
Contributor

nojaf commented Sep 19, 2024

Actually, we should reuse the logic of

let mkIdent (ident: Ident) =
let width = ident.idRange.EndColumn - ident.idRange.StartColumn
let text =
if ident.idText.Length + 4 = width then
// add backticks
$"``%s{ident.idText}``"
else
ident.idText
stn text ident.idRange
let mkSynIdent (SynIdent(ident, trivia)) =
match trivia with
| None -> mkIdent ident
| Some(IdentTrivia.OriginalNotation text) -> stn text ident.idRange
| Some(IdentTrivia.OriginalNotationWithParen(_, text, _)) -> stn $"(%s{text})" ident.idRange
| Some(IdentTrivia.HasParenthesis _) -> stn $"(%s{ident.idText})" ident.idRange
instead (ident.idText.Length + 4 = width) perhaps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants