diff --git a/spec/text-editor-spec.coffee b/spec/text-editor-spec.coffee index fe12d6532..2947eb06a 100644 --- a/spec/text-editor-spec.coffee +++ b/spec/text-editor-spec.coffee @@ -4018,3 +4018,25 @@ describe "TextEditor", -> editor.setPlaceholderText('OK') expect(handler).toHaveBeenCalledWith 'OK' expect(editor.getPlaceholderText()).toBe 'OK' + + describe ".checkoutHeadRevision()", -> + it "reverts to the version of its file checked into the project repository", -> + atom.config.set("editor.confirmCheckoutHeadRevision", false) + + editor.setCursorBufferPosition([0, 0]) + editor.insertText("---\n") + expect(editor.lineTextForBufferRow(0)).toBe "---" + + waitsForPromise -> + editor.checkoutHeadRevision() + + runs -> + expect(editor.lineTextForBufferRow(0)).toBe "var quicksort = function () {" + + describe "when there's no repository for the editor's file", -> + it "doesn't do anything", -> + editor = new TextEditor({}) + editor.setText("stuff") + editor.checkoutHeadRevision() + + waitsForPromise -> editor.checkoutHeadRevision() diff --git a/src/text-editor-element.coffee b/src/text-editor-element.coffee index 443786793..ccd69dca8 100644 --- a/src/text-editor-element.coffee +++ b/src/text-editor-element.coffee @@ -337,7 +337,7 @@ atom.commands.add 'atom-text-editor:not([mini])', stopEventPropagationAndGroupUn 'editor:newline-below': -> @insertNewlineBelow() 'editor:newline-above': -> @insertNewlineAbove() 'editor:toggle-line-comments': -> @toggleLineCommentsInSelection() - 'editor:checkout-head-revision': -> atom.project.getRepositories()[0]?.checkoutHeadForEditor(this) + 'editor:checkout-head-revision': -> @checkoutHeadRevision() 'editor:move-line-up': -> @moveLineUp() 'editor:move-line-down': -> @moveLineDown() 'editor:duplicate-lines': -> @duplicateLines() diff --git a/src/text-editor.coffee b/src/text-editor.coffee index bd3f76190..d9c01f4c1 100644 --- a/src/text-editor.coffee +++ b/src/text-editor.coffee @@ -12,6 +12,7 @@ DisplayBuffer = require './display-buffer' Cursor = require './cursor' Selection = require './selection' TextMateScopeSelector = require('first-mate').ScopeSelector +{Directory} = require "pathwatcher" # Public: This class represents all essential editing state for a single # {TextBuffer}, including cursor and selection positions, folds, and soft wraps. @@ -647,6 +648,14 @@ class TextEditor extends Model else @isModified() and not @buffer.hasMultipleEditors() + checkoutHeadRevision: -> + if filePath = this.getPath() + atom.project.repositoryForDirectory(new Directory(path.dirname(filePath))) + .then (repository) => + repository?.checkoutHeadForEditor(this) + else + Promise.resolve(false) + ### Section: Reading Text ###