Skip to content

Commit

Permalink
Merge pull request #32 from uros-5/toml-config
Browse files Browse the repository at this point in the history
support configuration outside of editor
  • Loading branch information
uros-5 authored Nov 9, 2024
2 parents 1c731b5 + f2aedf7 commit e2f538c
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 26 deletions.
83 changes: 79 additions & 4 deletions Cargo.lock

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

8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,5 +128,13 @@ nvim_lsp.jinja_lsp.setup {
}
```

You can also write configuration in: `pyproject.toml`, `Cargo.toml`, `jinja-lsp.toml`.

```toml
[jinja-lsp]
templates = "./templates"
backend = ["./src"]
lang = "rust"
```

Supported languages: Python, Rust
7 changes: 7 additions & 0 deletions example/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,10 @@ packages = ["src"]

[tool.pyright]
extraPaths = ["example/.venv/lib/python3.10/site-packages"]


[jinja-lsp]
templates = "./templates"
backend = ["./src"]
lang = "rust"
hide_undefined = false
2 changes: 1 addition & 1 deletion jinja-lsp-nodejs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
edition = "2021"
name = "jinja-lsp-nodejs"
version = "0.1.83"
version = "0.1.84"
license = "MIT"
authors = ["uros-5"]
description = "Bindings for jinja-lsp"
Expand Down
2 changes: 1 addition & 1 deletion jinja-lsp-queries/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "jinja-lsp-queries"
version = "0.1.83"
version = "0.1.84"
edition = "2021"
description = "TreeSitter queries for jinja-lsp"
license = "MIT"
Expand Down
26 changes: 15 additions & 11 deletions jinja-lsp-queries/src/lsp_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,26 @@ pub fn search_errors(
if empty && exist {
to_warn = true;
} else if empty {
if ignore_globals {
continue;
}
to_warn = true;
if !ignore_globals {
for file in variables {
let temp = file
.1
.iter()
.filter(|variable| variable.name == object.name);
if temp.count() != 0 {
err_type = JinjaDiagnostic::DefinedSomewhere;
to_warn = true;
break;
}
for file in variables {
let temp = file
.1
.iter()
.filter(|variable| variable.name == object.name);
if temp.count() != 0 {
err_type = JinjaDiagnostic::DefinedSomewhere;
to_warn = true;
break;
}
}
}
if to_warn {
if ignore_globals {
continue;
}
let diagnostic = (err_type, Identifier::from(&object));
diagnostics.push(diagnostic);
}
Expand Down
5 changes: 3 additions & 2 deletions jinja-lsp/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "jinja-lsp"
version = "0.1.83"
version = "0.1.84"
edition = "2021"
license = "MIT"
authors = ["uros-5"]
Expand Down Expand Up @@ -31,5 +31,6 @@ walkdir = "2.4.0"
anyhow = "1.0.75"
tree-sitter-jinja2 = "0.0.10"
tree-sitter-rust = "0.23.0"
jinja-lsp-queries = { path = "../jinja-lsp-queries", version = "0.1.83"}
jinja-lsp-queries = { path = "../jinja-lsp-queries", version = "0.1.84"}
tree-sitter-language = "0.1.0"
toml = "0.8.19"
15 changes: 8 additions & 7 deletions jinja-lsp/src/channels/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use tower_lsp::{
};

use crate::{
config::{walkdir, JinjaConfig},
config::{search_config, walkdir, JinjaConfig},
filter::init_filter_completions,
lsp_files::LspFiles,
};
Expand All @@ -47,15 +47,16 @@ pub fn lsp_task(
lsp_data.is_vscode = true;
}
}
params
config = params
.initialization_options
.map(serde_json::from_value)
.map(|res| res.ok())
.and_then(|c| -> Option<()> {
config = c?;
.and_then(|c| {
let mut config: JinjaConfig = c?;
config.user_defined = true;
None
});
Some(config)
})
.unwrap_or(search_config().unwrap_or(config));

let definition_provider = Some(OneOf::Left(true));
let references_provider = None;
Expand Down Expand Up @@ -100,7 +101,7 @@ pub fn lsp_task(
},
server_info: Some(ServerInfo {
name: String::from("jinja-lsp"),
version: Some(String::from("0.1.80")),
version: Some(String::from("0.1.84")),
}),
offset_encoding: None,
};
Expand Down
25 changes: 25 additions & 0 deletions jinja-lsp/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{
collections::HashMap,
fs::read_to_string,
path::{Path, PathBuf},
};

Expand All @@ -21,6 +22,29 @@ pub struct JinjaConfig {
pub lang: String,
#[serde(skip)]
pub user_defined: bool,
pub hide_undefined: Option<bool>,
}

#[derive(Deserialize, Debug, Default, Clone)]
pub struct ExternalConfig {
#[serde(rename(deserialize = "jinja-lsp"))]
jinja_lsp: JinjaConfig,
}

pub fn search_config() -> Option<JinjaConfig> {
let configs = ["pyproject.toml", "Cargo.toml", "jinja-lsp.toml"];
for config in configs {
let contents = read_to_string(config).unwrap_or_default();
if contents.is_empty() {
continue;
}
let config = toml::from_str::<ExternalConfig>(&contents);
if let Ok(mut config) = config {
config.jinja_lsp.user_defined = true;
return Some(config.jinja_lsp);
}
}
None
}

impl JinjaConfig {
Expand Down Expand Up @@ -52,6 +76,7 @@ pub fn walkdir(config: &JinjaConfig) -> anyhow::Result<InitLsp> {
all.append(&mut backend);
let mut lsp_files = LspFiles::default();
lsp_files.config = config.clone();
lsp_files.ignore_globals = config.hide_undefined.unwrap_or(false);
if config.lang == "python" {
lsp_files.queries.update_backend(&config.lang);
lsp_files.parsers.update_backend(&config.lang);
Expand Down

0 comments on commit e2f538c

Please sign in to comment.