Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ oxc_diagnostics = "0.111.0"
tree-sitter = "0.26.5"
tree-sitter-highlight = "0.26.3"
crossterm = "0.29"
lsp-types = "0.97"
lsp-types = { version = "0.97", features = ["proposed"] }
ts-rs = { version = "11.1", features = ["serde_json"] }
# Add more as needed during refactor

Expand Down
7 changes: 7 additions & 0 deletions crates/fresh-editor/plugins/config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"rainbow_brackets": true,
"quick_suggestions": true,
"quick_suggestions_delay_ms": 10,
"enable_ghost_text": true,
"suggest_on_trigger_characters": true,
"accept_suggestion_on_enter": "on",
"enable_inlay_hints": true,
Expand Down Expand Up @@ -308,6 +309,12 @@
"x-section": "Completion",
"default": 10
},
"enable_ghost_text": {
"description": "Show inline ghost text for the currently selected completion item.\nThis renders a dimmed suggestion directly in the buffer while the\ncompletion popup is open.\nDefault: true",
"type": "boolean",
"x-section": "Completion",
"default": true
},
"suggest_on_trigger_characters": {
"description": "Whether trigger characters (like `.`, `::`, `->`) immediately show completions.\nWhen true, typing a trigger character bypasses quick_suggestions_delay_ms.\nDefault: true",
"type": "boolean",
Expand Down
1 change: 1 addition & 0 deletions crates/fresh-editor/src/app/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,7 @@ impl Editor {
}
Action::LspCompletion => {
self.request_completion()?;
let _ = self.request_inline_completion_invoked();
}
Action::LspGotoDefinition => {
self.request_goto_definition()?;
Expand Down
286 changes: 281 additions & 5 deletions crates/fresh-editor/src/app/lsp_requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,118 @@
const SEMANTIC_TOKENS_FULL_DEBOUNCE_MS: u64 = 500;
const SEMANTIC_TOKENS_RANGE_DEBOUNCE_MS: u64 = 50;
const SEMANTIC_TOKENS_RANGE_PADDING_LINES: usize = 10;
const GHOST_TEXT_ID: &str = "ghost-text";

impl Editor {
/// Clear any active ghost text (inline completion) from the buffer.
pub(crate) fn clear_ghost_text(&mut self) {
if let Some(buffer_id) = self.ghost_text_buffer_id.take() {
if let Some(state) = self.buffers.get_mut(&buffer_id) {
state
.virtual_texts
.remove_by_id(&mut state.marker_list, GHOST_TEXT_ID);
}
}
}

/// Request inline completion (ghost text) with a specific trigger kind.
pub(crate) fn request_inline_completion_automatic(&mut self) -> AnyhowResult<()> {
self.request_inline_completion_with_trigger(
lsp_types::InlineCompletionTriggerKind::Automatic,
)
}

/// Request inline completion (ghost text) explicitly invoked by the user.
pub(crate) fn request_inline_completion_invoked(&mut self) -> AnyhowResult<()> {
self.request_inline_completion_with_trigger(lsp_types::InlineCompletionTriggerKind::Invoked)
}

fn request_inline_completion_with_trigger(
&mut self,
trigger_kind: lsp_types::InlineCompletionTriggerKind,
) -> AnyhowResult<()> {
if !self.config.editor.enable_ghost_text {
self.clear_ghost_text();
return Ok(());
}

self.clear_ghost_text();

let (buffer_id, line, character, language) = {

Check failure on line 66 in crates/fresh-editor/src/app/lsp_requests.rs

View workflow job for this annotation

GitHub Actions / clippy

the size for values of type `str` cannot be known at compilation time

error[E0277]: the size for values of type `str` cannot be known at compilation time --> crates/fresh-editor/src/app/lsp_requests.rs:66:42 | 66 | let (buffer_id, line, character, language) = { | ^^^^^^^^ doesn't have a size known at compile-time | = help: the trait `std::marker::Sized` is not implemented for `str` = note: all local variables must have a statically known size
let state = self.active_state();
if state.cursors.count() != 1 || !state.cursors.primary().collapsed() {
self.clear_ghost_text();
return Ok(());
}

let path = match state.buffer.file_path() {
Some(p) => p,
None => return Ok(()),
};

let language = match detect_language(path, &self.config.languages) {

Check failure on line 78 in crates/fresh-editor/src/app/lsp_requests.rs

View workflow job for this annotation

GitHub Actions / clippy

the size for values of type `str` cannot be known at compilation time

error[E0277]: the size for values of type `str` cannot be known at compilation time --> crates/fresh-editor/src/app/lsp_requests.rs:78:17 | 78 | let language = match detect_language(path, &self.config.languages) { | ^^^^^^^^ doesn't have a size known at compile-time | = help: the trait `std::marker::Sized` is not implemented for `str` = note: all local variables must have a statically known size

Check failure on line 78 in crates/fresh-editor/src/app/lsp_requests.rs

View workflow job for this annotation

GitHub Actions / clippy

cannot find function `detect_language` in this scope

error[E0425]: cannot find function `detect_language` in this scope --> crates/fresh-editor/src/app/lsp_requests.rs:78:34 | 78 | let language = match detect_language(path, &self.config.languages) { | ^^^^^^^^^^^^^^^ not found in this scope | help: consider importing this function | 13 + use crate::services::lsp::manager::detect_language; |
Some(lang) => lang,

Check failure on line 79 in crates/fresh-editor/src/app/lsp_requests.rs

View workflow job for this annotation

GitHub Actions / clippy

the size for values of type `str` cannot be known at compilation time

error[E0277]: the size for values of type `str` cannot be known at compilation time --> crates/fresh-editor/src/app/lsp_requests.rs:79:17 | 79 | Some(lang) => lang, | ^^^^^^^^^^ doesn't have a size known at compile-time | = help: the trait `std::marker::Sized` is not implemented for `str` note: required by a bound in `std::prelude::v1::Some` --> /rustc/ded5c06cf21d2b93bffd5d884aa6e96934ee4234/library/core/src/option.rs:607:5

Check failure on line 79 in crates/fresh-editor/src/app/lsp_requests.rs

View workflow job for this annotation

GitHub Actions / clippy

the size for values of type `str` cannot be known at compilation time

error[E0277]: the size for values of type `str` cannot be known at compilation time --> crates/fresh-editor/src/app/lsp_requests.rs:79:22 | 79 | Some(lang) => lang, | ^^^^ doesn't have a size known at compile-time | = help: the trait `std::marker::Sized` is not implemented for `str` = note: all local variables must have a statically known size
None => return Ok(()),
};

let cursor_pos = state.cursors.primary().position;
let (line, character) = state.buffer.position_to_lsp_position(cursor_pos);
(self.active_buffer(), line, character, language)
};

let inline_supported = self
.lsp
.as_ref()
.and_then(|lsp| lsp.inline_completion_support(&language));

if inline_supported == Some(false) {
self.clear_ghost_text();
return Ok(());
}

let request_id = self.next_lsp_request_id;

if let Some(pending_id) = self.pending_inline_completion_request.take() {
self.send_lsp_cancel_request(pending_id);
}

let sent = self
.with_lsp_for_buffer(buffer_id, |handle, uri, _language| {
let trigger_label =
if trigger_kind == lsp_types::InlineCompletionTriggerKind::Invoked {
"Invoked"
} else {
"Automatic"
};
let result = handle.inline_completion(
request_id,
uri.clone(),
line as u32,
character as u32,
trigger_kind,
None,
);
if result.is_ok() {
tracing::info!(
"Requested inline completion at {}:{}:{} ({})",
uri.as_str(),
line,
character,
trigger_label
);
}
result.is_ok()
})
.unwrap_or(false);

if sent {
self.next_lsp_request_id += 1;
self.pending_inline_completion_request = Some(request_id);
}

Ok(())
}

/// Handle LSP completion response
pub(crate) fn handle_completion_response(
&mut self,
Expand Down Expand Up @@ -169,6 +279,151 @@
Ok(())
}

/// Handle LSP inline completion response (textDocument/inlineCompletion)
pub(crate) fn handle_inline_completion_response(
&mut self,
request_id: u64,
items: Vec<lsp_types::InlineCompletionItem>,
) {
use crate::primitives::snippet::expand_snippet;
use crate::primitives::word_navigation::find_completion_word_start;
use crate::view::virtual_text::VirtualTextPosition;
use ratatui::style::{Modifier, Style};

if self.pending_inline_completion_request != Some(request_id) {
tracing::debug!(
"Ignoring inline completion response for outdated request {}",
request_id
);
return;
}

self.pending_inline_completion_request = None;

if !self.config.editor.enable_ghost_text {
self.clear_ghost_text();
return;
}

let (buffer_id, cursor_pos, cursor_count, cursor_collapsed, buffer_len, suggestion_item) = {
let state = self.active_state();
let cursor = state.cursors.primary();
(
self.active_buffer(),
cursor.position,
state.cursors.count(),
cursor.collapsed(),
state.buffer.len(),
items.into_iter().next(),
)
};

if cursor_count != 1 || !cursor_collapsed {
self.clear_ghost_text();
return;
}

let Some(item) = suggestion_item else {
self.clear_ghost_text();
return;
};

let lsp_types::InlineCompletionItem {
insert_text,
insert_text_format,
range,
..
} = item;

let mut suggestion = insert_text;
if insert_text_format == Some(lsp_types::InsertTextFormat::SNIPPET) {
suggestion = expand_snippet(&suggestion).text;
}

let suggestion = suggestion.lines().next().unwrap_or("").to_string();
if suggestion.is_empty() {
self.clear_ghost_text();
return;
}

let prefix = {
let state = self.active_state_mut();
if let Some(range) = range {
let start = state.buffer.line_col_to_position(
range.start.line as usize,
range.start.character as usize,
);
let end = state
.buffer
.line_col_to_position(range.end.line as usize, range.end.character as usize);
if cursor_pos >= start && cursor_pos <= end && start < cursor_pos {
state.get_text_range(start, cursor_pos)
} else {
let word_start = find_completion_word_start(&state.buffer, cursor_pos);
if word_start < cursor_pos {
state.get_text_range(word_start, cursor_pos)
} else {
String::new()
}
}
} else {
let word_start = find_completion_word_start(&state.buffer, cursor_pos);
if word_start < cursor_pos {
state.get_text_range(word_start, cursor_pos)
} else {
String::new()
}
}
};

let prefix_lower = prefix.to_lowercase();
let suggestion_lower = suggestion.to_lowercase();
let prefix_char_count = prefix.chars().count();

let suffix = if prefix.is_empty() {
suggestion
} else if suggestion.starts_with(&prefix) || suggestion_lower.starts_with(&prefix_lower) {
let byte_idx = suggestion
.char_indices()
.nth(prefix_char_count)
.map(|(idx, _)| idx)
.unwrap_or(suggestion.len());
suggestion[byte_idx..].to_string()
} else {
String::new()
};

if suffix.is_empty() || buffer_len == 0 {
self.clear_ghost_text();
return;
}

let (anchor_pos, position) = if cursor_pos >= buffer_len {
(buffer_len.saturating_sub(1), VirtualTextPosition::AfterChar)
} else {
(cursor_pos, VirtualTextPosition::BeforeChar)
};

let style = Style::default()
.fg(self.theme.line_number_fg)
.add_modifier(Modifier::DIM);

self.clear_ghost_text();
if let Some(state) = self.buffers.get_mut(&buffer_id) {
state.virtual_texts.add_with_id_and_padding(
&mut state.marker_list,
anchor_pos,
suffix,
style,
position,
100,
GHOST_TEXT_ID.to_string(),
false,
);
self.ghost_text_buffer_id = Some(buffer_id);
}
}

/// Handle LSP go-to-definition response
pub(crate) fn handle_goto_definition_response(
&mut self,
Expand Down Expand Up @@ -277,7 +532,9 @@

/// Check if there are any pending LSP requests
pub fn has_pending_lsp_requests(&self) -> bool {
self.pending_completion_request.is_some() || self.pending_goto_definition_request.is_some()
self.pending_completion_request.is_some()
|| self.pending_inline_completion_request.is_some()
|| self.pending_goto_definition_request.is_some()
}

/// Cancel any pending LSP requests
Expand All @@ -290,6 +547,13 @@
self.send_lsp_cancel_request(request_id);
self.lsp_status.clear();
}
if let Some(request_id) = self.pending_inline_completion_request.take() {
tracing::debug!(
"Canceling pending LSP inline completion request {}",
request_id
);
self.send_lsp_cancel_request(request_id);
}
if let Some(request_id) = self.pending_goto_definition_request.take() {
tracing::debug!(
"Canceling pending LSP goto-definition request {}",
Expand All @@ -299,6 +563,8 @@
self.send_lsp_cancel_request(request_id);
self.lsp_status.clear();
}

self.clear_ghost_text();
}

/// Send a cancel request to the LSP server for a specific request ID
Expand Down Expand Up @@ -467,6 +733,7 @@
// Cancel any pending scheduled trigger
self.scheduled_completion_trigger = None;
let _ = self.request_completion();
let _ = self.request_inline_completion_automatic();
return;
}

Expand Down Expand Up @@ -766,8 +1033,11 @@
use crate::view::virtual_text::VirtualTextPosition;
use ratatui::style::{Color, Style};

// Clear existing inlay hints
state.virtual_texts.clear(&mut state.marker_list);
// Clear existing inlay hints (preserve other virtual text like ghost text)
const INLAY_HINT_PREFIX: &str = "lsp-inlay:";
state
.virtual_texts
.remove_by_prefix(&mut state.marker_list, INLAY_HINT_PREFIX);

if hints.is_empty() {
return;
Expand All @@ -776,7 +1046,7 @@
// Style for inlay hints - dimmed to not distract from actual code
let hint_style = Style::default().fg(Color::Rgb(128, 128, 128));

for hint in hints {
for (idx, hint) in hints.iter().enumerate() {
// Convert LSP position to byte offset
let byte_offset = state.buffer.lsp_position_to_byte(
hint.position.line as usize,
Expand Down Expand Up @@ -813,13 +1083,19 @@
// Use the hint text as-is - spacing is handled during rendering
let display_text = text;

state.virtual_texts.add(
let string_id = format!(
"{}{}:{}:{}",
INLAY_HINT_PREFIX, hint.position.line, hint.position.character, idx
);

state.virtual_texts.add_with_id(
&mut state.marker_list,
byte_offset,
display_text,
hint_style,
position,
0, // Default priority
string_id,
);
}

Expand Down
Loading
Loading