From 53c3fa8ac809af78b6493510dada5e8de723f417 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Tue, 9 Apr 2013 14:33:36 -0700 Subject: [PATCH 1/7] Bind meta-< to scroll to cursor location --- spec/app/editor-spec.coffee | 11 +++++++++++ src/app/editor.coffee | 4 ++++ src/app/keymaps/editor.cson | 1 + 3 files changed, 16 insertions(+) diff --git a/spec/app/editor-spec.coffee b/spec/app/editor-spec.coffee index c7d21989e..868226f5f 100644 --- a/spec/app/editor-spec.coffee +++ b/spec/app/editor-spec.coffee @@ -2552,3 +2552,14 @@ describe "Editor", -> runs -> expect(editor.renderedLines.find('.line').text()).toBe 'hidden changes' + + describe "editor:scroll-to-cursor", -> + it "scrolls to and centers the editor on the cursor's position", -> + editor.attachToDom(heightInLines: 3) + editor.setCursorBufferPosition([1, 2]) + editor.scrollToBottom() + expect(editor.getFirstVisibleScreenRow()).not.toBe 0 + expect(editor.getLastVisibleScreenRow()).not.toBe 2 + editor.trigger('editor:scroll-to-cursor') + expect(editor.getFirstVisibleScreenRow()).toBe 0 + expect(editor.getLastVisibleScreenRow()).toBe 2 diff --git a/src/app/editor.coffee b/src/app/editor.coffee index 5ffdf3c45..1f66b8a42 100644 --- a/src/app/editor.coffee +++ b/src/app/editor.coffee @@ -164,6 +164,7 @@ class Editor extends View 'editor:toggle-indent-guide': => config.set('editor.showIndentGuide', !config.get('editor.showIndentGuide')) 'editor:save-debug-snapshot': @saveDebugSnapshot 'editor:toggle-line-numbers': => config.set('editor.showLineNumbers', !config.get('editor.showLineNumbers')) + 'editor:scroll-to-cursor': @scrollToCursorPosition documentation = {} for name, method of editorBindings @@ -523,6 +524,9 @@ class Editor extends View scrollToBottom: -> @scrollBottom(@screenLineCount() * @lineHeight) + scrollToCursorPosition: -> + @scrollToBufferPosition(@getCursorBufferPosition(), center: true) + scrollToBufferPosition: (bufferPosition, options) -> @scrollToPixelPosition(@pixelPositionForBufferPosition(bufferPosition), options) diff --git a/src/app/keymaps/editor.cson b/src/app/keymaps/editor.cson index 07568fc0b..f1af1b842 100644 --- a/src/app/keymaps/editor.cson +++ b/src/app/keymaps/editor.cson @@ -27,6 +27,7 @@ 'ctrl-meta-down': 'editor:move-line-down' 'meta-D': 'editor:duplicate-line' 'ctrl-J': 'editor:join-line' + 'meta-<': 'editor:scroll-to-cursor' '.editor.mini': 'enter': 'core:confirm', From badcc6c2bfbde64cbaea6a0b3492597164c18de8 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Wed, 10 Apr 2013 13:22:06 +0800 Subject: [PATCH 2/7] Update node-pathwatcher to 0.2.0, node-spellchecker to 0.3.0. --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 3bde864d6..85d88ccaa 100644 --- a/package.json +++ b/package.json @@ -14,8 +14,8 @@ "pegjs": "0.7.0", "async": "0.2.6", "nak": "0.2.12", - "spellchecker": "0.2.0", - "pathwatcher": "0.1.5", + "spellchecker": "0.3.0", + "pathwatcher": "0.2.0", "plist": "git://github.com/nathansobo/node-plist.git", "space-pen": "git://github.com/nathansobo/space-pen.git" }, From b74f2a3e9eabf8565361080b6aaa0d561d328f44 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Wed, 10 Apr 2013 12:05:57 -0700 Subject: [PATCH 3/7] Upgrade to git-utils 0.13 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 85d88ccaa..23116ab57 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "ctags": "0.3.0", "oniguruma": "0.8.0", "mkdirp": "0.3.5", - "git-utils": "0.12.0", + "git-utils": "0.13.0", "underscore": "1.4.4", "d3": "3.0.8", "coffee-cache": "0.1.0", From f868c0cd4fb2eec53bd4387fd6d3c928141d6055 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Wed, 10 Apr 2013 12:16:19 -0700 Subject: [PATCH 4/7] Add line diff decorations to editor gutter Added, modified, and deleted lines will now highlighted in the gutter for files already checked in to the repository. --- src/app/git.coffee | 5 +- src/app/gutter.coffee | 2 +- .../git-diff/lib/git-diff-view.coffee | 62 ++++++++++++++++++ src/packages/git-diff/lib/git-diff.coffee | 12 ++++ src/packages/git-diff/package.cson | 1 + .../git-diff/spec/git-diff-spec.coffee | 63 +++++++++++++++++++ .../git-diff/stylesheets/git-diff.less | 11 ++++ 7 files changed, 154 insertions(+), 2 deletions(-) create mode 100644 src/packages/git-diff/lib/git-diff-view.coffee create mode 100644 src/packages/git-diff/lib/git-diff.coffee create mode 100644 src/packages/git-diff/package.cson create mode 100644 src/packages/git-diff/spec/git-diff-spec.coffee create mode 100644 src/packages/git-diff/stylesheets/git-diff.less diff --git a/src/app/git.coffee b/src/app/git.coffee index b01cf826b..ee9c953ac 100644 --- a/src/app/git.coffee +++ b/src/app/git.coffee @@ -123,7 +123,7 @@ class Git @statusTask = null @statusTask.start() - getDirectoryStatus: (directoryPath) -> + getDirectoryStatus: (directoryPath) -> directoryPath = "#{directoryPath}/" directoryStatus = 0 for path, status of @statuses @@ -133,5 +133,8 @@ class Git getAheadBehindCounts: -> @getRepo().getAheadBehindCount() + getLineDiffs: (path, text) -> + @getRepo().getLineDiffs(@relativize(path), text) + _.extend Git.prototype, Subscriber _.extend Git.prototype, EventEmitter diff --git a/src/app/gutter.coffee b/src/app/gutter.coffee index da97e6e89..76bf22cad 100644 --- a/src/app/gutter.coffee +++ b/src/app/gutter.coffee @@ -76,7 +76,7 @@ class Gutter extends View rowValue = (row + 1).toString() classes = ['line-number'] classes.push('fold') if editor.isFoldedAtBufferRow(row) - @div class: classes.join(' '), => + @div lineNumber: row, class: classes.join(' '), => rowValuePadding = _.multiplyString(' ', maxDigits - rowValue.length) @raw("#{rowValuePadding}#{rowValue}") diff --git a/src/packages/git-diff/lib/git-diff-view.coffee b/src/packages/git-diff/lib/git-diff-view.coffee new file mode 100644 index 000000000..f49df2e42 --- /dev/null +++ b/src/packages/git-diff/lib/git-diff-view.coffee @@ -0,0 +1,62 @@ +_ = require 'underscore' + +module.exports = +class GitDiffView + diffs: null + editor: null + + constructor: (@editor) -> + @gutter = @editor.gutter + @diffs = {} + + @editor.on 'editor:path-changed', => @subscribeToBuffer() + @editor.on 'editor:display-updated', => @renderDiffs() + git.on 'statuses-changed', => + @diffs = {} + @scheduleDiffs() + git.on 'status-changed', (path) => + delete @diffs[path] + @scheduleDiffs() if path is @editor.getPath() + + @subscribeToBuffer() + + subscribeToBuffer: -> + if @buffer? + @removeDiffs() + delete @diffs[@buffer.getPath()] if @buffer.destroyed + @buffer.off '.git-diff' + @buffer = null + + if @buffer = @editor.getBuffer() + @scheduleDiffs() unless @diffs[@buffer.getPath()]? + @buffer.on 'contents-modified.git-diff', => + @generateDiffs() + @renderDiffs() + + scheduleDiffs: -> + _.nextTick => + @generateDiffs() + @renderDiffs() + + generateDiffs: -> + if path = @buffer.getPath() + @diffs[path] = git?.getLineDiffs(path, @buffer.getText()) + + removeDiffs: -> + @gutter.find('.line-number').removeClass('git-line-added git-line-modified git-line-removed') + + renderDiffs: -> + return unless @gutter.isVisible() + + @removeDiffs() + + hunks = @diffs[@editor.getPath()] ? [] + for hunk in hunks + if hunk.oldLines is 0 and hunk.newLines > 0 + for row in [hunk.newStart...hunk.newStart + hunk.newLines] + @gutter.find(".line-number[lineNumber=#{row - 1}]").addClass('git-line-added') + else if hunk.newLines is 0 and hunk.oldLines > 0 + @gutter.find(".line-number[lineNumber=#{hunk.newStart - 1}]").addClass('git-line-removed') + else + for row in [hunk.newStart...hunk.newStart + hunk.newLines] + @gutter.find(".line-number[lineNumber=#{row - 1}]").addClass('git-line-modified') diff --git a/src/packages/git-diff/lib/git-diff.coffee b/src/packages/git-diff/lib/git-diff.coffee new file mode 100644 index 000000000..127eb4b81 --- /dev/null +++ b/src/packages/git-diff/lib/git-diff.coffee @@ -0,0 +1,12 @@ +GitDiffView = require './git-diff-view' + +module.exports = + configDefaults: + enabled: false + + activate: -> + return unless git? + return unless config.get('git-diff.enabled') + + rootView.eachEditor (editor) => + new GitDiffView(editor) if git? and editor.getPane()? diff --git a/src/packages/git-diff/package.cson b/src/packages/git-diff/package.cson new file mode 100644 index 000000000..8fe8b9080 --- /dev/null +++ b/src/packages/git-diff/package.cson @@ -0,0 +1 @@ +'main': 'lib/git-diff' diff --git a/src/packages/git-diff/spec/git-diff-spec.coffee b/src/packages/git-diff/spec/git-diff-spec.coffee new file mode 100644 index 000000000..a800ac37a --- /dev/null +++ b/src/packages/git-diff/spec/git-diff-spec.coffee @@ -0,0 +1,63 @@ +RootView = require 'root-view' +_ = require 'underscore' + +describe "GitDiff package", -> + editor = null + + beforeEach -> + config.set('git-diff.enabled', true) + window.rootView = new RootView + rootView.attachToDom() + rootView.open('sample.js') + atom.activatePackage('git-diff') + editor = rootView.getActiveView() + + describe "when the editor has modified lines", -> + it "highlights the modified lines", -> + expect(editor.find('.git-line-modified').length).toBe 0 + editor.insertText('a') + advanceClock(editor.getBuffer().stoppedChangingDelay) + expect(editor.find('.git-line-modified').length).toBe 1 + expect(editor.find('.git-line-modified').attr('lineNumber')).toBe '0' + + describe "when the editor has added lines", -> + it "highlights the added lines", -> + expect(editor.find('.git-line-added').length).toBe 0 + editor.moveCursorToEndOfLine() + editor.insertNewline() + editor.insertText('a') + advanceClock(editor.getBuffer().stoppedChangingDelay) + expect(editor.find('.git-line-added').length).toBe 1 + expect(editor.find('.git-line-added').attr('lineNumber')).toBe '1' + + describe "when the editor has removed lines", -> + it "highlights the line preceeding the deleted lines", -> + expect(editor.find('.git-line-added').length).toBe 0 + editor.setCursorBufferPosition([5]) + editor.deleteLine() + advanceClock(editor.getBuffer().stoppedChangingDelay) + expect(editor.find('.git-line-removed').length).toBe 1 + expect(editor.find('.git-line-removed').attr('lineNumber')).toBe '4' + + describe "when a modified line is restored to the HEAD version contents", -> + it "removes the diff highlight", -> + expect(editor.find('.git-line-modified').length).toBe 0 + editor.insertText('a') + advanceClock(editor.getBuffer().stoppedChangingDelay) + expect(editor.find('.git-line-modified').length).toBe 1 + editor.backspace() + advanceClock(editor.getBuffer().stoppedChangingDelay) + expect(editor.find('.git-line-modified').length).toBe 0 + + describe "when a modified file is opened", -> + it "highlights the changed lines", -> + path = project.resolve('sample.txt') + buffer = project.buildBuffer(path) + buffer.setText("Some different text.") + rootView.open('sample.txt') + nextTick = false + _.nextTick -> nextTick = true + waitsFor -> nextTick + runs -> + expect(editor.find('.git-line-modified').length).toBe 1 + expect(editor.find('.git-line-modified').attr('lineNumber')).toBe '0' diff --git a/src/packages/git-diff/stylesheets/git-diff.less b/src/packages/git-diff/stylesheets/git-diff.less new file mode 100644 index 000000000..a04bed75d --- /dev/null +++ b/src/packages/git-diff/stylesheets/git-diff.less @@ -0,0 +1,11 @@ +.git-line-modified { + color: #f78a46; +} + +.git-line-added { + color: #5293d8; +} + +.git-line-removed { + color: #c41e3a; +} From 5bbc70e6de86aed485481a6820e99ffb03b18040 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Wed, 10 Apr 2013 12:40:36 -0700 Subject: [PATCH 5/7] Back out pathwatcher upgrade The v0.2.0 version does not appear to load within atom. --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 23116ab57..e6840ae7f 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,7 @@ "async": "0.2.6", "nak": "0.2.12", "spellchecker": "0.3.0", - "pathwatcher": "0.2.0", + "pathwatcher": "0.1.0", "plist": "git://github.com/nathansobo/node-plist.git", "space-pen": "git://github.com/nathansobo/space-pen.git" }, From a64b81090d929aeedf98ece3cdc80d97d8e9c2c9 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Wed, 10 Apr 2013 12:41:35 -0700 Subject: [PATCH 6/7] Only remove styles if line diffs exist --- src/packages/git-diff/lib/git-diff-view.coffee | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/packages/git-diff/lib/git-diff-view.coffee b/src/packages/git-diff/lib/git-diff-view.coffee index f49df2e42..bff48f146 100644 --- a/src/packages/git-diff/lib/git-diff-view.coffee +++ b/src/packages/git-diff/lib/git-diff-view.coffee @@ -43,7 +43,9 @@ class GitDiffView @diffs[path] = git?.getLineDiffs(path, @buffer.getText()) removeDiffs: -> - @gutter.find('.line-number').removeClass('git-line-added git-line-modified git-line-removed') + if @gutter.hasGitLineDiffs + @gutter.find('.line-number').removeClass('git-line-added git-line-modified git-line-removed') + @gutter.hasGitLineDiffs = false renderDiffs: -> return unless @gutter.isVisible() @@ -51,12 +53,14 @@ class GitDiffView @removeDiffs() hunks = @diffs[@editor.getPath()] ? [] + linesHighlighted = 0 for hunk in hunks if hunk.oldLines is 0 and hunk.newLines > 0 for row in [hunk.newStart...hunk.newStart + hunk.newLines] - @gutter.find(".line-number[lineNumber=#{row - 1}]").addClass('git-line-added') + linesHighlighted += @gutter.find(".line-number[lineNumber=#{row - 1}]").addClass('git-line-added').length else if hunk.newLines is 0 and hunk.oldLines > 0 - @gutter.find(".line-number[lineNumber=#{hunk.newStart - 1}]").addClass('git-line-removed') + linesHighlighted += @gutter.find(".line-number[lineNumber=#{hunk.newStart - 1}]").addClass('git-line-removed').length else for row in [hunk.newStart...hunk.newStart + hunk.newLines] - @gutter.find(".line-number[lineNumber=#{row - 1}]").addClass('git-line-modified') + linesHighlighted += @gutter.find(".line-number[lineNumber=#{row - 1}]").addClass('git-line-modified').length + @gutter.hasGitLineDiffs = linesHighlighted > 0 From 8182bd59f998b4c28056b834c1b759045b5ba7c7 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Wed, 10 Apr 2013 12:56:12 -0700 Subject: [PATCH 7/7] Upgrade to pathwatcher 0.2 This appears to work now after doing a clean and reinstall of all modules. --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index e6840ae7f..23116ab57 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,7 @@ "async": "0.2.6", "nak": "0.2.12", "spellchecker": "0.3.0", - "pathwatcher": "0.1.0", + "pathwatcher": "0.2.0", "plist": "git://github.com/nathansobo/node-plist.git", "space-pen": "git://github.com/nathansobo/space-pen.git" },