-
Notifications
You must be signed in to change notification settings - Fork 0
/
openedFiles.go
64 lines (52 loc) · 1.52 KB
/
openedFiles.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package langd
import (
"fmt"
"github.com/object88/rope"
)
// OpenedFiles is a collection of files opened across the caravan
type OpenedFiles struct {
ropes map[string]*rope.Rope
}
// NewOpenedFiles creates a new OpenedFiles instance
func NewOpenedFiles() *OpenedFiles {
return &OpenedFiles{
ropes: map[string]*rope.Rope{},
}
}
// EnsureOpened will create a new rope for a file that's not previously opened
func (of *OpenedFiles) EnsureOpened(absFilepath, text string) error {
if _, ok := of.ropes[absFilepath]; ok {
return fmt.Errorf("File %s is already opened", absFilepath)
}
of.ropes[absFilepath] = rope.CreateRope(text)
return nil
}
// Close will remove a rope from the collection
func (of *OpenedFiles) Close(absFilepath string) error {
_, ok := of.ropes[absFilepath]
if !ok {
return fmt.Errorf("openedFiles.Close:: File %s is not opened", absFilepath)
}
delete(of.ropes, absFilepath)
return nil
}
// Get returns the rope for a file
func (of *OpenedFiles) Get(absFilepath string) (*rope.Rope, error) {
buf, ok := of.ropes[absFilepath]
if !ok {
return nil, fmt.Errorf("openedFiles.Get:: File %s is not opened", absFilepath)
}
return buf, nil
}
// Replace will replace an existing rope with a completely new rope based on
// the provided text
func (of *OpenedFiles) Replace(absFilepath, text string) error {
_, ok := of.ropes[absFilepath]
if !ok {
return fmt.Errorf("File %s is not opened", absFilepath)
}
// Replace the entire document
buf := rope.CreateRope(text)
of.ropes[absFilepath] = buf
return nil
}