Working underline based on symbol origin
This commit is contained in:
@@ -316,6 +316,7 @@ pub fn init(cx: &mut MutableAppContext) {
|
||||
cx.add_async_action(Editor::find_all_references);
|
||||
|
||||
hover_popover::init(cx);
|
||||
link_go_to_definition::init(cx);
|
||||
|
||||
workspace::register_project_item::<Editor>(cx);
|
||||
workspace::register_followable_item::<Editor>(cx);
|
||||
@@ -4603,9 +4604,13 @@ impl Editor {
|
||||
workspace.update(&mut cx, |workspace, cx| {
|
||||
let nav_history = workspace.active_pane().read(cx).nav_history().clone();
|
||||
for definition in definitions {
|
||||
let range = definition.range.to_offset(definition.buffer.read(cx));
|
||||
let range = definition
|
||||
.target
|
||||
.range
|
||||
.to_offset(definition.target.buffer.read(cx));
|
||||
|
||||
let target_editor_handle = workspace.open_project_item(definition.buffer, cx);
|
||||
let target_editor_handle =
|
||||
workspace.open_project_item(definition.target.buffer, cx);
|
||||
target_editor_handle.update(cx, |target_editor, cx| {
|
||||
// When selecting a definition in a different buffer, disable the nav history
|
||||
// to avoid creating a history entry at the previous cursor location.
|
||||
|
||||
@@ -8,6 +8,7 @@ use crate::{
|
||||
hover_popover::HoverAt,
|
||||
EditorStyle,
|
||||
};
|
||||
use crate::{hover_popover::HoverAt, link_go_to_definition::FetchDefinition};
|
||||
use clock::ReplicaId;
|
||||
use collections::{BTreeMap, HashMap};
|
||||
use gpui::{
|
||||
@@ -1417,7 +1418,7 @@ impl Element for EditorElement {
|
||||
cx,
|
||||
),
|
||||
Event::LeftMouseUp { position, .. } => self.mouse_up(*position, cx),
|
||||
Event::LeftMouseDragged { position } => {
|
||||
Event::LeftMouseDragged { position, .. } => {
|
||||
self.mouse_dragged(*position, layout, paint, cx)
|
||||
}
|
||||
Event::ScrollWheel {
|
||||
@@ -1426,9 +1427,26 @@ impl Element for EditorElement {
|
||||
precise,
|
||||
} => self.scroll(*position, *delta, *precise, layout, paint, cx),
|
||||
Event::KeyDown { input, .. } => self.key_down(input.as_deref(), cx),
|
||||
Event::MouseMoved { position, .. } => {
|
||||
Event::MouseMoved { position, cmd, .. } => {
|
||||
// This will be handled more correctly once https://github.com/zed-industries/zed/issues/1218 is completed
|
||||
// Don't trigger hover popover if mouse is hovering over context menu
|
||||
|
||||
let point = if paint.text_bounds.contains_point(*position) {
|
||||
let (point, overshoot) =
|
||||
paint.point_for_position(&self.snapshot(cx), layout, *position);
|
||||
if overshoot.is_zero() {
|
||||
Some(point)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
if *cmd {
|
||||
cx.dispatch_action(FetchDefinition { point });
|
||||
}
|
||||
|
||||
if paint
|
||||
.context_menu_bounds
|
||||
.map_or(false, |context_menu_bounds| {
|
||||
@@ -1445,18 +1463,6 @@ impl Element for EditorElement {
|
||||
return false;
|
||||
}
|
||||
|
||||
let point = if paint.text_bounds.contains_point(*position) {
|
||||
let (point, overshoot) =
|
||||
paint.point_for_position(&self.snapshot(cx), layout, *position);
|
||||
if overshoot.is_zero() {
|
||||
Some(point)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
cx.dispatch_action(HoverAt { point });
|
||||
true
|
||||
}
|
||||
|
||||
@@ -5,7 +5,9 @@ use std::{
|
||||
|
||||
use gpui::{
|
||||
actions,
|
||||
color::Color,
|
||||
elements::{Flex, MouseEventHandler, Padding, Text},
|
||||
fonts::{HighlightStyle, Underline},
|
||||
impl_internal_actions,
|
||||
platform::CursorStyle,
|
||||
Axis, Element, ElementBox, ModelHandle, MutableAppContext, RenderContext, Task, ViewContext,
|
||||
@@ -45,9 +47,135 @@ pub struct LinkGoToDefinitionState {
|
||||
|
||||
pub fn fetch_definition(
|
||||
editor: &mut Editor,
|
||||
FetchDefinition { point }: &FetchDefinition,
|
||||
&FetchDefinition { point }: &FetchDefinition,
|
||||
cx: &mut ViewContext<Editor>,
|
||||
) {
|
||||
if let Some(point) = point {
|
||||
show_link_definition(editor, point, cx);
|
||||
} else {
|
||||
//TODO: Also needs to be dispatched when cmd modifier is released
|
||||
hide_link_definition(editor, cx);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn show_link_definition(
|
||||
editor: &mut Editor,
|
||||
point: DisplayPoint,
|
||||
cx: &mut ViewContext<Editor>,
|
||||
) {
|
||||
if editor.pending_rename.is_some() {
|
||||
return;
|
||||
}
|
||||
|
||||
let snapshot = editor.snapshot(cx);
|
||||
let multibuffer_offset = point.to_offset(&snapshot.display_snapshot, Bias::Left);
|
||||
|
||||
let (buffer, buffer_position) = if let Some(output) = editor
|
||||
.buffer
|
||||
.read(cx)
|
||||
.text_anchor_for_position(multibuffer_offset, cx)
|
||||
{
|
||||
output
|
||||
} else {
|
||||
return;
|
||||
};
|
||||
|
||||
let excerpt_id = if let Some((excerpt_id, _, _)) = editor
|
||||
.buffer()
|
||||
.read(cx)
|
||||
.excerpt_containing(multibuffer_offset, cx)
|
||||
{
|
||||
excerpt_id
|
||||
} else {
|
||||
return;
|
||||
};
|
||||
|
||||
let project = if let Some(project) = editor.project.clone() {
|
||||
project
|
||||
} else {
|
||||
return;
|
||||
};
|
||||
|
||||
// Get input anchor
|
||||
let anchor = snapshot
|
||||
.buffer_snapshot
|
||||
.anchor_at(multibuffer_offset, Bias::Left);
|
||||
|
||||
// Don't request again if the location is the same as the previous request
|
||||
if let Some(triggered_from) = &editor.link_go_to_definition_state.triggered_from {
|
||||
if triggered_from
|
||||
.cmp(&anchor, &snapshot.buffer_snapshot)
|
||||
.is_eq()
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
let task = cx.spawn_weak(|this, mut cx| {
|
||||
async move {
|
||||
// query the LSP for definition info
|
||||
let definition_request = cx.update(|cx| {
|
||||
project.update(cx, |project, cx| {
|
||||
project.definition(&buffer, buffer_position.clone(), cx)
|
||||
})
|
||||
});
|
||||
|
||||
let origin_range = definition_request.await.ok().and_then(|definition_result| {
|
||||
definition_result
|
||||
.into_iter()
|
||||
.filter_map(|link| {
|
||||
link.origin.map(|origin| {
|
||||
let start = snapshot
|
||||
.buffer_snapshot
|
||||
.anchor_in_excerpt(excerpt_id.clone(), origin.range.start);
|
||||
let end = snapshot
|
||||
.buffer_snapshot
|
||||
.anchor_in_excerpt(excerpt_id.clone(), origin.range.end);
|
||||
|
||||
start..end
|
||||
})
|
||||
})
|
||||
.next()
|
||||
});
|
||||
|
||||
if let Some(this) = this.upgrade(&cx) {
|
||||
this.update(&mut cx, |this, cx| {
|
||||
if let Some(origin_range) = origin_range {
|
||||
this.highlight_text::<LinkGoToDefinitionState>(
|
||||
vec![origin_range],
|
||||
HighlightStyle {
|
||||
underline: Some(Underline {
|
||||
color: Some(Color::red()),
|
||||
thickness: 1.0.into(),
|
||||
squiggly: false,
|
||||
}),
|
||||
..Default::default()
|
||||
},
|
||||
cx,
|
||||
)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Ok::<_, anyhow::Error>(())
|
||||
}
|
||||
.log_err()
|
||||
});
|
||||
|
||||
editor.link_go_to_definition_state.task = Some(task);
|
||||
}
|
||||
|
||||
pub fn hide_link_definition(editor: &mut Editor, cx: &mut ViewContext<Editor>) {
|
||||
// only notify the context once
|
||||
if editor.link_go_to_definition_state.symbol_range.is_some() {
|
||||
editor.link_go_to_definition_state.symbol_range.take();
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
editor.link_go_to_definition_state.task = None;
|
||||
editor.link_go_to_definition_state.triggered_from = None;
|
||||
|
||||
editor.clear_text_highlights::<LinkGoToDefinitionState>(cx);
|
||||
}
|
||||
|
||||
pub fn go_to_fetched_definition(
|
||||
|
||||
Reference in New Issue
Block a user