-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit reorganizes the source files in the project to improve code organization and maintainability. The key changes are: 1. Separating the Lua and YAML code action providers into their own subdirectories under `src/code_action_providers/`. 2. Renaming the `prompt_handlers` module to `llm_handlers` to better reflect its purpose. 3. Adding a new `server.rs` file to encapsulate the server-related functionality.
- Loading branch information
Showing
13 changed files
with
443 additions
and
372 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod bindings; | ||
pub mod provider; |
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 |
---|---|---|
@@ -1,7 +1,114 @@ | ||
pub mod config; | ||
use std::{collections::HashMap, path::PathBuf, sync::Arc}; | ||
|
||
use lua::provider::LuaProvider; | ||
use tower_lsp::lsp_types::CodeAction; | ||
use traits::ActionProvider; | ||
use yaml::{config, provider::YamlProvider}; | ||
|
||
use crate::{ | ||
llm_handlers::traits::Llm, | ||
nonsense::{self, IndexedText, TextAdapter}, | ||
read_language_config_files, | ||
}; | ||
|
||
pub mod helper; | ||
pub mod lua_jit; | ||
pub mod lua_provider; | ||
pub mod lua; | ||
pub mod parsed_document; | ||
pub mod traits; | ||
pub mod yaml_provider; | ||
pub mod yaml; | ||
|
||
const SUPPORTED_LANGUAGES: [&str; 7] = [ | ||
"gitcommit", | ||
"go", | ||
"markdown", | ||
"python", | ||
"rust", | ||
"text", | ||
"__all__", | ||
]; | ||
|
||
pub fn load_providers( | ||
code_actions_config_dir: PathBuf, | ||
prompt_handler: Arc<Llm>, | ||
) -> HashMap<String, Vec<Box<dyn ActionProvider>>> { | ||
let mut providers: HashMap<String, Vec<Box<dyn ActionProvider>>> = Default::default(); | ||
|
||
//log::info!("Processing config-dir: {:?}", config_dir); | ||
for language in SUPPORTED_LANGUAGES { | ||
let config_dir = code_actions_config_dir.join(language); | ||
for config_path in read_language_config_files(&config_dir, "yaml") { | ||
//log::info!("Processing language config: {:?}", config_path); | ||
match config::CodeActionConfig::from_yaml(&config_path) { | ||
Ok(language_config) => { | ||
for (k, config) in language_config.code_actions.into_iter().enumerate() { | ||
//log::info!("Register action {} for {:?}", config.name, config_path); | ||
providers | ||
.entry(language.to_owned()) | ||
.or_default() | ||
.push(Box::new(YamlProvider::from_config( | ||
config, | ||
&format!("{}.{k}", config_path.to_string_lossy()), | ||
prompt_handler.clone(), | ||
))); | ||
} | ||
} | ||
Err(_e) => { | ||
//log::warn!("Cannot read {:?} because of {}", &config_path, e); | ||
} | ||
}; | ||
} | ||
for config_path in read_language_config_files(&config_dir, "lua") { | ||
//log::info!("Processing language config: {:?}", config_path); | ||
providers | ||
.entry(language.to_owned()) | ||
.or_default() | ||
.push(Box::new( | ||
LuaProvider::try_new(&config_path.to_string_lossy(), prompt_handler.clone()) | ||
.unwrap(), | ||
)); | ||
} | ||
} | ||
providers | ||
} | ||
|
||
pub fn find_resolver<'a>( | ||
providers: &'a HashMap<String, Vec<Box<dyn ActionProvider>>>, | ||
code_action_id: &str, | ||
lang: &str, | ||
) -> Option<&'a Box<dyn ActionProvider>> { | ||
for target_lang in [lang, "__all__"] { | ||
if let Some(language_specific_providers) = providers.get(target_lang) { | ||
for provider in language_specific_providers.iter() { | ||
if provider.can_handle(code_action_id) { | ||
return Some(provider); | ||
} | ||
} | ||
} | ||
} | ||
None | ||
} | ||
|
||
pub fn map_to_lsp(r: &mut CodeAction, index: &IndexedText<String>) { | ||
// if let Ok(r) = r.as_mut() { | ||
if let Some(e) = r.edit.as_mut() { | ||
if let Some(c) = e.changes.as_mut() { | ||
for value in c.values_mut() { | ||
for text_edit in value.iter_mut() { | ||
let fake = std::ops::Range::<nonsense::Pos> { | ||
start: nonsense::Pos { | ||
line: text_edit.range.start.line, | ||
col: text_edit.range.start.character, | ||
}, | ||
end: nonsense::Pos { | ||
line: text_edit.range.end.line, | ||
col: text_edit.range.end.character, | ||
}, | ||
}; | ||
let rs = index.range_to_lsp_range(&fake).unwrap(); | ||
text_edit.range = rs; | ||
} | ||
} | ||
} | ||
} | ||
// } | ||
} |
File renamed without changes.
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,2 @@ | ||
pub mod config; | ||
pub mod provider; |
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
File renamed without changes.
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 |
---|---|---|
@@ -1,23 +1,23 @@ | ||
use super::bedrock::BedrockConverse; | ||
use super::mock::MockLLM; | ||
|
||
pub trait PromptHandler { | ||
pub trait LlmHandler { | ||
fn answer( | ||
&self, | ||
prompt: &str, | ||
) -> impl std::future::Future<Output = anyhow::Result<String>> + Send; | ||
} | ||
|
||
pub enum LLM { | ||
pub enum Llm { | ||
Bedrock(BedrockConverse), | ||
Mock(MockLLM), | ||
} | ||
|
||
impl LLM { | ||
impl Llm { | ||
pub async fn answer<'a>(&'a self, prompt: &'a str) -> anyhow::Result<String> { | ||
match self { | ||
LLM::Bedrock(b) => b.answer(prompt).await, | ||
LLM::Mock(b) => b.answer(prompt).await, | ||
Llm::Bedrock(b) => b.answer(prompt).await, | ||
Llm::Mock(b) => b.answer(prompt).await, | ||
} | ||
} | ||
} |
Oops, something went wrong.