Skip to content

Commit

Permalink
fix: Do not fail on empty glob patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
artempyanykh committed Oct 30, 2022
1 parent 7303f36 commit cf1743e
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 56 deletions.
23 changes: 14 additions & 9 deletions Marksman/GitIgnore.fs
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
module Marksman.GitIgnore

open System
open System.IO
open GlobExpressions
open Marksman.Misc

type GlobPattern =
| Include of Glob
| Exclude of Glob

let patternToGlob (pat: string) : array<Glob> =
let firstSlashIdx = pat.IndexOf('/')
let isAbsolute = firstSlashIdx <> pat.Length - 1
let isDir = pat[pat.Length - 1] = '/'
let pat = if pat.StartsWith("/") then pat.Substring(1) else pat
let pat = if isAbsolute then pat else "**/" + pat

if isDir then
[| Glob(pat + "**"); Glob(pat.Substring(0, pat.Length - 1)) |]
if String.IsNullOrWhiteSpace(pat) then
[||]
else
[| Glob(pat) |]
let firstSlashIdx = pat.IndexOf('/')
let isAbsolute = firstSlashIdx <> pat.Length - 1
let isDir = pat[pat.Length - 1] = '/'
let pat = if pat.StartsWith("/") then pat.Substring(1) else pat
let pat = if isAbsolute then pat else "**/" + pat

if isDir then
[| Glob(pat + "**"); Glob(pat.Substring(0, pat.Length - 1)) |]
else
[| Glob(pat) |]

let mkGlobPattern (pat: string) : array<GlobPattern> =
if pat.StartsWith("#") then
Expand Down
64 changes: 32 additions & 32 deletions Marksman/Marksman.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</PropertyGroup>
<Target Name="Version" BeforeTargets="BeforeBuild">
<Exec Command="git describe --always --dirty" ConsoleToMSBuild="true">
<Output TaskParameter="ConsoleOutput" PropertyName="VersionString" />
<Output TaskParameter="ConsoleOutput" PropertyName="VersionString"/>
</Exec>
<PropertyGroup>
<VersionSuffix>$(VersionString)</VersionSuffix>
Expand All @@ -20,43 +20,43 @@
<PropertyGroup>
<LintCommand>dotnet fsharplint -f msbuild lint --lint-config $(MSBuildProjectDirectory)/../fsharplint.json $(MSBuildProjectFullPath)</LintCommand>
</PropertyGroup>
<Exec Command="$(LintCommand)" ConsoleToMSBuild="true" IgnoreExitCode="true" />
<Exec Command="$(LintCommand)" ConsoleToMSBuild="true" IgnoreExitCode="true"/>
</Target>
<ItemGroup>
<Compile Include="Misc.fs" />
<Compile Include="GitIgnore.fs" />
<Compile Include="Config.fs" />
<Compile Include="Text.fs" />
<Compile Include="Cst.fs" />
<Compile Include="Parser.fs" />
<Compile Include="Index.fs" />
<Compile Include="Workspace.fsi" />
<Compile Include="Workspace.fs" />
<Compile Include="Semato.fs" />
<Compile Include="Refs.fs" />
<Compile Include="Diag.fs" />
<Compile Include="State.fs" />
<Compile Include="Toc.fs" />
<Compile Include="CodeActions.fs" />
<Compile Include="Compl.fs" />
<Compile Include="Refactor.fs" />
<Compile Include="Symbols.fs" />
<Compile Include="Server.fs" />
<Compile Include="Program.fs" />
<Compile Include="Misc.fs"/>
<Compile Include="GitIgnore.fs"/>
<Compile Include="Config.fs"/>
<Compile Include="Text.fs"/>
<Compile Include="Cst.fs"/>
<Compile Include="Parser.fs"/>
<Compile Include="Index.fs"/>
<Compile Include="Workspace.fsi"/>
<Compile Include="Workspace.fs"/>
<Compile Include="Semato.fs"/>
<Compile Include="Refs.fs"/>
<Compile Include="Diag.fs"/>
<Compile Include="State.fs"/>
<Compile Include="Toc.fs"/>
<Compile Include="CodeActions.fs"/>
<Compile Include="Compl.fs"/>
<Compile Include="Refactor.fs"/>
<Compile Include="Symbols.fs"/>
<Compile Include="Server.fs"/>
<Compile Include="Program.fs"/>
</ItemGroup>
<ItemGroup>
<PackageReference Update="FSharp.Core" Version="6.0.5" />
<PackageReference Include="FSharp.SystemCommandLine" Version="0.13.0-beta4" />
<PackageReference Include="FSharpPlus" Version="1.2.4" />
<PackageReference Include="Glob" Version="1.1.9" />
<PackageReference Include="Markdig" Version="0.30.2" />
<PackageReference Update="FSharp.Core" Version="6.0.5"/>
<PackageReference Include="FSharp.SystemCommandLine" Version="0.13.0-beta4"/>
<PackageReference Include="FSharpPlus" Version="1.2.4"/>
<PackageReference Include="Glob" Version="1.1.9"/>
<PackageReference Include="Markdig" Version="0.30.2"/>
<!-- <PackageReference Include="Ionide.LanguageServerProtocol" Version="0.3.1" />-->
<PackageReference Include="Serilog" Version="2.11.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.1" />
<PackageReference Include="Tomlyn" Version="0.16.0" />
<PackageReference Include="Serilog" Version="2.11.0"/>
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.1"/>
<PackageReference Include="Tomlyn" Version="0.16.0"/>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\LanguageServerProtocol\LanguageServerProtocol.fsproj" />
<ProjectReference Include="..\MarkdigPatches\MarkdigPatches.csproj" />
<ProjectReference Include="..\LanguageServerProtocol\LanguageServerProtocol.fsproj"/>
<ProjectReference Include="..\MarkdigPatches\MarkdigPatches.csproj"/>
</ItemGroup>
</Project>
33 changes: 18 additions & 15 deletions Tests/GitIgnoreTest.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ open Xunit

