Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Commit 0bca679

Browse files
authored
Merge pull request #3 from travco/findInCurrentDir
Fix regression (error) updated in upstream, add spec in show-in-current-directory for new functionality
2 parents 2aea3f9 + 3c695c4 commit 0bca679

17 files changed

+322
-156
lines changed

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
# Find and Replace package [![Build Status](https://travis-ci.org/atom/find-and-replace.svg?branch=master)](https://travis-ci.org/atom/find-and-replace)
1+
# Find and Replace package
2+
[![OS X Build Status](https://travis-ci.org/atom/find-and-replace.svg?branch=master)](https://travis-ci.org/atom/find-and-replace) [![Windows Build Status](https://ci.appveyor.com/api/projects/status/6w4baiiq5mw4nxky/branch/master?svg=true)](https://ci.appveyor.com/project/Atom/find-and-replace/branch/master) [![Dependency Status](https://david-dm.org/atom/find-and-replace.svg)](https://david-dm.org/atom/find-and-replace)
23

34
Find and replace in the current buffer or across the entire project in Atom.
45

5-
## Find in buffer (cmd-f)
6+
## Find in buffer
67

8+
Using the shortcut <kbd>cmd-f</kbd> (Mac) or <kbd>ctrl-f</kbd> (Windows and Linux).
79
![screen shot 2013-11-26 at 12 25 22 pm](https://f.cloud.github.com/assets/69169/1625938/a859fa70-56d9-11e3-8b2a-ac37c5033159.png)
810

9-
## Find in project (cmd-shift-f)
11+
## Find in project
1012

13+
Using the shortcut <kbd>cmd-shift-f</kbd> (Mac) or <kbd>ctrl-shift-f</kbd> (Windows and Linux).
1114
![screen shot 2013-11-26 at 12 26 02 pm](https://f.cloud.github.com/assets/69169/1625945/b216d7b8-56d9-11e3-8b14-6afc33467be9.png)
1215

1316
## Provided Service

appveyor.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
version: "{build}"
2+
3+
os: Windows Server 2012 R2
4+
5+
install:
6+
- choco install atom -y
7+
- cd %APPVEYOR_BUILD_FOLDER%
8+
- "%LOCALAPPDATA%/atom/bin/apm clean"
9+
- "%LOCALAPPDATA%/atom/bin/apm install"
10+
11+
build_script:
12+
- cd %APPVEYOR_BUILD_FOLDER%
13+
- "%LOCALAPPDATA%/atom/bin/apm test --path %LOCALAPPDATA%/atom/bin/atom.cmd"
14+
15+
test: off
16+
17+
deploy: off

lib/buffer-search.coffee

Lines changed: 16 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,12 @@ ResultsMarkerLayersByEditor = new WeakMap
77

88
module.exports =
99
class BufferSearch
10-
@markerClass: 'find-result'
11-
1210
constructor: (@findOptions) ->
1311
@emitter = new Emitter
1412
@patch = new Patch
1513
@subscriptions = null
1614
@markers = []
1715
@editor = null
18-
@useMarkerLayers = false
1916

2017
recreateMarkers = @recreateMarkers.bind(this)
2118
@findOptions.onDidChange (changedParams) =>
@@ -44,10 +41,9 @@ class BufferSearch
4441
@subscriptions.add @editor.buffer.onDidStopChanging(@bufferStoppedChanging.bind(this))
4542
@subscriptions.add @editor.onDidAddSelection(@setCurrentMarkerFromSelection.bind(this))
4643
@subscriptions.add @editor.onDidChangeSelectionRange(@setCurrentMarkerFromSelection.bind(this))
47-
if @useMarkerLayers = @editor.addMarkerLayer?
48-
@resultsMarkerLayer = @resultsMarkerLayerForTextEditor(@editor)
49-
@resultsLayerDecoration?.destroy()
50-
@resultsLayerDecoration = @editor.decorateMarkerLayer(@resultsMarkerLayer, {type: 'highlight', class: @constructor.markerClass})
44+
@resultsMarkerLayer = @resultsMarkerLayerForTextEditor(@editor)
45+
@resultsLayerDecoration?.destroy()
46+
@resultsLayerDecoration = @editor.decorateMarkerLayer(@resultsMarkerLayer, {type: 'highlight', class: 'find-result'})
5147
@recreateMarkers()
5248

5349
getEditor: -> @editor
@@ -58,7 +54,7 @@ class BufferSearch
5854

5955
resultsMarkerLayerForTextEditor: (editor) ->
6056
unless layer = ResultsMarkerLayersByEditor.get(editor)
61-
layer = editor.addMarkerLayer?()
57+
layer = editor.addMarkerLayer({maintainHistory: false})
6258
ResultsMarkerLayersByEditor.set(editor, layer)
6359
layer
6460

@@ -74,18 +70,17 @@ class BufferSearch
7470
@findOptions.set({replacePattern})
7571

7672
@editor.transact =>
73+
replacePattern = escapeHelper.unescapeEscapeSequence(replacePattern) if @findOptions.useRegex
7774
for marker in markers
7875
bufferRange = marker.getBufferRange()
7976
replacementText = null
8077
if @findOptions.useRegex
81-
replacePattern = escapeHelper.unescapeEscapeSequence(replacePattern)
8278
textToReplace = @editor.getTextInBufferRange(bufferRange)
8379
replacementText = textToReplace.replace(@getFindPatternRegex(), replacePattern)
8480
@editor.setTextInBufferRange(bufferRange, replacementText ? replacePattern)
8581

8682
marker.destroy()
8783
@markers.splice(@markers.indexOf(marker), 1)
88-
delete @decorationsByMarkerId[marker.id] unless @useMarkerLayers
8984

9085
@emitter.emit 'did-update', @markers.slice()
9186

@@ -99,7 +94,6 @@ class BufferSearch
9994
recreateMarkers: ->
10095
@markers.forEach (marker) -> marker.destroy()
10196
@markers.length = 0
102-
@decorationsByMarkerId = {} unless @useMarkerLayers
10397

10498
if markers = @createMarkers(Point.ZERO, Point.INFINITY)
10599
@markers = markers
@@ -113,8 +107,13 @@ class BufferSearch
113107
end = Point.min(end, selectedRange.end)
114108

115109
if regex = @getFindPatternRegex()
116-
@editor.scanInBufferRange regex, Range(start, end), ({range}) =>
117-
newMarkers.push(@createMarker(range))
110+
try
111+
@editor.scanInBufferRange regex, Range(start, end), ({range}) =>
112+
newMarkers.push(@createMarker(range))
113+
catch error
114+
error.message = "Search string is too large" if /RegExp too big$/.test(error.message)
115+
@emitter.emit 'did-error', error
116+
return false
118117
else
119118
return false
120119
newMarkers
@@ -166,7 +165,6 @@ class BufferSearch
166165
oldMarkers = @markers.splice(spliceStart, spliceEnd - spliceStart + 1, newMarkers...)
167166
for oldMarker in oldMarkers
168167
oldMarker.destroy()
169-
delete @decorationsByMarkerId[oldMarker.id] unless @useMarkerLayers
170168
markerIndex += newMarkers.length - oldMarkers.length
171169

172170
while marker = @markers[++markerIndex]
@@ -185,46 +183,25 @@ class BufferSearch
185183
return if marker is @currentResultMarker
186184

187185
if @currentResultMarker?
188-
if @useMarkerLayers
189-
@resultsLayerDecoration.setPropertiesForMarker(@currentResultMarker, null)
190-
else
191-
@decorationsByMarkerId[@currentResultMarker.id]?.setProperties(type: 'highlight', class: @constructor.markerClass)
186+
@resultsLayerDecoration.setPropertiesForMarker(@currentResultMarker, null)
192187
@currentResultMarker = null
193188

194189
if marker and not marker.isDestroyed()
195-
if @useMarkerLayers
196-
@resultsLayerDecoration.setPropertiesForMarker(marker, type: 'highlight', class: 'current-result')
197-
else
198-
@decorationsByMarkerId[marker.id]?.setProperties(type: 'highlight', class: 'current-result')
190+
@resultsLayerDecoration.setPropertiesForMarker(marker, type: 'highlight', class: 'current-result')
199191
@currentResultMarker = marker
200192

201193
@emitter.emit 'did-change-current-result', @currentResultMarker
202194

203195
findMarker: (range) ->
204196
if @markers?.length > 0
205-
(@resultsMarkerLayer ? @editor).findMarkers(
206-
class: @constructor.markerClass,
207-
startPosition: range.start,
208-
endPosition: range.end
209-
)[0]
197+
@resultsMarkerLayer.findMarkers({startPosition: range.start, endPosition: range.end})[0]
210198

211199
recreateMarker: (marker) ->
212-
delete @decorationsByMarkerId[marker.id] unless @useMarkerLayers
213200
marker.destroy()
214201
@createMarker(marker.getBufferRange())
215202

216203
createMarker: (range) ->
217-
marker = (@resultsMarkerLayer ? @editor).markBufferRange(range,
218-
invalidate: 'inside'
219-
class: @constructor.markerClass
220-
persistent: false
221-
maintainHistory: false
222-
)
223-
unless @useMarkerLayers
224-
@decorationsByMarkerId[marker.id] = @editor.decorateMarker(marker,
225-
type: 'highlight',
226-
class: @constructor.markerClass
227-
)
204+
marker = @resultsMarkerLayer.markBufferRange(range, {invalidate: 'inside'})
228205
marker
229206

230207
bufferChanged: ({oldRange, newRange, newText}) ->

lib/find-view.coffee

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -342,15 +342,21 @@ class FindView extends View
342342
@descriptionLabel.html('Find in Current Buffer <span class="subtle-info-message">Close this panel with the <span class="highlight">esc</span> key</span>').removeClass('text-error')
343343

344344
selectFirstMarkerAfterCursor: =>
345-
{index, wrapped} = @firstMarkerIndexAfterCursor()
345+
marker = @firstMarkerIndexAfterCursor()
346+
return unless marker
347+
{index, wrapped} = marker
346348
@selectMarkerAtIndex(index, wrapped)
347349

348350
selectFirstMarkerStartingFromCursor: =>
349-
{index, wrapped} = @firstMarkerIndexAfterCursor(true)
351+
marker = @firstMarkerIndexAfterCursor(true)
352+
return unless marker
353+
{index, wrapped} = marker
350354
@selectMarkerAtIndex(index, wrapped)
351355

352356
selectFirstMarkerBeforeCursor: =>
353-
{index, wrapped} = @firstMarkerIndexBeforeCursor()
357+
marker = @firstMarkerIndexBeforeCursor()
358+
return unless marker
359+
{index, wrapped} = marker
354360
@selectMarkerAtIndex(index, wrapped)
355361

356362
firstMarkerIndexStartingFromCursor: =>
@@ -467,7 +473,9 @@ class FindView extends View
467473
optionButton.removeClass 'selected'
468474

469475
anyMarkersAreSelected: =>
470-
@model.getEditor().getSelectedBufferRanges().some (selectedRange) =>
476+
editor = @model.getEditor()
477+
return false unless editor
478+
editor.getSelectedBufferRanges().some (selectedRange) =>
471479
@model.findMarker(selectedRange)
472480

473481
toggleRegexOption: =>
@@ -522,6 +530,7 @@ class FindView extends View
522530
@wrapIcon = $(wrapIcon)
523531

524532
showWrapIcon: (icon) ->
533+
return unless atom.config.get('find-and-replace.showSearchWrapIcon')
525534
editor = @model.getEditor()
526535
return unless editor?
527536
editorView = atom.views.getView(editor)

lib/find.coffee

Lines changed: 24 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,6 @@ ResultsModel = require './project/results-model'
1111
ResultsPaneView = require './project/results-pane'
1212

1313
module.exports =
14-
config:
15-
focusEditorAfterSearch:
16-
type: 'boolean'
17-
default: false
18-
description: 'Focus the editor and select the next match when a file search is executed. If no matches are found, the editor will not be focused.'
19-
openProjectFindResultsInRightPane:
20-
type: 'boolean'
21-
default: false
22-
description: 'When a project-wide search is executed, open the results in a split pane instead of a tab in the same pane.'
23-
closeFindPanelAfterSearch:
24-
type: 'boolean'
25-
default: false
26-
title: 'Close Project Find Panel After Search'
27-
description: 'Close the find panel after executing a project-wide search.'
28-
scrollToResultOnLiveSearch:
29-
type: 'boolean'
30-
default: false
31-
title: 'Scroll To Result On Live-Search (incremental find in buffer)'
32-
description: 'Scroll to and select the closest match while typing in the buffer find box.'
33-
liveSearchMinimumCharacters:
34-
type: 'integer'
35-
default: 3
36-
minimum: 0
37-
description: 'The minimum number of characters which need to be typed into the buffer find box before search starts matching and highlighting matches as you type.'
38-
3914
activate: ({findOptions, findHistory, replaceHistory, pathsHistory}={}) ->
4015
atom.workspace.addOpener (filePath) ->
4116
new ResultsPaneView() if filePath is ResultsPaneView.URI
@@ -60,18 +35,11 @@ module.exports =
6035

6136
@subscriptions.add atom.commands.add 'atom-workspace', 'project-find:show', =>
6237
@createViews()
63-
@findPanel.hide()
64-
@projectFindPanel.show()
65-
@projectFindView.focusFindElement()
38+
showPanel @projectFindPanel, @findPanel, => @projectFindView.focusFindElement()
6639

6740
@subscriptions.add atom.commands.add 'atom-workspace', 'project-find:toggle', =>
6841
@createViews()
69-
@findPanel.hide()
70-
71-
if @projectFindPanel.isVisible()
72-
@projectFindPanel.hide()
73-
else
74-
@projectFindPanel.show()
42+
togglePanel @projectFindPanel, @findPanel, => @projectFindView.focusFindElement()
7543

7644
@subscriptions.add atom.commands.add 'atom-workspace', 'project-find:show-in-current-directory', ({target}) =>
7745
@createViews()
@@ -82,33 +50,24 @@ module.exports =
8250

8351
@subscriptions.add atom.commands.add 'atom-workspace', 'find-and-replace:use-selection-as-find-pattern', =>
8452
return if @projectFindPanel?.isVisible() or @findPanel?.isVisible()
85-
8653
@createViews()
87-
@projectFindPanel.hide()
88-
@findPanel.show()
89-
@findView.focusFindEditor()
54+
showPanel @findPanel, @projectFindPanel, => @findView.focusFindEditor()
9055

9156
@subscriptions.add atom.commands.add 'atom-workspace', 'find-and-replace:toggle', =>
9257
@createViews()
93-
@projectFindPanel.hide()
94-
95-
if @findPanel.isVisible()
96-
@findPanel.hide()
97-
else
98-
@findPanel.show()
99-
@findView.focusFindEditor()
58+
togglePanel @findPanel, @projectFindPanel, => @findView.focusFindEditor()
10059

10160
@subscriptions.add atom.commands.add 'atom-workspace', 'find-and-replace:show', =>
10261
@createViews()
103-
@projectFindPanel.hide()
104-
@findPanel.show()
105-
@findView.focusFindEditor()
62+
showPanel @findPanel, @projectFindPanel, => @findView.focusFindEditor()
10663

10764
@subscriptions.add atom.commands.add 'atom-workspace', 'find-and-replace:show-replace', =>
10865
@createViews()
109-
@projectFindPanel?.hide()
110-
@findPanel.show()
111-
@findView.focusReplaceEditor()
66+
showPanel @findPanel, @projectFindPanel, => @findView.focusReplaceEditor()
67+
68+
@subscriptions.add atom.commands.add 'atom-workspace', 'find-and-replace:clear-history', =>
69+
@findHistory.clear()
70+
@replaceHistory.clear()
11271

11372
# Handling cancel in the workspace + code editors
11473
handleEditorCancel = ({target}) =>
@@ -130,6 +89,20 @@ module.exports =
13089
@selectNextObjects.set(editor, selectNext)
13190
selectNext
13291

92+
showPanel = (panelToShow, panelToHide, postShowAction) ->
93+
panelToHide.hide()
94+
panelToShow.show()
95+
postShowAction?()
96+
97+
togglePanel = (panelToToggle, panelToHide, postToggleAction) ->
98+
panelToHide.hide()
99+
100+
if panelToToggle.isVisible()
101+
panelToToggle.hide()
102+
else
103+
panelToToggle.show()
104+
postToggleAction?()
105+
133106
atom.commands.add '.editor:not(.mini)',
134107
'find-and-replace:select-next': (event) ->
135108
selectNextObjectForEditorElement(this).findAndSelectNext()

lib/history.coffee

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ class History
2525
@length = @items.length
2626
@emitter.emit 'did-add-item', text
2727

28+
clear: ->
29+
@items = []
30+
@length = 0
31+
2832
# Adds the ability to cycle through history
2933
class HistoryCycler
3034

lib/project-find-view.coffee

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,7 @@ class ProjectFindView extends View
274274
@model.replace(pathsPattern, replacePattern, @model.getPaths())
275275

276276
directoryPathForElement: (element) ->
277-
if element?.dataset.path
278-
elementPath = element?.querySelector('[data-path]')?.dataset.path
277+
elementPath = element?.dataset.path ? element?.querySelector('[data-path]')?.dataset.path
279278

280279
# Traverse up the DOM if the element and its children don't have a path
281280
unless elementPath

lib/project/match-view.coffee

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,13 @@ class MatchView extends View
4242
@replacementText.text('').hide()
4343
@matchText.removeClass('highlight-error').addClass('highlight-info')
4444

45-
confirm: ->
45+
confirm: (options = {}) ->
4646
openInRightPane = atom.config.get('find-and-replace.openProjectFindResultsInRightPane')
47-
options = {}
48-
options = {split: 'left'} if openInRightPane
49-
atom.workspace.open(@filePath, options).then (editor) =>
47+
options.split = 'left' if openInRightPane
48+
editorPromise = atom.workspace.open(@filePath, options)
49+
editorPromise.then (editor) =>
5050
editor.setSelectedBufferRange(@match.range, autoscroll: true)
51+
editorPromise
5152

5253
copy: ->
5354
atom.clipboard.write(@match.lineText)

lib/project/result-view.coffee

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,4 @@ class ResultView extends View
8080

8181
confirm: ->
8282
@expand(not @isExpanded)
83+
null

0 commit comments

Comments
 (0)