Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for relative path for gf #1917

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions evil-commands.el
tomdl89 marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I haven't tried your function yet (because it requires functions I don't have) but your diagram of accessing a/a.h from b/b.h doesn't seem to tally with the vim's documentation on path (by default, anyway). So I'm not sure if your diagram is wrong and your functionality is right, or the other way round, but that's worth checking too.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please specify which part of vim's documentation on path ? I checked vim doc, but found several entries on path.

I have tested the function on mac and linux, it worked. I don't have a windows os, but as far as I know, the different of windows and linux path is just the '/' and '' seperator. So after I add a logic to distinguish os type, it should work on windows too.

Original file line number Diff line number Diff line change
Expand Up @@ -3727,6 +3727,26 @@ Supports positions in the following formats: \"path:line path(line)\",
(when column-number
(move-to-column (1- column-number)))))))

(evil-define-command evil-open-file-under-cursor (file-path)
tomdl89 marked this conversation as resolved.
Show resolved Hide resolved
"Open the file under cursor and go to position if present.
Supports both relative path and absolute path. If path is relative, then find common
ancestor of path and current buffer file. If path is absolute path, go to the file
directly. Otherwise, fallback to `evil-find-file-at-point-with-line`."
(interactive
(let ((file-path (thing-at-point 'filename t)))
tomdl89 marked this conversation as resolved.
Show resolved Hide resolved
(list file-path)))

(if (file-name-absolute-p file-path)
(find-file file-path)

(if (and (stringp file-path)
tomdl89 marked this conversation as resolved.
Show resolved Hide resolved
(string-match-p "/" file-path))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not 100% sure on this, but have you checked if this check would work in windows? I believe they use backslashes over there.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have add a check on system-type to distinguish windows and other system.

(let ((dirname (locate-dominating-file default-directory file-path)))
(if (file-in-directory-p dirname (doom-project-root))
tomdl89 marked this conversation as resolved.
Show resolved Hide resolved
(find-file (doom-path dirname file-path))
tomdl89 marked this conversation as resolved.
Show resolved Hide resolved
(evil-find-file-at-point-with-line)))
(evil-find-file-at-point-with-line))))
tomdl89 marked this conversation as resolved.
Show resolved Hide resolved

(evil-define-command evil-find-file-at-point-visual ()
"Find the filename selected by the visual region.
Signal an error if the file does not exist."
Expand Down
6 changes: 3 additions & 3 deletions evil-maps.el
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@
(define-key evil-normal-state-map "gw" 'evil-fill)
(define-key evil-normal-state-map "gu" 'evil-downcase)
(define-key evil-normal-state-map "gU" 'evil-upcase)
(define-key evil-normal-state-map "gf" 'find-file-at-point)
(define-key evil-normal-state-map "]f" 'find-file-at-point)
(define-key evil-normal-state-map "[f" 'find-file-at-point)
(define-key evil-normal-state-map "gf" 'evil-open-file-under-cursor)
(define-key evil-normal-state-map "]f" 'evil-open-file-under-cursor)
(define-key evil-normal-state-map "[f" 'evil-open-file-under-cursor)
(define-key evil-normal-state-map "gF" 'evil-find-file-at-point-with-line)
(define-key evil-normal-state-map "]F" 'evil-find-file-at-point-with-line)
(define-key evil-normal-state-map "[F" 'evil-find-file-at-point-with-line)
Expand Down