Skip to content

Commit 2484fe7

Browse files
committed
yagni
1 parent 09e9cb8 commit 2484fe7

2 files changed

Lines changed: 19 additions & 53 deletions

File tree

src/core_editor/line_buffer.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,15 @@ impl LineBuffer {
9090
self.lines[..self.insertion_point].matches('\n').count()
9191
}
9292

93+
/// Calculates the character index in the line the user is on
94+
///
95+
/// Zero-based index
96+
pub fn col(&self) -> usize {
97+
self.lines[self.line_start()..self.insertion_point]
98+
.chars()
99+
.count()
100+
}
101+
93102
/// Counts the number of lines in the buffer
94103
pub fn num_lines(&self) -> usize {
95104
self.lines.split('\n').count()

src/engine.rs

Lines changed: 10 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
use std::{
2-
ffi::OsStr,
3-
num::NonZeroUsize,
4-
path::{Path, PathBuf},
5-
};
6-
71
use itertools::Itertools;
82
use nu_ansi_term::{Color, Style};
3+
use std::ffi::OsStr;
4+
use std::path::PathBuf;
95

106
use crate::{enums::ReedlineRawEvent, CursorConfig};
117
#[cfg(feature = "bashisms")]
@@ -230,45 +226,6 @@ struct BufferEditor {
230226
temp_file: PathBuf,
231227
}
232228

233-
/// a position in a buffer.
234-
/// indexes are one-based (hence the use of non-zero integer types)
235-
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
236-
pub(crate) struct BufferPosition {
237-
pub line: NonZeroUsize,
238-
pub col: NonZeroUsize,
239-
}
240-
241-
impl BufferPosition {
242-
fn from_editor(editor: &Editor) -> BufferPosition {
243-
// TODO: this impl can probably be faster (caching newline locations?)
244-
245-
let buffer = editor.get_buffer();
246-
247-
let content_len = editor.insertion_point().min(buffer.len());
248-
let content_until_cursor = &buffer[..content_len];
249-
250-
// impl note: see `core_editor::line_buffer::insert_newline`:
251-
// we don't expect any other char sequence for linebreak.
252-
let newline = '\n';
253-
254-
let line = {
255-
let line_one_based = content_until_cursor.matches(newline).count() + 1;
256-
NonZeroUsize::new(line_one_based).unwrap()
257-
};
258-
259-
let col = {
260-
let col_one_based = if let Some(newline_pos) = content_until_cursor.rfind(newline) {
261-
content_until_cursor[newline_pos + 1..].chars().count() + 1
262-
} else {
263-
content_until_cursor.chars().count() + 1
264-
};
265-
NonZeroUsize::new(col_one_based).unwrap()
266-
};
267-
268-
BufferPosition { line, col }
269-
}
270-
}
271-
272229
impl Drop for Reedline {
273230
fn drop(&mut self) {
274231
if self.cursor_shapes.is_some() {
@@ -1923,7 +1880,7 @@ impl Reedline {
19231880
/// opens the current buffer in the editor described in [`buffer_editor`]
19241881
/// returns the new buffer, after processing the changes via the editor
19251882
fn open_editor(&self, buffer_editor: &BufferEditor) -> Result<String> {
1926-
let mut command = self.render_buffer_editor_command(buffer_editor);
1883+
let mut command = Self::render_editor_command(buffer_editor, self.editor.line_buffer());
19271884

19281885
// flush buffer to temp file, so it can be read by the editor
19291886
{
@@ -1941,11 +1898,13 @@ impl Reedline {
19411898
Ok(buffer)
19421899
}
19431900

1944-
/// renders the template command described in [`buffer_editor`], if any
1945-
fn render_buffer_editor_command(&self, buffer_editor: &BufferEditor) -> Command {
1901+
/// renders the template command described in [`buffer_editor`],
1902+
/// by substituting the placeholders in the pattern, if any
1903+
fn render_editor_command(buffer_editor: &BufferEditor, line_buffer: &LineBuffer) -> Command {
1904+
use std::ops::Add as _;
1905+
19461906
let mut cmd = Command::new(buffer_editor.command.get_program());
19471907

1948-
// TODO: there are more efficient ways to do this.
19491908
const FILE: &str = "{file}";
19501909
const LINE: &str = "{line}";
19511910
const COL: &str = "{col}";
@@ -1959,14 +1918,12 @@ impl Reedline {
19591918
.any(|arg| arg.contains(FILE));
19601919

19611920
if is_template {
1962-
let pos = BufferPosition::from_editor(&self.editor);
1963-
19641921
// TODO: there are more efficient ways to do this.
19651922
// e.g. "format args"-style structs
19661923

19671924
let file = buffer_editor.temp_file.to_string_lossy();
1968-
let line = pos.line.to_string();
1969-
let col = pos.col.to_string();
1925+
let line = line_buffer.line().add(1).to_string();
1926+
let col = line_buffer.col().add(1).to_string();
19701927

19711928
let actual_args = buffer_editor
19721929
.command

0 commit comments

Comments
 (0)