Skip to content
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
44 changes: 38 additions & 6 deletions Marksman/Config.fs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ open Tomlyn.Model
type LookupError =
| NotFound of path: list<string>
| WrongType of path: list<string> * value: obj * expectedType: System.Type
| WrongValue of path: list<string> * value: obj * err: string

type LookupResult<'R> = Result<'R, LookupError>

Expand Down Expand Up @@ -129,7 +130,8 @@ type Config =
coreTextSync: option<TextSync>
coreIncrementalReferences: option<bool>
coreParanoid: option<bool>
complWikiStyle: option<ComplWikiStyle> }
complWikiStyle: option<ComplWikiStyle>
complCandidates: option<int> }

static member Default =
{ caTocEnable = Some true
Expand All @@ -138,7 +140,8 @@ type Config =
coreTextSync = Some Full
coreIncrementalReferences = Some false
coreParanoid = Some false
complWikiStyle = Some TitleSlug }
complWikiStyle = Some TitleSlug
complCandidates = Some 50 }

static member Empty =
{ caTocEnable = None
Expand All @@ -147,7 +150,8 @@ type Config =
coreTextSync = None
coreIncrementalReferences = None
coreParanoid = None
complWikiStyle = None }
complWikiStyle = None
complCandidates = None }

member this.CaTocEnable() =
this.caTocEnable
Expand Down Expand Up @@ -184,6 +188,11 @@ type Config =
|> Option.orElse Config.Default.complWikiStyle
|> Option.get

member this.ComplCandidates() =
this.complCandidates
|> Option.orElse Config.Default.complCandidates
|> Option.get

let private configOfTable (table: TomlTable) : LookupResult<Config> =
monad {
let! caTocEnable = getFromTableOpt<bool> table [] [ "code_action"; "toc"; "enable" ]
Expand All @@ -207,13 +216,29 @@ let private configOfTable (table: TomlTable) : LookupResult<Config> =
let complWikiStyle =
complWikiStyle |> Option.bind ComplWikiStyle.ofStringOpt

// TOML parser represents numbers as int64, hence extract as int64 and
// convert to int
let complCandidatesPath = [ "completion"; "candidates" ]
let! complCandidates = getFromTableOpt<int64> table [] complCandidatesPath

let! complCandidates =
match complCandidates with
| None -> Ok None
| Some v ->
if v > 0 then
Ok(Some(int v))
else
Error(WrongValue(complCandidatesPath, v, "expected a non-negative number"))


{ caTocEnable = caTocEnable
caCreateMissingFileEnable = caCreateMissingFileEnable
coreMarkdownFileExtensions = coreMarkdownFileExtensions
coreTextSync = coreTextSync
coreIncrementalReferences = coreIncrementalReferences
coreParanoid = coreParanoid
complWikiStyle = complWikiStyle }
complWikiStyle = complWikiStyle
complCandidates = complCandidates }
}

module Config =
Expand All @@ -232,7 +257,8 @@ module Config =
hi.coreIncrementalReferences
|> Option.orElse low.coreIncrementalReferences
coreParanoid = hi.coreParanoid |> Option.orElse low.coreParanoid
complWikiStyle = hi.complWikiStyle |> Option.orElse low.complWikiStyle }
complWikiStyle = hi.complWikiStyle |> Option.orElse low.complWikiStyle
complCandidates = hi.complCandidates |> Option.orElse low.complCandidates }

let mergeOpt hi low =
match low with
Expand All @@ -251,7 +277,13 @@ module Config =

match configOfTable table with
| Ok parsed -> Some parsed
| _ -> None
| err ->
logger.error (
Log.setMessage "Failed to parse configuration"
>> Log.addContext "error" err
)

None
else
logger.trace (Log.setMessage "Parsing as TOML failed")
None
Expand Down
2 changes: 1 addition & 1 deletion Marksman/Server.fs
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,7 @@ type MarksmanServer(client: MarksmanClient) =
monad' {
let! folder, doc = State.tryFindFolderAndDoc docUri state

let maxCompletions = 50
let maxCompletions = (Folder.configOrDefault folder).ComplCandidates()

match
Compl.findCandidatesInDoc folder doc pos
Expand Down
36 changes: 36 additions & 0 deletions Tests/ConfigTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,20 @@ paranoid = true

Assert.Equal(Some expected, actual)

[<Fact>]
let testParse_7 () =
let content =
"""
[completion]
candidates = 100
"""

let actual = Config.tryParse content

let expected = { Config.Empty with complCandidates = Some 100 }

Assert.Equal(Some expected, actual)

[<Fact>]
let testParse_broken_0 () =
let content =
Expand Down Expand Up @@ -143,6 +157,28 @@ markdown.file_extensions = [["md"], ["markdown"]]
let actual = Config.tryParse content
Assert.Equal(None, actual)

[<Fact>]
let testParse_broken_4 () =
let content =
"""
[completion]
candidates = "fifty"
"""

let actual = Config.tryParse content
Assert.Equal(None, actual)

[<Fact>]
let testParse_broken_5 () =
let content =
"""
[completion]
candidates = -1
"""

let actual = Config.tryParse content
Assert.Equal(None, actual)

[<Fact>]
let testDefault () =
let content =
Expand Down
2 changes: 2 additions & 0 deletions Tests/default.marksman.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ toc.enable = true
create_missing_file.enable = true

[completion]
# The maximum number of candidates returned for a completion
candidates = 50
# The style of wiki links completion.
# Other values include:
# * "file-stem" to complete using file name without an extension,
Expand Down