fix: clamp col pos to prevent an out of bound slicing range (#13)
This commit is contained in:
parent
15c4ef3088
commit
1550467f86
|
@ -15,9 +15,6 @@ use tracing::{error, info};
|
|||
use tracing_appender::rolling;
|
||||
use tracing_subscriber::EnvFilter;
|
||||
|
||||
// TODO:
|
||||
// * handle slice panic
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
struct RequestParams {
|
||||
max_new_tokens: u32,
|
||||
|
@ -153,12 +150,13 @@ fn build_prompt(
|
|||
let mut before_iter = text.lines_at(pos.line as usize + 1).reversed();
|
||||
let mut after_iter = text.lines_at(pos.line as usize);
|
||||
let mut before_line = before_iter.next();
|
||||
let col = pos.character as usize;
|
||||
if let Some(line) = before_line {
|
||||
let col = (pos.character as usize).clamp(0, line.len_chars());
|
||||
before_line = Some(line.slice(0..col));
|
||||
}
|
||||
let mut after_line = after_iter.next();
|
||||
if let Some(line) = after_line {
|
||||
let col = (pos.character as usize).clamp(0, line.len_chars());
|
||||
after_line = Some(line.slice(col..));
|
||||
}
|
||||
let mut before = vec![];
|
||||
|
@ -205,7 +203,8 @@ fn build_prompt(
|
|||
let mut first = true;
|
||||
for mut line in text.lines_at(pos.line as usize + 1).reversed() {
|
||||
if first {
|
||||
line = line.slice(0..pos.character as usize);
|
||||
let col = (pos.character as usize).clamp(0, line.len_chars());
|
||||
line = line.slice(0..col);
|
||||
first = false;
|
||||
}
|
||||
let line = line.to_string();
|
||||
|
|
Loading…
Reference in a new issue