Skip to content

Commit

Permalink
refactor(service): merge logic of "find references"
Browse files Browse the repository at this point in the history
  • Loading branch information
g-plane committed Dec 19, 2024
1 parent e858620 commit 31d65f2
Showing 1 changed file with 48 additions and 241 deletions.
289 changes: 48 additions & 241 deletions crates/service/src/features/references.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,289 +41,96 @@ impl LanguageService {
.find(|symbol| symbol.key == key)?;
match &current_symbol.kind {
SymbolItemKind::Module => None,
SymbolItemKind::Func => Some(
symbol_table
.symbols
.iter()
.filter(|symbol| match symbol.kind {
SymbolItemKind::Func => {
params.context.include_declaration
&& current_symbol.idx == symbol.idx
&& symbol.region == current_symbol.region
}
SymbolItemKind::Call => {
symbol.idx.is_defined_by(&current_symbol.idx)
&& symbol.region == current_symbol.region
}
_ => false,
})
.map(|symbol| create_location_by_symbol(&params, &line_index, symbol, &root))
.collect(),
),
SymbolItemKind::Param => Some(
symbol_table
.symbols
.iter()
.filter(|symbol| match &symbol.kind {
SymbolItemKind::Param => {
params.context.include_declaration
&& current_symbol.idx == symbol.idx
&& symbol.region == current_symbol.region
}
SymbolItemKind::LocalRef => {
symbol.idx.is_defined_by(&current_symbol.idx)
&& symbol.region == current_symbol.region
}
_ => false,
})
.map(|symbol| create_location_by_symbol(&params, &line_index, symbol, &root))
.collect(),
),
SymbolItemKind::Local => Some(
symbol_table
.symbols
.iter()
.filter(|symbol| match &symbol.kind {
SymbolItemKind::Local => {
params.context.include_declaration
&& current_symbol.idx == symbol.idx
&& symbol.region == current_symbol.region
}
SymbolItemKind::LocalRef => {
symbol.idx.is_defined_by(&current_symbol.idx)
&& symbol.region == current_symbol.region
}
_ => false,
})
.map(|symbol| create_location_by_symbol(&params, &line_index, symbol, &root))
.collect(),
),
SymbolItemKind::Call => {
let funcs = symbol_table
.find_defs(&current_symbol.key)?
.collect::<SmallVec<[_; 1]>>();
Some(
symbol_table
.symbols
.iter()
.filter(|symbol| match &symbol.kind {
SymbolItemKind::Func => {
params.context.include_declaration
&& current_symbol.idx.is_defined_by(&symbol.idx)
&& symbol.region == current_symbol.region
}
SymbolItemKind::Call => {
funcs.iter().any(|func| symbol.idx.is_defined_by(&func.idx))
&& symbol.region == current_symbol.region
}
_ => false,
})
.map(|symbol| {
create_location_by_symbol(&params, &line_index, symbol, &root)
})
.collect(),
)
}
SymbolItemKind::LocalRef => {
let param_or_local = symbol_table.find_param_or_local_def(&current_symbol.key)?;
SymbolItemKind::Func
| SymbolItemKind::Param
| SymbolItemKind::Local
| SymbolItemKind::Type
| SymbolItemKind::GlobalDef
| SymbolItemKind::MemoryDef
| SymbolItemKind::TableDef => {
let ref_kind = match current_symbol.kind {
SymbolItemKind::Func => SymbolItemKind::Call,
SymbolItemKind::Param | SymbolItemKind::Local => SymbolItemKind::LocalRef,
SymbolItemKind::Type => SymbolItemKind::TypeUse,
SymbolItemKind::GlobalDef => SymbolItemKind::GlobalRef,
SymbolItemKind::MemoryDef => SymbolItemKind::MemoryRef,
SymbolItemKind::TableDef => SymbolItemKind::TableRef,
_ => return None,
};
Some(
symbol_table
.symbols
.iter()
.filter(|symbol| match &symbol.kind {
SymbolItemKind::Param | SymbolItemKind::Local => {
.filter(|symbol| {
if symbol.kind == current_symbol.kind {
params.context.include_declaration
&& current_symbol.idx.is_defined_by(&symbol.idx)
&& current_symbol.idx == symbol.idx
&& symbol.region == current_symbol.region
}
SymbolItemKind::LocalRef => {
symbol.idx.is_defined_by(&param_or_local.idx)
} else if symbol.kind == ref_kind {
symbol.idx.is_defined_by(&current_symbol.idx)
&& symbol.region == current_symbol.region
} else {
false
}
_ => false,
})
.map(|symbol| {
create_location_by_symbol(&params, &line_index, symbol, &root)
})
.collect(),
)
}
SymbolItemKind::Type => Some(
symbol_table
.symbols
.iter()
.filter(|symbol| match &symbol.kind {
SymbolItemKind::Type => {
params.context.include_declaration
&& current_symbol.idx == symbol.idx
&& symbol.region == current_symbol.region
}
SymbolItemKind::TypeUse => {
symbol.idx.is_defined_by(&current_symbol.idx)
&& symbol.region == current_symbol.region
}
_ => false,
})
.map(|symbol| create_location_by_symbol(&params, &line_index, symbol, &root))
.collect(),
),
SymbolItemKind::TypeUse => {
let types = symbol_table
SymbolItemKind::Call
| SymbolItemKind::TypeUse
| SymbolItemKind::GlobalRef
| SymbolItemKind::MemoryRef
| SymbolItemKind::TableRef => {
let def_kind = match current_symbol.kind {
SymbolItemKind::Call => SymbolItemKind::Func,
SymbolItemKind::TypeUse => SymbolItemKind::Type,
SymbolItemKind::GlobalRef => SymbolItemKind::GlobalDef,
SymbolItemKind::MemoryRef => SymbolItemKind::MemoryDef,
SymbolItemKind::TableRef => SymbolItemKind::TableDef,
_ => return None,
};
let defs = symbol_table
.find_defs(&current_symbol.key)?
.collect::<SmallVec<[_; 1]>>();
Some(
symbol_table
.symbols
.iter()
.filter(|symbol| match &symbol.kind {
SymbolItemKind::Type => {
.filter(|symbol| {
if symbol.kind == def_kind {
params.context.include_declaration
&& current_symbol.idx.is_defined_by(&symbol.idx)
&& symbol.region == current_symbol.region
}
SymbolItemKind::TypeUse => {
types.iter().any(|ty| symbol.idx.is_defined_by(&ty.idx))
} else if symbol.kind == current_symbol.kind {
defs.iter().any(|func| symbol.idx.is_defined_by(&func.idx))
&& symbol.region == current_symbol.region
} else {
false
}
_ => false,
})
.map(|symbol| {
create_location_by_symbol(&params, &line_index, symbol, &root)
})
.collect(),
)
}
SymbolItemKind::GlobalDef => Some(
symbol_table
.symbols
.iter()
.filter(|symbol| match &symbol.kind {
SymbolItemKind::GlobalDef => {
params.context.include_declaration
&& current_symbol.idx == symbol.idx
&& symbol.region == current_symbol.region
}
SymbolItemKind::GlobalRef => {
symbol.idx.is_defined_by(&current_symbol.idx)
&& symbol.region == current_symbol.region
}
_ => false,
})
.map(|symbol| create_location_by_symbol(&params, &line_index, symbol, &root))
.collect(),
),
SymbolItemKind::GlobalRef => {
let globals = symbol_table
.find_defs(&current_symbol.key)?
.collect::<SmallVec<[_; 1]>>();
Some(
symbol_table
.symbols
.iter()
.filter(|symbol| match &symbol.kind {
SymbolItemKind::GlobalDef => {
params.context.include_declaration
&& current_symbol.idx.is_defined_by(&symbol.idx)
&& symbol.region == current_symbol.region
}
SymbolItemKind::GlobalRef => {
globals
.iter()
.any(|global| symbol.idx.is_defined_by(&global.idx))
&& symbol.region == current_symbol.region
}
_ => false,
})
.map(|symbol| {
create_location_by_symbol(&params, &line_index, symbol, &root)
})
.collect(),
)
}
SymbolItemKind::MemoryDef => Some(
symbol_table
.symbols
.iter()
.filter(|symbol| match &symbol.kind {
SymbolItemKind::MemoryDef => {
params.context.include_declaration
&& current_symbol.idx == symbol.idx
&& symbol.region == current_symbol.region
}
SymbolItemKind::MemoryRef => {
symbol.idx.is_defined_by(&current_symbol.idx)
&& symbol.region == current_symbol.region
}
_ => false,
})
.map(|symbol| create_location_by_symbol(&params, &line_index, symbol, &root))
.collect(),
),
SymbolItemKind::MemoryRef => {
let memories = symbol_table
.find_defs(&current_symbol.key)?
.collect::<SmallVec<[_; 1]>>();
Some(
symbol_table
.symbols
.iter()
.filter(|symbol| match &symbol.kind {
SymbolItemKind::MemoryDef => {
params.context.include_declaration
&& current_symbol.idx.is_defined_by(&symbol.idx)
&& symbol.region == current_symbol.region
}
SymbolItemKind::MemoryRef => {
memories
.iter()
.any(|memory| symbol.idx.is_defined_by(&memory.idx))
&& symbol.region == current_symbol.region
}
_ => false,
})
.map(|symbol| {
create_location_by_symbol(&params, &line_index, symbol, &root)
})
.collect(),
)
}
SymbolItemKind::TableDef => Some(
symbol_table
.symbols
.iter()
.filter(|symbol| match &symbol.kind {
SymbolItemKind::TableDef => {
params.context.include_declaration
&& current_symbol.idx == symbol.idx
&& symbol.region == current_symbol.region
}
SymbolItemKind::TableRef => {
symbol.idx.is_defined_by(&current_symbol.idx)
&& symbol.region == current_symbol.region
}
_ => false,
})
.map(|symbol| create_location_by_symbol(&params, &line_index, symbol, &root))
.collect(),
),
SymbolItemKind::TableRef => {
let tables = symbol_table
.find_defs(&current_symbol.key)?
.collect::<SmallVec<[_; 1]>>();
SymbolItemKind::LocalRef => {
let param_or_local = symbol_table.find_param_or_local_def(&current_symbol.key)?;
Some(
symbol_table
.symbols
.iter()
.filter(|symbol| match &symbol.kind {
SymbolItemKind::TableDef => {
SymbolItemKind::Param | SymbolItemKind::Local => {
params.context.include_declaration
&& current_symbol.idx.is_defined_by(&symbol.idx)
&& symbol.region == current_symbol.region
}
SymbolItemKind::TableRef => {
tables
.iter()
.any(|memory| symbol.idx.is_defined_by(&memory.idx))
SymbolItemKind::LocalRef => {
symbol.idx.is_defined_by(&param_or_local.idx)
&& symbol.region == current_symbol.region
}
_ => false,
Expand Down

0 comments on commit 31d65f2

Please sign in to comment.