interpreter: Add highlighting feature-gated functions

The preview can not leave it up to the interpreter to handle element selection
and highlighting. So add new functions to the interpreter (behind the
"highlight" feature-gate) to query positions of elements.

This exposes some of the code that is used by the existing highlighting code
and extends it where needed.

Two use-cases need to be covered:

1. Query the positions of a component (given by source file path and offset).
   This is then used to highlight all occurences of a component as the
   cursor position in a source file changes.

2. Query the position of an element (given as `ElementRc`).
   This is used when selecting elements in the UI. We need to work at
   the element level for this, not at the component level.

Also make the `highlight` module public but feature-gated, so that we
can put helper-types there.
This commit is contained in:
Tobias Hunger 2023-12-19 17:11:29 +01:00 committed by Tobias Hunger
parent 2ce161ced8
commit 9142336d5c
5 changed files with 174 additions and 45 deletions

View file

@ -5,6 +5,8 @@ use std::io::Read;
use std::path::{Path, PathBuf};
use std::rc::Rc;
use crate::parser::{TextRange, TextSize};
/// Span represent an error location within a file.
///
/// Currently, it is just an offset in byte within the file.
@ -112,6 +114,25 @@ impl SourceFileInner {
)
}
pub fn text_range_to_file_line_column(
&self,
range: TextRange,
) -> (String, usize, usize, usize, usize) {
let file_name = self.path().to_string_lossy().to_string();
let (start_line, start_column) = self.line_column(range.start().into());
let (end_line, end_column) = self.line_column(range.end().into());
(file_name, start_line, start_column, end_line, end_column)
}
pub fn text_size_to_file_line_column(
&self,
size: TextSize,
) -> (String, usize, usize, usize, usize) {
let file_name = self.path().to_string_lossy().to_string();
let (start_line, start_column) = self.line_column(size.into());
(file_name, start_line, start_column, start_line, start_column)
}
/// Returns the offset that corresponds to the line/column
pub fn offset(&self, line: usize, column: usize) -> usize {
let col_offset = column.saturating_sub(1);

View file

@ -825,6 +825,7 @@ pub struct RepeatedElementInfo {
}
pub type ElementRc = Rc<RefCell<Element>>;
pub type ElementWeak = Weak<RefCell<Element>>;
impl Element {
pub fn make_rc(self) -> ElementRc {