Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

go to def in function #69

Open
wants to merge 63 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
5f66905
skip trivial first tokens in parsing
NTTVy03 Nov 9, 2024
6913c55
remove comment in input test
NTTVy03 Nov 13, 2024
b2a750f
Merge branch 'master' of https://github.com/vuvoth/ccls
NTTVy03 Nov 18, 2024
caac481
manage out-of-bound case, update input test
NTTVy03 Nov 18, 2024
8efa4f7
update input test
NTTVy03 Nov 18, 2024
c78761e
update input test
NTTVy03 Dec 6, 2024
bfd1f15
make test programs private
NTTVy03 Dec 8, 2024
e054b57
remove comments in syntax tests
NTTVy03 Dec 8, 2024
15c9e5f
format
NTTVy03 Dec 8, 2024
c76246b
make Pragma optional, remove ROOT
NTTVy03 Dec 8, 2024
cd125ab
rename close() params, use advance() in eat(), fix typo scope
NTTVy03 Dec 8, 2024
15470b1
parse params using list_identity
NTTVy03 Dec 8, 2024
6dbb51a
return Option in token_value() and position_of()
NTTVy03 Dec 10, 2024
6d5f0c6
do not allow <--, <== in var declaration
NTTVy03 Dec 10, 2024
fbdc5b9
make public signal optional in main component
NTTVy03 Dec 10, 2024
07c4171
fix format before merge
NTTVy03 Dec 13, 2024
0bf3a04
remove empty test
NTTVy03 Dec 17, 2024
f1b0c39
replace expect by eat in block
NTTVy03 Dec 17, 2024
a10be25
add scope parsing test
NTTVy03 Dec 17, 2024
47df964
fix space in scope parsing test
NTTVy03 Dec 17, 2024
5c6cb56
update snapshot test for syntax
NTTVy03 Dec 19, 2024
372efd3
refactor syntax test
NTTVy03 Dec 19, 2024
46929c2
update input tests with snapshot
NTTVy03 Dec 20, 2024
d811037
change param type in find signal into str
NTTVy03 Dec 25, 2024
cafff02
add snapshot guild
NTTVy03 Dec 27, 2024
03da99a
add some combine arithmetic operators
NTTVy03 Dec 27, 2024
7792a96
error report in expect and expect_any
NTTVy03 Dec 27, 2024
6ac9ac9
update snapshot for combine operators
NTTVy03 Dec 27, 2024
ec73df8
update snapshot test
NTTVy03 Dec 28, 2024
5bdda2f
fix wrap trivial tokens before open a tree
NTTVy03 Dec 28, 2024
8e4853d
update template happy test
NTTVy03 Dec 28, 2024
bb95297
add operators into token kinds
NTTVy03 Dec 28, 2024
6949ca6
Merge branch 'fix-parser' into master
NTTVy03 Dec 30, 2024
7753149
Merge pull request #1 from vuvoth/master
NTTVy03 Dec 30, 2024
97bb689
add operators test
NTTVy03 Dec 30, 2024
179f6bc
re-priority token kinds
NTTVy03 Dec 30, 2024
40268bf
fix clippy check
NTTVy03 Dec 30, 2024
156b872
fix clippy check
NTTVy03 Dec 30, 2024
d18fba0
rebase add-token-kinds
NTTVy03 Dec 30, 2024
84e960b
fix clippy check
NTTVy03 Dec 30, 2024
d9f369e
Merge branch 'master' of https://github.com/NTTVy03/ccls
NTTVy03 Dec 30, 2024
a1bd25e
fix clippy
NTTVy03 Dec 30, 2024
3a08797
remove duplicate declaration in grammar tests
NTTVy03 Dec 30, 2024
39f1e12
add missing signal assign tokens
NTTVy03 Dec 30, 2024
f4db959
fix grammar of for statement
NTTVy03 Dec 30, 2024
a9edb76
add statements test
NTTVy03 Dec 30, 2024
c5a17c3
fix declaration, support matrix
NTTVy03 Dec 30, 2024
605c16b
fix format
NTTVy03 Dec 30, 2024
c9d93f7
refactor declaration
NTTVy03 Dec 31, 2024
8e92b83
use list identity parser in tuple
NTTVy03 Dec 31, 2024
6f6d678
fix right curly count in block
NTTVy03 Dec 31, 2024
df3113d
extract function params
NTTVy03 Dec 31, 2024
f68d36a
add declaration test
NTTVy03 Dec 31, 2024
ccc7226
comment statement in declaration test
NTTVy03 Jan 2, 2025
9a17705
fix format
NTTVy03 Jan 2, 2025
09bb4fa
add function parse test
NTTVy03 Jan 2, 2025
8089756
add comments
NTTVy03 Jan 6, 2025
0bf4056
comments: example in goto_definition_handler
NTTVy03 Jan 15, 2025
13769d6
go-to-defitition in function
NTTVy03 Jan 15, 2025
e8da632
lookup in params
NTTVy03 Jan 15, 2025
ca35cc2
remove lookup signal in function
NTTVy03 Jan 15, 2025
5ca451e
merge master after PR #68 into go-to-def-in-function
NTTVy03 Feb 3, 2025
37c39e5
merge main after PR #66
NTTVy03 Feb 9, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
200 changes: 195 additions & 5 deletions crates/lsp/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use lsp_types::{Position, Range, Url};
use rowan::{ast::AstNode, TextSize};
use syntax::{
abstract_syntax_tree::{
AstCircomProgram, AstComponentDecl, AstInputSignalDecl, AstOutputSignalDecl, AstSignalDecl,
AstTemplateDef, AstVarDecl,
AstCircomProgram, AstComponentDecl, AstFunctionDef, AstInputSignalDecl,
AstOutputSignalDecl, AstSignalDecl, AstTemplateDef, AstVarDecl,
},
syntax_node::{SyntaxNode, SyntaxToken},
};
Expand Down Expand Up @@ -160,8 +160,10 @@ impl SemanticLocations {
}
}

