From e67ef937e9cf0e3c1560154efa203a3e7ddfd723 Mon Sep 17 00:00:00 2001 From: Nimrod Kor Date: Mon, 20 May 2024 10:15:23 +0300 Subject: [PATCH] Json output upgrades (#3) * Sort JSON lines and enrich them * Fix release.yml * Slimmer testing * Cargo fmt * actionlint * Less testing * Replace map with list to preserve order --------- Co-authored-by: Nimrod Replace map with list to preserve order Replace map with list to preserve order --- .github/workflows/release.yml | 50 +++++++--------------------- .github/workflows/test.yml | 61 ++++++----------------------------- Cargo.lock | 1 + Cargo.toml | 2 +- sample_files/compare.expected | 2 +- src/display/json.rs | 31 ++++++------------ src/parse/syntax.rs | 3 +- 7 files changed, 36 insertions(+), 114 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index d0872ce14f..6050bc9a12 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -5,65 +5,39 @@ on: tags: - "[0-9]+.*" +permissions: + contents: write + packages: write + jobs: create-release: - runs-on: ubuntu-20.04 + runs-on: ubuntu-22.04 steps: - - uses: actions/checkout@v3 - - uses: taiki-e/create-gh-release-action@v1 + - uses: actions/checkout@v4 + - name: Create a Release + uses: elgohr/Github-Release-Action@v5 with: - # Create a draft release so we can add the changelog before publishing it - # so the changelog is included in the release email - draft: true + title: ${{ github.ref }} env: - # (required) - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} upload-assets: strategy: fail-fast: false matrix: include: - # Targets that match the OS of the runner. - - target: x86_64-unknown-linux-gnu - os: ubuntu-20.04 - - target: x86_64-apple-darwin - os: macos-12 - - target: x86_64-pc-windows-msvc - os: windows-latest - - # Targets using cross-compilation (upload-rust-binary-action - # detects that they need cross). - - # TODO: fix musl so it statically links during release, see - # https://github.com/Wilfred/difftastic/issues/563 - # - # - target: x86_64-unknown-linux-musl - # os: ubuntu-20.04 - target: aarch64-unknown-linux-gnu os: ubuntu-20.04 - target: aarch64-apple-darwin os: macos-latest runs-on: ${{ matrix.os }} steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - uses: taiki-e/upload-rust-binary-action@v1 with: bin: difft # (optional) On which platform to distribute the `.tar.gz` file. tar: unix - # (optional) On which platform to distribute the `.zip` file. - zip: windows # (optional) Target triple, default is host triple. target: ${{ matrix.target }} - env: - # (required) - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - - push_crates_io: - runs-on: ubuntu-20.04 - steps: - - uses: actions/checkout@v3 - - uses: katyo/publish-crates@v1 - with: - registry-token: ${{ secrets.CARGO_REGISTRY_TOKEN }} + token: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 89494dd8d6..7be131d652 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,4 +1,4 @@ -on: [push, pull_request] +on: [ push, pull_request ] name: Continuous integration @@ -10,65 +10,24 @@ jobs: fail-fast: false matrix: job: - # Operating systems available: https://docs.github.com/en/actions/using-github-hosted-runners/about-github-hosted-runners/about-github-hosted-runners#standard-github-hosted-runners-for-public-repositories - - { target: x86_64-unknown-linux-gnu, os: ubuntu-20.04 } - # mac-latest (currently mac-14) is an ARM device, so use macos-12 to get Intel. - # Update annoucement: https://github.blog/changelog/2024-04-01-macos-14-sonoma-is-generally-available-and-the-latest-macos-runner-image/ - - { target: x86_64-apple-darwin, os: macos-12 } - - { target: x86_64-pc-windows-msvc, os: windows-latest } - - - { target: aarch64-unknown-linux-gnu, os: ubuntu-20.04, use-cross: true } - - # mac-14 is an M1 ARM device. - { target: aarch64-apple-darwin, os: macos-14 } - - # musl binaries produced by GitHub actions segfault when run, see - # https://github.com/Wilfred/difftastic/issues/563 - # - # musl builds seem to work fine elsewhere, so just verify - # that compilation works for now. - - { target: x86_64-unknown-linux-musl, os: ubuntu-20.04, use-cross: true, build-only: true } env: BUILD_CMD: cargo SUBCOMMAND: test steps: - - name: Checkout source code - uses: actions/checkout@v3 - - - name: Install Rust toolchain - uses: dtolnay/rust-toolchain@1.65.0 - with: - targets: ${{ matrix.job.target }} - - - name: Install cross - if: matrix.job.use-cross - uses: taiki-e/install-action@v2 - with: - tool: cross@0.2.5 - - - name: Overwrite build command env variable - if: matrix.job.use-cross - shell: bash - run: echo "BUILD_CMD=cross" >> $GITHUB_ENV + - name: Checkout source code + uses: actions/checkout@v3 - - name: Overwrite build subcommand env variable - if: matrix.job.build-only - shell: bash - run: echo "SUBCOMMAND=build" >> $GITHUB_ENV + - name: Install Rust toolchain + uses: dtolnay/rust-toolchain@1.65.0 + with: + targets: ${{ matrix.job.target }} - - name: Test - shell: bash - run: $BUILD_CMD $SUBCOMMAND --target ${{ matrix.job.target }} - test_mime_db: - name: Test with MIME database - runs-on: ubuntu-20.04 - steps: - - uses: actions/checkout@v3 - - uses: dtolnay/rust-toolchain@1.65.0 - # This runs tests that rely on the MIME database being present. - - run: cargo test -- --ignored + - name: Test + shell: bash + run: $BUILD_CMD $SUBCOMMAND --target ${{ matrix.job.target }} test_linux_latest_stable: name: Test Linux (latest stable Rust) diff --git a/Cargo.lock b/Cargo.lock index 1a9effbae7..923e351c00 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -864,6 +864,7 @@ version = "1.0.114" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c5f09b1bd632ef549eaa9f60a1f8de742bdbc698e6cee2095fc84dde5f549ae0" dependencies = [ + "indexmap 2.2.6", "itoa", "ryu", "serde", diff --git a/Cargo.toml b/Cargo.toml index 994b814b48..b10e6f193c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -78,7 +78,7 @@ strum = { version = "0.25", features = ["derive"] } hashbrown = "0.14.0" humansize = "2.1.3" serde = { version = "1.0", features = ["derive"] } -serde_json = "1.0" +serde_json = { version = "1.0", features = ["preserve_order"] } line-numbers = "0.3.0" smallvec = "1.13.2" diff --git a/sample_files/compare.expected b/sample_files/compare.expected index 11c18bc447..42e6e6b948 100644 --- a/sample_files/compare.expected +++ b/sample_files/compare.expected @@ -68,7 +68,7 @@ sample_files/erlang_1.erl sample_files/erlang_2.erl dccdb8f65d2f099ab1a8cb66011376a2 - sample_files/f_sharp_1.fs sample_files/f_sharp_2.fs -6ba65e62a804124f56bdfb19afc8cd40 - +1a9173c15a42c1ebb9522109df172faf - sample_files/hack_1.php sample_files/hack_2.php c2bb0aa7d7b07d6ced79f6a5363e878b - diff --git a/src/display/json.rs b/src/display/json.rs index 2dc867dee1..91b29336c3 100644 --- a/src/display/json.rs +++ b/src/display/json.rs @@ -1,5 +1,3 @@ -use std::collections::HashMap; - use line_numbers::LineNumber; use serde::{ser::SerializeStruct, Serialize, Serializer}; @@ -7,7 +5,6 @@ use crate::{ display::{ context::{all_matched_lines_filled, opposite_positions}, hunks::{matched_lines_indexes_for_hunk, matched_pos_to_hunks, merge_adjacent}, - side_by_side::lines_with_novel, }, lines::MaxLine, parse::syntax::{self, MatchedPos, StringKind}, @@ -99,9 +96,6 @@ impl<'f> From<&'f DiffResult> for File<'f> { let lhs_lines = lhs_src.split('\n').collect::>(); let rhs_lines = rhs_src.split('\n').collect::>(); - let (lhs_lines_with_novel, rhs_lines_with_novel) = - lines_with_novel(&summary.lhs_positions, &summary.rhs_positions); - let matched_lines = all_matched_lines_filled( &summary.lhs_positions, &summary.rhs_positions, @@ -112,25 +106,15 @@ impl<'f> From<&'f DiffResult> for File<'f> { let mut chunks = Vec::with_capacity(hunks.len()); for hunk in &hunks { - let mut lines = HashMap::with_capacity(hunk.lines.len()); + let mut lines = Vec::with_capacity(hunk.lines.len()); - let (start_i, end_i) = matched_lines_indexes_for_hunk(matched_lines, hunk, 0); + let (start_i, end_i) = matched_lines_indexes_for_hunk(matched_lines, hunk, 3); let aligned_lines = &matched_lines[start_i..end_i]; matched_lines = &matched_lines[start_i..]; for (lhs_line_num, rhs_line_num) in aligned_lines { - if !lhs_lines_with_novel.contains(&lhs_line_num.unwrap_or(LineNumber(0))) - && !rhs_lines_with_novel - .contains(&rhs_line_num.unwrap_or(LineNumber(0))) - { - continue; - } - - let line = lines - .entry((lhs_line_num.map(|l| l.0), rhs_line_num.map(|l| l.0))) - .or_insert_with(|| { - Line::new(lhs_line_num.map(|l| l.0), rhs_line_num.map(|l| l.0)) - }); + let mut line = + Line::new(lhs_line_num.map(|l| l.0), rhs_line_num.map(|l| l.0)); if let Some(line_num) = lhs_line_num { add_changes_to_side( @@ -148,9 +132,10 @@ impl<'f> From<&'f DiffResult> for File<'f> { &summary.rhs_positions, ); } + lines.push(line); } - chunks.push(lines.into_values().collect()); + chunks.push(lines); } File::with_sections(&summary.file_format, &summary.display_path, chunks) @@ -230,6 +215,7 @@ struct Change<'c> { end: u32, content: &'c str, highlight: Highlight, + highlight_type: &'c str, } #[derive(Debug, Serialize)] @@ -297,7 +283,7 @@ fn add_changes_to_side<'s>( side: &mut Side<'s>, line_num: LineNumber, src_lines: &[&'s str], - all_matches: &[MatchedPos], + all_matches: &'s [MatchedPos], ) { let src_line = src_lines[line_num.0 as usize]; @@ -308,6 +294,7 @@ fn add_changes_to_side<'s>( end: m.pos.end_col, content: &src_line[(m.pos.start_col as usize)..(m.pos.end_col as usize)], highlight: Highlight::from_match(&m.kind), + highlight_type: m.kind.as_ref(), }) } } diff --git a/src/parse/syntax.rs b/src/parse/syntax.rs index bdde5934d5..12e190dced 100644 --- a/src/parse/syntax.rs +++ b/src/parse/syntax.rs @@ -6,6 +6,7 @@ use std::{cell::Cell, env, fmt, hash::Hash, num::NonZeroU32}; use line_numbers::LinePositions; use line_numbers::SingleLineSpan; +use strum::AsRefStr; use typed_arena::Arena; use self::Syntax::*; @@ -589,7 +590,7 @@ pub(crate) enum TokenKind { } /// A matched token (an atom, a delimiter, or a comment word). -#[derive(PartialEq, Eq, Debug, Clone)] +#[derive(PartialEq, Eq, Debug, Clone, AsRefStr)] pub(crate) enum MatchKind { UnchangedToken { highlight: TokenKind,