Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions crates/ide-diagnostics/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ fn minicore_smoke_test() {
}

fn check(minicore: MiniCore) {
let source = minicore.source_code();
let source = minicore.source_code(MiniCore::RAW_SOURCE);
let mut config = DiagnosticsConfig::test_sample();
// This should be ignored since we conditionally remove code which creates single item use with braces
config.disabled.insert("unused_braces".to_owned());
Expand All @@ -321,7 +321,7 @@ fn minicore_smoke_test() {
}

// Checks that there is no diagnostic in minicore for each flag.
for flag in MiniCore::available_flags() {
for flag in MiniCore::available_flags(MiniCore::RAW_SOURCE) {
if flag == "clone" {
// Clone without copy has `moved-out-of-ref`, so ignoring.
// FIXME: Maybe we should merge copy and clone in a single flag?
Expand All @@ -332,5 +332,5 @@ fn minicore_smoke_test() {
}
// And one time for all flags, to check codes which are behind multiple flags + prevent name collisions
eprintln!("Checking all minicore flags");
check(MiniCore::from_flags(MiniCore::available_flags()))
check(MiniCore::from_flags(MiniCore::available_flags(MiniCore::RAW_SOURCE)))
}
8 changes: 4 additions & 4 deletions crates/ide/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,16 @@ span.workspace = true
# something from some `hir-xxx` subpackage, reexport the API via `hir`.
hir.workspace = true

test-utils.workspace = true
# local deps
test-fixture.workspace = true

[target.'cfg(not(any(target_arch = "wasm32", target_os = "emscripten")))'.dependencies]
toolchain.workspace = true

[dev-dependencies]
expect-test = "1.5.1"

# local deps
test-utils.workspace = true
test-fixture.workspace = true

[features]
in-rust-tree = []

Expand Down
43 changes: 39 additions & 4 deletions crates/ide/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,41 @@ impl Analysis {
(host.analysis(), file_id)
}

pub fn from_ra_fixture(text: &str, minicore: &str) -> (Analysis, Vec<(FileId, usize)>) {
// We don't want a mistake in the fixture to crash r-a, so we wrap this in `catch_unwind()`.
std::panic::catch_unwind(|| {
let mut host = AnalysisHost::default();
let fixture = test_fixture::ChangeFixture::parse_with_proc_macros(
&host.db,
text,
minicore,
Vec::new(),
);
host.apply_change(fixture.change);
let files = fixture
.files
.into_iter()
.zip(fixture.file_lines)
.map(|(file_id, range)| (file_id.file_id(&host.db), range))
.collect();
(host.analysis(), files)
})
.unwrap_or_else(|error| {
tracing::error!(
"cannot crate the crate graph: {}\nCrate graph:\n{}\n",
if let Some(&s) = error.downcast_ref::<&'static str>() {
s
} else if let Some(s) = error.downcast_ref::<String>() {
s.as_str()
} else {
"Box<dyn Any>"
},
text,
);
(AnalysisHost::default().analysis(), Vec::new())
})
}

/// Debug info about the current state of the analysis.
pub fn status(&self, file_id: Option<FileId>) -> Cancellable<String> {
self.with_db(|db| status::status(db, file_id))
Expand Down Expand Up @@ -675,28 +710,28 @@ impl Analysis {
/// Computes syntax highlighting for the given file
pub fn highlight(
&self,
highlight_config: HighlightConfig,
highlight_config: HighlightConfig<'_>,
file_id: FileId,
) -> Cancellable<Vec<HlRange>> {
// highlighting may construct a new database for "speculative" execution, so we can't currently attach the database
// highlighting instead sets up the attach hook where neceesary for the trait solver
Cancelled::catch(|| {
syntax_highlighting::highlight(&self.db, highlight_config, file_id, None)
syntax_highlighting::highlight(&self.db, &highlight_config, file_id, None)
})
}

/// Computes syntax highlighting for the given file range.
pub fn highlight_range(
&self,
highlight_config: HighlightConfig,
highlight_config: HighlightConfig<'_>,
frange: FileRange,
) -> Cancellable<Vec<HlRange>> {
// highlighting may construct a new database for "speculative" execution, so we can't currently attach the database
// highlighting instead sets up the attach hook where neceesary for the trait solver
Cancelled::catch(|| {
syntax_highlighting::highlight(
&self.db,
highlight_config,
&highlight_config,
frange.file_id,
Some(frange.range),
)
Expand Down
16 changes: 11 additions & 5 deletions crates/ide/src/syntax_highlighting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub struct HlRange {
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct HighlightConfig {
pub struct HighlightConfig<'a> {
/// Whether to highlight strings
pub strings: bool,
/// Whether to highlight punctuation
Expand All @@ -61,6 +61,12 @@ pub struct HighlightConfig {
pub macro_bang: bool,
/// Whether to highlight unresolved things be their syntax
pub syntactic_name_ref_highlighting: bool,
/// This is a bit of a special field, it's not really a config, rather it's a way to pass information to highlight.
///
/// Fixture highlighting requires the presence of a `minicore.rs` file. If such file is believed to be found in
/// the workspace (based on the path), this field stores its contents. Otherwise, highlighting will use the `minicore`
/// that was baked into the rust-analyzer binary.
pub minicore: Option<&'a str>,
}

// Feature: Semantic Syntax Highlighting
Expand Down Expand Up @@ -188,7 +194,7 @@ pub struct HighlightConfig {
// ![Semantic Syntax Highlighting](https://user-images.githubusercontent.com/48062697/113187625-f7f50100-9250-11eb-825e-91c58f236071.png)
pub(crate) fn highlight(
db: &RootDatabase,
config: HighlightConfig,
config: &HighlightConfig<'_>,
file_id: FileId,
range_to_highlight: Option<TextRange>,
) -> Vec<HlRange> {
Expand Down Expand Up @@ -223,7 +229,7 @@ pub(crate) fn highlight(
fn traverse(
hl: &mut Highlights,
sema: &Semantics<'_, RootDatabase>,
config: HighlightConfig,
config: &HighlightConfig<'_>,
InRealFile { file_id, value: root }: InRealFile<&SyntaxNode>,
krate: Option<hir::Crate>,
range_to_highlight: TextRange,
Expand Down Expand Up @@ -488,7 +494,7 @@ fn traverse(
fn string_injections(
hl: &mut Highlights,
sema: &Semantics<'_, RootDatabase>,
config: HighlightConfig,
config: &HighlightConfig<'_>,
file_id: EditionedFileId,
krate: Option<hir::Crate>,
token: SyntaxToken,
Expand Down Expand Up @@ -585,7 +591,7 @@ fn descend_token(
})
}

fn filter_by_config(highlight: &mut Highlight, config: HighlightConfig) -> bool {
fn filter_by_config(highlight: &mut Highlight, config: &HighlightConfig<'_>) -> bool {
match &mut highlight.tag {
HlTag::StringLiteral if !config.strings => return false,
// If punctuation is disabled, make the macro bang part of the macro call again.
Expand Down
3 changes: 2 additions & 1 deletion crates/ide/src/syntax_highlighting/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub(crate) fn highlight_as_html(db: &RootDatabase, file_id: FileId, rainbow: boo

let hl_ranges = highlight(
db,
HighlightConfig {
&HighlightConfig {
strings: true,
punctuation: true,
specialize_punctuation: true,
Expand All @@ -38,6 +38,7 @@ pub(crate) fn highlight_as_html(db: &RootDatabase, file_id: FileId, rainbow: boo
inject_doc_comment: true,
macro_bang: true,
syntactic_name_ref_highlighting: false,
minicore: None,
},
file_id.file_id(db),
None,
Expand Down
Loading
Loading