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

Upgrade to the new APIs #311

Merged
merged 39 commits into from Dec 2, 2014
Commits
Jump to file or symbol
Failed to load files and symbols.
+742 −647
Diff settings

Always

Just for now

View
@@ -1,14 +1,13 @@
_ = require 'underscore-plus'
{Emitter} = require 'emissary'
{CompositeDisposable} = require 'event-kit'
{Emitter, CompositeDisposable} = require 'atom'
escapeHelper = require './escape-helper'
module.exports =
class FindModel
Emitter.includeInto(this)
@markerClass: 'find-result'
constructor: (state={}) ->
@emitter = new Emitter()
@pattern = ''
@useRegex = state.useRegex ? atom.config.get('find-and-replace.useRegex') ? false
@inCurrentSelection = state.inCurrentSelection ? atom.config.get('find-and-replace.inCurrentSelection') ? false
@@ -18,6 +17,15 @@ class FindModel
atom.workspace.observeActivePaneItem @activePaneItemChanged
onDidUpdate: (callback) ->
@emitter.on 'did-update', callback
onDidError: (callback) ->
@emitter.on 'did-error', callback
onDidChangeCurrentResult: (callback) ->
@emitter.on 'did-change-current-result', callback
activePaneItemChanged: (paneItem) =>
@editor = null
@subscriptions?.dispose()
@@ -62,7 +70,7 @@ class FindModel
@markers.splice(@markers.indexOf(marker), 1)
@replacing = false
@emit 'updated', _.clone(@markers)
@emitter.emit 'did-update', _.clone(@markers)
updateMarkers: ->
if not @editor? or not @pattern
@@ -92,11 +100,11 @@ class FindModel
marker.destroy() for id, marker of markersToRemoveById
@markers = updatedMarkers
@emit 'updated', _.clone(@markers)
@emitter.emit 'did-update', _.clone(@markers)
@setCurrentMarkerFromSelection()
catch e
@destroyAllMarkers()
@emit 'find-error', e
@emitter.emit 'did-error', e
setCurrentMarkerFromSelection: =>
marker = null
@@ -111,7 +119,7 @@ class FindModel
@decorationsByMarkerId[marker.id]?.setProperties(type: 'highlight', class: 'current-result')
@currentResultMarker = marker
@emit 'current-result-changed', @currentResultMarker
@emitter.emit 'did-change-current-result', @currentResultMarker
findMarker: (range) ->
if @markers? and @markers.length
@@ -137,7 +145,7 @@ class FindModel
@markers = []
@decorationsByMarkerId = {}
@currentResultMarker = null
@emit 'updated', _.clone(@markers)
@emitter.emit 'did-update', _.clone(@markers)
@setCurrentMarkerFromSelection()
getEditor: ->
View
@@ -1,5 +1,6 @@
_ = require 'underscore-plus'
{$$$, TextEditorView, View} = require 'atom'
{$$$, View, TextEditorView} = require 'atom-space-pen-views'
{CompositeDisposable} = require 'atom'
FindModel = require './find-model'
{HistoryCycler} = require './history'
@@ -40,6 +41,7 @@ class FindView extends View
@button outlet: 'replaceAllButton', class: 'btn btn-all', 'Replace All'
initialize: (@findModel, {findHistory, replaceHistory}) ->
@subscriptions = new CompositeDisposable
@findHistory = new HistoryCycler(@findEditor, findHistory)
@replaceHistory = new HistoryCycler(@replaceEditor, replaceHistory)
@handleEvents()
@@ -48,97 +50,119 @@ class FindView extends View
@clearMessage()
@updateOptionsLabel()
destroy: ->
@subscriptions?.dispose()
@tooltipSubscriptions?.dispose()
setPanel: (@panel) ->
@subscribe @panel.onDidChangeVisible (visible) =>
@subscriptions.add @panel.onDidChangeVisible (visible) =>
if visible then @didShow() else @didHide()
didShow: ->
atom.workspaceView.addClass('find-visible')
unless @tooltipsInitialized
@regexOptionButton.setTooltip("Use Regex", command: 'find-and-replace:toggle-regex-option', commandElement: @findEditor)
@caseOptionButton.setTooltip("Match Case", command: 'find-and-replace:toggle-case-option', commandElement: @findEditor)
@selectionOptionButton.setTooltip("Only In Selection", command: 'find-and-replace:toggle-selection-option', commandElement: @findEditor)
@wholeWordOptionButton.setTooltip("Whole Word", command: 'find-and-replace:toggle-whole-word-option', commandElement: @findEditor)
@nextButton.setTooltip("Find Next", command: 'find-and-replace:find-next', commandElement: @findEditor)
@replaceNextButton.setTooltip("Replace Next", command: 'find-and-replace:replace-next', commandElement: @replaceEditor)
@replaceAllButton.setTooltip("Replace All", command: 'find-and-replace:replace-all', commandElement: @replaceEditor)
@tooltipsInitialized = true
atom.views.getView(atom.workspace).classList.add('find-visible')
return if @tooltipSubscriptions?
@tooltipSubscriptions = subs = new CompositeDisposable

