Skip to content

Commit

Permalink
Merge 173ab23 into e3fa674
Browse files Browse the repository at this point in the history
  • Loading branch information
orpuente-MS committed Apr 8, 2024
2 parents e3fa674 + 173ab23 commit db957fa
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 7 deletions.
41 changes: 34 additions & 7 deletions language_service/src/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,20 @@ impl Compilation {
language_features,
);

let lints = qsc::linter::run_lints(&unit, Some(lints_config));
let mut lints = lints
.into_iter()
.map(|lint| WithSource::from_map(&unit.sources, qsc::compile::ErrorKind::Lint(lint)))
.collect();
errors.append(&mut lints);
// Compute new lints and append them to the errors Vec.
// Lints are only computed if the erros vector is empty. For performance
// reasons we don't want to waste time running lints every few keystrokes,
// if the user is in the middle of typing a statement, for example.
if errors.is_empty() {
let lints = qsc::linter::run_lints(&unit, Some(lints_config));
let lints: Vec<_> = lints
.into_iter()
.map(|lint| {
WithSource::from_map(&unit.sources, qsc::compile::ErrorKind::Lint(lint))
})
.collect();
errors.extend(lints);
}

let package_id = package_store.insert(unit);

Expand All @@ -94,6 +102,7 @@ impl Compilation {
cells: I,
target_profile: Profile,
language_features: LanguageFeatures,
lints_config: &[LintConfig],
) -> Self
where
I: Iterator<Item = (Arc<str>, Arc<str>)>,
Expand Down Expand Up @@ -123,6 +132,24 @@ impl Compilation {

let (package_store, package_id) = compiler.into_package_store();

// Compute new lints and append them to the errors Vec.
// Lints are only computed if the erros vector is empty. For performance
// reasons we don't want to waste time running lints every few keystrokes,
// if the user is in the middle of typing a statement, for example.
if errors.is_empty() {
let unit = package_store
.get(package_id)
.expect("user package should exist");
let lints = qsc::linter::run_lints(unit, Some(lints_config));
let lints: Vec<_> = lints
.into_iter()
.map(|lint| {
WithSource::from_map(&unit.sources, qsc::compile::ErrorKind::Lint(lint))
})
.collect();
errors.extend(lints);
}

Self {
package_store,
user_package_id: package_id,
Expand Down Expand Up @@ -211,7 +238,7 @@ impl Compilation {
lints_config,
),
CompilationKind::Notebook => {
Self::new_notebook(sources, target_profile, language_features)
Self::new_notebook(sources, target_profile, language_features, lints_config)
}
};
self.package_store = new.package_store;
Expand Down
4 changes: 4 additions & 0 deletions language_service/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,10 @@ impl<'a> CompilationStateUpdater<'a> {
}),
configuration.target_profile,
notebook_configuration.language_features.unwrap_or_default(),
notebook_configuration
.lints_config
.as_ref()
.unwrap_or(&vec![]),
);

state.compilations.insert(
Expand Down
62 changes: 62 additions & 0 deletions language_service/src/state/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,68 @@ fn notebook_document_errors() {
);
}

#[test]
fn notebook_document_lints() {
let errors = RefCell::new(Vec::new());
let mut updater = new_updater(&errors);

updater.update_notebook_document(
"notebook.ipynb",
&NotebookMetadata::default(),
[
("cell1", 1, "operation Foo() : Unit { let x = 4;;;; }"),
("cell2", 1, "operation Bar() : Unit { let y = 5 / 0; }"),
]
.into_iter(),
);

expect_errors(
&errors,
&expect![[r#"
[
(
"cell1",
Some(
1,
),
[
Lint(
Lint {
span: Span {
lo: 35,
hi: 38,
},
level: Warn,
message: "redundant semicolons",
help: "remove the redundant semicolons",
},
),
],
),
(
"cell2",
Some(
1,
),
[
Lint(
Lint {
span: Span {
lo: 74,
hi: 79,
},
level: Warn,
message: "attempt to divide by zero",
help: "division by zero is not allowed",
},
),
],
),
]
"#]],
);
}

#[test]
fn notebook_update_remove_cell_clears_errors() {
let errors = RefCell::new(Vec::new());
Expand Down

0 comments on commit db957fa

Please sign in to comment.