Skip to content

Commit

Permalink
use add_wip assist
Browse files Browse the repository at this point in the history
Summary: Update the testing machinery to be able to also apply fixes generated in code action fixes.

Reviewed By: perehonchuk

Differential Revision: D55967115

fbshipit-source-id: a946bfa9b5759aa19ea2f377117cd87225398efb
  • Loading branch information
alanz authored and facebook-github-bot committed Apr 11, 2024
1 parent 110ce5f commit 2daeff8
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 39 deletions.
34 changes: 0 additions & 34 deletions crates/ide/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,40 +237,6 @@ impl Diagnostic {
self
}

#[allow(dead_code)] // @oss-only
pub(crate) fn with_fixme_fix(mut self, sema: &Semantic, file_id: FileId) -> Diagnostic {
let mut builder = TextEdit::builder();
let parsed = sema.parse(file_id);
if let Some(token) = parsed
.value
.syntax()
.token_at_offset(self.range.start())
.right_biased()
{
let indent = IndentLevel::from_token(&token);
let text = format!("\n{}% elp:fixme {}", indent, self.code.as_labeled_code(),);

let offset = start_of_line(&token);
builder.insert(offset, text);
let edit = builder.finish();
let source_change = SourceChange::from_text_edit(file_id, edit);
let ignore_fix = Assist {
id: AssistId("add_fixme", AssistKind::QuickFix),
label: Label::new("Add Fixme comment"),
group: Some(GroupLabel::ignore()),
target: self.range,
source_change: Some(source_change),
user_input: None,
original_diagnostic: None,
};
match &mut self.fixes {
Some(fixes) => fixes.push(ignore_fix),
None => self.fixes = Some(vec![ignore_fix]),
};
}
self
}

pub fn print(&self, line_index: &LineIndex) -> String {
let start = line_index.line_col(self.range.start());
let end = line_index.line_col(self.range.end());
Expand Down
9 changes: 8 additions & 1 deletion crates/ide/src/diagnostics/head_mismatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ mod tests {
use crate::diagnostics::DiagnosticsConfig;
use crate::tests::check_diagnostics_with_config;
use crate::tests::check_nth_fix;
use crate::tests::IncludeCodeActionAssists;

#[track_caller]
fn check_diagnostics(ra_fixture: &str) {
Expand All @@ -370,7 +371,13 @@ mod tests {
.disable(DiagnosticCode::MissingCompileWarnMissingSpec)
.disable(DiagnosticCode::Unexpected("unexpected_semi".to_string()))
.disable(DiagnosticCode::Unexpected("unexpected_dot".to_string()));
check_nth_fix(0, fixture_before, fixture_after, config);
check_nth_fix(
0,
fixture_before,
fixture_after,
config,
IncludeCodeActionAssists::No,
);
}

// The followings tests exercise head_mismatch function indirectly.
Expand Down
43 changes: 40 additions & 3 deletions crates/ide/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,13 @@ pub(crate) fn check_fix(fixture_before: &str, fixture_after: &str) {
let config = DiagnosticsConfig::default()
.disable(DiagnosticCode::MissingCompileWarnMissingSpec)
.set_experimental(true);
check_nth_fix(0, fixture_before, fixture_after, config);
check_nth_fix(
0,
fixture_before,
fixture_after,
config,
IncludeCodeActionAssists::No,
);
}

/// Like `check_fix` but with a custom DiagnosticsConfig
Expand All @@ -139,7 +145,33 @@ pub(crate) fn check_fix_with_config(
fixture_before: &str,
fixture_after: &str,
) {
check_nth_fix(0, fixture_before, fixture_after, config);
check_nth_fix(
0,
fixture_before,
fixture_after,
config,
IncludeCodeActionAssists::No,
);
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum IncludeCodeActionAssists {
Yes,
No,
}

#[track_caller]
pub(crate) fn check_fix_including_assists(fixture_before: &str, fixture_after: &str) {
let config = DiagnosticsConfig::default()
.disable(DiagnosticCode::MissingCompileWarnMissingSpec)
.set_experimental(true);
check_nth_fix(
0,
fixture_before,
fixture_after,
config,
IncludeCodeActionAssists::Yes,
)
}

#[track_caller]
Expand All @@ -148,6 +180,7 @@ pub(crate) fn check_nth_fix(
fixture_before: &str,
fixture_after: &str,
config: DiagnosticsConfig,
include_assists: IncludeCodeActionAssists,
) {
let after = trim_indent(fixture_after);

Expand All @@ -161,7 +194,11 @@ pub(crate) fn check_nth_fix(
.last()
.expect("no diagnostics")
.clone();
let fix = &diagnostic.fixes.expect("diagnostic misses fixes")[nth];
let fixes = match include_assists {
IncludeCodeActionAssists::Yes => diagnostic.get_diagnostic_fixes(&analysis.db, pos.file_id),
IncludeCodeActionAssists::No => diagnostic.fixes.expect("diagnostic misses fixes"),
};
let fix = &fixes[nth];
let actual = {
let source_change = fix.source_change.as_ref().unwrap();
let file_id = *source_change.source_file_edits.keys().next().unwrap();
Expand Down
2 changes: 1 addition & 1 deletion crates/ide_db/src/diagnostic_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ impl DiagnosticCode {
DiagnosticCode::ErlangService(_) => false,
DiagnosticCode::Eqwalizer(_) => false,
DiagnosticCode::AdHoc(_) => false,
DiagnosticCode::MetaOnly(_) => false,
DiagnosticCode::MetaOnly(code) => code.allows_fixme_comment(),
}
}
}
Expand Down

0 comments on commit 2daeff8

Please sign in to comment.