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

Allow indexing from end #2517

Closed
XtremeOwnageDotCom opened this issue Oct 24, 2024 · 3 comments · Fixed by #2552
Closed

Allow indexing from end #2517

XtremeOwnageDotCom opened this issue Oct 24, 2024 · 3 comments · Fixed by #2552
Labels
S: triage Issue needs triage. T: feature Feature.

Comments

@XtremeOwnageDotCom
Copy link

Description

In the current state, when including specific line numbers, we can target specific lines.

  1. To set starting line number: myfile:3
    • This will include the entire file STARTING at line number 3.
  2. For lines up to a specific line: myfile::10
    • This will include lines 1 - 10
  3. For a range of lines: myfile:4:12
    • This will include lines 4 - 12.
  4. To include a SPECIFIC line: myfile:3:3
    • This will only include line number 3

BUT, what if we wanted to include the last 10 lines of the file. There currently is not a way to index from the end of the file.

Benefits

Easy feature, which adds more utility, for potentially niche use-cases.

Solution Idea

Add the ability to do negative indexing:

Given, a file that is 100 lines long-

  1. To set starting line number: myfile:-3
    • This will include lines 97-100
  2. For lines up to a specific line: myfile::-51
    • This will include lines 1 - 49
  3. For a range of lines: myfile:4:-25
    • This will include lines 4 - 75.
  4. To include a SPECIFIC line: myfile:-0:-0
    • This will only include the very last line.
@gir-bot gir-bot added the S: triage Issue needs triage. label Oct 24, 2024
@XtremeOwnageDotCom
Copy link
Author

@facelessuser
Copy link
Owner

Just noting that I've seen this and will be considering it. I don't recall if there are any complications involved here, but generally, it feels like a reasonable request, assuming the logic fits well with what we are doing.

@facelessuser
Copy link
Owner

Solution Idea

Add the ability to do negative indexing:

Given, a file that is 100 lines long-

  1. To set starting line number: myfile:-3

    • This will include lines 97-100
  2. For lines up to a specific line: myfile::-51

    • This will include lines 1 - 49
  3. For a range of lines: myfile:4:-25

    • This will include lines 4 - 75.
  4. To include a SPECIFIC line: myfile:-0:-0

    • This will only include the very last line.

Some comments on the proposal.

  • Negative indexes will be converted from negative indexes to positive indexes based on the total number of lines. If you have a 100 lines, -1 is equivalent to line 100, etc.

  • Negative zero has no significance. Lines increment from 1 to the end, so snippets reference lines with that assumption. When a user specifies zero in Snippets, it is currently clamped to 1 just for sanity, that will not change for a negative -0. As far as snippets is concerned, zero is zero, and you should not use zero, but if you do, it will be clamped to 1. So don't use zero.

  • Snippets, previously, did not account for trailing, empty newlines in snippets. This will be rectified to help avoid confusion when viewing a file in an editor and trying to decide what the last line actually is.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S: triage Issue needs triage. T: feature Feature.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants