[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/go-mode d91a157 257/495: Add goto functions for working wi
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/go-mode d91a157 257/495: Add goto functions for working with function signatures |
Date: |
Sat, 7 Aug 2021 09:05:27 -0400 (EDT) |
branch: elpa/go-mode
commit d91a1578b59f849f6c1bcdeb8ab972dac0bb3410
Author: Lowe Thiderman <lowe.thiderman@gmail.com>
Commit: Lowe Thiderman <lowe.thiderman@gmail.com>
Add goto functions for working with function signatures
Add several functions:
- `go-goto-arguments`
- `go-goto-docstring`
- `go-goto-function`
- `go-goto-function-name`
- `go-goto-return-value`
- `go-goto-type-signature`
They are all about going to different parts of function signatures, with
their names being self descriptive. They all operate on the current
function, the definition of which is "the closest `func` statement above
point". If no `func` is found above point, the functions will jump to
the first `func` in the file. Lastly, if the point is in the docstring
of a function, the goto functions will operate on that function and not
the function above.
Additionally, some of the functions will create their targets if they are
missing:
- `go-goto-docstring` will create a docstring if it does not
exist as well as update the docstring name for a function that has been
renamed.
- `go-goto-return-value` will add space for adding return values.
- `go-goto-type-signature` will add spaces and a pair of parenthesis for
adding a signature.
It should also be noted that the functions have been tested with
anonymous functions as well.
Add keymap at `C-c C-g` for goto functions.
Add mapping for the already existing `go-goto-imports` at `C-c C-g i`.
Add helper function `go--get-function-name`, which grabs the string of
the function above point.
Update README to reflect the addition of these functions.
---
README.md | 12 +++++-
go-mode.el | 129 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 140 insertions(+), 1 deletion(-)
diff --git a/README.md b/README.md
index 6e5a780..8143036 100644
--- a/README.md
+++ b/README.md
@@ -26,7 +26,8 @@ the following extra features to provide an improved
experience:
- `go-download-play` to download a Playground entry into a new
buffer
- Managing imports
- - A function for jumping to the file's imports (`go-goto-imports`)
+ - A function for jumping to the file's imports (`go-goto-imports` -
+ `C-c C-g i`)
- A function for adding imports, including tab completion
(`go-import-add`, bound to `C-c C-a`)
- A function for removing or commenting unused imports
@@ -40,6 +41,15 @@ the following extra features to provide an improved
experience:
- Basic support for imenu (functions and variables)
- Built-in support for displaying code coverage as calculated by `go
test` (`go-coverage`)
+- Several goto functions for manipulating function signatures:
+ - Going to the arguments (`go-goto-arguments` - `C-c C-g a`)
+ - Going to the docstring, creating it if it does not exist and updating it
+ if the name does not match (`go-goto-docstring` - `C-c C-g d`)
+ - Going to the function name (`go-goto-function-name` - `C-c C-g f`)
+ - Going to the return value, adding space for adding one if there are none
+ present (`go-goto-return-value` - `C-c C-g r`)
+ - Going to the type signature, adding parenthesis for adding one if there is
+ none present (`go-goto-type-signature` - `C-c C-g t`)
# Installation
diff --git a/go-mode.el b/go-mode.el
index 851b937..4ae3c40 100644
--- a/go-mode.el
+++ b/go-mode.el
@@ -845,7 +845,13 @@ The following extra functions are defined:
- `godoc'
- `go-import-add'
- `go-remove-unused-imports'
+- `go-goto-arguments'
+- `go-goto-docstring'
+- `go-goto-function'
+- `go-goto-function-name'
- `go-goto-imports'
+- `go-goto-return-value'
+- `go-goto-type-signature'
- `go-play-buffer' and `go-play-region'
- `go-download-play'
- `godef-describe' and `godef-jump'
@@ -1562,6 +1568,129 @@ for."
(if (not (eq cur-buffer (current-buffer)))
(display-buffer (current-buffer)
`(,go-coverage-display-buffer-func))))))
+(define-prefix-command 'go-goto-map)
+(define-key go-mode-map (kbd "C-c C-g") 'go-goto-map)
+
+(defun go-goto-function ()
+ "Go to the function defintion above point.
+
+If we are on a docstring, follow the docstring down.
+If no function is found, assume that we are at the top of a file
+and search forward instead."
+ (interactive)
+
+ (beginning-of-line)
+ (cond
+ ((looking-at "^//")
+ ;; In case we are looking at the docstring, move on forward until we are
not anymore
+ (while (looking-at "^//")
+ (forward-line 1)))
+ ((not (looking-at "^func"))
+ ;; If we are not looking at the beginning of a function line, do a regexp
search backwards
+ (re-search-backward "\\<func\\>" nil t)
+
+ (when (not (looking-at "func"))
+ (re-search-forward "\\<func\\>" nil t)
+ (backward-word)))))
+
+(defun go-goto-function-name ()
+ "Go to the name of the current function.
+
+If the function is a test, place point after 'Test'.
+If the function is anonymous, place point on the 'func' keyword."
+ (interactive)
+ (when (not (looking-at "func"))
+ (go-goto-function))
+ ;; If we are looking at func( we are on an anonymous function and
+ ;; nothing else should be done.
+ (when (not (looking-at "func("))
+ (let ((words 1)
+ (chars 1))
+ (when (looking-at "func (")
+ (setq words 3
+ chars 2))
+ (forward-word words)
+ (forward-char chars)
+ (when (looking-at "Test")
+ (forward-char 4)))))
+
+(define-key go-goto-map (kbd "f") 'go-goto-function-name)
+
+(defun go-goto-arguments ()
+ "Go to the return value declaration of the current function."
+ (interactive)
+ (go-goto-function-name)
+ (forward-word 1)
+ (forward-char 1))
+
+(define-key go-goto-map (kbd "a") 'go-goto-arguments)
+
+(defun go-goto-return-value ()
+ "Go to the return value declaration of the current function.
+
+If there is none, make space for one to be added."
+ (interactive)
+ (go-goto-arguments)
+ (re-search-forward ")" nil t)
+ (forward-char 1)
+
+ ;; Opening parenthesis, enter it
+ (when (looking-at "(")
+ (forward-char 1))
+ ;; First item is a pointer, move past the pointer
+ (when (looking-at "*")
+ (forward-char 1))
+ ;; No return arguments, add space for adding
+ (when (looking-at "{")
+ (insert " ")
+ (backward-char 1)))
+
+(define-key go-goto-map (kbd "r") 'go-goto-return-value)
+
+;; Since this is already defined, just add the key to it.
+(define-key go-goto-map (kbd "i") 'go-goto-imports)
+
+(defun go-goto-type-signature ()
+ "Go to the type signature of the current function.
+
+If there is none, add parenthesis to add one."
+ (interactive)
+ (go-goto-function)
+ (forward-char 5)
+ (when (not (looking-at "("))
+ (save-excursion
+ (insert "() ")))
+ (forward-char 1))
+
+(define-key go-goto-map (kbd "t") 'go-goto-type-signature)
+
+(defun go-goto-docstring ()
+ "Go to the top of the docstring of the current function.
+
+If there is none, add one."
+ (interactive)
+ (go-goto-function)
+ (forward-line -1)
+ (beginning-of-line)
+ (while (looking-at "^//")
+ (forward-line -1))
+ (next-line 1)
+ (beginning-of-line)
+ (if (not (looking-at "^func"))
+ (forward-char 3)
+ ;; If we are still at the function signature, we should add a new
docstring.
+ (forward-line -1)
+ (newline)
+ (insert (format "// %s " (go--get-function-name)))))
+
+(define-key go-goto-map (kbd "d") 'go-goto-docstring)
+
+(defun go--get-function-name ()
+ "Return the current function name as a string"
+ (save-excursion
+ (go-goto-function-name)
+ (symbol-name (symbol-at-point))))
+
(provide 'go-mode)
;;; go-mode.el ends here
- [nongnu] elpa/go-mode c576513 494/495: Fix fontification of multiplicand after index expr., (continued)
- [nongnu] elpa/go-mode c576513 494/495: Fix fontification of multiplicand after index expr., ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode 49a5380 493/495: Trivial fix of docstring and comment spelling typos., ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode 584628e 148/495: instead of killing lines, delete them, to avoid manipulating the kill ring, ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode a82c165 221/495: fix the license, ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode 27068bb 222/495: provide configuration option for how to display coverage buffer, ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode 86ed568 235/495: add link to `defgroup'., ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode a9b9871 247/495: Use view-mode in godoc-mode, ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode dce210f 250/495: Bump version to 1.3.1, ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode 8baa836 251/495: Don't break fontification when limiting point, ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode dccb56c 256/495: go-unused-imports-lines: only compile the current file, ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode d91a157 257/495: Add goto functions for working with function signatures,
ELPA Syncer <=
- [nongnu] elpa/go-mode 11e7b4e 259/495: Fix copy-pasted docstring of go-goto-arguments, ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode b261fac 263/495: Add support for (nested) anonymous functions in go-goto-function, ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode 6e8373e 267/495: go-goto-docstring: handle empty comments, ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode d1ebde8 271/495: Rename go--get-function-name to go--function-name, ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode eddbcff 277/495: Make sure we're looking at func keyword, ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode 45a8131 284/495: Implement go--goto-opening-curly-brace as in go-end-of-defun, ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode d6aa01a 287/495: Merge branch 'movement', ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode 27b7415 293/495: Allow passing additional arguments to gofmt, ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode 1214f77 294/495: Bind go-goto-map to C-f, not C-g, ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode 17bd2b2 296/495: Add GOPATH detection, ELPA Syncer, 2021/08/07