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

new resolve-git-conflict operator #1011

Merged
merged 2 commits into from
Dec 24, 2017
Merged
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
1 change: 1 addition & 0 deletions lib/json/command-table-pretty.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"vim-mode-plus:put-after-with-auto-indent",
"vim-mode-plus:add-blank-line-below",
"vim-mode-plus:add-blank-line-above",
"vim-mode-plus:resolve-git-conflict",
"vim-mode-plus:activate-insert-mode",
"vim-mode-plus:activate-replace-mode",
"vim-mode-plus:insert-after",
Expand Down
2 changes: 1 addition & 1 deletion lib/json/command-table.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion lib/json/file-table-pretty.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"PutBeforeWithAutoIndent",
"PutAfterWithAutoIndent",
"AddBlankLineBelow",
"AddBlankLineAbove"
"AddBlankLineAbove",
"ResolveGitConflict"
],
"./operator-insert": [
"ActivateInsertModeBase",
Expand Down
2 changes: 1 addition & 1 deletion lib/json/file-table.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"./operator":["Operator","SelectBase","Select","SelectLatestChange","SelectPreviousSelection","SelectPersistentSelection","SelectOccurrence","VisualModeSelect","CreatePersistentSelection","TogglePersistentSelection","TogglePresetOccurrence","TogglePresetSubwordOccurrence","AddPresetOccurrenceFromLastOccurrencePattern","Delete","DeleteRight","DeleteLeft","DeleteToLastCharacterOfLine","DeleteLine","Yank","YankLine","YankToLastCharacterOfLine","YankDiffHunk","Increase","Decrease","IncrementNumber","DecrementNumber","PutBefore","PutAfter","PutBeforeWithAutoIndent","PutAfterWithAutoIndent","AddBlankLineBelow","AddBlankLineAbove"],"./operator-insert":["ActivateInsertModeBase","ActivateInsertMode","ActivateReplaceMode","InsertAfter","InsertAtBeginningOfLine","InsertAfterEndOfLine","InsertAtFirstCharacterOfLine","InsertAtLastInsert","InsertAboveWithNewline","InsertBelowWithNewline","InsertByTarget","InsertAtStartOfTarget","InsertAtEndOfTarget","InsertAtHeadOfTarget","InsertAtStartOfOccurrence","InsertAtEndOfOccurrence","InsertAtHeadOfOccurrence","InsertAtStartOfSubwordOccurrence","InsertAtEndOfSubwordOccurrence","InsertAtHeadOfSubwordOccurrence","InsertAtStartOfSmartWord","InsertAtEndOfSmartWord","InsertAtPreviousFoldStart","InsertAtNextFoldStart","Change","ChangeOccurrence","Substitute","SubstituteLine","ChangeLine","ChangeToLastCharacterOfLine"],"./operator-transform-string":["TransformString","NoCase","DotCase","SwapCase","PathCase","UpperCase","LowerCase","CamelCase","SnakeCase","TitleCase","ParamCase","HeaderCase","PascalCase","ConstantCase","SentenceCase","UpperCaseFirst","LowerCaseFirst","DashCase","ToggleCase","ToggleCaseAndMoveRight","Replace","ReplaceCharacter","SplitByCharacter","EncodeUriComponent","DecodeUriComponent","TrimString","CompactSpaces","AlignOccurrence","AlignOccurrenceByPadLeft","AlignOccurrenceByPadRight","RemoveLeadingWhiteSpaces","ConvertToSoftTab","ConvertToHardTab","TransformStringByExternalCommand","TransformStringBySelectList","TransformWordBySelectList","TransformSmartWordBySelectList","ReplaceWithRegister","ReplaceOccurrenceWithRegister","SwapWithRegister","Indent","Outdent","AutoIndent","ToggleLineComments","Reflow","ReflowWithStay","SurroundBase","Surround","SurroundWord","SurroundSmartWord","MapSurround","DeleteSurround","DeleteSurroundAnyPair","DeleteSurroundAnyPairAllowForwarding","ChangeSurround","ChangeSurroundAnyPair","ChangeSurroundAnyPairAllowForwarding","JoinTarget","Join","JoinBase","JoinWithKeepingSpace","JoinByInput","JoinByInputWithKeepingSpace","SplitString","SplitStringWithKeepingSplitter","SplitArguments","SplitArgumentsWithRemoveSeparator","SplitArgumentsOfInnerAnyPair","ChangeOrder","Reverse","ReverseInnerAnyPair","Rotate","RotateBackwards","RotateArgumentsOfInnerPair","RotateArgumentsBackwardsOfInnerPair","Sort","SortCaseInsensitively","SortByNumber","NumberingLines","DuplicateWithCommentOutOriginal"],"./motion":["Motion","CurrentSelection","MoveLeft","MoveRight","MoveRightBufferColumn","MoveUp","MoveUpWrap","MoveDown","MoveDownWrap","MoveUpScreen","MoveDownScreen","MoveUpToEdge","MoveDownToEdge","WordMotion","MoveToNextWord","MoveToNextWholeWord","MoveToNextAlphanumericWord","MoveToNextSmartWord","MoveToNextSubword","MoveToPreviousWord","MoveToPreviousWholeWord","MoveToPreviousAlphanumericWord","MoveToPreviousSmartWord","MoveToPreviousSubword","MoveToEndOfWord","MoveToEndOfWholeWord","MoveToEndOfAlphanumericWord","MoveToEndOfSmartWord","MoveToEndOfSubword","MoveToPreviousEndOfWord","MoveToPreviousEndOfWholeWord","MoveToNextSentence","MoveToPreviousSentence","MoveToNextSentenceSkipBlankRow","MoveToPreviousSentenceSkipBlankRow","MoveToNextParagraph","MoveToPreviousParagraph","MoveToNextDiffHunk","MoveToPreviousDiffHunk","MoveToBeginningOfLine","MoveToColumn","MoveToLastCharacterOfLine","MoveToLastNonblankCharacterOfLineAndDown","MoveToFirstCharacterOfLine","MoveToFirstCharacterOfLineUp","MoveToFirstCharacterOfLineDown","MoveToFirstCharacterOfLineAndDown","MoveToScreenColumn","MoveToBeginningOfScreenLine","MoveToFirstCharacterOfScreenLine","MoveToLastCharacterOfScreenLine","MoveToFirstLine","MoveToLastLine","MoveToLineByPercent","MoveToRelativeLine","MoveToRelativeLineMinimumTwo","MoveToTopOfScreen","MoveToMiddleOfScreen","MoveToBottomOfScreen","Scroll","ScrollFullScreenDown","ScrollFullScreenUp","ScrollHalfScreenDown","ScrollHalfScreenUp","ScrollQuarterScreenDown","ScrollQuarterScreenUp","Find","FindBackwards","Till","TillBackwards","MoveToMark","MoveToMarkLine","MoveToPreviousFoldStart","MoveToNextFoldStart","MoveToPreviousFoldStartWithSameIndent","MoveToNextFoldStartWithSameIndent","MoveToPreviousFoldEnd","MoveToNextFoldEnd","MoveToPreviousFunction","MoveToNextFunction","MoveToPreviousFunctionAndRedrawCursorLineAtUpperMiddle","MoveToNextFunctionAndRedrawCursorLineAtUpperMiddle","MoveToPositionByScope","MoveToPreviousString","MoveToNextString","MoveToPreviousNumber","MoveToNextNumber","MoveToNextOccurrence","MoveToPreviousOccurrence","MoveToPair"],"./motion-search":["SearchBase","Search","SearchBackwards","SearchCurrentWord","SearchCurrentWordBackwards"],"./text-object":["TextObject","Word","WholeWord","SmartWord","Subword","Pair","APair","AnyPair","AnyPairAllowForwarding","AnyQuote","Quote","DoubleQuote","SingleQuote","BackTick","CurlyBracket","SquareBracket","Parenthesis","AngleBracket","Tag","Paragraph","Indentation","Comment","CommentOrParagraph","Fold","Function","Arguments","CurrentLine","Entire","Empty","LatestChange","SearchMatchForward","SearchMatchBackward","PreviousSelection","PersistentSelection","LastPastedRange","VisibleArea","AWord","InnerWord","AWholeWord","InnerWholeWord","ASmartWord","InnerSmartWord","ASubword","InnerSubword","AAnyPair","InnerAnyPair","AAnyPairAllowForwarding","InnerAnyPairAllowForwarding","AAnyQuote","InnerAnyQuote","ADoubleQuote","InnerDoubleQuote","ASingleQuote","InnerSingleQuote","ABackTick","InnerBackTick","ACurlyBracket","InnerCurlyBracket","ACurlyBracketAllowForwarding","InnerCurlyBracketAllowForwarding","ASquareBracket","InnerSquareBracket","ASquareBracketAllowForwarding","InnerSquareBracketAllowForwarding","AParenthesis","InnerParenthesis","AParenthesisAllowForwarding","InnerParenthesisAllowForwarding","AAngleBracket","InnerAngleBracket","AAngleBracketAllowForwarding","InnerAngleBracketAllowForwarding","ATag","InnerTag","AParagraph","InnerParagraph","AIndentation","InnerIndentation","AComment","InnerComment","ACommentOrParagraph","InnerCommentOrParagraph","AFold","InnerFold","AFunction","InnerFunction","AArguments","InnerArguments","ACurrentLine","InnerCurrentLine","AEntire","InnerEntire","ALatestChange","InnerLatestChange","APersistentSelection","InnerPersistentSelection","AVisibleArea","InnerVisibleArea","ADiffHunk","InnerDiffHunk"],"./misc-command":["MiscCommand","Mark","ReverseSelections","BlockwiseOtherEnd","Undo","Redo","FoldCurrentRow","UnfoldCurrentRow","ToggleFold","FoldCurrentRowRecursivelyBase","FoldCurrentRowRecursively","UnfoldCurrentRowRecursively","ToggleFoldRecursively","UnfoldAll","FoldAll","UnfoldNextIndentLevel","FoldNextIndentLevel","MiniScrollDown","MiniScrollUp","RedrawCursorLine","RedrawCursorLineAtTop","RedrawCursorLineAtTopAndMoveToFirstCharacterOfLine","RedrawCursorLineAtUpperMiddle","RedrawCursorLineAtUpperMiddleAndMoveToFirstCharacterOfLine","RedrawCursorLineAtMiddle","RedrawCursorLineAtMiddleAndMoveToFirstCharacterOfLine","RedrawCursorLineAtBottom","RedrawCursorLineAtBottomAndMoveToFirstCharacterOfLine","ScrollCursorToLeft","ScrollCursorToRight","ActivateNormalModeOnce","InsertRegister","InsertLastInserted","CopyFromLineAbove","CopyFromLineBelow","NextTab","PreviousTab"]}
{"./operator":["Operator","SelectBase","Select","SelectLatestChange","SelectPreviousSelection","SelectPersistentSelection","SelectOccurrence","VisualModeSelect","CreatePersistentSelection","TogglePersistentSelection","TogglePresetOccurrence","TogglePresetSubwordOccurrence","AddPresetOccurrenceFromLastOccurrencePattern","Delete","DeleteRight","DeleteLeft","DeleteToLastCharacterOfLine","DeleteLine","Yank","YankLine","YankToLastCharacterOfLine","YankDiffHunk","Increase","Decrease","IncrementNumber","DecrementNumber","PutBefore","PutAfter","PutBeforeWithAutoIndent","PutAfterWithAutoIndent","AddBlankLineBelow","AddBlankLineAbove","ResolveGitConflict"],"./operator-insert":["ActivateInsertModeBase","ActivateInsertMode","ActivateReplaceMode","InsertAfter","InsertAtBeginningOfLine","InsertAfterEndOfLine","InsertAtFirstCharacterOfLine","InsertAtLastInsert","InsertAboveWithNewline","InsertBelowWithNewline","InsertByTarget","InsertAtStartOfTarget","InsertAtEndOfTarget","InsertAtHeadOfTarget","InsertAtStartOfOccurrence","InsertAtEndOfOccurrence","InsertAtHeadOfOccurrence","InsertAtStartOfSubwordOccurrence","InsertAtEndOfSubwordOccurrence","InsertAtHeadOfSubwordOccurrence","InsertAtStartOfSmartWord","InsertAtEndOfSmartWord","InsertAtPreviousFoldStart","InsertAtNextFoldStart","Change","ChangeOccurrence","Substitute","SubstituteLine","ChangeLine","ChangeToLastCharacterOfLine"],"./operator-transform-string":["TransformString","NoCase","DotCase","SwapCase","PathCase","UpperCase","LowerCase","CamelCase","SnakeCase","TitleCase","ParamCase","HeaderCase","PascalCase","ConstantCase","SentenceCase","UpperCaseFirst","LowerCaseFirst","DashCase","ToggleCase","ToggleCaseAndMoveRight","Replace","ReplaceCharacter","SplitByCharacter","EncodeUriComponent","DecodeUriComponent","TrimString","CompactSpaces","AlignOccurrence","AlignOccurrenceByPadLeft","AlignOccurrenceByPadRight","RemoveLeadingWhiteSpaces","ConvertToSoftTab","ConvertToHardTab","TransformStringByExternalCommand","TransformStringBySelectList","TransformWordBySelectList","TransformSmartWordBySelectList","ReplaceWithRegister","ReplaceOccurrenceWithRegister","SwapWithRegister","Indent","Outdent","AutoIndent","ToggleLineComments","Reflow","ReflowWithStay","SurroundBase","Surround","SurroundWord","SurroundSmartWord","MapSurround","DeleteSurround","DeleteSurroundAnyPair","DeleteSurroundAnyPairAllowForwarding","ChangeSurround","ChangeSurroundAnyPair","ChangeSurroundAnyPairAllowForwarding","JoinTarget","Join","JoinBase","JoinWithKeepingSpace","JoinByInput","JoinByInputWithKeepingSpace","SplitString","SplitStringWithKeepingSplitter","SplitArguments","SplitArgumentsWithRemoveSeparator","SplitArgumentsOfInnerAnyPair","ChangeOrder","Reverse","ReverseInnerAnyPair","Rotate","RotateBackwards","RotateArgumentsOfInnerPair","RotateArgumentsBackwardsOfInnerPair","Sort","SortCaseInsensitively","SortByNumber","NumberingLines","DuplicateWithCommentOutOriginal"],"./motion":["Motion","CurrentSelection","MoveLeft","MoveRight","MoveRightBufferColumn","MoveUp","MoveUpWrap","MoveDown","MoveDownWrap","MoveUpScreen","MoveDownScreen","MoveUpToEdge","MoveDownToEdge","WordMotion","MoveToNextWord","MoveToNextWholeWord","MoveToNextAlphanumericWord","MoveToNextSmartWord","MoveToNextSubword","MoveToPreviousWord","MoveToPreviousWholeWord","MoveToPreviousAlphanumericWord","MoveToPreviousSmartWord","MoveToPreviousSubword","MoveToEndOfWord","MoveToEndOfWholeWord","MoveToEndOfAlphanumericWord","MoveToEndOfSmartWord","MoveToEndOfSubword","MoveToPreviousEndOfWord","MoveToPreviousEndOfWholeWord","MoveToNextSentence","MoveToPreviousSentence","MoveToNextSentenceSkipBlankRow","MoveToPreviousSentenceSkipBlankRow","MoveToNextParagraph","MoveToPreviousParagraph","MoveToNextDiffHunk","MoveToPreviousDiffHunk","MoveToBeginningOfLine","MoveToColumn","MoveToLastCharacterOfLine","MoveToLastNonblankCharacterOfLineAndDown","MoveToFirstCharacterOfLine","MoveToFirstCharacterOfLineUp","MoveToFirstCharacterOfLineDown","MoveToFirstCharacterOfLineAndDown","MoveToScreenColumn","MoveToBeginningOfScreenLine","MoveToFirstCharacterOfScreenLine","MoveToLastCharacterOfScreenLine","MoveToFirstLine","MoveToLastLine","MoveToLineByPercent","MoveToRelativeLine","MoveToRelativeLineMinimumTwo","MoveToTopOfScreen","MoveToMiddleOfScreen","MoveToBottomOfScreen","Scroll","ScrollFullScreenDown","ScrollFullScreenUp","ScrollHalfScreenDown","ScrollHalfScreenUp","ScrollQuarterScreenDown","ScrollQuarterScreenUp","Find","FindBackwards","Till","TillBackwards","MoveToMark","MoveToMarkLine","MoveToPreviousFoldStart","MoveToNextFoldStart","MoveToPreviousFoldStartWithSameIndent","MoveToNextFoldStartWithSameIndent","MoveToPreviousFoldEnd","MoveToNextFoldEnd","MoveToPreviousFunction","MoveToNextFunction","MoveToPreviousFunctionAndRedrawCursorLineAtUpperMiddle","MoveToNextFunctionAndRedrawCursorLineAtUpperMiddle","MoveToPositionByScope","MoveToPreviousString","MoveToNextString","MoveToPreviousNumber","MoveToNextNumber","MoveToNextOccurrence","MoveToPreviousOccurrence","MoveToPair"],"./motion-search":["SearchBase","Search","SearchBackwards","SearchCurrentWord","SearchCurrentWordBackwards"],"./text-object":["TextObject","Word","WholeWord","SmartWord","Subword","Pair","APair","AnyPair","AnyPairAllowForwarding","AnyQuote","Quote","DoubleQuote","SingleQuote","BackTick","CurlyBracket","SquareBracket","Parenthesis","AngleBracket","Tag","Paragraph","Indentation","Comment","CommentOrParagraph","Fold","Function","Arguments","CurrentLine","Entire","Empty","LatestChange","SearchMatchForward","SearchMatchBackward","PreviousSelection","PersistentSelection","LastPastedRange","VisibleArea","AWord","InnerWord","AWholeWord","InnerWholeWord","ASmartWord","InnerSmartWord","ASubword","InnerSubword","AAnyPair","InnerAnyPair","AAnyPairAllowForwarding","InnerAnyPairAllowForwarding","AAnyQuote","InnerAnyQuote","ADoubleQuote","InnerDoubleQuote","ASingleQuote","InnerSingleQuote","ABackTick","InnerBackTick","ACurlyBracket","InnerCurlyBracket","ACurlyBracketAllowForwarding","InnerCurlyBracketAllowForwarding","ASquareBracket","InnerSquareBracket","ASquareBracketAllowForwarding","InnerSquareBracketAllowForwarding","AParenthesis","InnerParenthesis","AParenthesisAllowForwarding","InnerParenthesisAllowForwarding","AAngleBracket","InnerAngleBracket","AAngleBracketAllowForwarding","InnerAngleBracketAllowForwarding","ATag","InnerTag","AParagraph","InnerParagraph","AIndentation","InnerIndentation","AComment","InnerComment","ACommentOrParagraph","InnerCommentOrParagraph","AFold","InnerFold","AFunction","InnerFunction","AArguments","InnerArguments","ACurrentLine","InnerCurrentLine","AEntire","InnerEntire","ALatestChange","InnerLatestChange","APersistentSelection","InnerPersistentSelection","AVisibleArea","InnerVisibleArea","ADiffHunk","InnerDiffHunk"],"./misc-command":["MiscCommand","Mark","ReverseSelections","BlockwiseOtherEnd","Undo","Redo","FoldCurrentRow","UnfoldCurrentRow","ToggleFold","FoldCurrentRowRecursivelyBase","FoldCurrentRowRecursively","UnfoldCurrentRowRecursively","ToggleFoldRecursively","UnfoldAll","FoldAll","UnfoldNextIndentLevel","FoldNextIndentLevel","MiniScrollDown","MiniScrollUp","RedrawCursorLine","RedrawCursorLineAtTop","RedrawCursorLineAtTopAndMoveToFirstCharacterOfLine","RedrawCursorLineAtUpperMiddle","RedrawCursorLineAtUpperMiddleAndMoveToFirstCharacterOfLine","RedrawCursorLineAtMiddle","RedrawCursorLineAtMiddleAndMoveToFirstCharacterOfLine","RedrawCursorLineAtBottom","RedrawCursorLineAtBottomAndMoveToFirstCharacterOfLine","ScrollCursorToLeft","ScrollCursorToRight","ActivateNormalModeOnce","InsertRegister","InsertLastInserted","CopyFromLineAbove","CopyFromLineBelow","NextTab","PreviousTab"]}
67 changes: 67 additions & 0 deletions lib/operator.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"use babel"

