-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
89 lines (77 loc) · 2.18 KB
/
main.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
package main
import (
"fmt"
"log"
"os"
"path"
"github.com/neovim/go-client/nvim/plugin"
)
type FileAutocmdEval struct {
Filename string `eval:"expand('<amatch>:h')"`
}
type FileManualCmdEval struct {
Cwd string `msgpack:",array"`
File string
}
func initLog(debug bool) {
tmpDir := os.TempDir()
var logPath string
if os.Getenv("NVIM_DBBG_LOG_FILE") != "" {
logPath = os.Getenv("NVIM_DBBG_LOG_FILE")
} else {
logPath = path.Join(tmpDir, "nvim-dbbg.log")
}
if debug {
fmt.Println("Logging to: " + logPath)
}
l, _ := os.Create(logPath)
log.SetOutput(l)
}
func main() {
// create a log to log to right away. It will help with debugging
filename := os.Getenv("NVIM_DBBG_FILE")
initLog(filename != "")
if filename != "" {
log.Print("Setting initial results file to: " + filename)
if _, err := os.Stat(filename); err == nil {
SetQueryResults(filename)
} else {
log.Print("File does not exist: " + filename)
}
}
port := os.Getenv("NVIM_DBBG_PORT")
if port == "" {
port = "4546"
}
altRootDir := os.Getenv("NVIM_DBBG_UI_ROOT_DIR")
go StartServer(port, altRootDir)
plugin.Main(func(p *plugin.Plugin) error {
p.HandleCommand(&plugin.CommandOptions{Name: "DBBGSetFile", NArgs: "?", Eval: "[getcwd(), expand('%:p')]"},
func(args []string, eval *FileManualCmdEval) {
if len(args) > 0 && len(args[0]) > 0 {
file := args[0]
if file[0] == '/' || file[0] == '~' {
log.Print("DBBGSetFile - using provided absolute path: " + file)
SetQueryResults(file)
BroadcastQueryResults()
} else {
file = path.Join(eval.Cwd, file)
log.Print("DBBGSetFile - using provided file and relative path: " + file)
SetQueryResults(file)
BroadcastQueryResults()
}
} else {
log.Print("DBBGSetFile - no args provided, using current file: " + eval.File)
SetQueryResults(eval.File)
BroadcastQueryResults()
}
})
p.HandleAutocmd(&plugin.AutocmdOptions{Event: "User", Group: "ExmplNvGoClientGrp", Pattern: "*DBExecutePost", Eval: "*"},
func(eval *FileAutocmdEval) {
log.Print("DBExecutePost - wrote file: " + eval.Filename)
SetQueryResults(eval.Filename)
BroadcastQueryResults()
})
return nil
})
}