Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lazy static completions #20

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lsp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ htmx-lsp-util = { version = "0.1", path = "../util" }
tree-sitter.workspace = true
tree-sitter-html.workspace = true
maplit = "1.0.2"
lazy_static = "1.4.0"
58 changes: 18 additions & 40 deletions lsp/src/htmx/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use lazy_static::lazy_static;
use log::debug;
use lsp_types::TextDocumentPositionParams;
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, path::PathBuf, sync::OnceLock};
use std::{collections::HashMap, path::PathBuf};

use crate::tree_sitter::Position;

Expand Down Expand Up @@ -46,51 +47,25 @@ pub fn hx_completion(text_params: TextDocumentPositionParams) -> Option<Vec<HxCo
debug!("result: {:?} params: {:?}", result, text_params);

match result {
Position::AttributeName(name) => {
if name.starts_with("hx-") {
return HX_TAGS.get().cloned();
}
}

Position::AttributeValue { name, .. } => {
let values = HX_ATTRIBUTE_VALUES.get()?.get(&name)?;
return Some(values.clone());
}
};

None
Position::AttributeName(name) if name.starts_with("hx-") => Some(HX_TAGS.clone()),
Position::AttributeValue { name, .. } => HX_ATTRIBUTE_VALUES.get(&name).cloned(),
_ => None,
}
}

pub fn hx_hover(text_params: TextDocumentPositionParams) -> Option<HxCompletion> {
let result = crate::tree_sitter::get_position_from_lsp_completion(text_params.clone())?;
debug!("handle_hover result: {:?}", result);

match result {
Position::AttributeName(name) => HX_TAGS
.get()
.expect("Why it can't get HX_TAGS?")
.iter()
.find(|x| x.name == name)
.cloned(),

Position::AttributeValue { name, .. } => HX_TAGS
.get()
.expect("Why it can't get HX_TAGS?")
.iter()
.find(|x| x.name == name)
.cloned(),
}
}

pub static HX_TAGS: OnceLock<Vec<HxCompletion>> = OnceLock::new();
pub static HX_ATTRIBUTE_VALUES: OnceLock<HashMap<String, Vec<HxCompletion>>> = OnceLock::new();
Position::AttributeName(name) => HX_TAGS.iter().find(|x| x.name == name).cloned(),

fn to_hx_completion(values: Vec<(&str, &str)>) -> Vec<HxCompletion> {
return values.iter().filter_map(|x| x.try_into().ok()).collect();
Position::AttributeValue { name, .. } => HX_TAGS.iter().find(|x| x.name == name).cloned(),
}
}

pub fn init_hx_tags() {
_ = HX_ATTRIBUTE_VALUES.set(maplit::hashmap! {
lazy_static! {
pub static ref HX_ATTRIBUTE_VALUES: HashMap<String, Vec<HxCompletion>> = maplit::hashmap! {
String::from("hx-swap") => to_hx_completion(vec![
("innerHTML", include_str!("./hx-swap/innerHTML.md")),
("outerHTML", include_str!("./hx-swap/outerHTML.md")),
Expand Down Expand Up @@ -195,9 +170,8 @@ pub fn init_hx_tags() {
("replace", include_str!("./hx-sync/replace.md")),
("queue", include_str!("./hx-sync/queue.md")),
])
});

_ = HX_TAGS.set(to_hx_completion(vec![
};
pub static ref HX_TAGS: Vec<HxCompletion> = to_hx_completion(vec![
("hx-boost", include_str!("./attributes/hx-boost.md")),
("hx-delete", include_str!("./attributes/hx-delete.md")),
("hx-get", include_str!("./attributes/hx-get.md")),
Expand Down Expand Up @@ -238,5 +212,9 @@ pub fn init_hx_tags() {
("hx-request", include_str!("./attributes/hx-request.md")),
("hx-sync", include_str!("./attributes/hx-sync.md")),
("hx-validate", include_str!("./attributes/hx-validate.md")),
]));
]);
}

fn to_hx_completion(values: Vec<(&str, &str)>) -> Vec<HxCompletion> {
return values.iter().filter_map(|x| x.try_into().ok()).collect();
}
2 changes: 0 additions & 2 deletions lsp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use lsp_server::{Connection, Message, Response};

use crate::{
handle::{handle_notification, handle_other, handle_request, HtmxResult},
htmx::init_hx_tags,
text_store::init_text_store,
};

Expand Down Expand Up @@ -114,7 +113,6 @@ fn main_loop(connection: Connection, params: serde_json::Value) -> Result<()> {

pub fn start_lsp() -> Result<()> {
init_text_store();
init_hx_tags();

// Note that we must have our logging only write out to stderr.
info!("starting generic LSP server");
Expand Down