This repository has been archived by the owner on Oct 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1257 from vasily-kirichenko/printf-specifiers-tagger
Printf specifiers tagger
- Loading branch information
Showing
25 changed files
with
942 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/FSharpVSPowerTools.Core/PrintfSpecifiersUsageGetter.fs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
module FSharpVSPowerTools.PrintfSpecifiersUsageGetter | ||
|
||
open Microsoft.FSharp.Compiler | ||
open FSharpVSPowerTools.UntypedAstUtils | ||
|
||
[<NoComparison>] | ||
type PrintfSpecifierUse = | ||
{ SpecifierRange: Range.range | ||
ArgumentRange: Range.range } | ||
|
||
let private startPos (r: Range.range) = r.StartLine, r.StartColumn | ||
|
||
let getAll (input: ParseAndCheckResults): PrintfSpecifierUse[] option Async = | ||
asyncMaybe { | ||
let! specifierRanges = input.GetFormatSpecifierLocations() | ||
let specifierRanges = | ||
specifierRanges | ||
|> Array.map (fun x -> | ||
Range.mkRange x.FileName x.Start (Range.mkPos x.EndLine (x.EndColumn + 1))) | ||
|
||
let printfFunctions = Printf.getAll input.ParseTree | ||
|
||
return | ||
printfFunctions | ||
|> Array.fold (fun (specifierRanges, acc) func -> | ||
let ownSpecifiers, restSpecifiers = | ||
specifierRanges | ||
|> Array.partition (Range.rangeContainsRange func.FormatString) | ||
|
||
match ownSpecifiers with | ||
| [||] -> restSpecifiers, acc | ||
| _ -> | ||
if func.Args.Length > ownSpecifiers.Length then | ||
failwithf "Too many Printf arguments for %+A (%d > %d)" func func.Args.Length ownSpecifiers.Length | ||
|
||
let uses = | ||
func.Args | ||
|> Array.sortBy startPos | ||
|> Array.zip (ownSpecifiers.[0..func.Args.Length - 1] |> Array.sortBy startPos) | ||
|> Array.map (fun (specifier, arg) -> { SpecifierRange = specifier; ArgumentRange = arg }) | ||
restSpecifiers, uses :: acc | ||
) (specifierRanges, []) | ||
|> snd | ||
|> List.toArray | ||
|> Array.concat | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.