branch: externals/javaimp commit f9f58c5f3be4c93228f774b9e5ca7dd90e192b4e Author: Filipp Gunbin <fgun...@fastmail.fm> Commit: Filipp Gunbin <fgun...@fastmail.fm>
Add javaimp-jump-to-enclosing-scope --- javaimp-parse.el | 34 ++++++++++++++++------------------ javaimp.el | 26 ++++++++++++++++++++++++-- 2 files changed, 40 insertions(+), 20 deletions(-) diff --git a/javaimp-parse.el b/javaimp-parse.el index bf2a453f1a..1c1bdab517 100644 --- a/javaimp-parse.el +++ b/javaimp-parse.el @@ -36,7 +36,7 @@ (defconst javaimp-scope-all-types - '(anon-class + '(anon-class array-init class enum @@ -702,14 +702,12 @@ either of symbols `normal' or 'static'." class-alist))) (defun javaimp-parse-get-all-scopes (&optional beg end pred no-filter) - "Return all scopes in the current buffer between positions BEG -and END, both exclusive, optionally filtering them with PRED. -PRED should not move point. Note that parents may be outside of -region given by BEG and END. BEG is the LIMIT argument to -`previous-single-property-change', and so may be nil. END -defaults to end of accessible portion of the buffer. - -The returned objects are copies, and so may be freely modified. + "Return copies of all scopes in the current buffer between +positions BEG and END, both exclusive, optionally filtering them +with PRED. PRED should not move point. Note that parents may be +outside of region given by BEG and END. BEG is the LIMIT +argument to `previous-single-property-change', and so may be nil. +END defaults to end of accessible portion of the buffer. Scope parents are filtered according to `javaimp-parse--scope-type-defun-p', but if NO-FILTER is non-nil @@ -733,21 +731,21 @@ then no filtering is done." (push scope res))) res)) -(defun javaimp-parse-get-enclosing-scope (&optional pred) - "Return innermost enclosing scope at point. If PRED is non-nil -then the scope must satisfy it, otherwise the next outer scope is -tried. - -The returned objects are copies, and so may be freely modified. +(defun javaimp-parse-get-enclosing-scope (&optional pred no-filter) + "Return copy of innermost enclosing scope at point. If PRED is +non-nil then the scope must satisfy it, otherwise the next outer +scope is tried. Scope parents are filtered according to -`javaimp-parse--scope-type-defun-p'." +`javaimp-parse--scope-type-defun-p', but if NO-FILTER is non-nil +then no filtering is done." (save-excursion (javaimp-parse--all-scopes)) (when-let ((scope (javaimp-parse--enclosing-scope pred))) (setq scope (javaimp-scope-copy scope)) - (javaimp-scope-filter-parents - #'javaimp-parse--scope-type-defun-p scope) + (unless no-filter + (javaimp-scope-filter-parents + #'javaimp-parse--scope-type-defun-p scope)) scope)) (defun javaimp-parse-get-defun-decl-start (&optional bound) diff --git a/javaimp.el b/javaimp.el index 2480744bd4..2522751575 100644 --- a/javaimp.el +++ b/javaimp.el @@ -1071,8 +1071,10 @@ buffer." (< target-idx 0) (>= target-idx (length siblings))) (if parent-start - ;; Move up to parent, trying to also skip its decl - ;; prefix + ;; It's not very clear what to do when we need to move + ;; out of current scope. Currently we just move up to + ;; parent, trying to also skip its decl prefix. This + ;; gives acceptable results. (goto-char (or (javaimp--beg-of-defun-decl parent-start) parent-start)) (goto-char (if (> arg 0) @@ -1182,6 +1184,25 @@ PREV-INDEX gives the index of the method itself." -1)) siblings))))) +(defun javaimp-jump-to-enclosing-scope () + "Jump to enclosing scope at point." + (interactive) + (if-let ((scope (save-excursion + (save-restriction + (widen) + (javaimp-parse-get-enclosing-scope #'always t))))) + (progn + (goto-char (or (and (javaimp-scope-type scope) + (not (memq (javaimp-scope-type scope) + '(array-init simple-statement statement))) + (javaimp--beg-of-defun-decl + (javaimp-scope-start scope))) + (javaimp-scope-start scope))) + (message "%s %s at position %d" + (javaimp-scope-type scope) (javaimp-scope-name scope) + (javaimp-scope-start scope))) + (user-error "There is no enclosing scope at point"))) + (defun javaimp-add-log-current-defun () "Function to be used as `add-log-current-defun-function'." @@ -1208,6 +1229,7 @@ PREV-INDEX gives the index of the method itself." ;; Main (defvar-keymap javaimp-basic-map + "e" #'javaimp-jump-to-enclosing-scope "i" #'javaimp-add-import "o" #'javaimp-organize-imports "s" #'javaimp-show-scopes)