mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-02 22:54:58 +00:00
allow compiling ra_ide_api on wasm
This commit is contained in:
parent
c733993658
commit
4d81432213
6 changed files with 76 additions and 12 deletions
|
@ -4,6 +4,9 @@ name = "ra_ide_api"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
authors = ["rust-analyzer developers"]
|
authors = ["rust-analyzer developers"]
|
||||||
|
|
||||||
|
[features]
|
||||||
|
wasm = []
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
format-buf = "1.0.0"
|
format-buf = "1.0.0"
|
||||||
itertools = "0.8.0"
|
itertools = "0.8.0"
|
||||||
|
|
|
@ -6,6 +6,7 @@ use ra_db::{
|
||||||
};
|
};
|
||||||
use ra_prof::{memory_usage, profile, Bytes};
|
use ra_prof::{memory_usage, profile, Bytes};
|
||||||
use ra_syntax::SourceFile;
|
use ra_syntax::SourceFile;
|
||||||
|
#[cfg(not(feature = "wasm"))]
|
||||||
use rayon::prelude::*;
|
use rayon::prelude::*;
|
||||||
use relative_path::RelativePathBuf;
|
use relative_path::RelativePathBuf;
|
||||||
use rustc_hash::FxHashMap;
|
use rustc_hash::FxHashMap;
|
||||||
|
@ -143,7 +144,12 @@ impl LibraryData {
|
||||||
root_id: SourceRootId,
|
root_id: SourceRootId,
|
||||||
files: Vec<(FileId, RelativePathBuf, Arc<String>)>,
|
files: Vec<(FileId, RelativePathBuf, Arc<String>)>,
|
||||||
) -> LibraryData {
|
) -> LibraryData {
|
||||||
let symbol_index = SymbolIndex::for_files(files.par_iter().map(|(file_id, _, text)| {
|
#[cfg(not(feature = "wasm"))]
|
||||||
|
let iter = files.par_iter();
|
||||||
|
#[cfg(feature = "wasm")]
|
||||||
|
let iter = files.iter();
|
||||||
|
|
||||||
|
let symbol_index = SymbolIndex::for_files(iter.map(|(file_id, _, text)| {
|
||||||
let parse = SourceFile::parse(text);
|
let parse = SourceFile::parse(text);
|
||||||
(*file_id, parse)
|
(*file_id, parse)
|
||||||
}));
|
}));
|
||||||
|
@ -234,8 +240,12 @@ impl RootDatabase {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn maybe_collect_garbage(&mut self) {
|
pub(crate) fn maybe_collect_garbage(&mut self) {
|
||||||
|
if cfg!(feature = "wasm") {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if self.last_gc_check.elapsed() > GC_COOLDOWN {
|
if self.last_gc_check.elapsed() > GC_COOLDOWN {
|
||||||
self.last_gc_check = time::Instant::now();
|
self.last_gc_check = crate::wasm_shims::Instant::now();
|
||||||
let retained_trees = syntax_tree_stats(self).retained;
|
let retained_trees = syntax_tree_stats(self).retained;
|
||||||
if retained_trees > 100 {
|
if retained_trees > 100 {
|
||||||
log::info!("automatic garbadge collection, {} retained trees", retained_trees);
|
log::info!("automatic garbadge collection, {} retained trees", retained_trees);
|
||||||
|
@ -245,8 +255,12 @@ impl RootDatabase {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn collect_garbage(&mut self) {
|
pub(crate) fn collect_garbage(&mut self) {
|
||||||
|
if cfg!(feature = "wasm") {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
let _p = profile("RootDatabase::collect_garbage");
|
let _p = profile("RootDatabase::collect_garbage");
|
||||||
self.last_gc = time::Instant::now();
|
self.last_gc = crate::wasm_shims::Instant::now();
|
||||||
|
|
||||||
let sweep = SweepStrategy::default().discard_values().sweep_all_revisions();
|
let sweep = SweepStrategy::default().discard_values().sweep_all_revisions();
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use std::{sync::Arc, time};
|
use std::sync::Arc;
|
||||||
|
|
||||||
use ra_db::{
|
use ra_db::{
|
||||||
salsa::{self, Database, Durability},
|
salsa::{self, Database, Durability},
|
||||||
|
@ -25,8 +25,8 @@ pub(crate) struct RootDatabase {
|
||||||
runtime: salsa::Runtime<RootDatabase>,
|
runtime: salsa::Runtime<RootDatabase>,
|
||||||
pub(crate) feature_flags: Arc<FeatureFlags>,
|
pub(crate) feature_flags: Arc<FeatureFlags>,
|
||||||
pub(crate) debug_data: Arc<DebugData>,
|
pub(crate) debug_data: Arc<DebugData>,
|
||||||
pub(crate) last_gc: time::Instant,
|
pub(crate) last_gc: crate::wasm_shims::Instant,
|
||||||
pub(crate) last_gc_check: time::Instant,
|
pub(crate) last_gc_check: crate::wasm_shims::Instant,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl hir::debug::HirDebugHelper for RootDatabase {
|
impl hir::debug::HirDebugHelper for RootDatabase {
|
||||||
|
@ -69,8 +69,8 @@ impl RootDatabase {
|
||||||
pub fn new(lru_capacity: Option<usize>, feature_flags: FeatureFlags) -> RootDatabase {
|
pub fn new(lru_capacity: Option<usize>, feature_flags: FeatureFlags) -> RootDatabase {
|
||||||
let mut db = RootDatabase {
|
let mut db = RootDatabase {
|
||||||
runtime: salsa::Runtime::default(),
|
runtime: salsa::Runtime::default(),
|
||||||
last_gc: time::Instant::now(),
|
last_gc: crate::wasm_shims::Instant::now(),
|
||||||
last_gc_check: time::Instant::now(),
|
last_gc_check: crate::wasm_shims::Instant::now(),
|
||||||
feature_flags: Arc::new(feature_flags),
|
feature_flags: Arc::new(feature_flags),
|
||||||
debug_data: Default::default(),
|
debug_data: Default::default(),
|
||||||
};
|
};
|
||||||
|
|
|
@ -40,6 +40,7 @@ mod typing;
|
||||||
mod matching_brace;
|
mod matching_brace;
|
||||||
mod display;
|
mod display;
|
||||||
mod inlay_hints;
|
mod inlay_hints;
|
||||||
|
mod wasm_shims;
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod marks;
|
mod marks;
|
||||||
|
|
|
@ -38,6 +38,7 @@ use ra_syntax::{
|
||||||
SyntaxKind::{self, *},
|
SyntaxKind::{self, *},
|
||||||
SyntaxNode, SyntaxNodePtr, TextRange, WalkEvent,
|
SyntaxNode, SyntaxNodePtr, TextRange, WalkEvent,
|
||||||
};
|
};
|
||||||
|
#[cfg(not(feature = "wasm"))]
|
||||||
use rayon::prelude::*;
|
use rayon::prelude::*;
|
||||||
|
|
||||||
use crate::{db::RootDatabase, FileId, Query};
|
use crate::{db::RootDatabase, FileId, Query};
|
||||||
|
@ -79,10 +80,17 @@ pub(crate) fn world_symbols(db: &RootDatabase, query: Query) -> Vec<FileSymbol>
|
||||||
|
|
||||||
let buf: Vec<Arc<SymbolIndex>> = if query.libs {
|
let buf: Vec<Arc<SymbolIndex>> = if query.libs {
|
||||||
let snap = Snap(db.snapshot());
|
let snap = Snap(db.snapshot());
|
||||||
db.library_roots()
|
#[cfg(not(feature = "wasm"))]
|
||||||
|
let buf = db
|
||||||
|
.library_roots()
|
||||||
.par_iter()
|
.par_iter()
|
||||||
.map_with(snap, |db, &lib_id| db.0.library_symbols(lib_id))
|
.map_with(snap, |db, &lib_id| db.0.library_symbols(lib_id))
|
||||||
.collect()
|
.collect();
|
||||||
|
|
||||||
|
#[cfg(feature = "wasm")]
|
||||||
|
let buf = db.library_roots().iter().map(|&lib_id| snap.0.library_symbols(lib_id)).collect();
|
||||||
|
|
||||||
|
buf
|
||||||
} else {
|
} else {
|
||||||
let mut files = Vec::new();
|
let mut files = Vec::new();
|
||||||
for &root in db.local_roots().iter() {
|
for &root in db.local_roots().iter() {
|
||||||
|
@ -91,7 +99,14 @@ pub(crate) fn world_symbols(db: &RootDatabase, query: Query) -> Vec<FileSymbol>
|
||||||
}
|
}
|
||||||
|
|
||||||
let snap = Snap(db.snapshot());
|
let snap = Snap(db.snapshot());
|
||||||
files.par_iter().map_with(snap, |db, &file_id| db.0.file_symbols(file_id)).collect()
|
#[cfg(not(feature = "wasm"))]
|
||||||
|
let buf =
|
||||||
|
files.par_iter().map_with(snap, |db, &file_id| db.0.file_symbols(file_id)).collect();
|
||||||
|
|
||||||
|
#[cfg(feature = "wasm")]
|
||||||
|
let buf = files.iter().map(|&file_id| snap.0.file_symbols(file_id)).collect();
|
||||||
|
|
||||||
|
buf
|
||||||
};
|
};
|
||||||
query.search(&buf)
|
query.search(&buf)
|
||||||
}
|
}
|
||||||
|
@ -135,9 +150,12 @@ impl SymbolIndex {
|
||||||
fn cmp_key<'a>(s1: &'a FileSymbol) -> impl Ord + 'a {
|
fn cmp_key<'a>(s1: &'a FileSymbol) -> impl Ord + 'a {
|
||||||
unicase::Ascii::new(s1.name.as_str())
|
unicase::Ascii::new(s1.name.as_str())
|
||||||
}
|
}
|
||||||
|
#[cfg(not(feature = "wasm"))]
|
||||||
symbols.par_sort_by(|s1, s2| cmp_key(s1).cmp(&cmp_key(s2)));
|
symbols.par_sort_by(|s1, s2| cmp_key(s1).cmp(&cmp_key(s2)));
|
||||||
|
|
||||||
|
#[cfg(feature = "wasm")]
|
||||||
|
symbols.sort_by(|s1, s2| cmp_key(s1).cmp(&cmp_key(s2)));
|
||||||
|
|
||||||
let mut builder = fst::MapBuilder::memory();
|
let mut builder = fst::MapBuilder::memory();
|
||||||
|
|
||||||
let mut last_batch_start = 0;
|
let mut last_batch_start = 0;
|
||||||
|
@ -169,6 +187,7 @@ impl SymbolIndex {
|
||||||
self.map.as_fst().size() + self.symbols.len() * mem::size_of::<FileSymbol>()
|
self.map.as_fst().size() + self.symbols.len() * mem::size_of::<FileSymbol>()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(not(feature = "wasm"))]
|
||||||
pub(crate) fn for_files(
|
pub(crate) fn for_files(
|
||||||
files: impl ParallelIterator<Item = (FileId, Parse<ast::SourceFile>)>,
|
files: impl ParallelIterator<Item = (FileId, Parse<ast::SourceFile>)>,
|
||||||
) -> SymbolIndex {
|
) -> SymbolIndex {
|
||||||
|
@ -178,6 +197,16 @@ impl SymbolIndex {
|
||||||
SymbolIndex::new(symbols)
|
SymbolIndex::new(symbols)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "wasm")]
|
||||||
|
pub(crate) fn for_files(
|
||||||
|
files: impl Iterator<Item = (FileId, Parse<ast::SourceFile>)>,
|
||||||
|
) -> SymbolIndex {
|
||||||
|
let symbols = files
|
||||||
|
.flat_map(|(file_id, file)| source_file_to_file_symbols(&file.tree(), file_id))
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
SymbolIndex::new(symbols)
|
||||||
|
}
|
||||||
|
|
||||||
fn range_to_map_value(start: usize, end: usize) -> u64 {
|
fn range_to_map_value(start: usize, end: usize) -> u64 {
|
||||||
debug_assert![start <= (std::u32::MAX as usize)];
|
debug_assert![start <= (std::u32::MAX as usize)];
|
||||||
debug_assert![end <= (std::u32::MAX as usize)];
|
debug_assert![end <= (std::u32::MAX as usize)];
|
||||||
|
|
17
crates/ra_ide_api/src/wasm_shims.rs
Normal file
17
crates/ra_ide_api/src/wasm_shims.rs
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
#[cfg(not(feature = "wasm"))]
|
||||||
|
pub use std::time::Instant;
|
||||||
|
|
||||||
|
#[cfg(feature = "wasm")]
|
||||||
|
#[derive(Clone, Copy, Debug)]
|
||||||
|
pub struct Instant;
|
||||||
|
|
||||||
|
#[cfg(feature = "wasm")]
|
||||||
|
impl Instant {
|
||||||
|
pub fn now() -> Self {
|
||||||
|
Self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn elapsed(&self) -> std::time::Duration {
|
||||||
|
std::time::Duration::new(0, 0)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue