Repository navigation
Preserve authorship when text moves #286
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
Draft
HamptonMakes
wants to merge
4
commits into
main
Choose a base branch
from
hampton-preserve-moved-authorship
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
60e548f
Preserve authorship when text moves
HamptonMakes 33e0658
Keep fresh input when restoring moved writing
HamptonMakes af2289f
Preserve input on duplicate line occurrences
HamptonMakes 7c8cd3e
Retain sources on punctuation-only moved lines
HamptonMakes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
engine/app/services/coplan/plans/writing_provenance_moves.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| module CoPlan | ||
| module Plans | ||
| # Exact lines carry their original character sources when reordered. Keep | ||
| # bounded deleted-line evidence so autosaving a cut does not erase it before | ||
| # the paste. Conflicting origins for identical lines are never guessed. | ||
| class WritingProvenanceMoves | ||
| MAX_CHARACTERS = 100_000 | ||
| MAX_LINES = 2048 | ||
| MAX_RUNS = 4096 | ||
|
|
||
| def self.call(before:, after:, prior_runs:, runs:, removed_lines:, fresh_source: nil) | ||
| new.call(before: before, after: after, prior_runs: prior_runs, runs: runs, removed_lines: removed_lines, fresh_source: fresh_source) | ||
| end | ||
|
|
||
| def call(before:, after:, prior_runs:, runs:, removed_lines:, fresh_source:) | ||
| @lines = {} | ||
| current_counts, archived_counts = Hash.new(0), Hash.new(0) | ||
| @characters = @run_count = 0 | ||
| each_line(before, prior_runs) do |text, line_runs, _| | ||
| current_counts[text] += 1 | ||
| remember(text, line_runs) | ||
| end | ||
| removed_lines.each do |text, line_runs, count| | ||
| remember(text, line_runs) | ||
| archived_counts[text] += count || 1 | ||
| end | ||
|
|
||
| # Reserve occurrences the normal diff already retained before assigning | ||
| # any lost moves. A new copy earlier in the document cannot spend the | ||
| # original occurrence's attribution before we reach the retained one. | ||
| matched, after_counts = Hash.new(0), Hash.new(0) | ||
| each_line(after, runs) do |text, line_runs, _| | ||
| after_counts[text] += 1 | ||
| if @lines[text] == line_runs && matched[text] < current_counts[text] | ||
| matched[text] += 1 | ||
| end | ||
| end | ||
| remaining = current_counts.dup | ||
| matched.each { |text, count| remaining[text] -= count } | ||
| archived_remaining = archived_counts.dup | ||
|
|
||
| result = [] | ||
| each_line(after, runs) do |text, line_runs, ending_runs| | ||
| restored = @lines[text] | ||
| extra_typed_copy = fresh_source && after_counts[text] > current_counts[text] + archived_counts[text] && | ||
| line_runs.any? { |_, source| source == fresh_source } | ||
| if extra_typed_copy | ||
| restored = nil | ||
| elsif restored && matched[text].positive? && line_runs == restored | ||
| matched[text] -= 1 | ||
| elsif restored && remaining[text].positive? | ||
| remaining[text] -= 1 | ||
| restored = retain_fresh_input(line_runs, restored, fresh_source) if fresh_source | ||
| elsif restored && archived_remaining[text].positive? | ||
| archived_remaining[text] -= 1 | ||
| restored = retain_fresh_input(line_runs, restored, fresh_source) if fresh_source | ||
| else | ||
| restored = nil | ||
| end | ||
| (restored || line_runs).each { |length, source| WritingProvenance.append(result, length, source) } | ||
| ending_runs.each { |length, source| WritingProvenance.append(result, length, source) } | ||
| end | ||
| deleted = @lines.filter_map do |text, line_runs| | ||
| count = current_counts[text] + archived_counts[text] - after_counts[text] | ||
| [ text, line_runs, count ] if count.positive? | ||
| end | ||
| [ result, deleted ] | ||
| end | ||
|
|
||
| private | ||
|
|
||
| # Recreating an old line can be a new action: typing an old sentence or | ||
| # checking a task again. Archived text cannot erase that observed input. | ||
| def retain_fresh_input(runs, archived, fresh_source) | ||
| result = [] | ||
| index = offset = 0 | ||
| runs.each do |length, source| | ||
| while length.positive? | ||
| old_length, old_source = archived.fetch(index) | ||
| count = [ length, old_length - offset ].min | ||
| WritingProvenance.append(result, count, source == fresh_source ? source : old_source) | ||
| length -= count | ||
| offset += count | ||
| if offset == old_length | ||
| index += 1 | ||
| offset = 0 | ||
| end | ||
| end | ||
| end | ||
| result | ||
| end | ||
|
|
||
| def remember(text, runs) | ||
| return unless text.match?(/[^[:space:]]/) | ||
| if @lines.key?(text) | ||
| @lines[text] = nil unless @lines[text] == runs | ||
| elsif @lines.size < MAX_LINES && @characters + text.length <= MAX_CHARACTERS && @run_count + runs.to_a.size <= MAX_RUNS | ||
| @lines[text] = runs | ||
| @characters += text.length | ||
| @run_count += runs.to_a.size | ||
| end | ||
| end | ||
|
|
||
| # Walk the run stream once; slicing from its start for every line would | ||
| # multiply work on documents with many lines and many small edits. | ||
| def each_line(content, runs) | ||
| index = offset = 0 | ||
| take = lambda do |length| | ||
| result = [] | ||
| while length.positive? | ||
| run_length, source = runs.fetch(index) | ||
| count = [ length, run_length - offset ].min | ||
| WritingProvenance.append(result, count, source) | ||
| length -= count | ||
| offset += count | ||
| if offset == run_length | ||
| index += 1 | ||
| offset = 0 | ||
| end | ||
| end | ||
| result | ||
| end | ||
| content.each_line do |line| | ||
| text = line.delete_suffix("\n") | ||
| line_runs = take.call(text.length) | ||
| yield text, line_runs, take.call(line.length - text.length) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| ## Current Architecture | ||
|
|
||
| Today, devices connect directly to the cloud for delivery and networking. | ||
|
|
||
| The original author wrote this section before moving it below the proposal. | ||
|
|
||
| ## Proposal | ||
|
|
||
| Let nearby devices exchange data securely over the local network. | ||
|
|
||
| Inspect writing to see Hampton as the author of both sections. The history records the cut and paste as separate saves, while the moved text keeps its original version. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.