Skip to content

Commit

Permalink
0.0.8
Browse files Browse the repository at this point in the history
  • Loading branch information
uros-5 committed Jun 13, 2024
1 parent 4be60a6 commit 218a7af
Show file tree
Hide file tree
Showing 24 changed files with 186 additions and 62 deletions.
2 changes: 1 addition & 1 deletion jinja-lsp-nodejs/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export class NodejsLspFiles {
/** Actions can come from unsaved context. */
addGlobalContext(uri: string, actions?: Array<Action> | undefined | null): void
deleteAll(filename: string): void
addOne(id: number, filename: string, content: string, line: number): Array<JsIdentifier>
addOne(id: number, filename: string, content: string, line: number, ext: string): Array<JsIdentifier>
getVariables(id: string, line: number): Array<JsIdentifier> | null
hover(id: number, filename: string, line: number, position: JsPosition): JsHover | null
complete(id: number, filename: string, line: number, position: JsPosition): Array<JsCompletionItem> | null
Expand Down
2 changes: 1 addition & 1 deletion jinja-lsp-nodejs/npm/android-arm-eabi/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jinja-lsp/functions-android-arm-eabi",
"version": "0.0.7",
"version": "0.0.8",
"os": [
"android"
],
Expand Down
2 changes: 1 addition & 1 deletion jinja-lsp-nodejs/npm/android-arm64/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jinja-lsp/functions-android-arm64",
"version": "0.0.7",
"version": "0.0.8",
"os": [
"android"
],
Expand Down
2 changes: 1 addition & 1 deletion jinja-lsp-nodejs/npm/darwin-arm64/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jinja-lsp/functions-darwin-arm64",
"version": "0.0.7",
"version": "0.0.8",
"os": [
"darwin"
],
Expand Down
2 changes: 1 addition & 1 deletion jinja-lsp-nodejs/npm/darwin-universal/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jinja-lsp/functions-darwin-universal",
"version": "0.0.7",
"version": "0.0.8",
"os": [
"darwin"
],
Expand Down
2 changes: 1 addition & 1 deletion jinja-lsp-nodejs/npm/freebsd-x64/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jinja-lsp/functions-freebsd-x64",
"version": "0.0.7",
"version": "0.0.8",
"os": [
"freebsd"
],
Expand Down
2 changes: 1 addition & 1 deletion jinja-lsp-nodejs/npm/linux-arm-gnueabihf/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jinja-lsp/functions-linux-arm-gnueabihf",
"version": "0.0.7",
"version": "0.0.8",
"os": [
"linux"
],
Expand Down
2 changes: 1 addition & 1 deletion jinja-lsp-nodejs/npm/linux-arm-musleabihf/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jinja-lsp/functions-linux-arm-musleabihf",
"version": "0.0.7",
"version": "0.0.8",
"os": [
"linux"
],
Expand Down
2 changes: 1 addition & 1 deletion jinja-lsp-nodejs/npm/linux-arm64-gnu/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jinja-lsp/functions-linux-arm64-gnu",
"version": "0.0.7",
"version": "0.0.8",
"os": [
"linux"
],
Expand Down
2 changes: 1 addition & 1 deletion jinja-lsp-nodejs/npm/linux-arm64-musl/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jinja-lsp/functions-linux-arm64-musl",
"version": "0.0.7",
"version": "0.0.8",
"os": [
"linux"
],
Expand Down
2 changes: 1 addition & 1 deletion jinja-lsp-nodejs/npm/linux-riscv64-gnu/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jinja-lsp/functions-linux-riscv64-gnu",
"version": "0.0.7",
"version": "0.0.8",
"os": [
"linux"
],
Expand Down
2 changes: 1 addition & 1 deletion jinja-lsp-nodejs/npm/linux-x64-gnu/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jinja-lsp/functions-linux-x64-gnu",
"version": "0.0.7",
"version": "0.0.8",
"os": [
"linux"
],
Expand Down
2 changes: 1 addition & 1 deletion jinja-lsp-nodejs/npm/linux-x64-musl/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jinja-lsp/functions-linux-x64-musl",
"version": "0.0.7",
"version": "0.0.8",
"os": [
"linux"
],
Expand Down
2 changes: 1 addition & 1 deletion jinja-lsp-nodejs/npm/win32-arm64-msvc/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jinja-lsp/functions-win32-arm64-msvc",
"version": "0.0.7",
"version": "0.0.8",
"os": [
"win32"
],
Expand Down
2 changes: 1 addition & 1 deletion jinja-lsp-nodejs/npm/win32-ia32-msvc/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jinja-lsp/functions-win32-ia32-msvc",
"version": "0.0.7",
"version": "0.0.8",
"os": [
"win32"
],
Expand Down
2 changes: 1 addition & 1 deletion jinja-lsp-nodejs/npm/win32-x64-msvc/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jinja-lsp/functions-win32-x64-msvc",
"version": "0.0.7",
"version": "0.0.8",
"os": [
"win32"
],
Expand Down
4 changes: 2 additions & 2 deletions jinja-lsp-nodejs/package-lock.json

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-nodejs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jinja-lsp/functions",
"version": "0.0.7",
"version": "0.0.8",
"main": "index.js",
"types": "index.d.ts",
"napi": {
Expand Down
99 changes: 75 additions & 24 deletions jinja-lsp-nodejs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use jinja_lsp_queries::{
parsers::Parsers,
search::{
objects::{objects_query, CompletionType, JinjaObject},
python_identifiers::PythonIdentifier,
queries::Queries,
snippets_completion::snippets,
Identifier, IdentifierType,
Expand Down Expand Up @@ -59,8 +60,11 @@ pub struct NodejsLspFiles {
impl NodejsLspFiles {
#[napi(constructor)]
pub fn new() -> Self {
let mut lsp_files2 = LspFiles::default();
lsp_files2.parsers.update_backend("python");
lsp_files2.queries.update_backend("python");
Self {
lsp_files: LspFiles::default(),
lsp_files: lsp_files2,
counter: 0,
filters: init_filter_completions(),
_snippets: snippets(),
Expand Down Expand Up @@ -102,43 +106,77 @@ impl NodejsLspFiles {
filename: String,
content: String,
line: u32,
ext: String,
) -> Vec<JsIdentifier> {
let mut all_identifiers = vec![];
let params: DidOpenTextDocumentParams = DidOpenTextDocumentParams {
text_document: TextDocumentItem::new(
Url::parse(&format!("file:///home/{filename}.{id}.jinja")).unwrap(),
Url::parse(&format!("file:///home/{filename}.{id}.{ext}")).unwrap(),
String::new(),
0,
content,
),
};
self.lsp_files.did_open(params);
let objects = self
.lsp_files
.read_objects(Url::parse(&format!("file:///home/{filename}.{id}.jinja")).unwrap());
if let Some(objects) = objects {
if let Some(global_actions) = self.action_objects.get(&filename.to_string()) {
for obj in &objects {
let action_object = ActionObject::from(obj);
for global_action in global_actions {
if global_action.compare(&action_object.fields) {
let mut start = JsPosition::from(obj.location.0);
let mut end = JsPosition::from(obj.last_field_end());
start.line += line;
end.line += line;
let identifier = JsIdentifier {
start,
end,
name: obj.name.to_owned(),
identifier_type: JsIdentifierType::Link,
error: None,
};
all_identifiers.push(identifier);
match ext.as_str() {
"jinja" => {
let objects = self
.lsp_files
.read_objects(Url::parse(&format!("file:///home/{filename}.{id}.{ext}")).unwrap());
if let Some(objects) = objects {
if let Some(global_actions) = self.action_objects.get(&filename.to_string()) {
for obj in &objects {
let action_object = ActionObject::from(obj);
for global_action in global_actions {
if global_action.compare(&action_object.fields) {
let mut start = JsPosition::from(obj.location.0);
let mut end = JsPosition::from(obj.last_field_end());
start.line += line;
end.line += line;
let identifier = JsIdentifier {
start,
end,
name: obj.name.to_owned(),
identifier_type: JsIdentifierType::Link,
error: None,
};
all_identifiers.push(identifier);
}
}
}
}
}
}
}
"py" => {
if let Some(ids) = self
.lsp_files
.read_python_ids(Url::parse(&format!("file:///home/{filename}.{id}.{ext}")).unwrap())
{
if let Some(global_actions) = self.action_objects.get(&filename.to_string()) {
for obj in &ids {
let action_object = ActionObject::from(obj);
for global_action in global_actions {
if global_action.compare(&action_object.fields) {
let mut start = JsPosition::from(obj.start);
let mut end = JsPosition::from(obj.end);
start.line += line;
end.line += line;
let identifier = JsIdentifier {
start,
end,
name: obj.field.to_owned(),
identifier_type: JsIdentifierType::Link,
error: None,
};
all_identifiers.push(identifier);
}
}
}
}
}
}
_ => (),
};
// let query = &self.lsp_files.queries.jinja_objects;
// let objects = objects_query(query, &tree, Point::new(0, 0), &content, true);
// let objects = objects.show();
Expand Down Expand Up @@ -630,6 +668,19 @@ impl From<&JinjaObject> for ActionObject {
}
}

impl From<&PythonIdentifier> for ActionObject {
fn from(value: &PythonIdentifier) -> Self {
let mut fields = vec![];
let parts = value.field.split('.');
for field in parts {
fields.push(field.to_string());
}
// fields.push(value.name.to_owned());
// for field in &value.fields {}
Self { fields }
}
}

impl ActionObject {
pub fn compare(&self, fields: &Vec<String>) -> bool {
&self.fields == fields
Expand Down
2 changes: 1 addition & 1 deletion jinja-lsp-queries/src/search/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use self::objects::JinjaObject;

pub mod definition;
pub mod objects;
mod python_identifiers;
pub mod python_identifiers;
pub mod queries;
pub mod rust_identifiers;
pub mod rust_template_completion;
Expand Down
80 changes: 68 additions & 12 deletions jinja-lsp-queries/src/search/python_identifiers.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,80 @@
use std::collections::HashMap;

use tree_sitter::{Point, Query, QueryCursor, Tree};

pub fn _python_identifiers(
pub struct PythonAttributes {
pub attributes: HashMap<Point, Vec<PythonIdentifier>>,
}

impl PythonAttributes {
pub fn merge(&self, line: u32) -> Vec<PythonIdentifier> {
let mut identifiers = vec![];
for i in &self.attributes {
let mut start = i.0.to_owned();
start.row += line as usize;
let mut end = i.0;
let mut name = String::new();
let len = i.1.len();
for (index, identifier) in i.1.iter().enumerate() {
name.push_str(&identifier.field);
end = &identifier.end;
if index != len - 1 {
name.push('.');
}
}
let mut end = end.to_owned();
end.row += line as usize;
let identifier = PythonIdentifier {
id: 0,
start,
end,
field: name,
};
identifiers.push(identifier);
}
identifiers
}
}

#[derive(Default, Debug, Clone, PartialEq, PartialOrd, Ord, Eq)]
pub struct PythonIdentifier {
pub id: usize,
pub start: Point,
pub end: Point,
pub field: String,
}

pub fn python_identifiers(
query: &Query,
tree: &Tree,
mut _trigger_point: Point,
text: &str,
all: bool,
) {
line: u32,
) -> Vec<PythonIdentifier> {
let closest_node = tree.root_node();
let mut cursor_qry = QueryCursor::new();
let _capture_names = query.capture_names();
let mut attributes = PythonAttributes {
attributes: HashMap::new(),
};
let matches = cursor_qry.matches(query, closest_node, text.as_bytes());
let captures = matches.into_iter().flat_map(|m| {
m.captures
.iter()
.filter(|capture| all || capture.node.start_position() <= _trigger_point)
});
for _capture in captures {
// if check.is_none() {
// break;
// }
for i in matches {
for capture in i.captures {
if let Some(parent) = capture.node.parent() {
let attribute = attributes
.attributes
.entry(parent.start_position())
.or_default();
let field = capture.node.utf8_text(text.as_bytes()).unwrap_or_default();
let identifier = PythonIdentifier {
id: capture.node.id(),
start: capture.node.start_position(),
end: capture.node.end_position(),
field: field.to_string(),
};
attribute.push(identifier);
}
}
}
attributes.merge(line)
}
Loading

0 comments on commit 218a7af

Please sign in to comment.