// template
#[derive(Debug, Clone)]
pub struct TemplateDataSemantic {
pub param: SemanticLocations,
pub signal: SemanticLocations,
pub variable: SemanticLocations,
pub component: SemanticLocations,
Expand All @@ -170,27 +172,61 @@ pub struct TemplateDataSemantic {
impl TemplateDataSemantic {
fn new() -> Self {
Self {
param: SemanticLocations::new(),
signal: SemanticLocations::new(),
variable: SemanticLocations::new(),
component: SemanticLocations::new(),
}
}
}

// function
#[derive(Debug, Clone)]
pub struct FunctionDataSemantic {
pub param: SemanticLocations,
// TODO: Functions cannot declare signals or generate constraints
pub variable: SemanticLocations,
pub component: SemanticLocations,
}

impl FunctionDataSemantic {
fn new() -> Self {
Self {
param: SemanticLocations::new(),
variable: SemanticLocations::new(),
component: SemanticLocations::new(),
}
}
}

#[derive(Debug, Clone)]
pub struct SemanticData {
pub template: SemanticLocations,
pub template_data_semantic: HashMap<Id, TemplateDataSemantic>,

pub function: SemanticLocations,
pub function_data_semantic: HashMap<Id, FunctionDataSemantic>,
}

pub enum TemplateDataInfo {
Param((Id, Range)),
Signal((Id, Range)),
Variable((Id, Range)),
Component((Id, Range)),
}

pub enum FunctionDataInfo {
Param((Id, Range)),
Variable((Id, Range)),
Component((Id, Range)),
}

pub enum SemanticInfo {
Template((Id, Range)),
TemplateData((Id, TemplateDataInfo)),

Function((Id, Range)),
FunctionData((Id, FunctionDataInfo)),
}

#[derive(Debug, Clone)]
Expand All @@ -215,6 +251,8 @@ impl SemanticDB {
let semantic = self.semantic.entry(file_id).or_insert(SemanticData {
template: SemanticLocations::new(),
template_data_semantic: HashMap::new(),
function: SemanticLocations::new(),
function_data_semantic: HashMap::new(),
});

match semantic_info {
Expand All @@ -233,6 +271,24 @@ impl SemanticDB {
}
TemplateDataInfo::Variable((id, r)) => template_semantic.variable.insert(id, r),
TemplateDataInfo::Signal((id, r)) => template_semantic.signal.insert(id, r),
TemplateDataInfo::Param((id, r)) => template_semantic.param.insert(id, r),
}
}
SemanticInfo::Function((id, range)) => {
semantic.function.insert(id, range);
}
SemanticInfo::FunctionData((function_id, function_data_info)) => {
let function_semantic = semantic
.function_data_semantic
.entry(function_id)
.or_insert(FunctionDataSemantic::new());

match function_data_info {
FunctionDataInfo::Component((id, r)) => {
function_semantic.component.insert(id, r)
}
FunctionDataInfo::Variable((id, r)) => function_semantic.variable.insert(id, r),
FunctionDataInfo::Param((id, r)) => function_semantic.param.insert(id, r),
}
}
}
Expand All @@ -253,11 +309,37 @@ impl SemanticDB {
self.template_semantic(file_db, &template);
}
}

