Skip to content

Commit

Permalink
Merge pull request #9 from uros-5/include_completion
Browse files Browse the repository at this point in the history
added completion for included templates
  • Loading branch information
uros-5 authored Mar 12, 2024
2 parents 5767bf1 + 399dcc7 commit 827f447
Show file tree
Hide file tree
Showing 14 changed files with 206 additions and 436 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

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

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.6"
version = "0.1.61"
edition = "2021"
description = "TreeSitter queries for jinja-lsp"
license = "MIT"
Expand Down
122 changes: 112 additions & 10 deletions jinja-lsp-queries/src/capturer/included.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use tower_lsp::lsp_types::Url;
use tower_lsp::lsp_types::{Position, Range, Url};
use tree_sitter::Point;

use super::Capturer;
use super::{object::CompletionType, Capturer};

#[derive(Default, Debug)]
pub struct IncludeCapturer {
Expand All @@ -11,15 +11,35 @@ pub struct IncludeCapturer {
impl IncludeCapturer {
pub fn in_template(&self, trigger_point: Point) -> Option<&IncludedTemplate> {
if let Some(last) = self.included.last() {
if trigger_point >= last.range.0 && trigger_point <= last.range.1 {
if trigger_point >= last.template.0 && trigger_point <= last.template.1 {
return Some(last);
}
}
None
}

pub fn find(&self, trigger_point: Point) -> Option<(&IncludedTemplate, Include)> {
if let Some(string) = self.in_template(trigger_point) {
return Some((string, Include::Template));
}
None
}

pub fn completion(&self, trigger_point: Point) -> Option<CompletionType> {
let part = self.find(trigger_point)?;
let location = part.1.location(trigger_point, part.0);
Some(CompletionType::IncludedTemplate {
name: location.0,
range: location.1,
})
}

pub fn add_template(&mut self, name: String, range: (Point, Point)) {
self.included.push(IncludedTemplate { name, range });
self.included.push(IncludedTemplate {
name,
template: range,
..Default::default()
});
}

pub fn last(&self) -> Option<&String> {
Expand All @@ -35,12 +55,33 @@ impl Capturer for IncludeCapturer {
source: &str,
) {
let key = capture_names[capture.index as usize].to_owned();
if key == "template" {
if let Ok(value) = capture.node.utf8_text(source.as_bytes()) {
if key == "keyword" {
self.add_template("".to_owned(), (Point::default(), Point::default()));
} else if key == "error" {
if let Some(last) = self.included.last_mut() {
let start = capture.node.start_position();
let end = capture.node.end_position();
last.error = (start, end);
}
} else if key == "id" {
if let Some(last) = self.included.last_mut() {
let start = capture.node.start_position();
let end = capture.node.end_position();
let name = value.replace(['\'', '\"'], "");
self.add_template(name, (start, end));
last.identifier = (start, end);
if let Ok(value) = capture.node.utf8_text(source.as_bytes()) {
let name = value.replace(['\'', '\"'], "");
last.name = name;
}
}
} else if key == "template" {
if let Ok(value) = capture.node.utf8_text(source.as_bytes()) {
if let Some(last) = self.included.last_mut() {
let start = capture.node.start_position();
let end = capture.node.end_position();
let name = value.replace(['\'', '\"'], "");
last.name = name;
last.template = (start, end);
}
}
}
}
Expand All @@ -49,14 +90,17 @@ impl Capturer for IncludeCapturer {
#[derive(Default, Debug)]
pub struct IncludedTemplate {
pub name: String,
pub range: (Point, Point),
pub template: (Point, Point),
pub error: (Point, Point),
pub identifier: (Point, Point),
}

impl IncludedTemplate {
pub fn new(name: &str) -> Self {
Self {
name: name.to_owned(),
range: (Point::default(), Point::default()),
template: (Point::default(), Point::default()),
..Default::default()
}
}

Expand All @@ -68,3 +112,61 @@ impl IncludedTemplate {
Some(uri)
}
}

#[derive(Debug)]
pub enum Include {
Id,
Template,
Error,
}

impl Include {
pub fn location(&self, trigger_point: Point, part: &IncludedTemplate) -> (String, Range) {
match self {
Include::Error => {
let range = self.to_range(part.error);
(String::from(""), range)
}
Include::Id => {
let l1 = part.identifier.1.column - trigger_point.column;
if part.name.len() < l1 {
let range = self.to_range(part.identifier);
return (String::from(""), range);
}
let end = part.name.len() - l1;
let mut name = String::new();
for (i, item) in part.name.char_indices() {
name.push(item);
if i == end {
break;
}
}
let range = self.to_range(part.identifier);
(name, range)
}
Include::Template => {
let l1 = part.template.1.column - trigger_point.column;
if part.name.len() < l1 || part.name.is_empty() {
let range = self.to_range(part.template);
return (String::from(""), range);
}
let end = part.name.len() - l1;
let mut name = String::new();
for (i, item) in part.name.char_indices() {
name.push(item);
if i == end {
break;
}
}
let range = self.to_range(part.template);
(name, range)
}
}
}

pub fn to_range(&self, points: (Point, Point)) -> Range {
let start = Position::new(points.0.row as u32, points.0.column as u32);
let end = Position::new(points.1.row as u32, points.1.column as u32);
Range::new(start, end)
}
}
4 changes: 3 additions & 1 deletion jinja-lsp-queries/src/capturer/object.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use tower_lsp::lsp_types::Range;
use tree_sitter::{Point, QueryCapture};

use super::Capturer;
Expand Down Expand Up @@ -134,10 +135,11 @@ impl JinjaObject {
}
}

#[derive(PartialEq, Eq, PartialOrd, Ord, Debug)]
#[derive(PartialEq, Debug)]
pub enum CompletionType {
Filter,
Identifier,
IncludedTemplate { name: String, range: Range },
}

static VALID_IDENTIFIERS: [&str; 4] = ["loop", "true", "false", "not"];
30 changes: 18 additions & 12 deletions jinja-lsp-queries/src/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,22 @@ pub static RUST: &str = r#"
"#;

pub static IMPORTS: &str = r#"
(
[
(statement
(statement_begin)
(keyword) @keyword
(string) @template
(statement_end)
(#eq? @keyword "include")
) @included
]
)
[
(statement
(statement_begin)
(keyword) @keyword
(string)? @template
(ERROR)? @error
(identifier)? @id
(statement_end)? @end
(#eq? @keyword "include")
)
(statement_begin)
(keyword) @keyword
(string)? @template
(ERROR)? @error
(identifier)? @id
(statement_end)? @end
(#eq? @keyword "include")
]
"#;
11 changes: 11 additions & 0 deletions jinja-lsp-queries/src/test_queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,4 +227,15 @@ mod query_tests {
assert_eq!(&template.unwrap().name, &case.1);
}
}

// #[test]
// fn included_template_completion() {
// let source = r#"
// <div>
// {% include "
// </div>
// "#;

// let cases = [(Point::new(2, 28), false, true, false)];
// }
}
4 changes: 2 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.6"
version = "0.1.61"
edition = "2021"
license = "MIT"
authors = ["uros-5"]
Expand Down Expand Up @@ -30,4 +30,4 @@ walkdir = "2.4.0"
anyhow = "1.0.75"
tree-sitter-jinja2 = "0.0.5"
tree-sitter-rust = "0.20.4"
jinja-lsp-queries = { path = "../jinja-lsp-queries", version = "0.1.6"}
jinja-lsp-queries = { path = "../jinja-lsp-queries", version = "0.1.61"}
7 changes: 7 additions & 0 deletions jinja-lsp/src/channels/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,13 @@ pub fn lsp_task(
items = Some(CompletionResponse::Array(variables));
}
}
CompletionType::IncludedTemplate { name, range } => {
if let Some(templates) =
lsp_data.read_templates(name, &config, range)
{
items = Some(CompletionResponse::Array(templates));
}
}
};
}
let _ = sender.send(items);
Expand Down
1 change: 0 additions & 1 deletion jinja-lsp/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ pub type InitLsp = (
);

pub fn walkdir(config: &JinjaConfig) -> anyhow::Result<InitLsp> {
//
let mut all = vec![config.templates.clone()];
let mut backend = config.backend.clone();
all.append(&mut backend);
Expand Down
40 changes: 0 additions & 40 deletions jinja-lsp/src/filter/md/filters/some.sh

This file was deleted.

7 changes: 0 additions & 7 deletions jinja-lsp/src/filter/md/filters/some2.sh

This file was deleted.

Loading

0 comments on commit 827f447

Please sign in to comment.