Skip to content

Commit

Permalink
Reorganize Source Files
Browse files Browse the repository at this point in the history
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
PatWie committed Aug 11, 2024
1 parent f10a11a commit ca7416f
Show file tree
Hide file tree
Showing 13 changed files with 443 additions and 372 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ use tree_sitter::{
Node,
};

use crate::code_action_providers::parsed_document::ParsedDocument;

use super::helper;
use crate::code_action_providers::{helper::ts_node_to_lsp_range, parsed_document::ParsedDocument};

#[derive(Copy, Clone, Debug)]
struct LuaNode(TSNode);
Expand Down Expand Up @@ -91,7 +89,7 @@ impl UserData for LuaNode {
methods.add_method("range", |_, node: &LuaNode, ()| {
let node: TSNode = node.0;
let node: Node = unsafe { Node::from_raw(node) };
let r: LuaRange = helper::ts_node_to_lsp_range(&node).into();
let r: LuaRange = ts_node_to_lsp_range(&node).into();
Result::Ok(r)
});
methods.add_method("next_sibling", |_, node: &LuaNode, ()| {
Expand All @@ -102,22 +100,25 @@ impl UserData for LuaNode {
false => Result::Ok(Some(LuaNode(pnode))),
}
});
methods.add_method("child_by_field_name", |_, node: &LuaNode, field_name: String| {
let node: TSNode = node.0;
let c_field_name = std::ffi::CString::new(field_name).unwrap();
let child_node = unsafe {
ts_node_child_by_field_name(
node,
c_field_name.as_ptr(),
c_field_name.as_bytes().len() as u32,
)
};
methods.add_method(
"child_by_field_name",
|_, node: &LuaNode, field_name: String| {
let node: TSNode = node.0;
let c_field_name = std::ffi::CString::new(field_name).unwrap();
let child_node = unsafe {
ts_node_child_by_field_name(
node,
c_field_name.as_ptr(),
c_field_name.as_bytes().len() as u32,
)
};

match unsafe { ts_node_is_null(child_node) } {
true => Result::Ok(None),
false => Result::Ok(Some(LuaNode(child_node))),
}
});
match unsafe { ts_node_is_null(child_node) } {
true => Result::Ok(None),
false => Result::Ok(Some(LuaNode(child_node))),
}
},
);
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/code_action_providers/lua/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod bindings;
pub mod provider;
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ use tower_lsp::lsp_types::{CodeAction, CodeActionKind, TextEdit, WorkspaceEdit};
use crate::code_action_providers::parsed_document::ParsedDocument;
use crate::code_action_providers::traits::ActionContext;
use crate::code_action_providers::traits::ActionProvider;
use crate::prompt_handlers::traits::LLM;
use crate::ResolveAction;
use crate::llm_handlers::traits::Llm;
use crate::server::ResolveAction;

use super::bindings::LuaInterface;

use super::lua_jit::LuaInterface;

pub struct LuaProvider {
prompt_handler: Arc<LLM>,
prompt_handler: Arc<Llm>,
lua_source: String,
id: String,
}
Expand All @@ -32,7 +33,7 @@ pub enum LuaProviderError {
impl LuaProvider {
pub fn try_new(
file_name: &str,
prompt_handler: Arc<LLM>,
prompt_handler: Arc<Llm>,
) -> anyhow::Result<Self, LuaProviderError> {
Ok(Self {
prompt_handler,
Expand Down
115 changes: 111 additions & 4 deletions src/code_action_providers/mod.rs
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.
2 changes: 2 additions & 0 deletions src/code_action_providers/yaml/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod config;
pub mod provider;
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use tower_lsp::lsp_types::{CodeAction, CodeActionKind, TextEdit, WorkspaceEdit};
use crate::code_action_providers::traits::ActionContext;
use crate::code_action_providers::traits::ActionProvider;
use crate::code_action_providers::{helper, parsed_document::ParsedDocument};
use crate::prompt_handlers::traits::LLM;
use crate::ResolveAction;
use crate::llm_handlers::traits::Llm;
use crate::server::ResolveAction;

use super::config;

Expand All @@ -24,14 +24,14 @@ fn build_prompt(template: &str, hints: &HashMap<String, String>) -> String {
}

pub struct YamlProvider {
prompt_handler: Arc<LLM>,
prompt_handler: Arc<Llm>,

config: config::CodeAction,
id: String,
}

impl YamlProvider {
pub fn from_config(config: config::CodeAction, id: &str, prompt_handler: Arc<LLM>) -> Self {
pub fn from_config(config: config::CodeAction, id: &str, prompt_handler: Arc<Llm>) -> Self {
Self {
prompt_handler,
config,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use aws_sdk_bedrockruntime::{

use crate::configuration::BedrockConfig;

use super::traits::PromptHandler;
use super::traits::LlmHandler;


#[derive(Debug)]
Expand All @@ -34,7 +34,7 @@ impl BedrockConverse {
}
}

impl PromptHandler for BedrockConverse {
impl LlmHandler for BedrockConverse {
async fn answer(&self, prompt: &str) -> anyhow::Result<String> {
let response = self
.client
Expand Down
6 changes: 3 additions & 3 deletions src/prompt_handlers/mock.rs → src/llm_handlers/mock.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use super::traits::PromptHandler;
use super::traits::LlmHandler;

#[derive(Debug)]
pub struct MockLLM {
answer: String,
pub answer: String,
}

impl MockLLM {
Expand All @@ -11,7 +11,7 @@ impl MockLLM {
}
}

impl PromptHandler for MockLLM {
impl LlmHandler for MockLLM {
async fn answer(&self, _: &str) -> anyhow::Result<String> {
Ok(self.answer.clone())
}
Expand Down
File renamed without changes.
10 changes: 5 additions & 5 deletions src/prompt_handlers/traits.rs → src/llm_handlers/traits.rs
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,
}
}
}
Loading

0 comments on commit ca7416f

Please sign in to comment.