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

Put attributes underneath each other if they exceeds the max line length. #979

Merged
merged 4 commits into from
Aug 10, 2020
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
123 changes: 123 additions & 0 deletions src/Fantomas.Tests/AttributeTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -451,3 +451,126 @@ type Commenter =

DisplayName: string }
"""

[<Test>]
let ``assembly attributes remain on own line, 629`` () =
formatSourceString false """
namespace AltCover.Visualizer

open System
open System.Reflection
open System.Runtime.InteropServices

[<assembly:CLSCompliant(true)>]
[<assembly:ComVisible(false)>]
[<assembly:AssemblyTitle("AltCover.Visualizer")>]
[<assembly:AssemblyDescription("Coverage and static analysis visualizer for NCover (possibly extended) and OpenCover")>]
[<assembly:System.Resources.NeutralResourcesLanguageAttribute("en-GB")>]
()
""" config
|> prepend newline
|> should equal """
namespace AltCover.Visualizer

open System
open System.Reflection
open System.Runtime.InteropServices

[<assembly:CLSCompliant(true)>]
[<assembly:ComVisible(false)>]
[<assembly:AssemblyTitle("AltCover.Visualizer")>]
[<assembly:AssemblyDescription("Coverage and static analysis visualizer for NCover (possibly extended) and OpenCover")>]
[<assembly:System.Resources.NeutralResourcesLanguageAttribute("en-GB")>]
()
"""

[<Test>]
let ``multiple attributes inside SynAttributes that exceeds max line length, 629`` () =
formatSourceString false """
//[<ApiExplorerSettings(IgnoreApi = true)>]
[<Route("api/v1/admin/import")>]
type RoleAdminImportController(akkaService: AkkaService) =
inherit Controller()

[<HttpGet("jobs/all");
ProducesResponseType(typeof<bool>, 200);
ProducesResponseType(404);
Authorize(AuthorizationScopePolicies.Read)>]
member _.ListJobs(): Task<UserCmdResponseMsg> =
task {
return!
akkaService.ImporterSystem.ApiMaster <? ApiMasterMsg.GetAllJobsCmd
}

[<HttpPost("jobs/create");
DisableRequestSizeLimit;
RequestFormLimits(MultipartBodyLengthLimit = 509715200L);
ProducesResponseType(typeof<RoleChangeSummaryDto list>, 200);
ProducesResponseType(404);
Authorize(AuthorizationScopePolicies.Write)>]
member _.StartJob(file: IFormFile, [<FromQuery>] args: ImporterJobArgs) =
let importer = akkaService.ImporterSystem

ActionResult.ofAsyncResult <| asyncResult {
let! state =
(LowerCaseString.create args.State, file)
|> pipeObjectThroughValidation [ (fst, [stateIsValid]); (snd, [(fun s -> Ok s)]) ]

let! filePath = FormFile.downloadAsTemp file

let job =
{ JobType = EsriBoundaryImport
FileToImport = filePath
State = state
DryRun = args.DryRun }

importer.ApiMaster <! StartImportCmd job
return Ok job
}
""" config
|> prepend newline
|> should equal """
//[<ApiExplorerSettings(IgnoreApi = true)>]
[<Route("api/v1/admin/import")>]
type RoleAdminImportController(akkaService: AkkaService) =
inherit Controller()

[<HttpGet("jobs/all");
ProducesResponseType(typeof<bool>, 200);
ProducesResponseType(404);
Authorize(AuthorizationScopePolicies.Read)>]
member _.ListJobs(): Task<UserCmdResponseMsg> =
task {
return! akkaService.ImporterSystem.ApiMaster
<? ApiMasterMsg.GetAllJobsCmd
}

[<HttpPost("jobs/create");
DisableRequestSizeLimit;
RequestFormLimits(MultipartBodyLengthLimit = 509715200L);
ProducesResponseType(typeof<RoleChangeSummaryDto list>, 200);
ProducesResponseType(404);
Authorize(AuthorizationScopePolicies.Write)>]
member _.StartJob(file: IFormFile, [<FromQuery>] args: ImporterJobArgs) =
let importer = akkaService.ImporterSystem

ActionResult.ofAsyncResult
<| asyncResult {
let! state =
(LowerCaseString.create args.State, file)
|> pipeObjectThroughValidation [ (fst, [ stateIsValid ])
(snd, [ (fun s -> Ok s) ]) ]

let! filePath = FormFile.downloadAsTemp file

let job =
{ JobType = EsriBoundaryImport
FileToImport = filePath
State = state
DryRun = args.DryRun }

importer.ApiMaster <! StartImportCmd job

return Ok job
}
"""
6 changes: 3 additions & 3 deletions src/Fantomas.Tests/FunctionDefinitionTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,9 @@ open System.Runtime.InteropServices
open Accessibility

[<DllImport("oleacc.dll")>]
extern int AccessibleChildren(IAccessible paccContainer, int iChildStart, int cChildren, [<Out; MarshalAs(UnmanagedType.LPArray,
nojaf marked this conversation as resolved.
Show resolved Hide resolved
SizeParamIndex =
4s)>] System.Object [] rgvarChildren, int* pcObtained)
extern int AccessibleChildren(IAccessible paccContainer, int iChildStart, int cChildren, [<Out;
MarshalAs(UnmanagedType.LPArray,
SizeParamIndex = 4s)>] System.Object [] rgvarChildren, int* pcObtained)
"""

[<Test>]
Expand Down
10 changes: 9 additions & 1 deletion src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ and genModuleDeclList astContext e =
let newlineAfterMultiline ctx =
let expr =
match List.tryHead rest with
| Some (SynModuleDecl.DoExpr(_, SynExpr.Const(SynConst.Unit,_),rm)) when (match m with SynModuleDecl.Attributes(a,_) -> List.length a > 1) ->
sepNlnConsideringTriviaContentBeforeWithAttributes rm attrs
| Some rm ->
let attrs = getRangesFromAttributesFromModuleDeclaration rm
sepNln +> sepNlnConsideringTriviaContentBeforeWithAttributes rm.Range attrs
Expand Down Expand Up @@ -414,7 +416,13 @@ and genAttributesCore astContext (ats: SynAttribute seq) =
if SourceTransformer.hasParenthesis e then id else sepSpace
opt sepColonFixed target (!-) -- s +> argSpacing +> genExpr astContext e
|> genTrivia attr.Range
ifElse (Seq.isEmpty ats) sepNone (!- "[<" +> atCurrentColumn (col sepSemi ats (genAttributeExpr astContext)) -- ">]")

let shortExpression = !- "[<" +> atCurrentColumn (col sepSemi ats (genAttributeExpr astContext)) -- ">]"
let longExpression = !- "[<" +> atCurrentColumn (col (sepSemi +> sepNln) ats (genAttributeExpr astContext)) -- ">]"

ifElse (Seq.isEmpty ats)
sepNone
(expressionFitsOnRestOfLine shortExpression longExpression)

and genOnelinerAttributes astContext ats =
let ats = List.collect (fun a -> a.Attributes) ats
Expand Down