Skip to content

Commit 1f9e1c4

Browse files
committed
README.md: latest updates
1 parent 3e49d1e commit 1f9e1c4

File tree

3 files changed

+152
-64
lines changed

3 files changed

+152
-64
lines changed

README.md

+140-58
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# motion
22

33
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.
77

88
It's optimized and created to work with
99
[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
1717

1818
# Usage
1919

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:
2122

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
2326
* `next`: returns the next function information for a given offset
2427
* `prev`: returns the previous function information for a given offset
2528

@@ -28,7 +31,11 @@ A `function information` is currently the following type definition (defined as
2831

2932
```
3033
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"`
3239
Lbrace *Position `json:"lbrace" vim:"lbrace"` // position of "{"
3340
Rbrace *Position `json:"rbrace" vim:"rbrace"` // position of "}"
3441
@@ -39,57 +46,83 @@ type Func struct {
3946
}
4047
```
4148

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`.
4550

4651
An example execution for the `enclosing` mode and output in `json` format is:
4752

4853
```
49-
$ motion -file testdata/foo.go -offset 160 -mode enclosing --format json
54+
$ motion -file testdata/main.go -offset 180 -mode enclosing --format json
5055
{
56+
"mode": "enclosing",
5157
"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+
}
6583
}
6684
}
6785
```
6886

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:
7089

7190
```
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
7392
{
93+
"mode": "enclosing",
7494
"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+
}
93126
}
94127
}
95128
```
@@ -98,22 +131,35 @@ For example the same query, but with mode `-mode next` returns a different
98131
result. Instead it returns the next function inside the source code:
99132

100133
```
101-
$ motion -file testdata/foo.go -offset 160 -mode next --format json
134+
$ motion -file testdata/main.go -offset 180 -mode next --format json
102135
{
136+
"mode": "next",
103137
"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+
}
117163
}
118164
}
119165
```
@@ -122,8 +168,44 @@ If there are not functions available for any mode, it returns an error in the
122168
specified format:
123169

124170
```
125-
$ motion -file testdata/foo.go -offset 330 -mode next --format json
171+
$ motion -file testdata/main.go -offset 330 -mode next --format json
126172
{
127173
"err": "no functions found"
128174
}
129175
```
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+
```

main.go

+7
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ func realMain() error {
3535
)
3636

3737
flag.Parse()
38+
if flag.NFlag() == 0 {
39+
flag.Usage()
40+
return nil
41+
}
42+
43+
a := flag.NFlag()
44+
fmt.Println("a", a)
3845

3946
if *flagMode == "" {
4047
return errors.New("no mode is passed")

testdata/main.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,20 @@ import (
44
"fmt"
55
)
66

7-
var a = func() { fmt.Println("tokyo") }
7+
var a = func() { fmt.Println("Ankara") }
88

99
func main() {
10-
fmt.Printf("Hello, world\n")
10+
fmt.Println("vim-go: text-objects")
1111
a()
1212
}
1313

14-
// Bar is something else
14+
// Bar is a simple function declaration
1515
func Bar() (string, error) {
1616
return "vim-go", nil
1717
}
1818

19-
// and this is a multi comment doc. I think
20-
// docs are really different kind of things,
21-
// bla bla
19+
// and this is a multi line doc comment. example is
20+
// just a function that prints zeynep
2221
func example() error {
2322
a := func() {
2423
fmt.Println("zeynep")

0 commit comments

Comments
 (0)