1// Copyright 2019 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package cache
6
7import (
8	"sort"
9
10	"golang.org/x/tools/internal/lsp/debug"
11	"golang.org/x/tools/internal/span"
12)
13
14type debugView struct{ *view }
15
16func (v debugView) ID() string             { return v.id }
17func (v debugView) Session() debug.Session { return DebugSession{v.session} }
18func (v debugView) Env() []string          { return v.Options().Env }
19
20type DebugSession struct{ *Session }
21
22func (s DebugSession) ID() string         { return s.id }
23func (s DebugSession) Cache() debug.Cache { return debugCache{s.cache} }
24func (s DebugSession) Files() []*debug.File {
25	var files []*debug.File
26	seen := make(map[span.URI]*debug.File)
27	s.overlayMu.Lock()
28	defer s.overlayMu.Unlock()
29	for _, overlay := range s.overlays {
30		f, ok := seen[overlay.uri]
31		if !ok {
32			f = &debug.File{Session: s, URI: overlay.uri}
33			seen[overlay.uri] = f
34			files = append(files, f)
35		}
36		f.Data = string(overlay.text)
37		f.Error = nil
38		f.Hash = overlay.hash
39	}
40	sort.Slice(files, func(i int, j int) bool {
41		return files[i].URI < files[j].URI
42	})
43	return files
44}
45
46func (s DebugSession) File(hash string) *debug.File {
47	s.overlayMu.Lock()
48	defer s.overlayMu.Unlock()
49	for _, overlay := range s.overlays {
50		if overlay.hash == hash {
51			return &debug.File{
52				Session: s,
53				URI:     overlay.uri,
54				Data:    string(overlay.text),
55				Error:   nil,
56				Hash:    overlay.hash,
57			}
58		}
59	}
60	return &debug.File{
61		Session: s,
62		Hash:    hash,
63	}
64}
65