const {Range} = require("atom")
const Base = require("./base")

class Operator extends Base {
Expand Down Expand Up @@ -831,6 +832,71 @@ class AddBlankLineAbove extends AddBlankLineBelow {
where = "above"
}

class ResolveGitConflict extends Operator {
target = "Empty"
restorePositions = false // do manually

mutateSelection(selection) {
const point = this.getCursorPositionForSelection(selection)
const rangeInfo = this.getConflictingRangeInfo(point.row)

if (rangeInfo) {
const {whole, sectionOurs, sectionTheirs, bodyOurs, bodyTheirs} = rangeInfo
const resolveConflict = range => {
const text = this.editor.getTextInBufferRange(range)
const dstRange = this.getBufferRangeForRowRange([whole.start.row, whole.end.row])
const newRange = this.editor.setTextInBufferRange(dstRange, text ? text + "\n" : "")
selection.cursor.setBufferPosition(newRange.start)
}
// NOTE: When cursor is at separator row '=======', no replace happens because it's ambiguous.
if (sectionOurs.containsPoint(point)) {
resolveConflict(bodyOurs)
} else if (sectionTheirs.containsPoint(point)) {
resolveConflict(bodyTheirs)
}
}
}

getConflictingRangeInfo(row) {
const from = [row, Infinity]
const conflictStart = this.findInEditor("backward", /^<<<<<<< .+$/, {from}, event => event.range.start)

if (conflictStart) {
const startRow = conflictStart.row
let separatorRow, endRow
const from = [startRow + 1, 0]
const regex = /(^<<<<<<< .+$)|(^=======$)|(^>>>>>>> .+$)/g
this.scanEditor("forward", regex, {from}, ({match, range, stop}) => {
if (match[1]) {
// incomplete conflict hunk, we saw next conflict startRow wihout seeing endRow
stop()
} else if (match[2]) {
separatorRow = range.start.row
} else if (match[3]) {
endRow = range.start.row
stop()
}
})
if (!endRow) return
const whole = new Range([startRow, 0], [endRow, Infinity])
const sectionOurs = new Range(whole.start, [(separatorRow || endRow) - 1, Infinity])
const sectionTheirs = new Range([(separatorRow || startRow) + 1, 0], whole.end)

const bodyOursStart = sectionOurs.start.translate([1, 0])
const bodyOurs =
sectionOurs.getRowCount() === 1
? new Range(bodyOursStart, bodyOursStart)
: new Range(bodyOursStart, sectionOurs.end)

const bodyTheirs =
sectionTheirs.getRowCount() === 1
? new Range(sectionTheirs.start, sectionTheirs.start)
: sectionTheirs.translate([0, 0], [-1, 0])
return {whole, sectionOurs, sectionTheirs, bodyOurs, bodyTheirs}
}
}
}

module.exports = {
Operator,
SelectBase,
Expand Down Expand Up @@ -864,4 +930,5 @@ module.exports = {
PutAfterWithAutoIndent,
AddBlankLineBelow,
AddBlankLineAbove,
ResolveGitConflict,
}
Loading