Skip to content

Commit

Permalink
Added Unregister method to packages package
Browse files Browse the repository at this point in the history
some tweaks in sublime package
open new window and file after calling init callbacks on
editor.Init()
  • Loading branch information
zoli committed Mar 17, 2016
1 parent b7e536d commit 7cd20ad
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 19 deletions.
6 changes: 4 additions & 2 deletions lib/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,11 @@ func (e *Editor) Init() {
e.loadKeyBindings()
e.loadSettings()

e.NewWindow()

OnInit.call()

// There should be usable window and view on startup
w := e.NewWindow()
w.NewFile()
}

func (e *Editor) SetClipboardFuncs(setter func(string) error, getter func() (string, error)) {
Expand Down
13 changes: 11 additions & 2 deletions lib/packages/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,21 @@ type (
)

// Keep track of all registered records
var recs []Record
var recs []*Record

func Register(r Record) {
func Register(r *Record) {
recs = append(recs, r)
}

func Unregister(r *Record) {
for i, rec := range recs {
if rec == r {
recs, recs[len(recs)-1] = append(recs[:i], recs[i+1:]...), nil
break
}
}
}

func record(path string) {
for _, rec := range recs {
if !rec.Check(path) {
Expand Down
31 changes: 30 additions & 1 deletion lib/packages/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func (d *dummyPackage) Name() string { return "" }
func TestRecordCheckAction(t *testing.T) {
count := 0
paths := []string{"a", "b", "c", "d"}
rec := Record{
rec := &Record{
func(s string) bool {
return (s == "a" || s == "b")
},
Expand All @@ -32,3 +32,32 @@ func TestRecordCheckAction(t *testing.T) {
t.Errorf("Expected count 2 but got: %d", count)
}
}

func TestRegisterUnregister(t *testing.T) {
recs = nil

r1 := &Record{func(s string) bool { return true },
func(s string) Package { return &dummyPackage{} }}
r2 := &Record{func(s string) bool { return true },
func(s string) Package { return &dummyPackage{} }}

Register(r1)
if len(recs) != 1 {
t.Errorf("Expected len of records be 1, but got: %d", len(recs))
}

Register(r2)
if len(recs) != 2 {
t.Errorf("Expected len of records be 2, but got: %d", len(recs))
}

Unregister(r1)
if len(recs) != 1 {
t.Errorf("Expected len of records be 1, but got: %d", len(recs))
}

Unregister(r2)
if len(recs) != 0 {
t.Errorf("Expected len of records be 0, but got: %d", len(recs))
}
}
19 changes: 10 additions & 9 deletions lib/sublime/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,14 @@ func (p *pkg) loadPlugins() {
return
}
for _, fi := range fis {
p.loadPlugin(fi.Name())
if isPlugin(fi.Name()) {
p.loadPlugin(path.Join(p.Name(), fi.Name()))
}
}
}

func (p *pkg) loadPlugin(fn string) {
if !isPlugin(fn) {
return
}
_, exist := p.plugins[fn]
if exist {
if _, exist := p.plugins[fn]; exist {
return
}

Expand Down Expand Up @@ -120,14 +118,17 @@ func (p *pkg) loadSettings() {
packages.NewSettingL(pt, p.Settings())
}

// Any directory in sublime is a package
func isPKG(dir string) bool {
fm, err := os.Stat(dir)
fi, err := os.Stat(dir)
if err != nil {
return false
}
return fm.IsDir()
return fi.IsDir()
}

var packageRecord *packages.Record = &packages.Record{isPKG, newPKG}

func init() {
// packages.Register(packages.Record{isPKG, newPKG})
packages.Register(packageRecord)
}
25 changes: 25 additions & 0 deletions lib/sublime/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,28 @@
// BSD-style license that can be found in the LICENSE file.

package sublime

import (
"path"
"testing"

"github.com/limetext/lime-backend/lib"
_ "github.com/limetext/lime-backend/lib/commands"
"github.com/limetext/lime-backend/lib/packages"
)

func TestLoadPlugin(t *testing.T) {

}

func TestLoadPlugins(t *testing.T) {

}

func TestLoadKeyBindings(t *testing.T) {

}

func TestLoadSettings(t *testing.T) {

}
14 changes: 10 additions & 4 deletions lib/sublime/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,22 @@ func newPlugin(fn string) packages.Package {
return &plugin{filename: fn}
}

// TODO: implement unload
func (p *plugin) Load() {
// in case error ocured on running onInit function
if module == nil {
return
}

log.Debug("Loading plugin %s", p.Name())
dir, file := filepath.Split(p.Name())
s, err := py.NewUnicode(filepath.Base(dir) + "." + file[:len(file)-3])
name := filepath.Base(dir) + "." + file[:len(file)-3]
s, err := py.NewUnicode(name)
if err != nil {
log.Warn(err)
return
}

log.Debug("Loading plugin %s", name)
l := py.NewLock()
defer l.Unlock()
if r, err := module.Base().CallMethodObjArgs("reload_plugin", s); err != nil {
Expand All @@ -58,7 +60,11 @@ func isPlugin(filename string) bool {
return filepath.Ext(filename) == ".py"
}

var module *py.Module
var (
pluginRecord *packages.Record = &packages.Record{isPlugin, newPlugin}

module *py.Module
)

func onInit() {
l := py.NewLock()
Expand All @@ -78,5 +84,5 @@ func onPackagesPathAdd(p string) {
func init() {
backend.OnInit.Add(onInit)
backend.OnPackagesPathAdd.Add(onPackagesPathAdd)
packages.Register(packages.Record{isPlugin, newPlugin})
packages.Register(pluginRecord)
}
4 changes: 3 additions & 1 deletion lib/sublime/sublime_api_generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,9 @@ func sublime_PackagesPath(tu *py.Tuple) (py.Object, error) {
var (
arg1 string
)
if v, err := tu.GetItem(0); err != nil {
if tu.Not() {
arg1 = "default"
} else if v, err := tu.GetItem(0); err != nil {
return nil, err
} else {
if v3, err2 := fromPython(v); err2 != nil {
Expand Down

0 comments on commit 7cd20ad

Please sign in to comment.