forked from limetext/lime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
packet.go
98 lines (83 loc) · 2.1 KB
/
packet.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright 2013 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 packages
import (
"encoding/json"
"github.com/limetext/lime/backend/loaders"
"github.com/limetext/lime/backend/log"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
type (
// Packets are small Packages containing 1 file.
// Individual settings, keymaps, snippets, etc. are Packets
Packet struct {
path string
// the Packet content will be Unmarshal to this variable
// so on reload we know where we should unmarshal it again
marshalTo json.Unmarshaler
}
// Useful for managing packets for plugins
// and loading user packets for editor
Packets []*Packet
)
// Initializes new packet with specific path
func NewPacket(path string, marshal json.Unmarshaler) *Packet {
return &Packet{path, marshal}
}
func (p *Packet) Name() string {
return p.path
}
// Returns packet file data if any error occurred
// on reading file we will return nil
func (p *Packet) Get() interface{} {
e := []byte(`{}`)
if p.group() == "keymap" {
e = []byte(`[]`)
}
if _, err := os.Stat(p.path); os.IsNotExist(err) {
log.Finest("%s doesn't exist yet", p.path)
return e
}
d, err := ioutil.ReadFile(p.path)
if err != nil {
log.Errorf("Couldn't read file: %s", err)
return e
}
return d
}
// Forces editor to load the packet again
func (p *Packet) FileChanged(name string) {
p.Load()
}
func (p *Packet) Load() error {
return loaders.LoadJSON(p.Get().([]byte), p)
}
func (p *Packet) MarshalTo() json.Unmarshaler {
return p.marshalTo
}
func (p *Packet) UnmarshalJSON(data []byte) error {
return p.marshalTo.UnmarshalJSON(data)
}
// Returns packet type(settings, commands, keymaps, ...)
func (p *Packet) group() string {
for _, key := range types {
if strings.Contains(filepath.Ext(p.Name()), key) {
return key
}
}
return ""
}
// Returns Packets with specific type
func (p Packets) Filter(key string) Packets {
var pckts Packets
for _, pckt := range p {
if strings.Contains(filepath.Ext(pckt.Name()), key) {
pckts = append(pckts, pckt)
}
}
return pckts
}