• Stars
    star
    180
  • Rank 213,097 (Top 5 %)
  • Language
    Go
  • License
    BSD 3-Clause "New...
  • Created almost 9 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Navigation and insight in Go

Motion

Motion is a tool that was designed to work with editors. It is providing contextual information for a given offset(option) from a file or directory of files. Editors can use these informations to implement navigation, text editing, etc... that are specific to a Go source code.

It's optimized and created to work with vim-go, but it's designed to work with any editor. It's currently work in progress and open to change.

Install

go get github.com/fatih/motion

Usage

motion is meant to be run via the editor. Currently it has the following modes you can use:

  • decls: returns a list of declarations based on the -include flag
  • enclosing: returns information about the enclosing function for a given offset
  • next: returns the next function information for a given offset
  • prev: returns the previous function information for a given offset
  • comment: returns information about the a comment block (if any).

A function information is currently the following type definition (defined as astcontext.Func):

type Func struct {
	// Signature of the function
	Signature *FuncSignature `json:"sig" vim:"sig"`

	// position of the "func" keyword
	FuncPos *Position `json:"func" vim:"func"`
	Lbrace  *Position `json:"lbrace" vim:"lbrace"` // position of "{"
	Rbrace  *Position `json:"rbrace" vim:"rbrace"` // position of "}"

	// position of the doc comment, only for *ast.FuncDecl
	Doc *Position `json:"doc,omitempty" vim:"doc,omitempty"`

	node ast.Node // either *ast.FuncDecl or *ast.FuncLit
}

motion can output the information currently in formats: json and vim.

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

$ motion -file testdata/main.go -offset 180 -mode enclosing --format json
{
	"mode": "enclosing",
	"func": {
		"sig": {
			"full": "func Bar() (string, error)",
			"recv": "",
			"name": "Bar",
			"in": "",
			"out": "string, error"
		},
		"func": {
			"filename": "testdata/main.go",
			"offset": 174,
			"line": 15,
			"col": 1
		},
		"lbrace": {
			"filename": "testdata/main.go",
			"offset": 201,
			"line": 15,
			"col": 28
		},
		"rbrace": {
			"filename": "testdata/main.go",
			"offset": 225,
			"line": 17,
			"col": 1
		}
	}
}

To include the doc comments for function declarations include the --parse-comments flag:

$ motion -file testdata/main.go -offset 180 -mode enclosing --format json --parse-comments
{
	"mode": "enclosing",
	"func": {
		"sig": {
			"full": "func Bar() (string, error)",
			"recv": "",
			"name": "Bar",
			"in": "",
			"out": "string, error"
		},
		"func": {
			"filename": "testdata/main.go",
			"offset": 174,
			"line": 15,
			"col": 1
		},
		"lbrace": {
			"filename": "testdata/main.go",
			"offset": 201,
			"line": 15,
			"col": 28
		},
		"rbrace": {
			"filename": "testdata/main.go",
			"offset": 225,
			"line": 17,
			"col": 1
		},
		"doc": {
			"filename": "testdata/main.go",
			"offset": 134,
			"line": 14,
			"col": 1
		}
	}
}

For example the same query, but with mode -mode next returns a different result. Instead it returns the next function inside the source code:

$ motion -file testdata/main.go -offset 180 -mode next --format json
{
	"mode": "next",
	"func": {
		"sig": {
			"full": "func example() error",
			"recv": "",
			"name": "example",
			"in": "",
			"out": "error"
		},
		"func": {
			"filename": "testdata/main.go",
			"offset": 318,
			"line": 21,
			"col": 1
		},
		"lbrace": {
			"filename": "testdata/main.go",
			"offset": 339,
			"line": 21,
			"col": 22
		},
		"rbrace": {
			"filename": "testdata/main.go",
			"offset": 402,
			"line": 29,
			"col": 1
		}
	}
}

If there are not functions available for any mode, it returns an error in the specified format:

$ motion -file testdata/main.go -offset 330 -mode next --format json
{
	"err": "no functions found"
}

For the mode decls, we pass the file (you can also pass a directory) and instruct to only include function declarations with the -include func flag:

$ motion -file testdata/main.go -mode decls -include func
{
	"mode": "decls",
	"decls": [
		{
			"keyword": "func",
			"ident": "main",
			"full": "func main()",
			"filename": "testdata/main.go",
			"line": 9,
			"col": 1
		},
		{
			"keyword": "func",
			"ident": "Bar",
			"full": "func Bar() (string, error)",
			"filename": "testdata/main.go",
			"line": 15,
			"col": 1
		},
		{
			"keyword": "func",
			"ident": "example",
			"full": "func example() error",
			"filename": "testdata/main.go",
			"line": 21,
			"col": 1
		}
	]
}

In the comment mode it will try to get information about the comment block for a given offset:

$ motion -mode comment -file ./vim/vim.go -offset 3
{
        "mode": "comment",
        "comment": {
                "startLine": 1,
                "startCol": 1,
                "endLine": 3,
                "endCol": 50
        }
}

More Repositories

1

vim-go

Go development plugin for Vim
Vim Script
15,577
star
2

color

Color package for Go (golang)
Go
6,536
star
3

structs

Utilities for Go structs
Go
3,811
star
4

vim-go-tutorial

Tutorial for vim-go
Vim Script
2,122
star
5

gomodifytags

Go tool to modify struct field tags
Go
1,995
star
6

pool

Connection pool for Go's net.Conn interface
Go
1,331
star
7

subvim

Vim customized to be like SublimeText
C++
1,122
star
8

dotfiles

My personal dotfiles
Lua
792
star
9

set

Set data structure for Go
Go
657
star
10

structtag

Parse and modify Go struct field tags
Go
568
star
11

errwrap

Go tool to wrap and fix errors with the new %w verb directive
Go
366
star
12

semgroup

Like errgroup/waitgroup, but only runs a maximum of tasks at any time.
Go
280
star
13

faillint

Report unwanted import path and declaration usages
Go
229
star
14

hclfmt

Format and prettify HCL files
Go
227
star
15

astrewrite

Go tool to walk & rewrite AST
Go
166
star
16

camelcase

Split a camelcase word into a slice of words in Go
Go
158
star
17

starhook

Manage & Analyze repositories at scale
Go
93
star
18

vim-hclfmt

Vim plugin for hclfmt
Vim Script
73
star
19

stopwatch

Stopwatch functionality for Go
Go
69
star
20

images

Images is a tool for managing machine images from multiple providers
Go
68
star
21

addlint

An example linter written with go/analysis for tutorial purposes
Go
53
star
22

gb-example

Example gb project with dependencies and CI integration
Go
47
star
23

hcl

HCL Parser and Printer in Go
Go
44
star
24

templatectl

Simple templating CLI
Go
42
star
25

twirpdemo

An example repository of using the Twirp RPC framework with Go
Go
32
star
26

talks

My personal talk slides
Go
24
star
27

unexport

Unexport notused exported identifiers in Go
Go
22
star
28

kodla-talk-2022

Code and slides for Kodla 2022
Go
20
star
29

flags

Flag parsing in Go
Go
18
star
30

vim-nginx

Nginx runtime files for Vim
Vim Script
17
star
31

dvb-t2

Software implementation of DVB-T2
Objective-C
16
star
32

sicp

My personal notes, solutions, thoughts, etc.. about SICP
16
star
33

amqp-examples

Examples to show basic amqp commands in different languages
Go
15
star
34

testmod

Testing Go modules
AMPL
11
star
35

RailsDashboard.kdapp

An easy way to learn, test and deploy Rails
CoffeeScript
7
star
36

cafetiere

An iOS app to make beautiful Coffee
Objective-C
6
star
37

koding-wiki

Koding framework docs to build KD Apps
6
star
38

blog.arsln.org-backup

Fatih Arslan's Personal Blog
CSS
4
star
39

docker-ubuntu-go

Docker image for Go and Ubuntu
Shell
4
star
40

sinerji

A gui written in PyQt4 that uses Avahi as backend for Synergy
Python
1
star
41

snippets

Snippets, code examples, etc..
C
1
star
42

pisi-vim

A vim plugin for pisi packaging
Vim Script
1
star