2017-07-18 18:52:09 -05:00
|
|
|
use rls_data::config::Config;
|
2018-09-24 09:38:54 -05:00
|
|
|
use rls_data::{self, Analysis, CompilationOptions, CratePreludeData, Def, DefKind, Impl, Import,
|
|
|
|
MacroRef, Ref, RefKind, Relation};
|
2017-03-14 15:14:15 -05:00
|
|
|
use rls_span::{Column, Row};
|
2017-03-13 23:08:47 -05:00
|
|
|
|
2017-11-14 16:17:09 -06:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Access {
|
|
|
|
pub reachable: bool,
|
|
|
|
pub public: bool,
|
|
|
|
}
|
|
|
|
|
2019-07-25 11:24:48 -05:00
|
|
|
pub struct JsonDumper {
|
2016-05-11 11:59:35 -05:00
|
|
|
result: Analysis,
|
2017-07-18 18:52:09 -05:00
|
|
|
config: Config,
|
2017-03-22 22:32:49 -05:00
|
|
|
}
|
|
|
|
|
2019-07-25 11:24:48 -05:00
|
|
|
impl JsonDumper {
|
|
|
|
pub fn new(config: Config) -> JsonDumper {
|
2017-08-01 23:57:50 -05:00
|
|
|
JsonDumper {
|
|
|
|
config: config.clone(),
|
2017-11-07 15:43:05 -06:00
|
|
|
result: Analysis::new(config),
|
2017-08-01 23:57:50 -05:00
|
|
|
}
|
2017-03-22 22:32:49 -05:00
|
|
|
}
|
|
|
|
|
2019-07-25 11:24:48 -05:00
|
|
|
pub fn to_output(self, f: impl FnOnce(&Analysis)) {
|
|
|
|
f(&self.result)
|
2017-03-22 22:32:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-25 11:24:48 -05:00
|
|
|
impl JsonDumper {
|
2017-07-18 18:52:09 -05:00
|
|
|
pub fn crate_prelude(&mut self, data: CratePreludeData) {
|
2016-05-11 11:59:35 -05:00
|
|
|
self.result.prelude = Some(data)
|
|
|
|
}
|
|
|
|
|
2018-09-11 09:39:07 -05:00
|
|
|
pub fn compilation_opts(&mut self, data: CompilationOptions) {
|
|
|
|
self.result.compilation = Some(data);
|
|
|
|
}
|
|
|
|
|
2018-10-30 17:56:11 -05:00
|
|
|
pub fn _macro_use(&mut self, data: MacroRef) {
|
2017-11-14 16:17:09 -06:00
|
|
|
if self.config.pub_only || self.config.reachable_only {
|
2017-07-18 18:52:09 -05:00
|
|
|
return;
|
|
|
|
}
|
2017-06-07 21:45:15 -05:00
|
|
|
self.result.macro_refs.push(data);
|
|
|
|
}
|
2016-05-11 11:59:35 -05:00
|
|
|
|
2017-11-14 16:17:09 -06:00
|
|
|
pub fn import(&mut self, access: &Access, import: Import) {
|
|
|
|
if !access.public && self.config.pub_only
|
|
|
|
|| !access.reachable && self.config.reachable_only {
|
2017-07-18 18:52:09 -05:00
|
|
|
return;
|
|
|
|
}
|
2017-06-07 21:45:15 -05:00
|
|
|
self.result.imports.push(import);
|
|
|
|
}
|
2016-05-11 11:59:35 -05:00
|
|
|
|
2017-07-18 18:52:09 -05:00
|
|
|
pub fn dump_ref(&mut self, data: Ref) {
|
2017-11-14 16:17:09 -06:00
|
|
|
if self.config.pub_only || self.config.reachable_only {
|
2017-07-18 18:52:09 -05:00
|
|
|
return;
|
|
|
|
}
|
2017-06-07 21:45:15 -05:00
|
|
|
self.result.refs.push(data);
|
|
|
|
}
|
2017-07-18 18:52:09 -05:00
|
|
|
|
2017-11-14 16:17:09 -06:00
|
|
|
pub fn dump_def(&mut self, access: &Access, mut data: Def) {
|
|
|
|
if !access.public && self.config.pub_only
|
|
|
|
|| !access.reachable && self.config.reachable_only {
|
2017-07-18 18:52:09 -05:00
|
|
|
return;
|
|
|
|
}
|
2017-06-07 21:45:15 -05:00
|
|
|
if data.kind == DefKind::Mod && data.span.file_name.to_str().unwrap() != data.value {
|
2017-11-14 16:17:09 -06:00
|
|
|
// If the module is an out-of-line definition, then we'll make the
|
|
|
|
// definition the first character in the module's file and turn
|
2016-11-24 21:50:47 -06:00
|
|
|
// the declaration into a reference to it.
|
|
|
|
let rf = Ref {
|
|
|
|
kind: RefKind::Mod,
|
2017-06-07 21:45:15 -05:00
|
|
|
span: data.span,
|
|
|
|
ref_id: data.id,
|
2016-11-24 21:50:47 -06:00
|
|
|
};
|
|
|
|
self.result.refs.push(rf);
|
2017-06-07 21:45:15 -05:00
|
|
|
data.span = rls_data::SpanData {
|
|
|
|
file_name: data.value.clone().into(),
|
2016-11-24 21:50:47 -06:00
|
|
|
byte_start: 0,
|
|
|
|
byte_end: 0,
|
2017-03-14 15:14:15 -05:00
|
|
|
line_start: Row::new_one_indexed(1),
|
|
|
|
line_end: Row::new_one_indexed(1),
|
|
|
|
column_start: Column::new_one_indexed(1),
|
|
|
|
column_end: Column::new_one_indexed(1),
|
2016-11-24 21:50:47 -06:00
|
|
|
}
|
|
|
|
}
|
2017-06-07 21:45:15 -05:00
|
|
|
self.result.defs.push(data);
|
2016-05-11 11:59:35 -05:00
|
|
|
}
|
|
|
|
|
2017-07-18 18:52:09 -05:00
|
|
|
pub fn dump_relation(&mut self, data: Relation) {
|
2017-06-07 21:45:15 -05:00
|
|
|
self.result.relations.push(data);
|
2017-02-12 22:50:58 -06:00
|
|
|
}
|
2018-02-02 01:29:59 -06:00
|
|
|
|
|
|
|
pub fn dump_impl(&mut self, data: Impl) {
|
|
|
|
self.result.impls.push(data);
|
|
|
|
}
|
2017-02-12 22:50:58 -06:00
|
|
|
}
|