Skip to content

Commit 68957b0

Browse files
committed
refactor: Introduce ParserSetting type
Right now it includes only the extensions. Soon it will be extended with title/header config settings.
1 parent e596265 commit 68957b0

15 files changed

Lines changed: 61 additions & 50 deletions

File tree

Benchmarks/Program.fs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ open BenchmarkDotNet.Running
66
open Ionide.LanguageServerProtocol.Types
77

88
open Marksman.Misc
9+
open Marksman.Config
910
open Marksman.Names
1011
open Marksman.Paths
1112
open Marksman.Cst
@@ -32,7 +33,7 @@ type ReferenceResolution() =
3233
let contentLines = $"# Doc {i}" :: links
3334
let content = String.concat "\n" contentLines
3435
let text = Text.mkText content
35-
Doc.mk Config.defaultMarkdownExtensions docId None text)
36+
Doc.mk ParserSettings.Default docId None text)
3637

3738
Folder.multiFile "docs" folderId docs None
3839

Marksman/Ast.fs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ module Element =
109109
| _ -> None
110110

111111
// TODO: instead of checking for 'all whitespace' symbols all the time, create smart constructors
112-
let toSym (exts: seq<string>) (el: Element) : option<Sym> =
112+
let toSym (parserSettings: Config.ParserSettings) (el: Element) : option<Sym> =
113113
match el with
114114
| Element.H { level = level; id = id } ->
115115
if Slug.isEmpty id then
@@ -144,7 +144,9 @@ module Element =
144144
// Markdown links mapping
145145
| Element.ML { url = url; anchor = anchor } ->
146146
let urlIsRef =
147-
url |> Option.map (fun url -> url, isPotentiallyInternalRef exts url)
147+
url
148+
|> Option.map (fun url ->
149+
url, isPotentiallyInternalRef parserSettings.mdFileExt url)
148150

149151
match urlIsRef, anchor with
150152
| None, None -> None

Marksman/Config.fs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,3 +331,10 @@ module Config =
331331

332332
let defaultMarkdownExtensions =
333333
Config.Default.CoreMarkdownFileExtensions() |> Seq.ofArray
334+
335+
type ParserSettings = {
336+
mdFileExt: string[]
337+
} with
338+
339+
static member OfConfig(config: Config) = { mdFileExt = config.CoreMarkdownFileExtensions() }
340+
static member Default = ParserSettings.OfConfig(Config.Default)

Marksman/Doc.fs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ module Doc =
6262

6363
let logger = LogProvider.getLoggerByName "Doc"
6464

65-
let mk exts id version text =
66-
let structure = Parser.parse exts text
65+
let mk parserSettings id version text =
66+
let structure = Parser.parse parserSettings text
6767
let index = Index.ofCst (Structure.concreteElements structure)
6868

6969
{
@@ -77,8 +77,8 @@ module Doc =
7777
let id { id = id } = id
7878
let text doc = doc.text
7979

80-
let withText exts newText doc =
81-
let newStructure = Parser.parse exts newText
80+
let withText config newText doc =
81+
let newStructure = Parser.parse config newText
8282
let newIndex = Index.ofCst (Structure.concreteElements newStructure)
8383

8484
{
@@ -89,7 +89,7 @@ module Doc =
8989
}
9090

9191

92-
let applyLspChange exts (change: DidChangeTextDocumentParams) (doc: Doc) : Doc =
92+
let applyLspChange parserSettings (change: DidChangeTextDocumentParams) (doc: Doc) : Doc =
9393
let newVersion = change.TextDocument.Version
9494

9595
logger.trace (
@@ -101,16 +101,16 @@ module Doc =
101101

102102
let newText = applyTextChange change.ContentChanges doc.text
103103

104-
{ withText exts newText doc with version = Some newVersion }
104+
{ withText parserSettings newText doc with version = Some newVersion }
105105

106-
let fromLsp exts (folderId: FolderId) (item: TextDocumentItem) : Doc =
106+
let fromLsp parserSettings (folderId: FolderId) (item: TextDocumentItem) : Doc =
107107
let path = LocalPath.ofUri item.Uri
108108
let id = DocId(UriWith.mkRooted folderId path)
109109
let text = mkText item.Text
110110

111-
mk exts id (Some item.Version) text
111+
mk parserSettings id (Some item.Version) text
112112

113-
let tryLoad exts (folderId: FolderId) (path: LocalPath) : option<Doc> =
113+
let tryLoad parserSettings (folderId: FolderId) (path: LocalPath) : option<Doc> =
114114
try
115115
let content =
116116
using (new StreamReader(LocalPath.toSystem path)) (fun f -> f.ReadToEnd())
@@ -119,7 +119,7 @@ module Doc =
119119

120120
let id = DocId(UriWith.mkRooted folderId path)
121121

122-
Some(mk exts id None text)
122+
Some(mk parserSettings id None text)
123123
with :? FileNotFoundException ->
124124
None
125125

Marksman/Doc.fsi

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module Marksman.Doc
33
open Ionide.LanguageServerProtocol.Types
44

55
open Marksman.Misc
6+
open Marksman.Config
67
open Marksman.Names
78
open Marksman.Structure
89
open Marksman.Paths
@@ -41,7 +42,7 @@ module Doc =
4142
val slug: Doc -> Slug
4243
val index: Doc -> Index
4344

44-
val tryLoad: exts: seq<string> -> FolderId -> path: LocalPath -> option<Doc>
45-
val mk: exts: seq<string> -> DocId -> version: option<int> -> Text -> Doc
46-
val fromLsp: exts: seq<string> -> FolderId -> TextDocumentItem -> Doc
47-
val applyLspChange: exts: seq<string> -> DidChangeTextDocumentParams -> Doc -> Doc
45+
val tryLoad: ParserSettings -> FolderId -> path: LocalPath -> option<Doc>
46+
val mk: ParserSettings -> DocId -> version: option<int> -> Text -> Doc
47+
val fromLsp: ParserSettings -> FolderId -> TextDocumentItem -> Doc
48+
val applyLspChange: ParserSettings -> DidChangeTextDocumentParams -> Doc -> Doc

Marksman/Folder.fs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ module Folder =
301301

302302
lines.ToArray()
303303

304-
let private loadDocs (configuredExts: array<string>) (folderId: FolderId) : seq<Doc> =
304+
let private loadDocs (parserSettings: ParserSettings) (folderId: FolderId) : seq<Doc> =
305305
let rec collect (cur: LocalPath) (ignoreMatchers: list<GlobMatcher>) =
306306
let ignoreMatchers =
307307
match readIgnoreFiles cur with
@@ -317,12 +317,12 @@ module Folder =
317317
seq {
318318
for file in files do
319319
if
320-
(isMarkdownFile configuredExts file.FullName)
320+
(isMarkdownFile parserSettings.mdFileExt file.FullName)
321321
&& not (GlobMatcher.ignoresAny ignoreMatchers file.FullName)
322322
then
323323
let pathUri = LocalPath.ofSystem file.FullName
324324

325-
let document = Doc.tryLoad configuredExts folderId pathUri
325+
let document = Doc.tryLoad parserSettings folderId pathUri
326326

327327
match document with
328328
| Some document -> yield document
@@ -505,8 +505,9 @@ module Folder =
505505
(Option.defaultValue Config.Default folderConfig)
506506
.CoreMarkdownFileExtensions()
507507

508-
let documents = loadDocs configuredExts folderId
508+
let parserSettings = { mdFileExt = configuredExts }
509509

510+
let documents = loadDocs parserSettings folderId
510511

511512
multiFile name folderId documents folderConfig |> Some
512513
else
@@ -612,12 +613,14 @@ module Folder =
612613
else
613614
None
614615

616+
let parserSettings folder = ParserSettings.OfConfig(configOrDefault folder)
617+
615618
let closeDoc (docId: DocId) (folder: Folder) : option<Folder> =
616-
let exts = (configOrDefault folder).CoreMarkdownFileExtensions()
619+
let parserSettings = parserSettings folder
617620

618621
match folder.data with
619622
| MultiFile { root = root } ->
620-
match Doc.tryLoad exts root (Abs <| RootedRelPath.toAbs docId.Path) with
623+
match Doc.tryLoad parserSettings root (Abs <| RootedRelPath.toAbs docId.Path) with
621624
| Some doc -> withDoc doc folder |> Some
622625
| _ -> withoutDoc docId folder
623626
| SingleFile { doc = doc } ->

Marksman/Folder.fsi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ module Folder =
1919
val withConfig: option<Config> -> Folder -> Folder
2020

2121
val configuredMarkdownExts: Folder -> seq<string>
22+
val parserSettings: Folder -> ParserSettings
2223

2324
val docs: Folder -> seq<Doc>
2425
val docCount: Folder -> int

Marksman/Parser.fs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -476,11 +476,11 @@ module Markdown =
476476

477477
{ elements = elements; childMap = childMap }
478478

479-
let parse (exts: seq<string>) (text: Text) : Structure =
479+
let parse (parserSettings: Config.ParserSettings) (text: Text) : Structure =
480480
if String.IsNullOrEmpty text.content then
481481
let cst: Cst.Cst = { elements = [||]; childMap = Map.empty }
482-
Structure.ofCst exts cst
482+
Structure.ofCst parserSettings cst
483483
else
484484
let flatElements = Markdown.scrapeText text
485485
let cst = Markdown.buildCst text flatElements
486-
Structure.ofCst exts cst
486+
Structure.ofCst parserSettings cst

Marksman/Server.fs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -637,8 +637,7 @@ type MarksmanServer(client: MarksmanClient) =
637637
let newState =
638638
match State.tryFindFolderAndDoc docPath state with
639639
| Some(folder, doc) ->
640-
let newDoc =
641-
Doc.applyLspChange (Folder.configuredMarkdownExts folder) par doc
640+
let newDoc = Doc.applyLspChange (Folder.parserSettings folder) par doc
642641

643642
let newFolder = Folder.withDoc newDoc folder
644643

@@ -677,10 +676,10 @@ type MarksmanServer(client: MarksmanClient) =
677676
let newState =
678677
match State.tryFindFolderEnclosing path state with
679678
| None ->
680-
let configuredExts =
681-
(State.userConfigOrDefault state).CoreMarkdownFileExtensions()
679+
let config = State.userConfigOrDefault state
680+
let parserSettings = ParserSettings.OfConfig(config)
682681

683-
if isMarkdownFile configuredExts (AbsPath.toSystem path.data) then
682+
if isMarkdownFile parserSettings.mdFileExt (AbsPath.toSystem path.data) then
684683
let singletonRoot = UriWith.mkRoot par.TextDocument.Uri
685684

686685
logger.trace (
@@ -689,17 +688,17 @@ type MarksmanServer(client: MarksmanClient) =
689688
>> Log.addContext "root" singletonRoot
690689
)
691690

692-
let doc = Doc.fromLsp configuredExts singletonRoot par.TextDocument
691+
let doc = Doc.fromLsp parserSettings singletonRoot par.TextDocument
693692
let userConfig = (State.workspace state) |> Workspace.userConfig
694693
let newFolder = Folder.singleFile doc userConfig
695694
State.updateFolder newFolder state
696695
else
697696
state
698697
| Some folder ->
699-
let configuredExts = (Folder.configuredMarkdownExts folder)
698+
let parserSettings = Folder.parserSettings folder
700699

701-
if isMarkdownFile configuredExts (AbsPath.toSystem path.data) then
702-
let doc = Doc.fromLsp configuredExts (Folder.id folder) par.TextDocument
700+
if isMarkdownFile parserSettings.mdFileExt (AbsPath.toSystem path.data) then
701+
let doc = Doc.fromLsp parserSettings (Folder.id folder) par.TextDocument
703702
let newFolder = Folder.withDoc doc folder
704703
State.updateFolder newFolder state
705704
else
@@ -732,11 +731,10 @@ type MarksmanServer(client: MarksmanClient) =
732731
match State.tryFindFolderEnclosing docUri newState with
733732
| None -> ()
734733
| Some folder ->
735-
let configuredExts =
736-
(Folder.configOrDefault folder).CoreMarkdownFileExtensions()
734+
let parserSettings = Folder.parserSettings folder
737735

738-
if isMarkdownFile configuredExts (AbsPath.toSystem docUri.data) then
739-
match Doc.tryLoad configuredExts (Folder.id folder) (Abs docUri.data) with
736+
if isMarkdownFile parserSettings.mdFileExt (AbsPath.toSystem docUri.data) then
737+
match Doc.tryLoad parserSettings (Folder.id folder) (Abs docUri.data) with
740738
| Some doc ->
741739
let newFolder = Folder.withDoc doc folder
742740
newState <- State.updateFolder newFolder newState

Marksman/Structure.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@ module Structure =
6161
findAbstractForSymbol sym structure
6262
|> Set.fold (fun acc ael -> acc + findConcreteForAbstract ael structure) Set.empty
6363

64-
let ofCst (exts: seq<string>) (cst: Cst.Cst) : Structure =
64+
let ofCst (parserSettings: Config.ParserSettings) (cst: Cst.Cst) : Structure =
6565
let rec go cst =
6666
seq {
6767
for cel in cst do
6868
let ael = Cst.Element.toAbstract cel
69-
let sym = ael |> Option.bind (Ast.Element.toSym exts)
69+
let sym = ael |> Option.bind (Ast.Element.toSym parserSettings)
7070

7171
match ael with
7272
| Some ael -> yield cel, ael, sym

0 commit comments

Comments
 (0)