Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: expand context #53

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open

Conversation

RudolfMan
Copy link
Contributor

The ability to expand the context of the code around the change.

There are definitely stuff to improve.. especially in my frontend spaghetti JS (not sure if it makes sense to rewrite Diffs views to live_view since that page isn't meant to be live)

So currently onclick on any button "expanUp" or "expandDown" script tries to read numbers from lines before and after the button and makes the request:
GET /diff/:package/:version/expand/:from_line/:to_line/:right_line?file_name=<file_name>

In the backend we unpack that file from, extract the requested chunk and render the html

To be able to unpack particular file I propose a patch to hex_core to add :hex_tarball.unpack/3 to accept the list of files. see hexpm/hex_core#95

expand_context

RudolfMan and others added 7 commits April 26, 2020 03:00
add chunk.sh shell script that returns cut of the file
based on starting line, number of lines to read
and direction (up or down)

add ChunkExtractor module that sanitizes params and handles response of
the chunk.sh script

add a json endpoint to be able to request chunks
simplify the logic, get rid of direction parameter
now it only tries to retrieve the chunk from the top to the down
got rid of direction param and custom chunk.sh script
use shell sed command
@@ -43,7 +43,7 @@ defmodule Diff.MixProject do
{:jason, "~> 1.0"},
{:plug_cowboy, "~> 2.0"},
{:phoenix_live_view, "~> 0.6"},
{:hex_core, "~> 0.6.1"},
{:hex_core, github: "RudolfMan/hex_core", branch: "unpack-list-of-files"},
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR for the branch hexpm/hex_core#95

@ericmj
Copy link
Member

ericmj commented Jun 10, 2020

Thanks for the PR, it looks really great. Just a few points that I think we can improve:

Fetching a tarball and unpacking it every time a section expanded is a bit wasteful. It would be better to upload the individual files to the bucket after the tarball is unpacked for diffing and when we expand only fetch that file instead of the whole tarball.

The expand buttons should not be displayed when there is nothing to expand:

Screen Shot 2020-06-10 at 15 03 04

Screen Shot 2020-06-10 at 14 59 51

I am also a bit concerned about the amount of javascript, but I am not sure how well liveview would work on the potentially large diffs we show. If we start using liveview I think it would be worth showing smaller diffs and ask users to expand the diff to show more files.

For now the javascript is fine but if you want to try out liveview for this page in a separate PR then go ahead :).

// expandUp always follows by diff line.. so we take the number
const toLine = numberFrom(lineAfter) - 1

const _fromLine = lineBefore ? numberFrom(lineBefore) + 1 : 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's avoid the underscored variable names. Please pick a different name or don't use const.

assets/js/app.js Outdated Show resolved Hide resolved
assets/js/app.js Outdated Show resolved Hide resolved
@@ -0,0 +1,112 @@
defmodule Diff.Hex.ChunkExtractor do
@enforce_keys [:file_path, :from_line, :to_line, :right_line]
defstruct Enum.map(@enforce_keys, &{&1, nil}) ++ [errors: [], raw: nil, parsed: nil]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use a map internally and only return the parsed part, we don't need the struct.

%{errors: []} = chunk -> {:ok, chunk}
%{errors: _} = chunk -> {:error, chunk}
end
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of having every function match on %{errors: ...} use with instead.

@@ -28,6 +28,27 @@ defmodule DiffWeb.PageController do
end
end

def expand_context(conn, %{"file_name" => _file_name} = params) do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is file_name the only required parameter?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually since file_name is required parameter - makes sense to add it to "path" rather that "query_params" that way we won't need to handle that missing file_name in the controller. Additionally, I see some other parameters might be not strictly required and pronbably makes sense to have them in query params..

I'll revisit this part

lib/diff_web/controllers/page_controller.ex Outdated Show resolved Hide resolved
Co-authored-by: Eric Meadows-Jönsson <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants