-
Notifications
You must be signed in to change notification settings - Fork 138
Add code highlighting based on absolute positions #2584
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 code highlighting based on absolute positions #2584
Conversation
groups.pop() was called without considering the number of capturing groups within. This caused some issues with the end bound of word matching being popped and removed from the array.
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.
Looking at the use case of highlighting, it seems that the reason why this feature was developed this way was because of the usecase of highlighting code blocks, which are appropriately indented. (Given that the documentation is under the code formatting section).
Additionally, to capture whitespace from front of line, it could be better to add symbol in front rather than the back?
* @returns {[number, number]} The actual bound computed | ||
*/ | ||
static computeCharBounds(bound: [number, number], line: string): [number, number] { | ||
static computeCharBounds(bound: [number, number], line: string, | ||
highlightSpaces: boolean): [number, number] { |
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.
isHighlightSpaces
instead of highlightSpaces for boolean name choice would be better.
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.
Good catch! Thanks
Good idea, I'll try moving the symbol to the front, such as an underscore like "_1[2:4]" to make it more intuitive |
Sorry, would something like
Does anyone else have any suggestions? |
Thanks @AgentHagu for this PR, and others for comments. |
Thanks for the feedback! Just to clarify, do you mean having char-bound highlighting ignore the indentation level by default? So the spaces within the indents are now automatically considered in the char bounds? |
Oh, so that's why spaces were not counted before! I didn't realise. So, the current counting is relative to the first non-space char, by default? |
Yep, by default the indentation level is checked and accounted for and the index counting is relative to the first non-whitespace char.
To clarify, do you mean introducing something like |
@AgentHagu I was thinking of something like |
I see. In that case, I believe the implementation should already support absolute char counting, so only the syntax and PR description needs to be updated |
Just so we don't forget later, this is a user-visible change, which means UG needs to be updated as well. But you can wait till the feature behaviour is finalised before adding the UG update to the PR. |
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.
Thanks for the PR, @AgentHagu!
Revisiting a discussion point that we brought up earlier - in the cases of other kinds of whitespaces being present (e.g. tab or newline), how do we handle it? E.g. I can see a situation where the user copy-pastes in an existing chunk of code with whitespace, and non-space whitespace characters are added in directly. This may just be a documentation problem though given that this is not highly likely & usually these are converted to whitespaces in many IDEs etc, what do you think? Or is there a tweak we can do that will efficiently cover these cases?
const lineNumber = HighlightRuleComponent | ||
.isValidLineNumber(groups.shift() ?? '', 1, lines.length, lineNumberOffset); | ||
if (!lineNumber) return null; | ||
|
||
const isUnbounded = groups.every(x => x === ''); | ||
|
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.
}); | ||
|
||
test('handles line-length end correctly', () => { | ||
const bounds = HighlightRuleComponent.computeCharBounds([0, 4], ' abcd', true); |
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.
Why abcd
here in particular? (Let me know if it's a non-issue)
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.
There's no underlying reason for abcd
, I can update the test-case to follow the other test-cases' string input of ' some text'
.
Thanks for the feedback! After investigating, it seems that tabs are treated as 1 "character" by the current code, leading to weird highlighting such as the following: My current idea is to see whether it's possible to convert such tabs to the corresponding number of whitespace, which should remedy the issue automatically. What do you think of this solution? |
That would make sense - we'll just need to specify this behavior in the documentation :) |
@AgentHagu Note that a tab may be considered a different number of spaces by different tools: some use 2 spaces, some use 4, and some use 8 (e.g., GitHub). But yes, we can pick one (e.g., 4) and keep the users informed about it. |
@AgentHagu What is the current status of this PR? |
@lhw-1 Sorry for the lack of updates, currently fixing some issues with the tab conversion. The user guide has been updated to reflect the new behavior as well. |
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #2584 +/- ##
=======================================
Coverage 51.86% 51.87%
=======================================
Files 127 127
Lines 5474 5473 -1
Branches 1201 1201
=======================================
Hits 2839 2839
+ Misses 2340 2339 -1
Partials 295 295 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Upon initial investigations, it seems that as of the current Code with highlighting to indicate spaces or tabs If the consensus is that tabs should instead be 4 spaces wide instead of 8, this PR will also target this issue with its auto-conversion of tabs to 4 spaces wide. The UG has also been updated to indicate this change. However, it is possible that existing MarkBind sites that use tabs in their fenced code will be affected by this change, since visually the tabs will be cut in half. To avoid breaking existing sites, we could also choose to retain the 8 space wide tabs for backward compatibility. What does everyone think? |
@damithc in terms of the tab conversion to 4 instead of 8, would this comment still hold?
Given that this is a visual change, and the only functionality (that I can think of at the moment) that might be affected by tab size is the highlight feature being merged in with this PR, I think it should be fine to make this change? I don't think the 8 spaces was an intentional implementation (given that 2 or 4 is the usual standard). |
@lhw-1 Yes, it should be fine. I don't use tabs in my code anyway. So, none of my MarkBind sites will be affected. |
@AgentHagu What is the status of this PR? |
@lhw-1 The PR should be finished, accomplishing the original task and adding auto conversion of tabs to 4 spaces. |
Once the PR is tested and good to go, we can merge it in. Thanks @AgentHagu! |
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.
LGTM! Works on testing. Thanks @AgentHagu for the PR!
@lhw-1 Each PR must have a SEMVER impact label, please remember to label the PR properly. |
@all-contributors please add @AgentHagu for code |
I've put up a pull request to add @AgentHagu! 🎉 |
@all-contributors please add @AgentHagu for code |
@AgentHagu already contributed before to code |
What is the purpose of this pull request?
Overview of changes:
This PR closes #2573. In summary, it allows users to now optionally highlight leading whitespace, giving them more options when using MarkBind.
Users can now add an optional
+
before their char bound specifier to indicate that the index should be considered as its absolute value, i.e. inclusive of leading whitespace/indentation. For example, previously1[:4]
would highlight theabcd
inabcd
. But now, by using+1[:4]
, MarkBind will highlightab
inabcd
(i.e. it will also highlight the first 2 spaces in the string since it considers the end bound of 4 as its absolute value, rather than relative to the indentation level).Anything you'd like to highlight/discuss:
This PR also adds automatic conversion of tabs to 4 spaces by default. The User Guide has been updated to reflect the changes.
Testing instructions:
A test similar to the example brought up in #2573 can be used to test the new functionality. For example:
abcd
should be highlighted, i.e. exclusive of the 2 whitespaces in the beginning.Now,
ab
should be highlighted, since the "+" symbol was added, so the absolute value of the bounds is considered in the bounds calculation.Proposed commit message: (wrap lines at 72 characters)
Add optional absolute char indexing for highlighting
Currently, there is no way to include highlighting of leading
whitespace when using the
highlight-lines
attribute. Providing suchan option may be beneficial to users, giving them more freedom
when using MarkBind.
Checklist: ☑️
Reviewer checklist:
Indicate the SEMVER impact of the PR:
At the end of the review, please label the PR with the appropriate label:
r.Major
,r.Minor
,r.Patch
.Breaking change release note preparation (if applicable):