Skip to content

Commit fdbb8fe

Browse files
committed
refactor scriptinfo code
1 parent e313166 commit fdbb8fe

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

internal/project/scriptinfo.go

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -133,22 +133,30 @@ func (s *ScriptInfo) markContainingProjectsAsDirty() {
133133
// attachToProject attaches the script info to the project if it's not already attached
134134
// and returns true if the script info was newly attached.
135135
func (s *ScriptInfo) attachToProject(project *Project) bool {
136-
if !s.isAttached(project) {
137-
s.containingProjectsMu.Lock()
138-
s.containingProjects = append(s.containingProjects, project)
136+
if s.isAttached(project) {
137+
return false
138+
}
139+
s.containingProjectsMu.Lock()
140+
if s.isAttachedLocked(project) {
139141
s.containingProjectsMu.Unlock()
140-
if project.compilerOptions.PreserveSymlinks != core.TSTrue {
141-
s.ensureRealpath(project.FS())
142-
}
143-
project.onFileAddedOrRemoved()
144-
return true
142+
return false
143+
}
144+
s.containingProjects = append(s.containingProjects, project)
145+
s.containingProjectsMu.Unlock()
146+
if project.compilerOptions.PreserveSymlinks != core.TSTrue {
147+
s.ensureRealpath(project)
145148
}
146-
return false
149+
project.onFileAddedOrRemoved()
150+
return true
147151
}
148152

149153
func (s *ScriptInfo) isAttached(project *Project) bool {
150154
s.containingProjectsMu.RLock()
151155
defer s.containingProjectsMu.RUnlock()
156+
return s.isAttachedLocked(project)
157+
}
158+
159+
func (s *ScriptInfo) isAttachedLocked(project *Project) bool {
152160
return slices.Contains(s.containingProjects, project)
153161
}
154162

@@ -171,15 +179,9 @@ func (s *ScriptInfo) editContent(change ls.TextChange) {
171179
s.markContainingProjectsAsDirty()
172180
}
173181

174-
func (s *ScriptInfo) ensureRealpath(fs vfs.FS) {
182+
func (s *ScriptInfo) ensureRealpath(project *Project) {
175183
if s.realpath == "" {
176-
s.containingProjectsMu.RLock()
177-
defer s.containingProjectsMu.RUnlock()
178-
if len(s.containingProjects) == 0 {
179-
panic("scriptInfo must be attached to a project before calling ensureRealpath")
180-
}
181-
realpath := fs.Realpath(string(s.path))
182-
project := s.containingProjects[0]
184+
realpath := project.FS().Realpath(string(s.path))
183185
s.realpath = project.toPath(realpath)
184186
if s.realpath != s.path {
185187
project.host.OnDiscoveredSymlink(s)

0 commit comments

Comments
 (0)