Skip to content

Commit

Permalink
jump to defition in libs (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
vuvoth authored Feb 19, 2024
1 parent 7d3d3c2 commit 3f36274
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 33 deletions.
58 changes: 45 additions & 13 deletions crates/lsp/src/global_state.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
use std::env;
use std::{fs, path::PathBuf};

use anyhow::Result;
use dashmap::DashMap;
use lsp_server::{RequestId, Response};
use lsp_types::{
DidChangeTextDocumentParams, DidOpenTextDocumentParams, GotoDefinitionParams,
GotoDefinitionResponse, Location, Url,
GotoDefinitionResponse, Location, Range, Url,
};
use parser::{
ast::{AstCircomProgram, AstNode},
parser::Parser,
syntax_node::SyntaxNode,
syntax_node::{SyntaxNode, SyntaxToken},
};

use crate::handler::{
Expand Down Expand Up @@ -53,26 +56,54 @@ impl GlobalState {
}
}

pub fn lookup_definition(
&self,
root: &FileUtils,
ast: &AstCircomProgram,
token: &SyntaxToken,
) -> Vec<Location> {
let mut result = lookup_definition(root, ast, token);

let p = root.get_path();

for lib in ast.libs() {
let lib_abs_path = PathBuf::from(lib.lib().unwrap().value());
let lib_path = p.parent().unwrap().join(lib_abs_path).clone();
let url = Url::from_file_path(lib_path.clone()).unwrap();
if let Ok(src) = fs::read_to_string(lib_path) {
let text_doc = TextDocument {
text: src,
uri: url.clone(),
};

let file = &FileUtils::create(&text_doc.text, url.clone());
let green = Parser::parse_circom(&text_doc.text);
let syntax = SyntaxNode::new_root(green);

if let Some(lib_ast) = AstCircomProgram::cast(syntax) {
let ans = lookup_definition(file, &lib_ast, token);
result.extend(ans);
}

}
}

result
}
pub fn goto_definition_handler(&self, id: RequestId, params: GotoDefinitionParams) -> Response {
let uri = params.text_document_position_params.text_document.uri;

let ast = self.ast_map.get(&uri.to_string()).unwrap();
let file = self.file_map.get(&uri.to_string()).unwrap();

let mut ranges = Vec::new();

let mut locations = Vec::new();
if let Some(token) =
lookup_token_at_postion(&file, &ast, params.text_document_position_params.position)
{
ranges = lookup_definition(&file, &ast, &token);
locations = self.lookup_definition(&file, &ast, &token);
};

let locations = ranges
.into_iter()
.map(|range| Location::new(uri.clone(), range))
.collect();

let result = Some(GotoDefinitionResponse::Array(locations));
let result: Option<GotoDefinitionResponse> = Some(GotoDefinitionResponse::Array(locations));

let result = serde_json::to_value(result).unwrap();

Expand All @@ -83,7 +114,7 @@ impl GlobalState {
}
}

pub(crate) fn handle_update(&mut self, text_document: &TextDocument) -> Result<()> {
pub(crate) fn handle_update(&self, text_document: &TextDocument) -> Result<()> {
let text = &text_document.text;
let url = text_document.uri.to_string();

Expand All @@ -93,7 +124,8 @@ impl GlobalState {
self.ast_map
.insert(url.clone(), AstCircomProgram::cast(syntax).unwrap());

self.file_map.insert(url, FileUtils::create(text));
self.file_map
.insert(url, FileUtils::create(text, text_document.uri.clone()));
Ok(())
}
}
35 changes: 26 additions & 9 deletions crates/lsp/src/handler/goto_definition.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use std::env;
use std::fs;
use std::path::PathBuf;


use lsp_types::Location;
use lsp_types::{Position, Range};
use parser::{
ast::{
AstCircomProgram, AstComponentCall, AstNode, AstTemplateDef,
AstTemplateName,
},
ast::{AstCircomProgram, AstComponentCall, AstNode, AstTemplateDef, AstTemplateName},
syntax_node::{SyntaxNode, SyntaxToken},
token_kind::TokenKind,
};
use rowan::{SyntaxText};
use rowan::SyntaxText;

use super::lsp_utils::FileUtils;

Expand Down Expand Up @@ -62,7 +62,7 @@ pub fn lookup_definition(
file: &FileUtils,
ast: &AstCircomProgram,
token: &SyntaxToken,
) -> Vec<Range> {
) -> Vec<Location> {
eprintln!("{token:?}");
let template_list = ast.template_list();

Expand Down Expand Up @@ -138,7 +138,13 @@ pub fn lookup_definition(
}
}
}
res

let locations = res
.into_iter()
.map(|range| Location::new(file.file_path.clone(), range))
.collect();

locations
}

