Github - link to function in source
Asked Answered
S

3

36

I know I can have anchors to a certain line, but if the source changes that line might become irrelevant. Example:

...source.php#L33 < line 33 may become line 40 later :(

Is there some way to tell GitHub to link to a certain function or property from the source, without specifying the line?

(The source is written in PHP code.)

Sinhalese answered 13/8, 2013 at 23:0 Comment(2)
It might be worth raising a ticket for this, it'd be very useful - I would imagine Github have a way of accepting feature suggestions. They'd have to implement a language parser to understand function/method definitions (and they would probably wish to add several, to remain language agnostic) so it's likely not a trivial undertaking :).Orianna
I solved this by linking to the file instead the line of code. At least with this method you can route where's defined that function.Bottoms
R
22

AFAIK, no, but you can link to a certain revision of the file, and thus, you can know for sure that line will always point at the start of the function.

The URL looks like /{user}/{repo}/blob/{hash}/{file}#L{line}

To get it, click "Commits", select the last commit, click "Browse code", and find your file and line as usual.

documentation

Ravenna answered 13/8, 2013 at 23:11 Comment(0)
S
14

Yes you can. Newer browsers support highlighting a portion of text and scrolling to it.

Example: common.c#:~:text=static%20ssize_t%20led_current_store

All you have to do is to add this to the end of your URL:

#:~:text=function_name

Some browsers do not support this, though: https://caniuse.com/url-scroll-to-text-fragment

Enjoy!

Strafford answered 14/1, 2022 at 12:4 Comment(1)
Good solution. But it did not work when I clicked the link in readme - GitHub loads it dynamically in the same tab. But works when opening in a new tab.Dromond
K
0

https://github.com/orgs/community/discussions/131838 is about this. Github parses most languages (to highlight syntaxes.) Asof 6/2/2024, https://github.com/ parses C++ (presume parses most languages); if you click "listsIntersect", the symbols panel launches, with "listsIntersect" defined as a function: https://github.com/SwuduSusuwu/SubStack/blob/trunk/cxx/ClassResultList.hxx#L42 But has no option to link to this. Lots of commits change the line numbers, thus line numbers ("#LX") do not have much uses. https://github.com/SwuduSusuwu/SubStack/blob/trunk/cxx/ClassResultList.hxx#:~:text=listsIntersect does not work (#:~:text=listsIntersect is ignored). Response to https://bugzilla.mozilla.org/show_bug.cgi?id=1907336 is to set dom.text_fragments.enabled to true in about:config. This fixes Firefox's #:~:text, but, as the response says; "For better or worse, Github seems to disable text fragments (ie., highlighting the text fragment) for code blocks. I can see in Firefox Nightly, Chrome and Safari, that the target text testHarnesses gets scrolled into view, but it is not highlighted. If you however set the target text to something outside of the code box (I tried it with :~:text=Files, it gets highlighted and scrolled in all browser engines. (Also, Github seems to do some client-side redirecting which removes the text fragment from the urlbar, so that they are gone after a refresh. Again, happening on all browsers.)"

Kurt answered 13/6, 2024 at 16:40 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.