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

Git conflict experiment #5500

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 73 additions & 1 deletion src/Lean/Parser/Basic.lean
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,56 @@ partial def finishCommentBlock (nesting : Nat) : ParserFn := fun c s =>
where
eoi s := s.mkUnexpectedError (pushMissing := pushMissingOnError) "unterminated comment"

def checkColZero (errorMsg : String) : ParserFn := fun c s =>
let pos := c.fileMap.toPosition s.pos
if pos.column = 0 then s
else s.mkError errorMsg

def parseStartDiff (errorMsg : String) : ParserFn :=
let p := satisfyFn (· == '<') errorMsg
andthenFn (checkColZero errorMsg)
(andthenFn p (andthenFn p (andthenFn p (andthenFn p (andthenFn p (andthenFn p (andthenFn p
Copy link

@adomani adomani Oct 2, 2024

Choose a reason for hiding this comment

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

What do you think of starting the diff with 6, rather than 7, <?

This means that you can remove the first <, exclude all editor diff-related noise, and view a "clean" file?

(takeUntilFn (fun c => c = '\n')))))))))

def parseMidDiff (errorMsg : String) : ParserFn :=
let p := satisfyFn (· == '=') errorMsg
andthenFn (checkColZero errorMsg)
(andthenFn p (andthenFn p (andthenFn p (andthenFn p (andthenFn p (andthenFn p p))))))

partial def parseIncomingDiffBody : ParserFn := fun c s =>
let input := c.input
let i := s.pos
if h : input.atEnd i then s.mkUnexpectedError (pushMissing := false) "unterminated git conflict"
else
let s' := parseMidDiff "mid conflict" c s
if s'.hasError then
parseIncomingDiffBody c (s.next' input i h)
else
s'

def parseEndDiff (errorMsg : String) : ParserFn :=
let p := satisfyFn (· == '>') errorMsg
andthenFn (checkColZero errorMsg)
(andthenFn p (andthenFn p (andthenFn p (andthenFn p (andthenFn p (andthenFn p (andthenFn p
(takeUntilFn (fun c => c = '\n')))))))))

partial def parseOutgoingDiffBody : ParserFn := fun c s =>
let input := c.input
let i := s.pos
if h : input.atEnd i then s.mkUnexpectedError (pushMissing := false) "unterminated git conflict"
else
let s' := parseEndDiff "end conflict" c s
if s'.hasError then
parseOutgoingDiffBody c (s.next' input i h)
else
s'

register_builtin_option parser.git.useIncoming : Bool := {
defValue := true
group := "pp"
descr := "(parser) for git conflicts, when true parses only the incoming code, and when false parses only the current code"
}

/-- Consume whitespace and comments -/
partial def whitespace : ParserFn := fun c s =>
let input := c.input
Expand All @@ -575,7 +625,29 @@ partial def whitespace : ParserFn := fun c s =>
if curr == '-' || curr == '!' then s -- "/--" and "/-!" doc comment are actual tokens
else andthenFn (finishCommentBlock (pushMissingOnError := false) 1) whitespace c (s.next input i)
else s
else s
else
if parser.git.useIncoming.get c.options then
-- `<<<<<<< ... =======` and `>>>>>>>` are comments
let s' := parseStartDiff "start conflict" c s
if !s'.hasError then
(andthenFn parseIncomingDiffBody whitespace) c s'
else
let s' := parseEndDiff "end conflict" c s
if !s'.hasError then
whitespace c s'
else
s
else
-- `<<<<<<<` and `======= ... >>>>>>>` are comments
let s' := parseMidDiff "mid conflict" c s
if !s'.hasError then
(andthenFn parseOutgoingDiffBody whitespace) c s'
else
let s' := parseStartDiff "start conflict" c s
if !s'.hasError then
whitespace c s'
else
s

def mkEmptySubstringAt (s : String) (p : String.Pos) : Substring := {
str := s, startPos := p, stopPos := p
Expand Down
21 changes: 12 additions & 9 deletions stage0/src/include/lean/lean.h

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

6 changes: 6 additions & 0 deletions stage0/src/kernel/type_checker.cpp

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

17 changes: 17 additions & 0 deletions stage0/src/lake/README.md

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

Loading