1
1
# motion
2
2
3
3
Motion is a tool that was designed to work with editors. It is providing
4
- contextual information for a given offset. Editors can use these informations
5
- to implement navigation, text editing, etc... that are specific to a Go source
6
- code.
4
+ contextual information for a given offset(option) from a file or directory of
5
+ files. Editors can use these informations to implement navigation, text
6
+ editing, etc... that are specific to a Go source code.
7
7
8
8
It's optimized and created to work with
9
9
[ vim-go] ( https://github.com/fatih/vim-go ) , but it's designed to work with any
@@ -17,9 +17,12 @@ go get github.com/fatih/motion
17
17
18
18
# Usage
19
19
20
- ` motion ` is meant to be run via the editor. Currently it has three modes you can use:
20
+ ` motion ` is meant to be run via the editor. Currently it has the following
21
+ modes you can use:
21
22
22
- * ` enclosing ` : returns information about the enclosing function for a given offset
23
+ * ` decls ` : returns a list of declarations based on the ` -include ` flag
24
+ * ` enclosing ` : returns information about the enclosing function for a given
25
+ offset
23
26
* ` next ` : returns the next function information for a given offset
24
27
* ` prev ` : returns the previous function information for a given offset
25
28
@@ -28,7 +31,11 @@ A `function information` is currently the following type definition (defined as
28
31
29
32
```
30
33
type Func struct {
31
- FuncPos *Position `json:"func" vim:"func"` // position of the "func" keyword
34
+ // Signature of the function
35
+ Signature *FuncSignature `json:"sig" vim:"sig"`
36
+
37
+ // position of the "func" keyword
38
+ FuncPos *Position `json:"func" vim:"func"`
32
39
Lbrace *Position `json:"lbrace" vim:"lbrace"` // position of "{"
33
40
Rbrace *Position `json:"rbrace" vim:"rbrace"` // position of "}"
34
41
@@ -39,57 +46,83 @@ type Func struct {
39
46
}
40
47
```
41
48
42
- ` motion ` can output the information currently in three formats: ` plain ` , ` json `
43
- and ` vim ` .
44
-
49
+ ` motion ` can output the information currently in formats: ` json ` and ` vim ` .
45
50
46
51
An example execution for the ` enclosing ` mode and output in ` json ` format is:
47
52
48
53
```
49
- $ motion -file testdata/foo .go -offset 160 -mode enclosing --format json
54
+ $ motion -file testdata/main .go -offset 180 -mode enclosing --format json
50
55
{
56
+ "mode": "enclosing",
51
57
"func": {
52
- "offset": 151,
53
- "line": 15,
54
- "col": 1
55
- },
56
- "lbrace": {
57
- "offset": 178,
58
- "line": 15,
59
- "col": 28
60
- },
61
- "rbrace": {
62
- "offset": 202,
63
- "line": 17,
64
- "col": 1
58
+ "sig": {
59
+ "full": "func Bar() (string, error)",
60
+ "recv": "",
61
+ "name": "Bar",
62
+ "in": "",
63
+ "out": "string, error"
64
+ },
65
+ "func": {
66
+ "filename": "testdata/main.go",
67
+ "offset": 174,
68
+ "line": 15,
69
+ "col": 1
70
+ },
71
+ "lbrace": {
72
+ "filename": "testdata/main.go",
73
+ "offset": 201,
74
+ "line": 15,
75
+ "col": 28
76
+ },
77
+ "rbrace": {
78
+ "filename": "testdata/main.go",
79
+ "offset": 225,
80
+ "line": 17,
81
+ "col": 1
82
+ }
65
83
}
66
84
}
67
85
```
68
86
69
- To include the doc comments for function declarations include the ` --parse-comments ` flag:
87
+ To include the doc comments for function declarations include the
88
+ ` --parse-comments ` flag:
70
89
71
90
```
72
- $ motion -file testdata/foo .go -offset 160 -mode enclosing --format json --parse-comments
91
+ $ motion -file testdata/main .go -offset 180 -mode enclosing --format json --parse-comments
73
92
{
93
+ "mode": "enclosing",
74
94
"func": {
75
- "offset": 151,
76
- "line": 15,
77
- "col": 1
78
- },
79
- "lbrace": {
80
- "offset": 178,
81
- "line": 15,
82
- "col": 28
83
- },
84
- "rbrace": {
85
- "offset": 202,
86
- "line": 17,
87
- "col": 1
88
- },
89
- "doc": {
90
- "offset": 126,
91
- "line": 14,
92
- "col": 1
95
+ "sig": {
96
+ "full": "func Bar() (string, error)",
97
+ "recv": "",
98
+ "name": "Bar",
99
+ "in": "",
100
+ "out": "string, error"
101
+ },
102
+ "func": {
103
+ "filename": "testdata/main.go",
104
+ "offset": 174,
105
+ "line": 15,
106
+ "col": 1
107
+ },
108
+ "lbrace": {
109
+ "filename": "testdata/main.go",
110
+ "offset": 201,
111
+ "line": 15,
112
+ "col": 28
113
+ },
114
+ "rbrace": {
115
+ "filename": "testdata/main.go",
116
+ "offset": 225,
117
+ "line": 17,
118
+ "col": 1
119
+ },
120
+ "doc": {
121
+ "filename": "testdata/main.go",
122
+ "offset": 134,
123
+ "line": 14,
124
+ "col": 1
125
+ }
93
126
}
94
127
}
95
128
```
@@ -98,22 +131,35 @@ For example the same query, but with mode `-mode next` returns a different
98
131
result. Instead it returns the next function inside the source code:
99
132
100
133
```
101
- $ motion -file testdata/foo .go -offset 160 -mode next --format json
134
+ $ motion -file testdata/main .go -offset 180 -mode next --format json
102
135
{
136
+ "mode": "next",
103
137
"func": {
104
- "offset": 302,
105
- "line": 21,
106
- "col": 1
107
- },
108
- "lbrace": {
109
- "offset": 319,
110
- "line": 21,
111
- "col": 18
112
- },
113
- "rbrace": {
114
- "offset": 382,
115
- "line": 29,
116
- "col": 1
138
+ "sig": {
139
+ "full": "func example() error",
140
+ "recv": "",
141
+ "name": "example",
142
+ "in": "",
143
+ "out": "error"
144
+ },
145
+ "func": {
146
+ "filename": "testdata/main.go",
147
+ "offset": 318,
148
+ "line": 21,
149
+ "col": 1
150
+ },
151
+ "lbrace": {
152
+ "filename": "testdata/main.go",
153
+ "offset": 339,
154
+ "line": 21,
155
+ "col": 22
156
+ },
157
+ "rbrace": {
158
+ "filename": "testdata/main.go",
159
+ "offset": 402,
160
+ "line": 29,
161
+ "col": 1
162
+ }
117
163
}
118
164
}
119
165
```
@@ -122,8 +168,44 @@ If there are not functions available for any mode, it returns an error in the
122
168
specified format:
123
169
124
170
```
125
- $ motion -file testdata/foo .go -offset 330 -mode next --format json
171
+ $ motion -file testdata/main .go -offset 330 -mode next --format json
126
172
{
127
173
"err": "no functions found"
128
174
}
129
175
```
176
+
177
+ Lastly for the mode ` decls ` , we pass the file (you can also pass a directory)
178
+ and instruct to only include function declarations with the ` -include func `
179
+ flag:
180
+ ```
181
+ $ motion -file testdata/main.go -mode decls -include func
182
+ {
183
+ "mode": "decls",
184
+ "decls": [
185
+ {
186
+ "keyword": "func",
187
+ "ident": "main",
188
+ "full": "func main()",
189
+ "filename": "testdata/main.go",
190
+ "line": 9,
191
+ "col": 1
192
+ },
193
+ {
194
+ "keyword": "func",
195
+ "ident": "Bar",
196
+ "full": "func Bar() (string, error)",
197
+ "filename": "testdata/main.go",
198
+ "line": 15,
199
+ "col": 1
200
+ },
201
+ {
202
+ "keyword": "func",
203
+ "ident": "example",
204
+ "full": "func example() error",
205
+ "filename": "testdata/main.go",
206
+ "line": 21,
207
+ "col": 1
208
+ }
209
+ ]
210
+ }
211
+ ```
0 commit comments