Skip to content

Commit

Permalink
Merge branch 'scroll'
Browse files Browse the repository at this point in the history
  • Loading branch information
mkleehammer committed Jun 3, 2014
2 parents 3066f17 + 9117788 commit 79f56f3
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 5 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,12 @@ A whitespace and/or comma separated list of globs (filenames or wildcards) to ig
to both files and directories. This can be useful for speeding up the rebuilding of the index.

Example: `node_modules, *.sql`

### Auto Scroll

By default the Goto File Symbol command will scroll the selected command into view. Pressing
`Esc` to cancel the command restores the position of the screen. Uncheck this option to
disable the scrolling.

Note that the Goto Project Symbol does not scroll the editor since it displays choices from
multiple files.
57 changes: 54 additions & 3 deletions lib/goto-view.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,73 @@ module.exports =
class GotoView extends SelectListView

initialize: ->
super
@addClass('goto-view overlay from-top')
super
@addClass('goto-view overlay from-top')

@currentView = null
# If this is non-null, then the command was 'Goto File Symbol' and this is the current file's
# editor view. Autoscroll to the selected item.

@cancelPosition = null
# The original position of the screen and selections so they can be restored if the user
# cancels. This is only set by the Goto File Symbol command when auto-scrolling is enabled.
# If set, it is an object containing:
# :firstRow - the editor's first visible row
# :selections - the original selections

destroy: ->
@cancel()
@detach()

cancel: ->
super
@restoreCancelPosition()
@currentView = null
@cancelPosition = null

attach: ->
@storeFocusedElement()
atom.workspaceView.appendToTop(this)
@focusFilterEditor()

populate: (symbols) ->
populate: (symbols, view) ->
@rememberCancelPosition(view)
@setItems(symbols)
@attach()

rememberCancelPosition: (view) ->
if not view or not atom.config.get('goto.autoScroll')
return

@currentView = view
@cancelPosition =
top: view.scrollTop()
selections: view.getEditor().getSelectedBufferRanges()

restoreCancelPosition: ->
if @currentView and @cancelPosition
@currentView.getEditor().setSelectedBufferRanges(@cancelPosition.selections)
@currentView.scrollTop(@cancelPosition.top)

forgetCancelPosition: ->
@currentView = null
@cancelPosition = null

getFilterKey: -> 'name'

scrollToItemView: (view) ->
# Hook the selection of an item so we can scroll the current buffer to the item.
super
symbol = @getSelectedItem()
@onItemSelected(view, symbol)

onItemSelected: (view, symbol) ->
if @currentView
editor = @currentView.getEditor()
@currentView.scrollToBufferPosition(symbol.position, center: true)
editor.setCursorBufferPosition(symbol.position)
editor.moveCursorToFirstCharacterOfLine()

viewForItem: (symbol) ->
$$ ->
@li class: 'two-lines', =>
Expand All @@ -41,6 +90,8 @@ class GotoView extends SelectListView
super

confirmed: (symbol) ->
@forgetCancelPosition()

if not fs.existsSync(atom.project.resolve(symbol.path))
@setError('Selected file does not exist')
setTimeout((=> @setError()), 2000)
Expand Down
6 changes: 4 additions & 2 deletions lib/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module.exports =
configDefaults:
logToConsole: false
moreIgnoredNames: ''
autoScroll: true

index: null
gotoView: null
Expand Down Expand Up @@ -38,8 +39,9 @@ module.exports =
@gotoView.populate(symbols)

gotoFileSymbol: ->
e = atom.workspace.getActiveEditor()
v = atom.workspaceView.getActiveView()
e = v?.getEditor()
filePath = e?.getPath()
if filePath
symbols = @index.getEditorSymbols(e)
@gotoView.populate(symbols)
@gotoView.populate(symbols, v)

0 comments on commit 79f56f3

Please sign in to comment.