|
| 1 | +use crate::config::Config; |
| 2 | +use anyhow::Context; |
| 3 | +use itertools::Itertools; |
| 4 | +use log::info; |
| 5 | +use ra_ap_base_db::SourceDatabase; |
| 6 | +use ra_ap_hir::Semantics; |
| 7 | +use ra_ap_ide_db::RootDatabase; |
| 8 | +use ra_ap_load_cargo::{load_workspace_at, LoadCargoConfig, ProcMacroServerChoice}; |
| 9 | +use ra_ap_paths::Utf8PathBuf; |
| 10 | +use ra_ap_project_model::CargoConfig; |
| 11 | +use ra_ap_project_model::RustLibSource; |
| 12 | +use ra_ap_span::Edition; |
| 13 | +use ra_ap_span::EditionedFileId; |
| 14 | +use ra_ap_span::TextRange; |
| 15 | +use ra_ap_span::TextSize; |
| 16 | +use ra_ap_syntax::SourceFile; |
| 17 | +use ra_ap_syntax::SyntaxError; |
| 18 | +use ra_ap_vfs::AbsPathBuf; |
| 19 | +use ra_ap_vfs::Vfs; |
| 20 | +use ra_ap_vfs::VfsPath; |
| 21 | +use std::borrow::Cow; |
| 22 | +use std::collections::HashMap; |
| 23 | +use std::path::{Path, PathBuf}; |
| 24 | +use triomphe::Arc; |
| 25 | +pub struct RustAnalyzer { |
| 26 | + workspace: HashMap<PathBuf, (Vfs, RootDatabase)>, |
| 27 | +} |
| 28 | + |
| 29 | +impl RustAnalyzer { |
| 30 | + pub fn new(cfg: &Config) -> anyhow::Result<RustAnalyzer> { |
| 31 | + let mut workspace = HashMap::new(); |
| 32 | + let config = CargoConfig { |
| 33 | + sysroot: Some(RustLibSource::Discover), |
| 34 | + target_dir: ra_ap_paths::Utf8PathBuf::from_path_buf(cfg.scratch_dir.to_path_buf()) |
| 35 | + .map(|x| x.join("target")) |
| 36 | + .ok(), |
| 37 | + ..Default::default() |
| 38 | + }; |
| 39 | + let progress = |t| (log::info!("progress: {}", t)); |
| 40 | + let load_config = LoadCargoConfig { |
| 41 | + load_out_dirs_from_check: true, |
| 42 | + with_proc_macro_server: ProcMacroServerChoice::Sysroot, |
| 43 | + prefill_caches: false, |
| 44 | + }; |
| 45 | + let projects = find_project_manifests(&cfg.inputs).context("loading inputs")?; |
| 46 | + for project in projects { |
| 47 | + let manifest = project.manifest_path(); |
| 48 | + let (db, vfs, _macro_server) = |
| 49 | + load_workspace_at(manifest.as_ref(), &config, &load_config, &progress)?; |
| 50 | + let path: &Path = manifest.parent().as_ref(); |
| 51 | + workspace.insert(path.to_path_buf(), (vfs, db)); |
| 52 | + } |
| 53 | + Ok(RustAnalyzer { workspace }) |
| 54 | + } |
| 55 | + pub fn parse( |
| 56 | + &self, |
| 57 | + path: &PathBuf, |
| 58 | + ) -> ( |
| 59 | + SourceFile, |
| 60 | + Arc<str>, |
| 61 | + Vec<SyntaxError>, |
| 62 | + Option<Semantics<'_, RootDatabase>>, |
| 63 | + ) { |
| 64 | + let mut p = path.as_path(); |
| 65 | + while let Some(parent) = p.parent() { |
| 66 | + p = parent; |
| 67 | + if let Some((vfs, db)) = self.workspace.get(parent) { |
| 68 | + if let Some(file_id) = Utf8PathBuf::from_path_buf(path.to_path_buf()) |
| 69 | + .ok() |
| 70 | + .and_then(|x| AbsPathBuf::try_from(x).ok()) |
| 71 | + .map(VfsPath::from) |
| 72 | + .and_then(|x| vfs.file_id(&x)) |
| 73 | + { |
| 74 | + let semi = Semantics::new(db); |
| 75 | + let file_id = EditionedFileId::current_edition(file_id); |
| 76 | + |
| 77 | + return ( |
| 78 | + semi.parse(file_id), |
| 79 | + db.file_text(file_id.into()), |
| 80 | + db.parse_errors(file_id) |
| 81 | + .map(|x| x.to_vec()) |
| 82 | + .unwrap_or_default(), |
| 83 | + Some(semi), |
| 84 | + ); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + let input = std::fs::read(&path).unwrap(); |
| 89 | + let (input, err) = from_utf8_lossy(&input); |
| 90 | + let parse = ra_ap_syntax::ast::SourceFile::parse(&input, Edition::CURRENT); |
| 91 | + let mut errors = parse.errors(); |
| 92 | + errors.extend(err.into_iter()); |
| 93 | + (parse.tree(), input.as_ref().into(), errors, None) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +fn find_project_manifests( |
| 98 | + files: &[PathBuf], |
| 99 | +) -> anyhow::Result<Vec<ra_ap_project_model::ProjectManifest>> { |
| 100 | + let current = std::env::current_dir()?; |
| 101 | + let abs_files: Vec<_> = files |
| 102 | + .iter() |
| 103 | + .map(|path| AbsPathBuf::assert_utf8(current.join(path))) |
| 104 | + .collect(); |
| 105 | + let ret = ra_ap_project_model::ProjectManifest::discover_all(&abs_files); |
| 106 | + info!( |
| 107 | + "found manifests: {}", |
| 108 | + ret.iter().map(|m| format!("{m}")).join(", ") |
| 109 | + ); |
| 110 | + Ok(ret) |
| 111 | +} |
| 112 | +fn from_utf8_lossy(v: &[u8]) -> (Cow<'_, str>, Option<SyntaxError>) { |
| 113 | + let mut iter = v.utf8_chunks(); |
| 114 | + let (first_valid, first_invalid) = if let Some(chunk) = iter.next() { |
| 115 | + let valid = chunk.valid(); |
| 116 | + let invalid = chunk.invalid(); |
| 117 | + if invalid.is_empty() { |
| 118 | + debug_assert_eq!(valid.len(), v.len()); |
| 119 | + return (Cow::Borrowed(valid), None); |
| 120 | + } |
| 121 | + (valid, invalid) |
| 122 | + } else { |
| 123 | + return (Cow::Borrowed(""), None); |
| 124 | + }; |
| 125 | + |
| 126 | + const REPLACEMENT: &str = "\u{FFFD}"; |
| 127 | + let error_start = first_valid.len() as u32; |
| 128 | + let error_end = error_start + first_invalid.len() as u32; |
| 129 | + let error_range = TextRange::new(TextSize::new(error_start), TextSize::new(error_end)); |
| 130 | + let error = SyntaxError::new("invalid utf-8 sequence".to_owned(), error_range); |
| 131 | + let mut res = String::with_capacity(v.len()); |
| 132 | + res.push_str(first_valid); |
| 133 | + |
| 134 | + res.push_str(REPLACEMENT); |
| 135 | + |
| 136 | + for chunk in iter { |
| 137 | + res.push_str(chunk.valid()); |
| 138 | + if !chunk.invalid().is_empty() { |
| 139 | + res.push_str(REPLACEMENT); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + (Cow::Owned(res), Some(error)) |
| 144 | +} |
0 commit comments