This repository was archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 194
/
Copy pathfind-options.coffee
85 lines (70 loc) · 2.58 KB
/
find-options.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
_ = require 'underscore-plus'
{Emitter} = require 'atom'
Params = [
'findPattern'
'replacePattern'
'paths'
'pathsPattern'
'useRegex'
'wholeWord'
'caseSensitive'
'inCurrentSelection'
'leadingContextLineCount'
'trailingContextLineCount'
]
module.exports =
class FindOptions
constructor: (state={}) ->
@emitter = new Emitter
@findPattern = ''
@replacePattern = state.replacePattern ? ''
@paths = state.paths ? []
@pathsPattern = state.pathsPattern ? ''
@useRegex = state.useRegex ? atom.config.get('find-and-replace.useRegex') ? false
@caseSensitive = state.caseSensitive ? atom.config.get('find-and-replace.caseSensitive') ? false
@wholeWord = state.wholeWord ? atom.config.get('find-and-replace.wholeWord') ? false
@inCurrentSelection = state.inCurrentSelection ? atom.config.get('find-and-replace.inCurrentSelection') ? false
@leadingContextLineCount = state.leadingContextLineCount ? atom.config.get('find-and-replace.leadingContextLineCount') ? 0
@trailingContextLineCount = state.trailingContextLineCount ? atom.config.get('find-and-replace.trailingContextLineCount') ? 0
onDidChange: (callback) ->
@emitter.on('did-change', callback)
onDidChangeUseRegex: (callback) ->
@emitter.on('did-change-useRegex', callback)
onDidChangeReplacePattern: (callback) ->
@emitter.on('did-change-replacePattern', callback)
serialize: ->
result = {}
for param in Params
result[param] = this[param]
result
set: (newParams={}) ->
changedParams = {}
for key in Params
if newParams[key]? and newParams[key] isnt this[key]
changedParams ?= {}
this[key] = changedParams[key] = newParams[key]
if Object.keys(changedParams).length
for param, val of changedParams
@emitter.emit("did-change-#{param}")
@emitter.emit('did-change', changedParams)
return changedParams
getFindPatternRegex: (forceUnicode = false) ->
for i in [[email protected]]
if @findPattern.charCodeAt(i) > 128
forceUnicode = true
break
flags = 'gm'
flags += 'i' unless @caseSensitive
flags += 'u' if forceUnicode
if @useRegex
expression = @findPattern
else
expression = escapeRegExp(@findPattern)
expression = "\\b#{expression}\\b" if @wholeWord
new RegExp(expression, flags)
# This is different from _.escapeRegExp, which escapes dashes. Escaped dashes
# are not allowed outside of character classes in RegExps with the `u` flag.
#
# See atom/find-and-replace#1022
escapeRegExp = (string) ->
string.replace(/[\/\\^$*+?.()|[\]{}]/g, '\\$&')