-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: enhance lla_plugin_utils with syntax highlighting and interacti…
…ve features - Updated Cargo.toml to include new optional dependencies: `syntect`, `lazy_static`, and `dialoguer` for syntax highlighting and interactive selection. - Introduced `syntax` module for code highlighting functionality using `syntect`. - Added `CodeHighlighter` struct with methods for highlighting code and retrieving available themes. - Implemented `InteractiveSelector` for user interaction, allowing single and multiple selections, confirmations, and custom inputs. - Refactored UI components to support new features, including enhanced `BoxComponent` styling options. - Improved module organization by separating text-related functionality into a new `text` module.
- Loading branch information
Showing
7 changed files
with
443 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#[cfg(feature = "syntax")] | ||
use lazy_static::lazy_static; | ||
#[cfg(feature = "syntax")] | ||
use syntect::{ | ||
easy::HighlightLines, | ||
highlighting::{Style, ThemeSet}, | ||
parsing::SyntaxSet, | ||
util::{as_24_bit_terminal_escaped, LinesWithEndings}, | ||
}; | ||
|
||
#[cfg(feature = "syntax")] | ||
lazy_static! { | ||
static ref SYNTAX_SET: SyntaxSet = SyntaxSet::load_defaults_newlines(); | ||
static ref THEME_SET: ThemeSet = ThemeSet::load_defaults(); | ||
} | ||
|
||
pub struct CodeHighlighter; | ||
|
||
impl CodeHighlighter { | ||
#[cfg(feature = "syntax")] | ||
pub fn highlight(code: &str, language: &str) -> String { | ||
let syntax = SYNTAX_SET | ||
.find_syntax_by_token(language) | ||
.unwrap_or_else(|| SYNTAX_SET.find_syntax_plain_text()); | ||
let mut h = HighlightLines::new(syntax, &THEME_SET.themes["base16-ocean.dark"]); | ||
|
||
let mut highlighted = String::new(); | ||
for line in LinesWithEndings::from(code) { | ||
let ranges: Vec<(Style, &str)> = | ||
h.highlight_line(line, &SYNTAX_SET).unwrap_or_default(); | ||
let escaped = as_24_bit_terminal_escaped(&ranges[..], false); | ||
highlighted.push_str(&escaped); | ||
} | ||
highlighted | ||
} | ||
|
||
#[cfg(not(feature = "syntax"))] | ||
pub fn highlight(code: &str, _language: &str) -> String { | ||
code.to_string() | ||
} | ||
|
||
pub fn highlight_with_line_numbers(code: &str, language: &str, start_line: usize) -> String { | ||
let highlighted = Self::highlight(code, language); | ||
let mut result = String::new(); | ||
for (i, line) in highlighted.lines().enumerate() { | ||
result.push_str(&format!("{:4} │ {}\n", i + start_line, line)); | ||
} | ||
result | ||
} | ||
} | ||
|
||
#[cfg(feature = "syntax")] | ||
pub fn get_available_themes() -> Vec<String> { | ||
THEME_SET.themes.keys().cloned().collect() | ||
} | ||
|
||
#[cfg(not(feature = "syntax"))] | ||
pub fn get_available_themes() -> Vec<String> { | ||
vec![] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.