@@ -16,59 +16,47 @@ package jsconfig
1616import (
1717 "path/filepath"
1818 "sort"
19- "sync"
19+
20+ "github.com/bep/helpers/maphelpers"
2021)
2122
2223// Builder builds a jsconfig.json file that, currently, is used only to assist
2324// IntelliSense in editors.
2425type Builder struct {
25- sourceRootsMu sync.RWMutex
26- sourceRoots map [string ]bool
26+ sourceRoots * maphelpers.ConcurrentSet [string ]
2727}
2828
2929// NewBuilder creates a new Builder.
3030func NewBuilder () * Builder {
31- return & Builder {sourceRoots : make ( map [string ]bool )}
31+ return & Builder {sourceRoots : maphelpers . NewConcurrentSet [string ]( )}
3232}
3333
3434// Build builds a new Config with paths relative to dir.
3535// This method is thread safe.
3636func (b * Builder ) Build (dir string ) * Config {
37- b .sourceRootsMu .RLock ()
38- defer b .sourceRootsMu .RUnlock ()
39-
40- if len (b .sourceRoots ) == 0 {
37+ if b .sourceRoots .Len () == 0 {
4138 return nil
4239 }
43- conf := newJSConfig ()
44-
4540 var roots []string
46- for root := range b .sourceRoots {
41+ for root := range b .sourceRoots . All () {
4742 rel , err := filepath .Rel (dir , filepath .Join (root , "*" ))
4843 if err == nil {
4944 roots = append (roots , rel )
5045 }
5146 }
47+ if len (roots ) == 0 {
48+ return nil
49+ }
5250 sort .Strings (roots )
51+ conf := newJSConfig ()
5352 conf .CompilerOptions .Paths ["*" ] = roots
54-
5553 return conf
5654}
5755
5856// AddSourceRoot adds a new source root.
5957// This method is thread safe.
6058func (b * Builder ) AddSourceRoot (root string ) {
61- b .sourceRootsMu .RLock ()
62- found := b .sourceRoots [root ]
63- b .sourceRootsMu .RUnlock ()
64-
65- if found {
66- return
67- }
68-
69- b .sourceRootsMu .Lock ()
70- b .sourceRoots [root ] = true
71- b .sourceRootsMu .Unlock ()
59+ b .sourceRoots .AddIfAbsent (root )
7260}
7361
7462// CompilerOptions holds compilerOptions for jsonconfig.json.
0 commit comments