Skip to content

Commit

Permalink
live preview: Fix thinko leading to wrong elements getting selected
Browse files Browse the repository at this point in the history
The `SelectedElement` does not need a `debug_index` as the interesting node
is fully specified by the source location it already contains.
  • Loading branch information
hunger committed Feb 28, 2024
1 parent 6ab0a30 commit e668a0a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
9 changes: 1 addition & 8 deletions tools/lsp/preview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ fn drop_component(
element_selection::select_element_at_source_code_position(
path,
drop_data.selection_offset,
drop_data.debug_index,
is_layout,
None,
true,
Expand Down Expand Up @@ -421,7 +420,6 @@ pub fn load_preview(preview_component: PreviewComponent) {
element_selection::select_element_at_source_code_position(
se.path.clone(),
se.offset,
se.debug_index,
se.is_layout,
None,
false,
Expand Down Expand Up @@ -558,12 +556,7 @@ pub fn highlight(url: Option<Url>, offset: u32) {
let is_layout =
e.borrow().debug.get(debug_index).map_or(false, |(_, l)| l.is_some());
element_selection::select_element_at_source_code_position(
path,
offset,
debug_index,
is_layout,
None,
false,
path, offset, is_layout, None, false,
);
} else {
element_selection::unselect_element();
Expand Down
4 changes: 1 addition & 3 deletions tools/lsp/preview/drop_location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@ pub struct DropData {
/// The offset to select next. This is different from the insert position
/// due to indentation, etc.
pub selection_offset: u32,
/// The index into the `debug` vector of the target element.
pub debug_index: usize,
}

/// Find a location in a file that would be a good place to insert the new component at
Expand Down Expand Up @@ -152,6 +150,6 @@ pub fn drop_at(
import_path: if import_path.is_empty() { None } else { Some(import_path) },
insert_position: drop_info.insertion_position,
},
DropData { selection_offset, debug_index: drop_info.target_element_node.debug_index },
DropData { selection_offset },
))
}
21 changes: 15 additions & 6 deletions tools/lsp/preview/element_selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ impl ElementRcNode {
pub struct ElementSelection {
pub path: PathBuf,
pub offset: u32,
pub debug_index: usize,
pub instance_index: usize,
pub is_layout: bool,
}
Expand All @@ -74,6 +73,20 @@ impl ElementSelection {
let elements = component_instance.element_at_source_code_position(&self.path, self.offset);
elements.get(self.instance_index).or_else(|| elements.first()).cloned()
}

pub fn as_element_node(&self) -> Option<ElementRcNode> {
let element = self.as_element()?;

let debug_index = {
let e = element.borrow();
e.debug.iter().position(|(n, _)| {
n.source_file.path() == &self.path
&& u32::from(n.text_range().start()) == self.offset
})
};

debug_index.map(|i| ElementRcNode { element, debug_index: i })
}
}

// Look at an element and if it is a sub component, jump to its root_element()
Expand Down Expand Up @@ -124,7 +137,6 @@ pub fn unselect_element() {
pub fn select_element_at_source_code_position(
path: PathBuf,
offset: u32,
debug_index: usize,
is_layout: bool,
position: Option<LogicalPoint>,
notify_editor_about_selection_after_update: bool,
Expand All @@ -136,7 +148,6 @@ pub fn select_element_at_source_code_position(
&component_instance,
path,
offset,
debug_index,
is_layout,
position,
notify_editor_about_selection_after_update,
Expand All @@ -147,7 +158,6 @@ fn select_element_at_source_code_position_impl(
component_instance: &ComponentInstance,
path: PathBuf,
offset: u32,
debug_index: usize,
is_layout: bool,
position: Option<LogicalPoint>,
notify_editor_about_selection_after_update: bool,
Expand All @@ -161,7 +171,7 @@ fn select_element_at_source_code_position_impl(
.unwrap_or_default();

super::set_selected_element(
Some(ElementSelection { path, offset, debug_index, instance_index, is_layout }),
Some(ElementSelection { path, offset, instance_index, is_layout }),
positions,
notify_editor_about_selection_after_update,
);
Expand All @@ -178,7 +188,6 @@ fn select_element_node(
component_instance,
path,
offset,
selected_element.debug_index,
selected_element
.element
.borrow()
Expand Down

0 comments on commit e668a0a

Please sign in to comment.