This comment has been minimized.

@kevinsawicki

kevinsawicki Dec 2, 2014

Member

Just curious, why subs and @tooltipSubscriptions?

@kevinsawicki

kevinsawicki Dec 2, 2014

Member

Just curious, why subs and @tooltipSubscriptions?

This comment has been minimized.

@benogle

benogle Dec 2, 2014

Contributor

shorter lines

@benogle

benogle Dec 2, 2014

Contributor

shorter lines

This comment has been minimized.

@kevinsawicki

kevinsawicki Dec 2, 2014

Member

I mean, why both variables?

@kevinsawicki

kevinsawicki Dec 2, 2014

Member

I mean, why both variables?

This comment has been minimized.

@kevinsawicki

kevinsawicki Dec 2, 2014

Member

Never mind, I see what you are saying now.

@kevinsawicki

kevinsawicki Dec 2, 2014

Member

Never mind, I see what you are saying now.

subs.add atom.tooltips.add @regexOptionButton,
title: "Use Regex"
keyBindingCommand: 'find-and-replace:toggle-regex-option',
keyBindingTarget: @findEditor.element
subs.add atom.tooltips.add @caseOptionButton,
title: "Match Case",
keyBindingCommand: 'find-and-replace:toggle-case-option',
keyBindingTarget: @findEditor.element
subs.add atom.tooltips.add @selectionOptionButton,
title: "Only In Selection",
keyBindingCommand: 'find-and-replace:toggle-selection-option',
keyBindingTarget: @findEditor.element
subs.add atom.tooltips.add @nextButton,
title: "Find Next",
keyBindingCommand: 'find-and-replace:find-next',
keyBindingTarget: @findEditor.element
subs.add atom.tooltips.add @replaceNextButton,
title: "Replace Next",
keyBindingCommand: 'find-and-replace:replace-next',
keyBindingTarget: @replaceEditor.element
subs.add atom.tooltips.add @replaceAllButton,
title: "Replace All",
keyBindingCommand: 'find-and-replace:replace-all',
keyBindingTarget: @replaceEditor.element
didHide: ->
@hideAllTooltips()
atom.workspaceView.focus()
atom.workspaceView.removeClass('find-visible')
workspaceElement = atom.views.getView(atom.workspace)
workspaceElement.focus()
workspaceElement.classList.remove('find-visible')
hideAllTooltips: ->
@regexOptionButton.hideTooltip()
@caseOptionButton.hideTooltip()
@selectionOptionButton.hideTooltip()
@wholeWordOptionButton.hideTooltip()
@nextButton.hideTooltip()
@replaceNextButton.hideTooltip()
@replaceAllButton.hideTooltip()
@tooltipSubscriptions.dispose()
@tooltipSubscriptions = null
handleEvents: ->
@handleFindEvents()
@handleReplaceEvents()
@findEditor.on 'core:confirm', => @confirm()
@findEditor.on 'find-and-replace:confirm', => @confirm()
@findEditor.on 'find-and-replace:show-previous', => @showPrevious()
@findEditor.on 'find-and-replace:find-all', => @findAll()
@replaceEditor.on 'core:confirm', => @replaceNext()
@on 'find-and-replace:focus-next', @toggleFocus
@on 'find-and-replace:focus-previous', @toggleFocus
@on 'core:cancel core:close', => @panel?.hide()
@on 'focus', (e) => @findEditor.focus()
@command 'find-and-replace:toggle-regex-option', @toggleRegexOption
@command 'find-and-replace:toggle-case-option', @toggleCaseOption
@command 'find-and-replace:toggle-selection-option', @toggleSelectionOption
@command 'find-and-replace:toggle-whole-word-option', @toggleWholeWordOption
@subscriptions.add atom.commands.add @findEditor.element,
'core:confirm': => @confirm()
'find-and-replace:confirm': => @confirm()
'find-and-replace:show-previous': => @showPrevious()
'find-and-replace:find-all': => @findAll()
@subscriptions.add atom.commands.add @replaceEditor.element,
'core:confirm': => @replaceNext()
@subscriptions.add atom.commands.add @element,
'core:close': => @panel?.hide()
'core:cancel': => @panel?.hide()
'find-and-replace:focus-next': @toggleFocus
'find-and-replace:focus-previous': @toggleFocus
'find-and-replace:toggle-regex-option': @toggleRegexOption
'find-and-replace:toggle-case-option': @toggleCaseOption
'find-and-replace:toggle-selection-option': @toggleSelectionOption
'find-and-replace:toggle-whole-word-option': @toggleWholeWordOption
@subscriptions.add @findModel.onDidUpdate @markersUpdated
@subscriptions.add @findModel.onDidError @findError
@subscriptions.add @findModel.onDidChangeCurrentResult @updateResultCounter
@regexOptionButton.on 'click', @toggleRegexOption
@caseOptionButton.on 'click', @toggleCaseOption
@selectionOptionButton.on 'click', @toggleSelectionOption
@wholeWordOptionButton.on 'click', @toggleWholeWordOption
@subscribe @findModel, 'updated', @markersUpdated
@subscribe @findModel, 'find-error', @findError
@subscribe @findModel, 'current-result-changed', @updateResultCounter
@on 'focus', => @findEditor.focus()
@find('button').on 'click', =>
atom.workspaceView.focus()
workspaceElement = atom.views.getView(atom.workspace)
workspaceElement.focus()
handleFindEvents: ->
@findEditor.getModel().onDidStopChanging => @liveSearch()
@nextButton.on 'click', => @findNext(focusEditorAfter: true)
atom.workspaceView.command 'find-and-replace:find-next', => @findNext(focusEditorAfter: true)
atom.workspaceView.command 'find-and-replace:find-previous', => @findPrevious(focusEditorAfter: true)
atom.workspaceView.command 'find-and-replace:use-selection-as-find-pattern', @setSelectionAsFindPattern
@subscriptions.add atom.commands.add 'atom-workspace',
'find-and-replace:find-next': => @findNext(focusEditorAfter: true)
'find-and-replace:find-previous': => @findPrevious(focusEditorAfter: true)
'find-and-replace:use-selection-as-find-pattern': @setSelectionAsFindPattern
handleReplaceEvents: ->
@replaceNextButton.on 'click', @replaceNext
@replaceAllButton.on 'click', @replaceAll
atom.workspaceView.command 'find-and-replace:replace-previous', @replacePrevious
atom.workspaceView.command 'find-and-replace:replace-next', @replaceNext
atom.workspaceView.command 'find-and-replace:replace-all', @replaceAll
@subscriptions.add atom.commands.add 'atom-workspace',
'find-and-replace:replace-previous': @replacePrevious
'find-and-replace:replace-next': @replaceNext
'find-and-replace:replace-all': @replaceAll
focusFindEditor: =>
selectedText = atom.workspace.getActiveEditor()?.getSelectedText?()
if selectedText and selectedText.indexOf('\n') < 0
@findEditor.setText(selectedText)
@findEditor.focus()
@findEditor.getEditor().selectAll()
@findEditor.getModel().selectAll()
focusReplaceEditor: =>
@replaceEditor.focus()
@replaceEditor.getEditor().selectAll()
@replaceEditor.getModel().selectAll()
toggleFocus: =>
if @findEditor.find(':focus').length > 0
@@ -177,7 +201,8 @@ class FindView extends View
if fieldToFocus
fieldToFocus.focus()
else if focusEditorAfter
atom.workspaceView.focus()
workspaceElement = atom.views.getView(atom.workspace)
workspaceElement.focus()
else
@findEditor.focus()
View
@@ -1,5 +1,5 @@
{$} = require 'atom'
{Subscriber} = require 'emissary'
{$} = require 'atom-space-pen-views'
{CompositeDisposable} = require 'atom'
SelectNext = require './select-next'
{History} = require './history'
@@ -22,20 +22,20 @@ module.exports =
atom.workspace.addOpener (filePath) =>
new ResultsPaneView() if filePath is ResultsPaneView.URI
@subscriber = new Subscriber()
@subscriptions = new CompositeDisposable
@findModel = new FindModel(@modelState)
@resultsModel = new ResultsModel(@resultsModelState)
@findHistory = new History(findHistory)
@replaceHistory = new History(replaceHistory)
@pathsHistory = new History(pathsHistory)
@subscriber.subscribeToCommand atom.workspaceView, 'project-find:show', =>
@subscriptions.add atom.commands.add 'atom-workspace', 'project-find:show', =>
@createViews()
@findPanel.hide()
@projectFindPanel.show()
@projectFindView.focusFindElement()
@subscriber.subscribeToCommand atom.workspaceView, 'project-find:toggle', =>
@subscriptions.add atom.commands.add 'atom-workspace', 'project-find:toggle', =>
@createViews()
@findPanel.hide()
@@ -44,21 +44,21 @@ module.exports =
else
@projectFindPanel.show()
@subscriber.subscribeToCommand atom.workspaceView, 'project-find:show-in-current-directory', ({target}) =>
@subscriptions.add atom.commands.add 'atom-workspace', 'project-find:show-in-current-directory', ({target}) =>
@createViews()
@findPanel.hide()
@projectFindPanel.show()
@projectFindView.findInCurrentlySelectedDirectory(target)
@subscriber.subscribeToCommand atom.workspaceView, 'find-and-replace:use-selection-as-find-pattern', =>
@subscriptions.add atom.commands.add 'atom-workspace', 'find-and-replace:use-selection-as-find-pattern', =>
return if @projectFindPanel?.isVisible() or @findPanel?.isVisible()
@createViews()
@projectFindPanel.hide()
@findPanel.show()
@findView.focusFindEditor()
@subscriber.subscribeToCommand atom.workspaceView, 'find-and-replace:toggle', =>
@subscriptions.add atom.commands.add 'atom-workspace', 'find-and-replace:toggle', =>
@createViews()
@projectFindPanel.hide()
@@ -68,31 +68,28 @@ module.exports =
@findPanel.show()
@findView.focusFindEditor()
@subscriber.subscribeToCommand atom.workspaceView, 'find-and-replace:show', =>
@subscriptions.add atom.commands.add 'atom-workspace', 'find-and-replace:show', =>
@createViews()
@projectFindPanel.hide()
@findPanel.show()
@findView.focusFindEditor()
@subscriber.subscribeToCommand atom.workspaceView, 'find-and-replace:show-replace', =>
@subscriptions.add atom.commands.add 'atom-workspace', 'find-and-replace:show-replace', =>
@createViews()
@projectFindPanel?.hide()
@findPanel.show()
@findView.focusReplaceEditor()
# in code editors
@subscriber.subscribeToCommand atom.workspaceView, 'core:cancel core:close', ({target}) =>
if target isnt atom.workspaceView.getActivePaneView()?[0]
$target = $(target)
if $target.is('atom-text-editor')
editor = $target
else
editor = $target.parents('.editor:not(.mini)')
# Handling cancel in the workspace + code editors
handleEditorCancel = ({target}) =>
isMiniEditor = target.tagName is 'ATOM-TEXT-EDITOR' and target.hasAttribute('mini')
unless isMiniEditor
@findPanel?.hide()
@projectFindPanel?.hide()
return unless editor.length
@findPanel?.hide()
@projectFindPanel?.hide()
@subscriptions.add atom.commands.add 'atom-workspace',
'core:cancel': handleEditorCancel
'core:close': handleEditorCancel
selectNextObjectForEditorElement = (editorElement) =>
@selectNextObjects ?= new WeakMap()
@@ -143,19 +140,21 @@ module.exports =
deactivate: ->
@findPanel?.destroy()
@findPanel = null
@findView?.destroy()
@findView = null
@findModel = null
@projectFindPanel?.destroy()
@projectFindPanel = null
@projectFindView?.destroy()
@projectFindView = null
ResultsPaneView.model = null
@resultsModel = null
@subscriber?.unsubscribe()
@subscriber = null
@subscriptions?.dispose()
@subscriptions = null
serialize: ->
viewState: @findView?.serialize() ? @viewState
Oops, something went wrong.