|
| 1 | +use std::{ |
| 2 | + collections::HashMap, |
| 3 | + path::{Path, PathBuf}, |
| 4 | +}; |
| 5 | + |
1 | 6 | use anyhow::Context;
|
2 |
| -use ra_ap_ide_db::line_index::LineIndex; |
3 |
| -use ra_ap_parser::Edition; |
4 |
| -use std::borrow::Cow; |
| 7 | +use archive::Archiver; |
| 8 | +use ra_ap_ide_db::line_index::{LineCol, LineIndex}; |
| 9 | +use ra_ap_project_model::ProjectManifest; |
| 10 | +use rust_analyzer::{ParseResult, RustAnalyzer}; |
5 | 11 | mod archive;
|
6 | 12 | mod config;
|
7 | 13 | pub mod generated;
|
| 14 | +mod rust_analyzer; |
8 | 15 | mod translate;
|
9 | 16 | pub mod trap;
|
10 |
| -use ra_ap_syntax::ast::SourceFile; |
11 |
| -use ra_ap_syntax::{AstNode, SyntaxError, TextRange, TextSize}; |
12 |
| - |
13 |
| -fn from_utf8_lossy(v: &[u8]) -> (Cow<'_, str>, Option<SyntaxError>) { |
14 |
| - let mut iter = v.utf8_chunks(); |
15 |
| - let (first_valid, first_invalid) = if let Some(chunk) = iter.next() { |
16 |
| - let valid = chunk.valid(); |
17 |
| - let invalid = chunk.invalid(); |
18 |
| - if invalid.is_empty() { |
19 |
| - debug_assert_eq!(valid.len(), v.len()); |
20 |
| - return (Cow::Borrowed(valid), None); |
21 |
| - } |
22 |
| - (valid, invalid) |
23 |
| - } else { |
24 |
| - return (Cow::Borrowed(""), None); |
25 |
| - }; |
26 |
| - |
27 |
| - const REPLACEMENT: &str = "\u{FFFD}"; |
28 |
| - let error_start = first_valid.len() as u32; |
29 |
| - let error_end = error_start + first_invalid.len() as u32; |
30 |
| - let error_range = TextRange::new(TextSize::new(error_start), TextSize::new(error_end)); |
31 |
| - let error = SyntaxError::new("invalid utf-8 sequence".to_owned(), error_range); |
32 |
| - let mut res = String::with_capacity(v.len()); |
33 |
| - res.push_str(first_valid); |
34 |
| - |
35 |
| - res.push_str(REPLACEMENT); |
36 |
| - |
37 |
| - for chunk in iter { |
38 |
| - res.push_str(chunk.valid()); |
39 |
| - if !chunk.invalid().is_empty() { |
40 |
| - res.push_str(REPLACEMENT); |
41 |
| - } |
42 |
| - } |
43 |
| - |
44 |
| - (Cow::Owned(res), Some(error)) |
45 |
| -} |
46 | 17 |
|
47 | 18 | fn extract(
|
48 |
| - archiver: &archive::Archiver, |
| 19 | + rust_analyzer: &mut rust_analyzer::RustAnalyzer, |
| 20 | + archiver: &Archiver, |
49 | 21 | traps: &trap::TrapFileProvider,
|
50 |
| - file: std::path::PathBuf, |
51 |
| -) -> anyhow::Result<()> { |
52 |
| - let file = std::path::absolute(&file).unwrap_or(file); |
53 |
| - let file = std::fs::canonicalize(&file).unwrap_or(file); |
54 |
| - archiver.archive(&file); |
55 |
| - let input = std::fs::read(&file)?; |
56 |
| - let (input, err) = from_utf8_lossy(&input); |
57 |
| - let line_index = LineIndex::new(&input); |
| 22 | + file: &std::path::Path, |
| 23 | +) { |
| 24 | + archiver.archive(file); |
| 25 | + |
| 26 | + let ParseResult { |
| 27 | + ast, |
| 28 | + text, |
| 29 | + errors, |
| 30 | + file_id, |
| 31 | + semantics, |
| 32 | + } = rust_analyzer.parse(file); |
| 33 | + let line_index = LineIndex::new(text.as_ref()); |
58 | 34 | let display_path = file.to_string_lossy();
|
59 |
| - let mut trap = traps.create("source", &file); |
60 |
| - let label = trap.emit_file(&file); |
61 |
| - let mut translator = translate::Translator::new(trap, label, line_index); |
62 |
| - if let Some(err) = err { |
63 |
| - translator.emit_parse_error(display_path.as_ref(), err); |
64 |
| - } |
65 |
| - let parse = ra_ap_syntax::ast::SourceFile::parse(&input, Edition::CURRENT); |
66 |
| - for err in parse.errors() { |
67 |
| - translator.emit_parse_error(display_path.as_ref(), err); |
| 35 | + let mut trap = traps.create("source", file); |
| 36 | + let label = trap.emit_file(file); |
| 37 | + let mut translator = translate::Translator::new( |
| 38 | + trap, |
| 39 | + display_path.as_ref(), |
| 40 | + label, |
| 41 | + line_index, |
| 42 | + file_id, |
| 43 | + semantics, |
| 44 | + ); |
| 45 | + |
| 46 | + for err in errors { |
| 47 | + translator.emit_parse_error(&ast, &err); |
68 | 48 | }
|
69 |
| - if let Some(ast) = SourceFile::cast(parse.syntax_node()) { |
70 |
| - translator.emit_source_file(ast); |
71 |
| - } else { |
72 |
| - log::warn!("Skipped {}", display_path); |
| 49 | + let no_location = (LineCol { line: 0, col: 0 }, LineCol { line: 0, col: 0 }); |
| 50 | + if translator.semantics.is_none() { |
| 51 | + translator.emit_diagnostic( |
| 52 | + trap::DiagnosticSeverity::Warning, |
| 53 | + "semantics".to_owned(), |
| 54 | + "semantic analyzer unavailable".to_owned(), |
| 55 | + "semantic analyzer unavailable: macro expansion, call graph, and type inference will be skipped.".to_owned(), |
| 56 | + no_location, |
| 57 | + ); |
73 | 58 | }
|
74 |
| - translator.trap.commit()?; |
75 |
| - Ok(()) |
| 59 | + translator.emit_source_file(ast); |
| 60 | + translator.trap.commit().unwrap_or_else(|err| { |
| 61 | + log::error!( |
| 62 | + "Failed to write trap file for: {}: {}", |
| 63 | + display_path, |
| 64 | + err.to_string() |
| 65 | + ) |
| 66 | + }); |
76 | 67 | }
|
77 | 68 | fn main() -> anyhow::Result<()> {
|
78 | 69 | let cfg = config::Config::extract().context("failed to load configuration")?;
|
79 | 70 | stderrlog::new()
|
80 | 71 | .module(module_path!())
|
81 |
| - .verbosity(2 + cfg.verbose as usize) |
| 72 | + .verbosity(1 + cfg.verbose as usize) |
82 | 73 | .init()?;
|
83 |
| - log::info!("{cfg:?}"); |
| 74 | + |
84 | 75 | let traps = trap::TrapFileProvider::new(&cfg).context("failed to set up trap files")?;
|
85 | 76 | let archiver = archive::Archiver {
|
86 | 77 | root: cfg.source_archive_dir,
|
87 | 78 | };
|
88 |
| - for file in cfg.inputs { |
89 |
| - extract(&archiver, &traps, file)?; |
| 79 | + let files: Vec<PathBuf> = cfg |
| 80 | + .inputs |
| 81 | + .iter() |
| 82 | + .map(|file| { |
| 83 | + let file = std::path::absolute(file).unwrap_or(file.to_path_buf()); |
| 84 | + std::fs::canonicalize(&file).unwrap_or(file) |
| 85 | + }) |
| 86 | + .collect(); |
| 87 | + let manifests = rust_analyzer::find_project_manifests(&files)?; |
| 88 | + let mut map: HashMap<&Path, (&ProjectManifest, Vec<&Path>)> = manifests |
| 89 | + .iter() |
| 90 | + .map(|x| (x.manifest_path().parent().as_ref(), (x, Vec::new()))) |
| 91 | + .collect(); |
| 92 | + let mut other_files = Vec::new(); |
| 93 | + |
| 94 | + 'outer: for file in &files { |
| 95 | + let mut p = file.as_path(); |
| 96 | + while let Some(parent) = p.parent() { |
| 97 | + p = parent; |
| 98 | + if let Some((_, files)) = map.get_mut(parent) { |
| 99 | + files.push(file); |
| 100 | + continue 'outer; |
| 101 | + } |
| 102 | + } |
| 103 | + other_files.push(file); |
| 104 | + } |
| 105 | + for (manifest, files) in map.values() { |
| 106 | + if files.is_empty() { |
| 107 | + break; |
| 108 | + } |
| 109 | + let mut rust_analyzer = RustAnalyzer::new(manifest, &cfg.scratch_dir); |
| 110 | + for file in files { |
| 111 | + extract(&mut rust_analyzer, &archiver, &traps, file); |
| 112 | + } |
| 113 | + } |
| 114 | + let mut rust_analyzer = RustAnalyzer::WithoutDatabase(); |
| 115 | + for file in other_files { |
| 116 | + extract(&mut rust_analyzer, &archiver, &traps, file); |
90 | 117 | }
|
91 | 118 |
|
92 | 119 | Ok(())
|
|
0 commit comments