forked from limetext/lime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.go
122 lines (112 loc) · 2.87 KB
/
plugin.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// 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 (
"github.com/limetext/lime/backend/keys"
"github.com/limetext/lime/backend/log"
"github.com/limetext/text"
"os"
pt "path"
"strings"
)
type (
// Plugin is a Package type containing some files
// with specific suffix that could be interpreted by
// lime text api(currently python) and some
// settings, snippets, commands and etc as packets
Plugin struct {
text.HasSettings
keys.HasKeyBindings
path string
suffix string
files []os.FileInfo
defaultSettings *text.HasSettings
platformSettings *text.HasSettings
defaultBindings *keys.HasKeyBindings
}
)
// Initializes a new plugin whith loading all of the
// settings, keymaps and etc. Suffix variable show's
// which file types we need for plugin for example if
// the plugin is written in python the suffix should
// be ".py". We will use this function at initialization
// to add user plugins and on new_plugin command
func NewPlugin(path string, suffix string) (p *Plugin) {
p = &Plugin{path: path, suffix: suffix}
p.defaultSettings = new(text.HasSettings)
p.platformSettings = new(text.HasSettings)
p.defaultBindings = new(keys.HasKeyBindings)
p.Settings().SetParent(p.platformSettings)
p.platformSettings.Settings().SetParent(p.defaultSettings)
p.KeyBindings().SetParent(p.defaultBindings)
return
}
func (p *Plugin) Name() string {
return p.path
}
// Returns slice of files with plugin suffix
// loaded at initialization
func (p *Plugin) Get() interface{} {
return p.files
}
// On plugin reload we will scan for plugin files
// and packets in plugin path
func (p *Plugin) Reload() {
var files []os.FileInfo
log.Info("Reloading plugin %s", p.Name())
f, err := os.Open(p.path)
if err != nil {
log.Errorf("Couldn't open dir: %s", err)
return
}
defer f.Close()
fi, err := f.Readdir(-1)
if err != nil {
log.Errorf("Couldn't read dir: %s", err)
return
}
for _, f := range fi {
if p.suffix != "" && strings.HasSuffix(f.Name(), p.suffix) {
files = append(files, f)
}
}
p.files = files
}
// Scaning path for finding plugins that contain files
// whith specific suffix
func ScanPlugins(path string, suffix string) []*Plugin {
var plugins []*Plugin
f, err := os.Open(path)
if err != nil {
log.Warn(err)
return nil
}
defer f.Close()
dirs, err := f.Readdirnames(-1)
if err != nil {
log.Warn(err)
return nil
}
for _, dir := range dirs {
dir2 := pt.Join(path, dir)
f2, err := os.Open(dir2)
if err != nil {
log.Warn(err)
continue
}
defer f2.Close()
fi, err := f2.Readdir(-1)
if err != nil {
continue
}
for _, f := range fi {
fn := f.Name()
if strings.HasSuffix(fn, suffix) {
plugins = append(plugins, NewPlugin(dir2, suffix))
break
}
}
}
return plugins
}