Use SourceKind::diff for formatter (#8240)

## Summary

This PR refactors the formatter diff code to reuse the
`SourceKind::diff` logic. This has the benefit that the Notebook diff
now includes the cell numbers which was not present before.

## Test Plan

Update the snapshots and verified the cell numbers.
This commit is contained in:
Dhruv Manilawala 2023-10-26 11:08:13 +05:30 committed by GitHub
parent 88c8b47326
commit a7d1f7e1ec
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 46 additions and 23 deletions

View file

@ -2,7 +2,7 @@ use std::io;
use std::io::Write;
use std::path::Path;
use anyhow::{bail, Result};
use anyhow::Result;
use similar::TextDiff;
use thiserror::Error;
@ -88,7 +88,12 @@ impl SourceKind {
}
/// Write a diff of the transformed source file to `stdout`.
pub fn diff(&self, other: &Self, path: Option<&Path>, writer: &mut dyn Write) -> Result<()> {
pub fn diff(
&self,
other: &Self,
path: Option<&Path>,
writer: &mut dyn Write,
) -> io::Result<()> {
match (self, other) {
(SourceKind::Python(src), SourceKind::Python(dst)) => {
let text_diff = TextDiff::from_lines(src, dst);
@ -154,7 +159,7 @@ impl SourceKind {
Ok(())
}
_ => bail!("cannot diff Python source code with Jupyter notebook source code"),
_ => panic!("cannot diff Python source code with Jupyter notebook source code"),
}
}
}