Skip to content

Commit

Permalink
add type declaration to v2 schema
Browse files Browse the repository at this point in the history
Summary: as per title

Reviewed By: alanz

Differential Revision: D55076701

fbshipit-source-id: db4054b61cb04667cf90f4fcf2638c54a9f10228
  • Loading branch information
perehonchuk authored and facebook-github-bot committed Apr 5, 2024
1 parent 807c296 commit a43040f
Showing 1 changed file with 46 additions and 2 deletions.
48 changes: 46 additions & 2 deletions crates/elp/src/bin/glean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@ pub(crate) enum Declaration {
FunctionDeclaration(FuncDecl),
#[serde(rename = "macro")]
MacroDeclaration(MacroDecl),
#[serde(rename = "ttype")]
TypeDeclaration(TypeDecl),
}

#[derive(Serialize, Debug)]
Expand Down Expand Up @@ -309,6 +311,25 @@ impl MacroDecl {
}
}

#[derive(Serialize, Debug)]
pub(crate) struct TypeDecl {
name: String,
arity: u32,
span: Location,
exported: bool,
}

impl TypeDecl {
fn new(name: &Name, arity: u32, span: Location, exported: bool) -> Self {
Self {
name: name.to_string(),
arity,
span,
exported,
}
}
}

#[derive(Debug, Default)]
struct IndexedFacts {
file_facts: Vec<FileFact>,
Expand Down Expand Up @@ -500,7 +521,7 @@ impl GleanIndexer {

let module_index = db.module_index(project_id);
if let Some(module) = module_index.module_for_file(file_id) {
let decl = Self::declarations(db, file_id, module);
let decl = Self::declarations_v1(db, file_id, module);
let xref = Self::xrefs(db, file_id);
return Some((file_fact, line_fact, file_decl, Some((decl, xref))));
}
Expand Down Expand Up @@ -546,7 +567,7 @@ impl GleanIndexer {
FileLinesFact::new(file_id, lengths, ends_with_new_line)
}

fn declarations(
fn declarations_v1(
db: &RootDatabase,
file_id: FileId,
module: &ModuleName,
Expand Down Expand Up @@ -609,6 +630,17 @@ impl GleanIndexer {
)));
}

for (ty, def) in def_map.get_types() {
let range = def.source(db).syntax().text_range();
let loc = range.into();
declarations.push(Declaration::TypeDeclaration(TypeDecl::new(
ty.name(),
ty.arity(),
loc,
def.exported,
)));
}

FileDeclaration::new(file_id.into(), declarations)
}

Expand Down Expand Up @@ -915,13 +947,17 @@ mod tests {
//- /glean/app_glean/src/glean_module5.erl
-module(glean_module5).
-export([foo/0, doc_foo/1]).
-export_type([person/1]).
-deprecated({depr_foo, 1, "use foo/0 instead"}).
-define(PI, 3.14).
%% ^^^^^^^^^^^^^^^^^^ macro/PI/no_arity
-define(MAX(X, Y), if X > Y -> X; true -> Y end).
%% ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ macro/MAX/2
-type person(Name :: string()) :: {name, string()}.
%% ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type/person/1/exported
foo() -> 1.
%% ^^^^^^^^^^^ func/foo/0/not_deprecated/exported/no_docs
depr_foo(B) -> B.
Expand Down Expand Up @@ -1198,6 +1234,7 @@ mod tests {
match self {
Declaration::FunctionDeclaration(decl) => &decl.span,
Declaration::MacroDeclaration(decl) => &decl.span,
Declaration::TypeDeclaration(decl) => &decl.span,
}
}
}
Expand Down Expand Up @@ -1238,6 +1275,13 @@ mod tests {
};
f.write_str(format!("macro/{}/{}", decl.name, arity).as_str())
}
Declaration::TypeDeclaration(decl) => {
let exported = match decl.exported {
true => "exported",
false => "not_exported",
};
f.write_str(format!("type/{}/{}/{}", decl.name, decl.arity, exported).as_str())
}
}
}
}
Expand Down

0 comments on commit a43040f

Please sign in to comment.