-
Notifications
You must be signed in to change notification settings - Fork 63
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 new function magithub-browse-file #373
Closed
Closed
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
dba4ebe
Add new function magithub-browse-file
manateelazycat aac9e82
Make magithub-browse-file support file line number and dired mode.
manateelazycat d8998d9
Improve code of magithub-browse-file with vermiculus suggestions
manateelazycat dac7894
Use let-alist instead alist-get
manateelazycat 8737949
If if-let and use (magit-git-string "rev-parse" "--abbrev-ref" "HEAD"…
manateelazycat 946c331
Fix with vermiculus suggestions
manateelazycat 5a95627
Support file and line argument now.
manateelazycat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -89,6 +89,33 @@ | |
(user-error "Not a GitHub repository")) | ||
(magithub-repo-visit (magithub-repo))) | ||
|
||
(defun magithub-browse-file () | ||
"Open the git file in your browser. | ||
|
||
Jump to file with line number if you in buffer of git file. | ||
Jump to file if you in dired mode. | ||
Jump to parent directory if you not in buffer with non-dired mode." | ||
(interactive) | ||
(unless (magithub-github-repository-p) | ||
(user-error "Not a GitHub repository")) | ||
(let* ((github-branch-path (let-alist (magithub-repo) | ||
(format "%s/%s/%s/" | ||
.html_url | ||
"blob" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can incorporate this literal value directly into the format string There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand how to improve this line with your suggestion. ;) |
||
(magit-git-string "rev-parse" "HEAD")))) | ||
(file-relative-path (string-remove-prefix | ||
(magit-toplevel) | ||
(expand-file-name | ||
(if-let ((filepath (buffer-file-name))) | ||
;; Get file relative path and line number if `buffer-file-name' is non-nil. | ||
(format "%s#L%s" filepath (line-number-at-pos)) | ||
(if (derived-mode-p 'dired-mode) | ||
;; Get file relative path if in `dired-mode' | ||
(dired-file-name-at-point) | ||
;; Otherwise, get current directory as relative path | ||
default-directory)))))) | ||
(browse-url (concat github-branch-path file-relative-path)))) | ||
|
||
(defvar magithub-after-create-messages | ||
'("Don't be shy!" | ||
"Don't let your dreams be dreams!") | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
(interactive)
form can give us a value for a formal parameter when used interactively. That way,magithub-browse-file
could take a file directly (e.g.,(magithub-browse-file "some/path/relative/to/topdir.txt")
).Your interactive form could look like this (untested):
Note this moves a lot of the logic out of the main body of the function -- logic that's for interactive use only, anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or better yet, split out the arguments into FILE and LINE.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, it's ugly that put everything in interactive, I will write new version to handle file and line argument.