get inlay hints only when editing

This commit is contained in:
Dongdong Zhou 2022-07-02 20:49:16 +01:00
parent fafa115fdc
commit 4999f003f3
4 changed files with 57 additions and 62 deletions

View File

@ -39,7 +39,10 @@
};
use serde::{Deserialize, Serialize};
use smallvec::SmallVec;
use xi_rope::{spans::Spans, Rope, RopeDelta};
use xi_rope::{
spans::{Spans, SpansBuilder},
Interval, Rope, RopeDelta,
};
use crate::{
command::{LapceUICommand, LAPCE_UI_COMMAND},
@ -610,9 +613,61 @@ pub fn update_history_styles(
}
}
pub fn get_inlay_hints(&self) {
if !self.loaded() {
return;
}
if !self.content().is_file() {
return;
}
if let BufferContent::File(path) = self.content() {
let tab_id = self.tab_id;
let path = path.clone();
let buffer_id = self.id();
let rev = self.rev();
let len = self.buffer().len();
let buffer = self.buffer().clone();
let event_sink = self.event_sink.clone();
self.proxy.get_inlay_hints(
buffer_id,
Box::new(move |result| {
if let Ok(res) = result {
if let Ok(resp) =
serde_json::from_value::<Vec<InlayHint>>(res)
{
let mut hints_span = SpansBuilder::new(len);
for hint in resp {
let offset = buffer
.offset_of_position(&hint.position)
.min(len);
hints_span.add_span(
Interval::new(offset, (offset + 1).min(len)),
hint.clone(),
);
}
let hints = hints_span.build();
let _ = event_sink.submit_command(
LAPCE_UI_COMMAND,
LapceUICommand::UpdateInlayHints {
path,
rev,
hints,
},
Target::Widget(tab_id),
);
}
}
}),
);
}
}
fn on_update(&mut self, delta: Option<&RopeDelta>) {
self.find.borrow_mut().unset();
*self.find_progress.borrow_mut() = FindProgress::Started;
self.get_inlay_hints();
self.clear_style_cache();
self.trigger_syntax_change(delta);
self.trigger_head_change();

View File

@ -44,7 +44,6 @@
use lsp_types::CompletionTextEdit;
use lsp_types::DocumentChangeOperation;
use lsp_types::DocumentChanges;
use lsp_types::InlayHint;
use lsp_types::OneOf;
use lsp_types::TextEdit;
use lsp_types::Url;
@ -60,8 +59,6 @@
use std::{collections::HashMap, sync::Arc};
use std::{iter::Iterator, path::PathBuf};
use std::{str::FromStr, time::Duration};
use xi_rope::spans::SpansBuilder;
use xi_rope::Interval;
use xi_rope::{RopeDelta, Transformer};
pub struct LapceUI {}
@ -153,62 +150,6 @@ fn inline_find(
}
}
pub fn get_inlay_hints(&self, ctx: &mut EventCtx) {
if !self.doc.loaded() {
return;
}
if !self.doc.content().is_file() {
return;
}
if !self.config.editor.enable_inlay_hints {
return;
}
if let BufferContent::File(path) = self.doc.content() {
// TODO: check if we already have inlay hints for this file?
let tab_id = self.doc.tab_id;
let path = path.clone();
let buffer_id = self.doc.id();
let rev = self.doc.rev();
let event_sink = ctx.get_external_handle();
let len = self.doc.buffer().len();
let buffer = self.doc.buffer().clone();
self.proxy.get_inlay_hints(
buffer_id,
Box::new(move |result| {
if let Ok(res) = result {
if let Ok(resp) =
serde_json::from_value::<Vec<InlayHint>>(res)
{
let mut hints_span = SpansBuilder::new(len);
for hint in resp {
let offset = buffer
.offset_of_position(&hint.position)
.min(len);
hints_span.add_span(
Interval::new(offset, (offset + 1).min(len)),
hint.clone(),
);
}
let hints = hints_span.build();
let _ = event_sink.submit_command(
LAPCE_UI_COMMAND,
LapceUICommand::UpdateInlayHints {
path,
rev,
hints,
},
Target::Widget(tab_id),
);
}
}
}),
);
}
}
pub fn get_code_actions(&self, ctx: &mut EventCtx) {
if !self.doc.loaded() {
return;

View File

@ -627,7 +627,6 @@ fn event(
editor_data.sync_buffer_position(
self.editor.widget().editor.widget().inner().offset(),
);
editor_data.get_inlay_hints(ctx);
editor_data.get_code_actions(ctx);
data.keypress = keypress.clone();

View File

@ -957,7 +957,7 @@ fn handle_event(
{
let editor_data =
data.editor_view_content(*view_id);
editor_data.get_inlay_hints(ctx);
editor_data.doc.get_inlay_hints();
}
for i in data
.progresses