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
Changes from 1 commit
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
35 changes: 16 additions & 19 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,28 +3727,25 @@ 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 ()
(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)))
(if (file-name-absolute-p file-path)
(find-file file-path)
(let* ((parts (split-string (buffer-file-name) "/"))
(target-filename "")
(target-exists nil))

(dolist (n (number-sequence 1 (length parts)) target-filename)
(let* ((new-filename (string-join (append (-take n parts) (list file-path)) "/")))
(if (file-exists-p new-filename)
(setq target-filename new-filename
target-exists t))))

(if target-exists
(find-file target-filename)
(evil-find-file-at-point-with-line))))))
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.
Expand Down