Skip to content

Commit f58f25c

Browse files
committed
fix(language-server): when ignoring according to gitignore, properly handle comments and blank lines (fixes #108)
1 parent b928d2d commit f58f25c

File tree

7 files changed

+15
-6
lines changed

7 files changed

+15
-6
lines changed

src/language-server/Contextive.LanguageServer.Tests/Component/FileScannerTests.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ let tests =
5353
let files = scanner EXPECTED_GLOSSARY_FILE_GLOB
5454

5555
let expectedFiles =
56-
[ "test.glossary.yml"; ".nested/shouldfind.glossary.yml" ]
56+
[ "test.glossary.yml"; "nested/shouldfind.glossary.yml" ]
5757
|> reBaseLinePaths basePath
5858

5959
test <@ Set.ofSeq expectedFiles = Set.ofSeq files @>
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
.ignored
1+
# Blank lines shouldn't cause it to ignore everything
2+
3+
ignored # comments shouldn't prevent it from ignoring this entry

src/language-server/Contextive.LanguageServer.Tests/fixtures/scanning_tests_ignore/.nested/shouldfind.glossary.yml renamed to src/language-server/Contextive.LanguageServer.Tests/fixtures/scanning_tests_ignore/ignored/ignored.glossary.yml

File renamed without changes.

src/language-server/Contextive.LanguageServer.Tests/fixtures/scanning_tests_ignore/nested/shouldfind.glossary.yml

Whitespace-only changes.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
.nested
1+
nested

src/language-server/Contextive.LanguageServer.Tests/fixtures/scanning_tests_ignore/subfolder/nested/nested.glossary.yml

Whitespace-only changes.

src/language-server/Contextive.LanguageServer/FileScanner.fs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,23 @@ module Contextive.LanguageServer.FileScanner
33
open Microsoft.Extensions.FileSystemGlobbing
44
open Microsoft.Extensions.FileSystemGlobbing.Abstractions
55
open System.IO
6+
open System
7+
8+
let private stripComments (ignorePattern: string) =
9+
ignorePattern.Split "#" |> Seq.head |> _.Trim()
10+
11+
let private matchAnywhereInSubFolders (gitIgnoreBasePath: string) (ignorePattern: string) =
12+
$"{gitIgnoreBasePath}/**/{ignorePattern}"
613

714
let private loadGitIgnore (basePath: string) (gitIgnorePath: string) =
815
let gitIgnoreBasePath =
916
Path.GetRelativePath(basePath, Path.GetDirectoryName gitIgnorePath)
1017

1118
if File.Exists gitIgnorePath then
1219
File.ReadAllLines gitIgnorePath
13-
|> Seq.collect (fun ignorePattern ->
14-
[ $"{gitIgnoreBasePath}/**/{ignorePattern}"
15-
$"{gitIgnoreBasePath}/{ignorePattern}" ])
20+
|> Seq.map stripComments
21+
|> Seq.filter (not << String.IsNullOrEmpty)
22+
|> Seq.map (matchAnywhereInSubFolders gitIgnoreBasePath)
1623
else
1724
[||]
1825

0 commit comments

Comments
 (0)