forked from limetext/lime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
watched.go
80 lines (64 loc) · 1.66 KB
/
watched.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright 2014 The lime Authors.
// Use of this source code is governed by a 2-clause
// BSD-style license that can be found in the LICENSE file.
package backend
import (
"code.google.com/p/log4go"
"fmt"
"github.com/limetext/lime/backend/packages"
. "github.com/quarnster/util/text"
"io/ioutil"
)
type (
Watched interface {
Name() string
Reload()
}
WatchedUserFile struct {
view *View
}
WatchedPackage struct {
pkg packages.Package
}
)
func NewWatchedUserFile(view *View) *WatchedUserFile {
return &WatchedUserFile{view}
}
func (o WatchedUserFile) String() string {
return fmt.Sprintf("%s (%d)", o.Name(), o.view.Id())
}
func (o *WatchedUserFile) Name() string {
return o.view.Buffer().FileName()
}
func (o *WatchedUserFile) Reload() {
log4go.Finest("\"%v\".Reload()", o)
view := o.view
filename := o.Name()
if saving, ok := view.Settings().Get("lime.saving", false).(bool); ok && saving {
// This reload was triggered by ourselves saving to this file, so don't reload it
return
}
if !GetEditor().Frontend().OkCancelDialog("File was changed by another program, reload?", "reload") {
return
}
if d, err := ioutil.ReadFile(filename); err != nil {
log4go.Error("Could not read file: %s\n. Error was: %v", filename, err)
} else {
edit := view.BeginEdit()
end := view.Buffer().Size()
view.Replace(edit, Region{0, end}, string(d))
view.EndEdit(edit)
}
}
func NewWatchedPackage(pkg packages.Package) *WatchedPackage {
return &WatchedPackage{pkg}
}
func (o *WatchedPackage) Name() string {
return o.pkg.Name()
}
func (o *WatchedPackage) Reload() {
o.pkg.Reload()
}
func (o *WatchedPackage) Package() packages.Package {
return o.pkg
}