diff --git a/spec/app/editor-spec.coffee b/spec/app/editor-spec.coffee index b796898b4..61a5cb93c 100644 --- a/spec/app/editor-spec.coffee +++ b/spec/app/editor-spec.coffee @@ -2096,3 +2096,37 @@ describe "Editor", -> eventHandler.reset() editor.buffer.setPath("new.txt") expect(eventHandler).toHaveBeenCalled() + + describe ".loadNextEditorState()", -> + it "loads the next editor state and wraps to beginning when end is reached", -> + buffer0 = new Buffer("0") + buffer1 = new Buffer("1") + buffer2 = new Buffer("2") + editor = new Editor {buffer: buffer0} + editor.setBuffer(buffer1) + editor.setBuffer(buffer2) + + expect(editor.buffer.path).toBe "2" + editor.loadNextEditorState() + expect(editor.buffer.path).toBe "0" + editor.loadNextEditorState() + expect(editor.buffer.path).toBe "1" + editor.loadNextEditorState() + expect(editor.buffer.path).toBe "2" + + describe ".loadPreviousEditorState()", -> + it "loads the next editor state and wraps to beginning when end is reached", -> + buffer0 = new Buffer("0") + buffer1 = new Buffer("1") + buffer2 = new Buffer("2") + editor = new Editor {buffer: buffer0} + editor.setBuffer(buffer1) + editor.setBuffer(buffer2) + + expect(editor.buffer.path).toBe "2" + editor.loadPreviousEditorState() + expect(editor.buffer.path).toBe "1" + editor.loadPreviousEditorState() + expect(editor.buffer.path).toBe "0" + editor.loadPreviousEditorState() + expect(editor.buffer.path).toBe "2" diff --git a/src/app/command-panel.coffee b/src/app/command-panel.coffee index e7eb9d0f7..fe027b1f5 100644 --- a/src/app/command-panel.coffee +++ b/src/app/command-panel.coffee @@ -32,11 +32,11 @@ class CommandPanel extends View toggle: -> if @parent().length then @hide() else @show() - show: -> + show: (text='') -> @rootView.append(this) @prompt.css 'font', @editor.css('font') @editor.focus() - @editor.buffer.setText('') + @editor.buffer.setText(text) hide: -> @detach() diff --git a/src/app/editor.coffee b/src/app/editor.coffee index b50abd3e3..c1c105521 100644 --- a/src/app/editor.coffee +++ b/src/app/editor.coffee @@ -48,7 +48,7 @@ class Editor extends View @autoIndent = true @buildCursorAndSelection() @handleEvents() - @editorStatesByBufferId = {} + @editorStates = [] @setEditorState(editorState) bindKeys: -> @@ -84,6 +84,8 @@ class Editor extends View @on 'split-up', => @splitUp() @on 'split-down', => @splitDown() @on 'close', => @remove(); false + @on 'show-next-buffer', => @loadNextEditorState() + @on 'show-previous-buffer', => @loadPreviousEditorState() @on 'move-to-top', => @moveCursorToTop() @on 'move-to-bottom', => @moveCursorToBottom() @@ -217,15 +219,48 @@ class Editor extends View @buffer.on "change.editor#{@id}", (e) => @handleBufferChange(e) @renderer.on 'change', (e) => @handleRendererChange(e) + getEditorStateForBuffer: (buffer) -> + _.find @editorStates, (editorState) => + editorState.buffer.id == buffer.id + + setEditorStateForBuffer: (buffer, editorState) -> + editorState.buffer = buffer + index = @indexOfEditorState(editorState) + if index? + @editorStates[index] = editorState + else + @editorStates.push(editorState) + + indexOfEditorState: (editorState) -> + for o, i in @editorStates + return i if o.buffer.id == editorState.buffer.id + + return null + loadEditorStateForBuffer: (buffer) -> - editorState = (@editorStatesByBufferId[buffer.id] ?= {}) + editorState = @getEditorStateForBuffer(buffer) + if not editorState + editorState = {} + @setEditorStateForBuffer(buffer, editorState) @setCursorScreenPosition(editorState.cursorScreenPosition ? [0, 0]) @scroller.scrollTop(editorState.scrollTop ? 0) @scroller.scrollLeft(editorState.scrollLeft ? 0) - setEditorState: (editorState) -> - buffer = editorState.buffer ? new Buffer - @editorStatesByBufferId[buffer.id] = editorState + loadNextEditorState: -> + index = @indexOfEditorState(@getEditorState()) + if index? + nextIndex = (index + 1) % @editorStates.length + @setEditorState(@editorStates[nextIndex]) + + loadPreviousEditorState: -> + index = @indexOfEditorState(@getEditorState()) + if index? + previousIndex = if --index >= 0 then index else @editorStates.length - 1 + @setEditorState(@editorStates[previousIndex]) + + setEditorState: (editorState={}) -> + buffer = editorState.buffer ?= new Buffer + @setEditorStateForBuffer(buffer, editorState) @setBuffer(buffer) @isFocused = editorState.isFocused @@ -237,7 +272,7 @@ class Editor extends View isFocused: @isFocused saveEditorStateForCurrentBuffer: -> - @editorStatesByBufferId[@buffer.id] = @getEditorState() + @setEditorStateForBuffer(@buffer, @getEditorState()) handleBufferChange: (e) -> @compositeCursor.handleBufferChange(e) diff --git a/src/app/keymaps/editor.coffee b/src/app/keymaps/editor.coffee index 91946d070..883c2e6e6 100644 --- a/src/app/keymaps/editor.coffee +++ b/src/app/keymaps/editor.coffee @@ -30,4 +30,6 @@ window.keymap.bindKeys '.editor', 'alt-meta-up': 'split-up' 'alt-meta-down': 'split-down' 'meta-[': 'outdent-selected-rows' - 'meta-]': 'indent-selected-rows' \ No newline at end of file + 'meta-]': 'indent-selected-rows' + 'meta-{': 'show-previous-buffer' + 'meta-}': 'show-next-buffer' \ No newline at end of file diff --git a/src/app/root-view.coffee b/src/app/root-view.coffee index 1b9b10db2..8d321c936 100644 --- a/src/app/root-view.coffee +++ b/src/app/root-view.coffee @@ -25,10 +25,8 @@ class RootView extends View @createProject(path) @on 'toggle-file-finder', => @toggleFileFinder() - @on 'show-console', -> window.showConsole() - @on 'find-in-file', => - @commandPanel.show() - @commandPanel.editor.setText("/") + @on 'show-console', => window.showConsole() + @on 'find-in-file', => @commandPanel.show("/") @one 'attach', => @focus() @on 'focus', (e) =>