-
Notifications
You must be signed in to change notification settings - Fork 133
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 optional absolute char indexing for highlighting #2584
base: master
Are you sure you want to change the base?
Add optional absolute char indexing for highlighting #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.
start += indents.length; | ||
// Clamp values | ||
if (start < indents.length) { | ||
start = indents.length; | ||
} else if (start > line.length) { | ||
start = line.length; | ||
} | ||
} else if (start > line.length) { | ||
start = line.length; | ||
} |
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 conditional logic is slightly convoluted for me here. If start !== UNBOUNDED
is true, and isHighlightSpaces
is true, and start > line.length
is false, what happens? (Will this even happen in theory?)
Also, given that Lines 129-130 and 132-133 are duplicated, I wonder if there is a way to simplify this chunk of code. We don't need to shy away from ternary conditional operators if they make the code easier to understand.
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.
If start !== UNBOUNDED is true, and isHighlightSpaces is true, and start > line.length is false, what happens? (Will this even happen in theory?)
In theory, it's possible for that to occur. For example, for some text
, start = 2 would be bounded and <= line.length and if isHighlightSpaces
is true, then that means we should leave the start value as is, since we wish to use the absolute value of those bounds.
Also, given that Lines 129-130 and 132-133 are duplicated, I wonder if there is a way to simplify this chunk of code. We don't need to shy away from ternary conditional operators if they make the code easier to understand.
Thanks for the feedback! I'll see if there's a way to simplify it more to improve readability.
}); | ||
|
||
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? |
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:
nil
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):