Skip to content

Commit

Permalink
copilot virtual render working
Browse files Browse the repository at this point in the history
  • Loading branch information
nkitsaini committed Feb 9, 2024
1 parent 29e40e3 commit 7a53512
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 2 deletions.
13 changes: 13 additions & 0 deletions helix-term/src/ui/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ use std::{mem::take, num::NonZeroUsize, path::PathBuf, rc::Rc, sync::Arc};

use tui::{buffer::Buffer as Surface, text::Span};

use super::text_decorations::CopilotDecoration;

pub struct EditorView {
pub keymaps: Keymaps,
on_next_key: Option<OnKeyCallback>,
Expand Down Expand Up @@ -193,6 +195,17 @@ impl EditorView {
primary_cursor,
config.lsp.inline_diagnostics.clone(),
));

if let Some((text, pos)) = doc.copilot_state.lock().get_completion_text_and_pos() {
decorations.add_decoration(CopilotDecoration::new(
theme.get("ui.text.focused"),
doc.text().slice(..),
text.to_string(),
pos,
inner.width,
));
}

render_document(
surface,
inner,
Expand Down
104 changes: 102 additions & 2 deletions helix-term/src/ui/text_decorations.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use std::cmp::Ordering;

use helix_core::doc_formatter::FormattedGrapheme;
use helix_core::Position;
use helix_core::doc_formatter::{DocumentFormatter, FormattedGrapheme, TextFormat};
use helix_core::text_annotations::TextAnnotations;
use helix_core::{coords_at_pos, Position, RopeSlice};
use helix_view::editor::CursorCache;
use helix_view::theme::Style;

use crate::ui::document::{LinePos, TextRenderer};

Expand Down Expand Up @@ -171,3 +173,101 @@ impl Decoration for Cursor<'_> {
usize::MAX
}
}
pub struct CopilotDecoration {
style: Style,
text: String,
row: usize,
col: usize,
view_width: u16,
}

impl CopilotDecoration {
pub fn new(
style: Style,
doc_text: RopeSlice,
completion_text: String,
completion_pos: usize,
view_width: u16,
) -> CopilotDecoration {
let coords = coords_at_pos(doc_text, completion_pos);
CopilotDecoration {
style,
text: completion_text,
row: coords.row,
col: coords.col,
view_width,
}
}
}

impl Decoration for CopilotDecoration {
fn render_virt_lines(
&mut self,
_renderer: &mut TextRenderer,
_pos: LinePos,
_virt_off: helix_core::Position, // u16
) -> helix_core::Position {
if _pos.doc_line != self.row {
return helix_core::Position::new(0, 0);
}

let mut lines = self.text.split('\n').enumerate();
lines.next();
let n_lines = lines.clone().count();

let mut text_fmt = TextFormat::default();
text_fmt.viewport_width = self.view_width;
let annotations = TextAnnotations::default();

while let Some((idx, line)) = lines.next() {
let formatter =
DocumentFormatter::new_at_prev_checkpoint(line.into(), &text_fmt, &annotations, 0);

for grapheme in formatter {
_renderer.draw_decoration_grapheme(
grapheme.raw,
self.style,
_pos.visual_line + grapheme.visual_pos.row as u16 + idx as u16,
grapheme.visual_pos.col as u16,
);
}
}

return helix_core::Position::new(0, n_lines);
// n_lines as u16
}

fn decorate_line(&mut self, _renderer: &mut TextRenderer, _pos: LinePos) {
if self.row != _pos.doc_line {
return;
}

let first_line = if let Some(split) = self.text.split_once('\n') {
split.0
} else {
&self.text
};

let mut text_fmt = TextFormat::default();
text_fmt.viewport_width = self.view_width;
let annotations = TextAnnotations::default();
let formatter = DocumentFormatter::new_at_prev_checkpoint(
first_line.into(),
&text_fmt,
&annotations,
0,
);

for grapheme in formatter {
if grapheme.char_idx < self.col {
continue;
}
_renderer.draw_decoration_grapheme(
grapheme.raw,
self.style,
_pos.visual_line + grapheme.visual_pos.row as u16,
grapheme.visual_pos.col as u16,
);
}
}
}

0 comments on commit 7a53512

Please sign in to comment.