Skip to content

Commit

Permalink
Hot fix 1 (#54)
Browse files Browse the repository at this point in the history
* fix hash and semantic query map

* hot fix
  • Loading branch information
vuvoth authored Mar 1, 2024
1 parent 6fc1488 commit 2a40402
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 24 deletions.
1 change: 1 addition & 0 deletions crates/lsp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ syntax.workspace = true

anyhow = "1.0.79"
dashmap = "5.5.3"
path-absolutize = "3.1.1"

[profile]
dev.debug = 2
20 changes: 17 additions & 3 deletions crates/lsp/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,17 @@ pub struct FileDB {
pub end_line_vec: Vec<u32>,
}

use path_absolutize::*;

impl FileDB {
pub fn create(content: &str, file_path: Url) -> Self {
let mut hasher = DefaultHasher::new();
file_path.hash(&mut hasher);

file_path
.to_file_path()
.unwrap()
.absolutize()
.unwrap()
.hash(&mut hasher);
Self::new(FileId(hasher.finish()), content, file_path)
}

Expand Down Expand Up @@ -374,6 +380,13 @@ mod tests {

use super::TokenId;

#[test]
fn file_id_test() {
let file_1 = FileDB::create("a", Url::from_file_path(Path::new("/a/../a/c")).unwrap());
let file_2 = FileDB::create("a", Url::from_file_path(Path::new("/a/c")).unwrap());

assert_eq!(file_1.file_id, file_2.file_id);
}
#[test]
fn token_id_hash_test() {
let source: String = r#"pragma circom 2.0.0;
Expand All @@ -394,6 +407,7 @@ mod tests {
assert_eq!(first_id, second_id);
}
}
#[test]
fn off_set_test() {
let str = r#"
one
Expand All @@ -404,7 +418,7 @@ three
let file_utils = FileDB::new(
FileId(1),
str,
Url::from_file_path(Path::new("tmp.txt")).unwrap(),
Url::from_file_path(Path::new("/tmp.txt")).unwrap(),
);

let position = Position::new(0, 1);
Expand Down
40 changes: 21 additions & 19 deletions crates/lsp/src/global_state.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::{fs, path::PathBuf};

use crate::database::{FileDB, SemanticDB};
use crate::{
database::{FileDB, SemanticDB},
handler::goto_definition::lookup_node_wrap_token,
};
use anyhow::Result;
use dashmap::DashMap;
use lsp_server::{RequestId, Response};
Expand All @@ -11,7 +14,7 @@ use lsp_types::{

use parser::token_kind::TokenKind;
use rowan::ast::AstNode;
use syntax::abstract_syntax_tree::AstCircomProgram;
use syntax::abstract_syntax_tree::{AstCircomProgram, AstComponentDecl};
use syntax::syntax::SyntaxTreeBuilder;
use syntax::syntax_node::SyntaxToken;

Expand Down Expand Up @@ -68,8 +71,6 @@ impl GlobalState {
ast: &AstCircomProgram,
token: &SyntaxToken,
) -> Vec<Location> {
eprintln!("token {}", token.text());

let semantic_data = self.db.semantic.get(&root.file_id).unwrap();
let mut result = lookup_definition(root, ast, semantic_data, token);

Expand All @@ -79,16 +80,22 @@ impl GlobalState {

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 lib_url = Url::from_file_path(lib_path.clone()).unwrap();

if let Some(file_lib) = self.file_map.get(&lib_url.to_string()) {
let ast_lib = self.ast_map.get(&lib_url.to_string()).unwrap();
let semantic_data_lib = self.db.semantic.get(&file_lib.file_id).unwrap();
let lib_result = lookup_definition(&file_lib, &ast_lib, semantic_data_lib, token);
result.extend(lib_result);
if lookup_node_wrap_token(TokenKind::ComponentDecl, token).is_some()
|| lookup_node_wrap_token(TokenKind::ComponentCall, token).is_some()
{
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 lib_url = Url::from_file_path(lib_path.clone()).unwrap();

if let Some(file_lib) = self.file_map.get(&lib_url.to_string()) {
let ast_lib = self.ast_map.get(&lib_url.to_string()).unwrap();
if let Some(semantic_data_lib) = self.db.semantic.get(&file_lib.file_id) {
let lib_result =
lookup_definition(&file_lib, &ast_lib, semantic_data_lib, token);
result.extend(lib_result);
}
}
}
}
result
Expand Down Expand Up @@ -120,7 +127,6 @@ impl GlobalState {
}

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

Expand All @@ -129,13 +135,11 @@ impl GlobalState {
let file_id = file_db.file_id;

let p: PathBuf = file_db.get_path();
eprintln!("syntax...");
if let Some(ast) = AstCircomProgram::cast(syntax) {
self.db.semantic.remove(&file_id);
self.db.circom_program_semantic(&file_db, &ast);

for lib in ast.libs() {
eprintln!("{:?}", lib.syntax().text());
if let Some(lib_abs_path) = lib.lib() {
let lib_path = p.parent().unwrap().join(lib_abs_path.value()).clone();
let lib_url = Url::from_file_path(lib_path.clone()).unwrap();
Expand All @@ -146,7 +150,6 @@ impl GlobalState {
};
let lib_file = FileDB::create(&text_doc.text, lib_url.clone());
let syntax = SyntaxTreeBuilder::syntax_tree(&text_doc.text);
eprintln!("{}", syntax.text());

if let Some(lib_ast) = AstCircomProgram::cast(syntax) {
self.db.semantic.remove(&lib_file.file_id);
Expand All @@ -163,7 +166,6 @@ impl GlobalState {

self.file_map.insert(url.to_string(), file_db);

eprintln!("Finish {}", text_document.uri);
Ok(())
}
}
2 changes: 0 additions & 2 deletions crates/lsp/src/handler/goto_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ pub fn lookup_definition(

let mut res = Vec::new();
let mut signal_outside = false;
eprintln!("{}", token.text());
if let Some(include_lib) = lookup_node_wrap_token(TokenKind::IncludeKw, token) {
if let Some(ast_include) = AstInclude::cast(include_lib) {
if let Some(abs_lib_ans) = ast_include.lib() {
Expand All @@ -86,7 +85,6 @@ pub fn lookup_definition(
}
}

eprintln!("{}", token.text());
if let Some(component_call) = lookup_node_wrap_token(TokenKind::ComponentCall, token) {
// find template called.
if let Some(ast_component_call) = AstComponentCall::cast(component_call) {
Expand Down

0 comments on commit 2a40402

Please sign in to comment.