Skip to content

Commit

Permalink
Highlight conflict markers
Browse files Browse the repository at this point in the history
  • Loading branch information
walles committed Oct 10, 2024
1 parent ae19ea5 commit 37629bd
Show file tree
Hide file tree
Showing 13 changed files with 209 additions and 168 deletions.
17 changes: 13 additions & 4 deletions SCREENSHOTS.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
To update `screenshot-git-merge.png`, you should be running
[iTerm2](https://iterm2.com/) and have [`moar`](https://github.com/walles/moar)
as your pager.
# Screenshot updating instructions

Then:
Screenshots are done using:

- Terminal: [iTerm2](https://iterm2.com/)
- Pager: [`moar`](https://github.com/walles/moar)

## `screenshot-git-merge.png`

1. Scale your terminal window to 65x17
2. Copy the below example to the clipboard
Expand All @@ -25,3 +28,9 @@ index 0399cd5,59727f0..0000000

hello()
```

## `screenshot-diff2-conflict.png`

1. Scale your window to 72x10
2. `cargo run < testdata/conflict-markers.txt.riff-output`
3. Screenshot the window and store it as `screenshot-diff2-conflict.png`
Binary file modified screenshot-diff2-conflict.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/conflicts_highlighter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ impl ConflictsHighlighter {
let (header_prefix, c1_prefix, c2_prefix, reset) = if self.c1_header.starts_with("++") {
(INVERSE_VIDEO, " +", "+ ", NORMAL)
} else {
("", "", "", "")
(INVERSE_VIDEO, "", "", "")
};

let c1_header = self.c1_header.clone();
Expand Down
12 changes: 6 additions & 6 deletions testdata/conflict-markers-diff3-baseless.txt.riff-output
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
This is an example of git conflict markers.

<<<<<<< HEAD
This line was added on the main branch.
||||||| 07ffb9b
=======
This line was added on the branch named "branch".
>>>>>>> branch
<<<<<<< HEAD
This line was added on the main branch.
||||||| 07ffb9b
=======
This line was added on the branch named "branch".
>>>>>>> branch
12 changes: 6 additions & 6 deletions testdata/conflict-markers-diff3-removed.txt.riff-output
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
This is an example of git conflict markers.

<<<<<<< HEAD
This line is changed on the main branch.
||||||| 07ffb9b
This line is from the initial commit on the main branch.
=======
>>>>>>> branch
<<<<<<< HEAD
This line is changed on the main branch.
||||||| 07ffb9b
This line is from the initial commit on the main branch.
=======
>>>>>>> branch
14 changes: 7 additions & 7 deletions testdata/conflict-markers-diff3.txt.riff-output
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
This is an example of git conflict markers.

<<<<<<< HEAD
This line is changed on the main branch.
||||||| 07ffb9b
This line is from the initial commit on the main branch.
=======
This line is from the branch named "branch".
>>>>>>> branch
<<<<<<< HEAD
This line is changed on the main branch.
||||||| 07ffb9b
This line is from the initial commit on the main branch.
=======
This line is from the branch named "branch".
>>>>>>> branch
10 changes: 5 additions & 5 deletions testdata/conflict-markers.txt.riff-output
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
This is an example of git conflict markers.

<<<<<<< HEAD
This line is changed on the main branch.
=======
This line is from the branch named "branch".
>>>>>>> branch
<<<<<<< HEAD
This line is changed on the main branch.
=======
This line is from the branch named "branch".
>>>>>>> branch
146 changes: 73 additions & 73 deletions testdata/conflict-with-context.riff-output
Original file line number Diff line number Diff line change
Expand Up @@ -6,79 +6,79 @@
"github.com/walles/moar/m/linenumbers"
)

++<<<<<<< HEAD
 +func (p *Pager) scrollToSearchHits() {
 + if p.searchPattern == nil {
 + // This is not a search
 + return
 + }
 +
 + lineNumber := p.scrollPosition.lineNumber(p)
 + if lineNumber == nil {
 + // No lines to search
 + return
 + }
 +
 + firstHitPosition := p.findFirstHit(*lineNumber, nil, false)
 + if firstHitPosition == nil && (*lineNumber != linenumbers.LineNumber{}) {
 + // Try again from the top
 + firstHitPosition = p.findFirstHit(linenumbers.LineNumber{}, lineNumber, false)
 + }
 + if firstHitPosition == nil {
 + // No match, give up
 + return
 + }
 +
 + if firstHitPosition.isVisible(p) {
 + // Already on-screen, never mind
 + return
 + }
 +
 + p.scrollPosition = *firstHitPosition
 +}
 +
 +// NOTE: When we search, we do that by looping over the *input lines*, not the
 +// screen lines. That's why startPosition is a LineNumber rather than a
 +// scrollPosition.
 +//
 +// The `beforePosition` parameter is exclusive, meaning that line will not be
 +// searched.
 +//
 +// For the actual searching, this method will call _findFirstHit() in parallel
 +// on multiple cores, to help large file search performance.
++||||||| parent of b835e9a (Fix the warnings)
++func (p *Pager) scrollToSearchHits() {
++ if p.searchPattern == nil {
++ // This is not a search
++ return
++ }
++
++ firstHitPosition := p.findFirstHit(*p.scrollPosition.lineNumber(p), nil, false)
++ if firstHitPosition == nil {
++ // Try again from the top
++ firstHitPosition = p.findFirstHit(linenumbers.LineNumber{}, p.scrollPosition.lineNumber(p), false)
++ }
++ if firstHitPosition == nil {
++ // No match, give up
++ return
++ }
++
++ if firstHitPosition.isVisible(p) {
++ // Already on-screen, never mind
++ return
++ }
++
++ p.scrollPosition = *firstHitPosition
++}
++
++// NOTE: When we search, we do that by looping over the *input lines*, not
++// the screen lines. That's why we're using a line number rather than a
++// scrollPosition for searching.
++=======
+ // NOTE: When we search, we do that by looping over the *input lines*, not
+ // the screen lines. That's why we're using a line number rather than a
+ // scrollPosition for searching.
++>>>>>>> b835e9a (Fix the warnings)
++<<<<<<< HEAD
 +func (p *Pager) scrollToSearchHits() {
 + if p.searchPattern == nil {
 + // This is not a search
 + return
 + }
 +
 + lineNumber := p.scrollPosition.lineNumber(p)
 + if lineNumber == nil {
 + // No lines to search
 + return
 + }
 +
 + firstHitPosition := p.findFirstHit(*lineNumber, nil, false)
 + if firstHitPosition == nil && (*lineNumber != linenumbers.LineNumber{}) {
 + // Try again from the top
 + firstHitPosition = p.findFirstHit(linenumbers.LineNumber{}, lineNumber, false)
 + }
 + if firstHitPosition == nil {
 + // No match, give up
 + return
 + }
 +
 + if firstHitPosition.isVisible(p) {
 + // Already on-screen, never mind
 + return
 + }
 +
 + p.scrollPosition = *firstHitPosition
 +}
 +
 +// NOTE: When we search, we do that by looping over the *input lines*, not the
 +// screen lines. That's why startPosition is a LineNumber rather than a
 +// scrollPosition.
 +//
 +// The `beforePosition` parameter is exclusive, meaning that line will not be
 +// searched.
 +//
 +// For the actual searching, this method will call _findFirstHit() in parallel
 +// on multiple cores, to help large file search performance.
++||||||| parent of b835e9a (Fix the warnings)
++func (p *Pager) scrollToSearchHits() {
++ if p.searchPattern == nil {
++ // This is not a search
++ return
++ }
++
++ firstHitPosition := p.findFirstHit(*p.scrollPosition.lineNumber(p), nil, false)
++ if firstHitPosition == nil {
++ // Try again from the top
++ firstHitPosition = p.findFirstHit(linenumbers.LineNumber{}, p.scrollPosition.lineNumber(p), false)
++ }
++ if firstHitPosition == nil {
++ // No match, give up
++ return
++ }
++
++ if firstHitPosition.isVisible(p) {
++ // Already on-screen, never mind
++ return
++ }
++
++ p.scrollPosition = *firstHitPosition
++}
++
++// NOTE: When we search, we do that by looping over the *input lines*, not
++// the screen lines. That's why we're using a line number rather than a
++// scrollPosition for searching.
++=======
+ // NOTE: When we search, we do that by looping over the *input lines*, not
+ // the screen lines. That's why we're using a line number rather than a
+ // scrollPosition for searching.
++>>>>>>> b835e9a (Fix the warnings)
//
// FIXME: We should take startPosition.deltaScreenLines into account as well!
func (p *Pager) findFirstHit(startPosition linenumbers.LineNumber, beforePosition *linenumbers.LineNumber, backwards bool) *scrollPosition {
16 changes: 16 additions & 0 deletions testdata/git-diff-conflict-diff3-edit-remove.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
diff --cc lines.txt
index 97515f1,f42366c..0000000
--- lines.txt
+++ lines.txt
@@@ -1,3 -1,6 +1,11 @@@
First line
++<<<<<<< HEAD
++||||||| 3b5ce9b
++Caesar
++=======
+ Adam
+ Bertil
+ Caesar
++>>>>>>> branch
Last line

16 changes: 16 additions & 0 deletions testdata/git-diff-conflict-diff3-edit-remove.riff-output
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
diff --cc lines.txt
index 97515f1,f42366c..0000000
--- lines.txt
+++ lines.txt
@@@ -1,3 -1,6 +1,11 @@@
First line
++<<<<<<< HEAD
++||||||| 3b5ce9b
++Caesar
++=======
+ Adam
+ Bertil
+ Caesar
++>>>>>>> branch
Last line

14 changes: 7 additions & 7 deletions testdata/git-diff-conflict-diff3.riff-output
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
@@@ -1,3 -1,3 +1,9 @@@
This is an example of git conflict markers.

++<<<<<<< HEAD
 +This line is changed on the main branch.
++||||||| c26bbf8
++This line is from the initial commit on the main branch.
++=======
+ This line is from the branch named "branch".
++>>>>>>> branch
++<<<<<<< HEAD
 +This line is changed on the main branch.
++||||||| c26bbf8
++This line is from the initial commit on the main branch.
++=======
+ This line is from the branch named "branch".
++>>>>>>> branch
10 changes: 5 additions & 5 deletions testdata/git-diff-conflict.riff-output
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
@@@ -1,3 -1,3 +1,7 @@@
This is an example of git conflict markers.

++<<<<<<< HEAD
 +This line is changed on the main branch.
++=======
+ This line is from the branch named "branch".
++>>>>>>> branch
++<<<<<<< HEAD
 +This line is changed on the main branch.
++=======
+ This line is from the branch named "branch".
++>>>>>>> branch
Loading

0 comments on commit 37629bd

Please sign in to comment.