-
-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract shared logic between json config and editorconfig. (#663)
* Extracted json specific logic from ConfigFile.fs * Setting up editor format parsing. * Clean up some warnings.
- Loading branch information
Showing
11 changed files
with
177 additions
and
153 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
2 changes: 2 additions & 0 deletions
2
src/Fantomas.Tests/FormatConfigEditorConfigurationFileTests.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,2 @@ | ||
module Fantomas.Tests.FormatConfigEditorConfigurationFileTests | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,5 @@ | ||
namespace Fantomas | ||
|
||
open Fantomas | ||
open FormatConfig | ||
|
||
[<Sealed>] | ||
type CodeFormatter = | ||
static member ParseAsync(fileName, source, parsingOptions, checker) = | ||
|
@@ -35,23 +32,6 @@ type CodeFormatter = | |
static member MakeRange(fileName, startLine, startCol, endLine, endCol) = | ||
CodeFormatterImpl.makeRange fileName startLine startCol endLine endCol | ||
|
||
static member GetVersion() = Fantomas.Version.fantomasVersion.Value | ||
|
||
static member ReadConfiguration(fileOrFolder) = | ||
try | ||
let configurationFiles = | ||
ConfigFile.findConfigurationFiles fileOrFolder | ||
|
||
if List.isEmpty configurationFiles then failwithf "No configuration files were found for %s" fileOrFolder | ||
|
||
let (config,warnings) = | ||
List.fold (fun (currentConfig, warnings) configPath -> | ||
let updatedConfig, warningsForPath = ConfigFile.applyOptionsToConfig currentConfig configPath | ||
(updatedConfig, warnings @ warningsForPath) | ||
) (FormatConfig.Default, []) configurationFiles | ||
static member GetVersion() = Version.fantomasVersion.Value | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
nojaf
Author
Contributor
|
||
|
||
match warnings with | ||
| [] -> FormatConfigFileParseResult.Success config | ||
| w -> FormatConfigFileParseResult.PartialSuccess (config, w) | ||
with | ||
| exn -> FormatConfigFileParseResult.Failure exn | ||
static member ReadConfiguration(fileOrFolder) = CodeFormatterImpl.readConfiguration fileOrFolder |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
module internal Fantomas.ConfigFile | ||
|
||
open System.IO | ||
open Fantomas.FormatConfig | ||
open Fantomas.Version | ||
|
||
let jsonConfigFileName = "fantomas-config.json" | ||
let editorConfigFileName = ".editor-config" | ||
|
||
let private allowedFileNames = [jsonConfigFileName; editorConfigFileName] | ||
|
||
let rec private getParentFolders acc current = | ||
let parent = Directory.GetParent(current) |> Option.ofObj | ||
match parent with | ||
| Some p -> getParentFolders (current::acc) p.FullName | ||
| None -> current::acc | ||
|
||
/// Returns all the found configuration files for the given path | ||
/// fileOrFolder can be a concrete json file or a directory path | ||
let rec findConfigurationFiles fileOrFolder : string list = | ||
let findConfigInFolder folderPath = | ||
allowedFileNames | ||
|> List.map (fun fn -> Path.Combine(folderPath, fn)) | ||
|> List.filter (File.Exists) | ||
|
||
if Path.GetExtension(fileOrFolder) = "" && Directory.Exists fileOrFolder then | ||
getParentFolders [] fileOrFolder | ||
|> List.collect findConfigInFolder | ||
elif File.Exists(fileOrFolder) then | ||
let parentFolder = | ||
Directory.GetParent(Path.GetDirectoryName(fileOrFolder)) | ||
|> Option.ofObj | ||
match parentFolder with | ||
| Some pf -> findConfigurationFiles pf.FullName @ [fileOrFolder] | ||
| None -> [fileOrFolder] | ||
else | ||
[] | ||
|
||
let makeWarningLocationAware configPath warning = | ||
sprintf "%s, in %s" warning configPath | ||
|
||
let private fantomasFields = Reflection.getRecordFields FormatConfig.Default |> Array.map fst | ||
let private (|FantomasSetting|_|) (s:string) = | ||
let s = s.Trim('\"') | ||
Array.tryFind ((=) s) fantomasFields | ||
|
||
let private (|Number|_|) d = | ||
match System.Int32.TryParse(d) with | ||
| true, d -> Some (box d) | ||
| _ -> None | ||
let private (|Boolean|_|) b = | ||
if b = "true" then Some (box true) | ||
elif b = "false" then Some (box false) | ||
else None | ||
|
||
let processSetting originalLine key value = | ||
match (key, value) with | ||
| (FantomasSetting(fs), Number(v)) | ||
| (FantomasSetting(fs), Boolean(v)) -> Ok (fs, v) | ||
| _ -> | ||
let warning = sprintf "%s is no valid setting for Fantomas v%s" originalLine (fantomasVersion.Value) | ||
Error warning |
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,7 @@ | ||
module internal Fantomas.EditorConfig | ||
|
||
/// Similar to how to processing of the JSON file happens, this function should the options found in the editor config. | ||
/// The first argument in the return tuple are the options. Listed as (key,value) where key is a member name of the FormatConfig record. Value is either a boolean or an int boxed as object. | ||
/// The second argument in the return tuple are warnings. F.ex. invalid settings | ||
let parseOptionsFromEditorConfig editorConfig : (string * obj) array * string array = | ||
Array.empty, Array.empty |
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.
Thinking out loud: probably it could also be great to have some kind of an API to get available editor.config Fantomas options (and their default values?), so tooling could update its integrations based on Fantomas version used.