Skip to content

Commit

Permalink
Json output upgrades (#3)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
nimrodkor and Nimrod authored May 20, 2024
1 parent a9bcee7 commit 5ed4a53
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 110 deletions.
37 changes: 3 additions & 34 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,16 @@ jobs:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v3
- uses: taiki-e/create-gh-release-action@v1
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
- name: Create a Release
uses: elgohr/Github-Release-Action@v5
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
Expand All @@ -52,18 +32,7 @@ jobs:
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 }}
61 changes: 10 additions & 51 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
on: [push, pull_request]
on: [ push, pull_request ]

name: Continuous integration

Expand All @@ -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/[email protected]
with:
targets: ${{ matrix.job.target }}

- name: Install cross
if: matrix.job.use-cross
uses: taiki-e/install-action@v2
with:
tool: [email protected]

- 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/[email protected]
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/[email protected]
# 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)
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
2 changes: 1 addition & 1 deletion sample_files/compare.expected
Original file line number Diff line number Diff line change
Expand Up @@ -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 -
Expand Down
31 changes: 9 additions & 22 deletions src/display/json.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
use std::collections::HashMap;

use line_numbers::LineNumber;
use serde::{ser::SerializeStruct, Serialize, Serializer};

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},
Expand Down Expand Up @@ -99,9 +96,6 @@ impl<'f> From<&'f DiffResult> for File<'f> {
let lhs_lines = lhs_src.split('\n').collect::<Vec<&str>>();
let rhs_lines = rhs_src.split('\n').collect::<Vec<&str>>();

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,
Expand All @@ -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(
Expand All @@ -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)
Expand Down Expand Up @@ -230,6 +215,7 @@ struct Change<'c> {
end: u32,
content: &'c str,
highlight: Highlight,
highlight_type: &'c str,
}

#[derive(Debug, Serialize)]
Expand Down Expand Up @@ -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];

Expand All @@ -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(),
})
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/parse/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 5ed4a53

Please sign in to comment.