-
Notifications
You must be signed in to change notification settings - Fork 31
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 highlight component #237
Open
nkanderson
wants to merge
1
commit into
rust-dev-tools:master
Choose a base branch
from
nkanderson:highlight_component
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Copyright 2018 The Rustw Project Developers. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
import React from 'react'; | ||
|
||
export const Highlight = (props) => { | ||
const src_line_prefix = 'src_line_'; | ||
const highlight = props.highlight; | ||
const is_valid_highlight = validate_highlight(highlight); | ||
|
||
if (!is_valid_highlight) { | ||
return null; | ||
} | ||
|
||
const lhs = (highlight.column_start - 1); | ||
const rhs = (highlight.column_end - 1); | ||
const highlight_specs = make_highlight(src_line_prefix, highlight.line_start, lhs, rhs); | ||
if (highlight_specs) { | ||
const { top, left, width } = highlight_specs; | ||
const style = { | ||
top: top, | ||
left: left, | ||
width: width, | ||
} | ||
return <div className="selected floating_highlight" key={highlight.line_start} style={style}> </div>; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
function make_highlight(src_line_prefix, line_number, left, right) { | ||
const line_div = $("#" + src_line_prefix + line_number); | ||
|
||
// TODO: get adjust variable as prop through diffIndent in FileResult | ||
// if Highlight component is to be used in the SearchResults component | ||
// const adjust = line_div.data('adjust'); | ||
// if (adjust) { | ||
// left -= adjust; | ||
// right -= adjust; | ||
// } | ||
|
||
left *= CHAR_WIDTH; | ||
right *= CHAR_WIDTH; | ||
if (right === 0) { | ||
right = line_div.width(); | ||
} | ||
|
||
let width = right - left; | ||
const paddingLeft = parseInt(line_div.css("padding-left")); | ||
const paddingTop = parseInt(line_div.css("padding-top")); | ||
if (left >= 0) { | ||
left -= paddingLeft; | ||
} else { | ||
width += paddingLeft; | ||
} | ||
|
||
const position = line_div.position(); | ||
if (position) { | ||
position.left += left; | ||
position.top += paddingTop; | ||
return { top: position.top, left: position.left, width }; | ||
} | ||
// If no position, don't render the highlight | ||
return null; | ||
} | ||
|
||
// TODO: this could maybe be validated in app.js, at srcHighlight declaration | ||
function validate_highlight(highlight) { | ||
const required_keys = ['line_start', 'line_end', 'column_start', 'column_end']; | ||
const has_keys = required_keys.reduce((acc, k) => { | ||
return acc && highlight[k] !== undefined; | ||
}, true); | ||
|
||
if (!has_keys || highlight.column_start <= 0) { | ||
return false; | ||
} | ||
return true; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@nrc since the position is now calculated differently, I adjusted the conditional for the
left
padding subtraction to check forleft >=0
rather thanleft > 0
. Do you know if there would ever be a case whereleft
is less than zero, or could I remove theelse
here?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.
We call it here: https://github.com/nrc/cargo-src/pull/237/files/ac2f86ab6bc765177f3e12af715b890c5de80476#diff-4018adfb8de39d7a30dd31dd44e3b4cdR22 and lhs is highlight.column - 1, so I think that should always be greater than 0 since the column should be 1-indexed, but we might want to check that it really is 1-indexed, not 0-indexed, and that seems like an easy mistake to make so it might be worth asserting that highlight.column >= 1 there.
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.
I found a case when testing a full-line highlight where
lhs
ends up being 0, and without changing theleft > 0
(as it is inutils.js
) toleft >= 0
, the highlight calculation is off slightly. But it sounds like we could just adjust thevalidate_highlight
function to ensurehighlight.column_start
is not <= 1 rather than 0, as it is currently?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.
@nrc Ok, I'm re-thinking my previous comment - I think the
validate_highlight
logic can be left as-is, but the conditional adjustment inmake_highlight
could be reduced toleft -= paddingLeft
, with no check forleft >= 0
or anyelse
clause. At least that's working for the test cases I've been running through.