open Marksman.GitIgnore

[<Fact>]
let patternToGlob_Empty () = Assert.Equal<GlobExpressions.Glob>([||], patternToGlob "")

[<Fact>]
let absGlob_Unix () =
if not (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) then
Expand All @@ -15,35 +18,35 @@ let absGlob_Unix () =

let notIgnored = "/Users/john/notes/real.md"
GlobMatcher.ignores glob notIgnored |> Assert.False

[<Fact>]
let relGlob_Unix_1 () =
if not (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) then
let root = "/Users/john/notes"
let glob = GlobMatcher.mk root [| "node_modules/" |]
let ignored = "/Users/john/notes/node_modules"
GlobMatcher.ignores glob ignored |> Assert.True

let ignored = "/Users/john/notes/node_modules/"
GlobMatcher.ignores glob ignored |> Assert.True

let ignored = "/Users/john/notes/node_modules/foo.md"
GlobMatcher.ignores glob ignored |> Assert.True

let notIgnored = "/Users/john/notes/real.md"
GlobMatcher.ignores glob notIgnored |> Assert.False

[<Fact>]
let relGlob_Unix_2 () =
if not (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) then
let root = "/Users/john/notes"
let glob = GlobMatcher.mk root [| "node_modules/" |]
let ignored = "/Users/john/notes/sub/node_modules"
GlobMatcher.ignores glob ignored |> Assert.True

let ignored = "/Users/john/notes/sub/node_modules/"
GlobMatcher.ignores glob ignored |> Assert.True

let ignored = "/Users/john/notes/sub/node_modules/foo.md"
GlobMatcher.ignores glob ignored |> Assert.True

Expand Down Expand Up @@ -71,38 +74,38 @@ let absGlob_Win () =

let notIgnored = "C:\\notes\\real.md"
GlobMatcher.ignores glob notIgnored |> Assert.False


[<Fact>]
let relGlob_Win_1 () =
if RuntimeInformation.IsOSPlatform(OSPlatform.Windows) then
let root = "C:\\notes"
let glob = GlobMatcher.mk root [| "node_modules/" |]

let ignored = "C:\\notes\\node_modules"
GlobMatcher.ignores glob ignored |> Assert.True

let ignored = "C:\\notes\\node_modules\\"
GlobMatcher.ignores glob ignored |> Assert.True

let ignored = "C:\\notes\\node_modules\\foo.md"
GlobMatcher.ignores glob ignored |> Assert.True

let notIgnored = "C:\\notes\\real.md"
GlobMatcher.ignores glob notIgnored |> Assert.False

[<Fact>]
let relGlob_Win_2 () =
if RuntimeInformation.IsOSPlatform(OSPlatform.Windows) then
let root = "C:\\notes"
let glob = GlobMatcher.mk root [| "node_modules/" |]

let ignored = "C:\\notes\\sub\\node_modules"
GlobMatcher.ignores glob ignored |> Assert.True

let ignored = "C:\\notes\\sub\\node_modules\\"
GlobMatcher.ignores glob ignored |> Assert.True

let ignored = "C:\\notes\\sub\\node_modules\\foo.md"
GlobMatcher.ignores glob ignored |> Assert.True

Expand Down

0 comments on commit cf1743e

Please sign in to comment.