Refactor: simplify word navigation logic, improve line wrapping, and enhance parameter initialization across core and TUI modules.

This commit is contained in:
2025-09-30 02:54:07 +02:00
parent 8ee4c5f384
commit 053f389b1e
6 changed files with 27 additions and 45 deletions

View File

@@ -106,13 +106,11 @@ impl Router {
return true;
}
if pattern.ends_with('*') {
let prefix = &pattern[..pattern.len() - 1];
if let Some(prefix) = pattern.strip_suffix('*') {
return model.starts_with(prefix);
}
if pattern.starts_with('*') {
let suffix = &pattern[1..];
if let Some(suffix) = pattern.strip_prefix('*') {
return model.ends_with(suffix);
}

View File

@@ -72,7 +72,7 @@ pub struct ChatRequest {
}
/// Parameters for chat completion
#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ChatParameters {
/// Temperature for randomness (0.0 to 2.0)
#[serde(skip_serializing_if = "Option::is_none")]
@@ -85,20 +85,10 @@ pub struct ChatParameters {
pub stream: bool,
/// Additional provider-specific parameters
#[serde(flatten)]
#[serde(default)]
pub extra: HashMap<String, serde_json::Value>,
}
impl Default for ChatParameters {
fn default() -> Self {
Self {
temperature: None,
max_tokens: None,
stream: false,
extra: HashMap::new(),
}
}
}
/// Response from a chat completion request
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChatResponse {

View File

@@ -305,9 +305,7 @@ pub fn find_word_end(line: &str, col: usize) -> Option<usize> {
pos += 1;
}
// Move back one to be ON the last character
if pos > 0 {
pos -= 1;
}
pos = pos.saturating_sub(1);
} else {
// Skip non-word characters
while pos < chars.len() && !is_word_char(chars[pos]) {
@@ -317,9 +315,7 @@ pub fn find_word_end(line: &str, col: usize) -> Option<usize> {
while pos < chars.len() && is_word_char(chars[pos]) {
pos += 1;
}
if pos > 0 {
pos -= 1;
}
pos = pos.saturating_sub(1);
}
Some(pos)
@@ -336,9 +332,7 @@ pub fn find_prev_word_boundary(line: &str, col: usize) -> Option<usize> {
let is_word_char = |c: char| c.is_alphanumeric() || c == '_';
// Move back one position first
if pos > 0 {
pos -= 1;
}
pos = pos.saturating_sub(1);
// Skip non-word characters
while pos > 0 && !is_word_char(chars[pos]) {

View File

@@ -52,23 +52,21 @@ pub fn build_cursor_map(text: &str, width: u16) -> Vec<ScreenPos> {
word_start_col = col;
word_start_idx = byte_offset + grapheme.len();
}
} else {
if col + grapheme_width > width {
if word_start_col > 0 && byte_offset == word_start_idx {
// This is the first character of a new word that won't fit, wrap it
row += 1;
col = grapheme_width;
} else if word_start_col == 0 {
// No previous word boundary, hard break
row += 1;
col = grapheme_width;
} else {
// This is part of a word already on the line, let it extend beyond width
col += grapheme_width;
}
} else if col + grapheme_width > width {
if word_start_col > 0 && byte_offset == word_start_idx {
// This is the first character of a new word that won't fit, wrap it
row += 1;
col = grapheme_width;
} else if word_start_col == 0 {
// No previous word boundary, hard break
row += 1;
col = grapheme_width;
} else {
// This is part of a word already on the line, let it extend beyond width
col += grapheme_width;
}
} else {
col += grapheme_width;
}
// Set position for the end of this grapheme and any intermediate bytes