for function in abstract_syntax_tree.function_list() {
if let Some(name) = function.function_name() {
let function_id = name.syntax().token_id();
self.insert(
file_db.file_id,
SemanticInfo::Function((function_id, file_db.range(function.syntax()))),
);
self.function_semantic(file_db, &function);
}
}
}

pub fn template_semantic(&mut self, file_db: &FileDB, ast_template: &AstTemplateDef) {
let template_id = ast_template.syntax().token_id();

if let Some(params) = ast_template.parameter_list() {
for param_name in params.parameters() {
self.insert(
file_db.file_id,
SemanticInfo::TemplateData((
template_id,
TemplateDataInfo::Param((
param_name.syntax().token_id(),
file_db.range(param_name.syntax()),
)),
)),
);
}
};

if let Some(statements) = ast_template.statements() {
for signal in statements.find_children::<AstInputSignalDecl>() {
if let Some(name) = signal.signal_identifier().unwrap().name() {
Expand Down Expand Up @@ -336,25 +418,99 @@ impl SemanticDB {
}
}
}

pub fn function_semantic(&mut self, file_db: &FileDB, ast_function: &AstFunctionDef) {
let function_id = ast_function.syntax().token_id();

if let Some(params) = ast_function.parameter_list() {
for param_name in params.parameters() {
self.insert(
file_db.file_id,
SemanticInfo::FunctionData((
function_id,
FunctionDataInfo::Param((
param_name.syntax().token_id(),
file_db.range(param_name.syntax()),
)),
)),
);
}
};

if let Some(statements) = ast_function.statements() {
// function does not contains signal decalrations --> skip signals

for var in statements.find_children::<AstVarDecl>() {
if let Some(name) = var.var_identifier().unwrap().name() {
self.insert(
file_db.file_id,
SemanticInfo::FunctionData((
function_id,
FunctionDataInfo::Variable((
name.syntax().token_id(),
file_db.range(var.syntax()),
)),
)),
);
}
}

for component in statements.find_children::<AstComponentDecl>() {
if let Some(component_var) = component.component_identifier() {
if let Some(name) = component_var.name() {
self.insert(
file_db.file_id,
SemanticInfo::FunctionData((
function_id,
FunctionDataInfo::Component((
name.syntax().token_id(),
file_db.range(component.syntax()),
)),
)),
);
}
}
}
}
}
}

impl SemanticData {
pub fn lookup_signal(&self, template_id: Id, signal: &SyntaxToken) -> Option<&Vec<Range>> {
pub fn lookup_template_param(
&self,
template_id: Id,
signal: &SyntaxToken,
) -> Option<&Vec<Range>> {
if let Some(semantic_template) = self.template_data_semantic.get(&template_id) {
return semantic_template.param.0.get(&signal.token_id());
}
None
}

pub fn lookup_template_signal(
&self,
template_id: Id,
signal: &SyntaxToken,
) -> Option<&Vec<Range>> {
if let Some(semantic_template) = self.template_data_semantic.get(&template_id) {
return semantic_template.signal.0.get(&signal.token_id());
}
None
}

// TODO: remove duplicate code here.
pub fn lookup_variable(&self, template_id: Id, variable: &SyntaxToken) -> Option<&Vec<Range>> {
pub fn lookup_template_variable(
&self,
template_id: Id,
variable: &SyntaxToken,
) -> Option<&Vec<Range>> {
if let Some(semantic_template) = self.template_data_semantic.get(&template_id) {
return semantic_template.variable.0.get(&variable.token_id());
}
None
}

pub fn lookup_component(
pub fn lookup_template_component(
&self,
template_id: Id,
component: &SyntaxToken,
Expand All @@ -364,6 +520,40 @@ impl SemanticData {
}
None
}

// ------------- function
pub fn lookup_function_param(
&self,
function_id: Id,
signal: &SyntaxToken,
) -> Option<&Vec<Range>> {
if let Some(semantic_function) = self.function_data_semantic.get(&function_id) {
return semantic_function.param.0.get(&signal.token_id());
}
None
}

pub fn lookup_function_variable(
&self,
function_id: Id,
variable: &SyntaxToken,
) -> Option<&Vec<Range>> {
if let Some(semantic_function) = self.function_data_semantic.get(&function_id) {
return semantic_function.variable.0.get(&variable.token_id());
}
None
}

pub fn lookup_function_component(
&self,
function_id: Id,
component: &SyntaxToken,
) -> Option<&Vec<Range>> {
if let Some(semantic_function) = self.function_data_semantic.get(&function_id) {
return semantic_function.component.0.get(&component.token_id());
}
None
}
}

#[cfg(test)]
Expand Down
Loading