forked from godoctor/godoctor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.go
58 lines (48 loc) · 1.96 KB
/
engine.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
// Copyright 2015 Auburn University. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package engine is the programmatic entrypoint to the Go refactoring engine.
package engine
import (
"fmt"
"github.com/godoctor/godoctor/refactoring"
)
// All available refactorings, keyed by a unique, one-word, all-lowercase name
var refactorings map[string]refactoring.Refactoring
// All available refactorings' keys, in the order the refactorings should be
// displayed in a menu presented to the end user
var refactoringsInOrder []string
func init() {
refactorings = map[string]refactoring.Refactoring{}
refactoringsInOrder = []string{}
AddRefactoring("rename", new(refactoring.Rename))
AddRefactoring("extract", new(refactoring.ExtractFunc))
AddRefactoring("toggle", new(refactoring.ToggleVar))
AddRefactoring("godoc", new(refactoring.AddGoDoc))
AddRefactoring("debug", new(refactoring.Debug))
AddRefactoring("null", new(refactoring.Null))
}
// AllRefactoringNames returns the short names of all refactorings in an
// order suitable for display in a menu.
func AllRefactoringNames() []string {
return refactoringsInOrder
}
// GetRefactoring returns a Refactoring keyed by the given short name. The
// short name must be one of the keys in the map returned by AllRefactorings.
func GetRefactoring(shortName string) refactoring.Refactoring {
return refactorings[shortName]
}
// AddRefactoring allows custom refactorings to be added to the refactoring
// engine. Invoke this method before starting the command line or protocol
// driver.
func AddRefactoring(shortName string, newRefac refactoring.Refactoring) error {
if r, ok := refactorings[shortName]; ok {
return fmt.Errorf("The short name \"%s\" is already "+
"associated with a refactoring (%s)",
shortName,
r.Description().Name)
}
refactorings[shortName] = newRefac
refactoringsInOrder = append(refactoringsInOrder, shortName)
return nil
}