From 6a741975226a0e1d10ce81caec99448ebcbf543d Mon Sep 17 00:00:00 2001 From: Artem Pyanykh Date: Fri, 22 Nov 2024 00:38:31 +0000 Subject: [PATCH] feat: Add a configuration option to treat level 1 headings as title Previously, "level 1 heading = title" was the default. Now there's a toggle for this. The config option is not wired yet. This will be done separately. stack-info: PR: https://github.com/artempyanykh/marksman/pull/366, branch: artempyanykh/stack/10 --- Marksman/Config.fs | 22 +++++++++++++++++++--- Tests/ConfigTests.fs | 16 ++++++++++++++++ Tests/default.marksman.toml | 5 +++++ 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/Marksman/Config.fs b/Marksman/Config.fs index 1482a6b..470079f 100644 --- a/Marksman/Config.fs +++ b/Marksman/Config.fs @@ -128,6 +128,7 @@ type Config = { caCreateMissingFileEnable: option coreMarkdownFileExtensions: option> coreTextSync: option + coreTitleFromHeading: option coreIncrementalReferences: option coreParanoid: option complWikiStyle: option @@ -139,6 +140,7 @@ type Config = { caCreateMissingFileEnable = Some true coreMarkdownFileExtensions = Some [| "md"; "markdown" |] coreTextSync = Some Full + coreTitleFromHeading = Some true coreIncrementalReferences = Some false coreParanoid = Some false complWikiStyle = Some TitleSlug @@ -150,6 +152,7 @@ type Config = { caCreateMissingFileEnable = None coreMarkdownFileExtensions = None coreTextSync = None + coreTitleFromHeading = None coreIncrementalReferences = None coreParanoid = None complWikiStyle = None @@ -176,6 +179,11 @@ type Config = { |> Option.orElse Config.Default.coreTextSync |> Option.get + member this.CoreTitleFromHeading() = + this.coreTitleFromHeading + |> Option.orElse Config.Default.coreTitleFromHeading + |> Option.get + member this.CoreIncrementalReferences() = this.coreIncrementalReferences |> Option.orElse Config.Default.coreIncrementalReferences @@ -187,9 +195,13 @@ type Config = { |> Option.get member this.ComplWikiStyle() = - this.complWikiStyle - |> Option.orElse Config.Default.complWikiStyle - |> Option.get + match this.complWikiStyle with + | Some x -> x + | None -> + if this.CoreTitleFromHeading() then + Option.get Config.Default.complWikiStyle + else + ComplWikiStyle.FileStem member this.ComplCandidates() = this.complCandidates @@ -209,6 +221,8 @@ let private configOfTable (table: TomlTable) : LookupResult = let! coreTextSync = getFromTableOpt table [] [ "core"; "text_sync" ] let coreTextSync = coreTextSync |> Option.bind TextSync.ofStringOpt + let! coreTitleFromHeading = getFromTableOpt table [] [ "core"; "title_from_heading" ] + let! coreIncrementalReferences = getFromTableOpt table [] [ "core"; "incremental_references" ] @@ -239,6 +253,7 @@ let private configOfTable (table: TomlTable) : LookupResult = caCreateMissingFileEnable = caCreateMissingFileEnable coreMarkdownFileExtensions = coreMarkdownFileExtensions coreTextSync = coreTextSync + coreTitleFromHeading = coreTitleFromHeading coreIncrementalReferences = coreIncrementalReferences coreParanoid = coreParanoid complWikiStyle = complWikiStyle @@ -258,6 +273,7 @@ module Config = hi.coreMarkdownFileExtensions |> Option.orElse low.coreMarkdownFileExtensions coreTextSync = hi.coreTextSync |> Option.orElse low.coreTextSync + coreTitleFromHeading = hi.coreTitleFromHeading |> Option.orElse low.coreTitleFromHeading coreIncrementalReferences = hi.coreIncrementalReferences |> Option.orElse low.coreIncrementalReferences diff --git a/Tests/ConfigTests.fs b/Tests/ConfigTests.fs index abfc941..4e68098 100644 --- a/Tests/ConfigTests.fs +++ b/Tests/ConfigTests.fs @@ -2,6 +2,7 @@ module Marksman.ConfigTests open System.IO open System.Reflection +open FSharpPlus.Data.Validation open Xunit open Marksman.Config @@ -189,3 +190,18 @@ let testDefault () = let content = using (new StreamReader(content)) (fun f -> f.ReadToEnd()) let parsed = Config.tryParse content Assert.Equal(Some Config.Default, parsed) + +[] +let testDefault_titleVsCompletionStyle () = + let content = + """ +[core] +title_from_heading = false +""" + + let actual = + Config.tryParse content + |> Option.defaultWith (fun () -> failwith "Expected a successful parse") + + Assert.False(actual.CoreTitleFromHeading()) + Assert.Equal(ComplWikiStyle.FileStem, actual.ComplWikiStyle()) diff --git a/Tests/default.marksman.toml b/Tests/default.marksman.toml index 100ad78..11606cc 100644 --- a/Tests/default.marksman.toml +++ b/Tests/default.marksman.toml @@ -17,6 +17,11 @@ markdown.file_extensions = ["md", "markdown"] # sync which result in slightly correpted state and are really hard # to diagnose. text_sync = "full" +# When set to true, level 1 headings will be treated as document titles +# (this includes an assumption of having a single title in the document). +# Setting this to false automatically changes the default wiki link +# completion style to a file-based one. +title_from_heading = true # Use incremental resolution of project-wide references. # This is much more efficient but is currently experimental incremental_references = false