emgithub icon indicating copy to clipboard operation
emgithub copied to clipboard

[Question] Use markers instead of line numbers

Open marcinkunert opened this issue 2 years ago • 1 comments

Is there a way of embedding code using some kind of markers instead of line numbers?

Let's say I have this file:

import sth
import sth not important

fun myFun() {
 stuff here
 stuff here
}

I've embedded the myFun() part: L4-L7

fun myFun() {
 stuff here
 stuff here
}

Afterward I've noticed one of the imports is not important, so I delete it from the code base,

import sth

fun myFun() {
 stuff here
 stuff here
}

but forget to update the embedded line numbers, so the embedded part looks like this:

 stuff here
 stuff here
}

I'm aware that instead of pointing to a branch, I can point to a commit/permalink, which won't change. But this is also a disadvantage, since the code samples would show something outdated.

I was thinking if there is a way to embed code using some kind of markers. I've tried googling it, but no luck so far. Maybe you could point me to something useful.

marcinkunert avatar Feb 28 '23 08:02 marcinkunert

The scene you describe is interesting. But sorry I can't find an elegant way to do it after googling either.

I believe there should be roughly two ways to implement the feature:

  • on the script side: specifing the contents of the start line and end line, instead of the line numbers. Say using the regex: start=fun myFun\(\) \{\n, end= stuff here\n\}.
  • on the code side: changing the original code by adding some special tokens in comments. For example,
// emgithub: start-next-line
fun myFun() { // or, emgithub: start-this-line
 stuff here
 stuff here
} // or, emgithub: end-this-line
// emgithub: end-last-line

Personally I prefer the second way. The first way may encounter some problems, say if there are multiple lines with the same contents. The second way is more precise in locating code, although we have to pollute the original code.

However both of them are somewhat dirty and may not be implemented in the main repo. You can try forking the repo and implement it to fit your specific needs.

Anyway I'll mark this as help-wanted. In case someone come up with an elegant way, we can do it in the main repo.

yusanshi avatar Feb 28 '23 10:02 yusanshi