fn lookup_signal_in_template(
Expand Down Expand Up @@ -178,6 +184,9 @@ fn lookup_signal_in_template(
}
#[cfg(test)]
mod tests {
use std::path::Path;

use lsp_types::Url;
use parser::{
ast::AstCircomProgram, parser::Parser, syntax_node::SyntaxNode, token_kind::TokenKind,
};
Expand Down Expand Up @@ -230,7 +239,7 @@ template Y() {
"#
.to_string();

let file = FileUtils::create(&source);
let file = FileUtils::create(&source, Url::from_file_path(Path::new("tmp")).unwrap());

let green_node = Parser::parse_circom(&source);
let syntax_node = SyntaxNode::new_root(green_node.clone());
Expand All @@ -257,4 +266,12 @@ template Y() {
}
}
}

#[test]
fn url_test() {
let url = Url::from_file_path(Path::new("/hello/abc.tx"));
let binding = url.unwrap();
let p = binding.path();
println!("{:?}", Path::new(p).parent());
}
}
37 changes: 28 additions & 9 deletions crates/lsp/src/handler/lsp_utils.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
use lsp_types::{Position, Range};
use std::path::{self, Path, PathBuf};

use lsp_types::{Position, Range, Url};
use parser::syntax_node::SyntaxNode;
use rowan::TextSize;

pub struct FileId(u32);

pub struct FileUtils {
file_id: FileId,
end_line_vec: Vec<u32>,
pub file_id: FileId,
pub file_path: Url,
pub end_line_vec: Vec<u32>,
}

impl FileUtils {
pub fn create(content: &str) -> Self {
Self::new(FileId(0), content)
pub fn create(content: &str, file_path: Url) -> Self {
Self::new(FileId(0), content, file_path)
}

pub(super) fn new(file_id: FileId, content: &str) -> Self {
pub(super) fn new(file_id: FileId, content: &str, file_path: Url) -> Self {
let mut file_utils = Self {
file_id,
file_path,
end_line_vec: Vec::new(),
};

Expand All @@ -29,6 +33,11 @@ impl FileUtils {
file_utils
}

pub fn get_path(&self) -> PathBuf {
let p = self.file_path.path();
PathBuf::from(p)
}

pub fn off_set(&self, position: Position) -> TextSize {
if position.line == 0 {
return position.character.into();
Expand Down Expand Up @@ -63,7 +72,9 @@ impl FileUtils {

#[cfg(test)]
mod tests {
use lsp_types::Position;
use std::path::Path;

use lsp_types::{Position, Url};

use crate::handler::lsp_utils::{FileId, FileUtils};

Expand All @@ -75,7 +86,11 @@ two
three
"#;

let file_utils = FileUtils::new(FileId(1), str);
let file_utils = FileUtils::new(
FileId(1),
str,
Url::from_file_path(Path::new("tmp.txt")).unwrap(),
);

let position = Position::new(0, 1);

Expand All @@ -95,7 +110,11 @@ three
"#;

// 0, 4, 8
let file_utils = FileUtils::new(FileId(1), str);
let file_utils = FileUtils::new(
FileId(1),
str,
Url::from_file_path(Path::new("tmp.txt")).unwrap(),
);
assert_eq!(Position::new(1, 1), file_utils.position(2.into()));
assert_eq!(Position::new(0, 0), file_utils.position(0.into()));
}
Expand Down
27 changes: 25 additions & 2 deletions crates/parser/src/ast.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::syntax_node::{CircomLanguage};
use crate::syntax_node::CircomLanguage;
use logos::Source;
pub use rowan::ast::{support, AstChildren, AstNode};
use rowan::{SyntaxText};
use rowan::SyntaxText;

use crate::{
syntax_node::SyntaxNode,
Expand Down Expand Up @@ -261,6 +262,12 @@ impl AstCircomProgram {
pub fn pragma(&self) -> Option<AstPragma> {
self.syntax().children().find_map(AstPragma::cast)
}
pub fn libs(&self) -> Vec<AstInclude> {
self.syntax()
.children()
.filter_map(AstInclude::cast)
.collect()
}

pub fn template_list(&self) -> Vec<AstTemplateDef> {
self.syntax()
Expand Down Expand Up @@ -309,3 +316,19 @@ impl AstComponentIdentifier {
support::child(self.syntax())
}
}

ast_node!(AstCircomString, CircomString);
impl AstCircomString {
pub fn value(&self) -> String {
let text = self.syntax().to_string();

text.slice(1..text.len() - 1).unwrap().to_string()
}
}
ast_node!(AstInclude, IncludeKw);

impl AstInclude {
pub fn lib(&self) -> Option<AstCircomString> {
support::child(self.syntax())
}
}
Empty file added crates/syntax/src/utils.rs
Empty file.

0 comments on commit 3f36274

Please sign in to comment.