From 5f18bc79215a093ebbae8d6d855a8ce2eba921b4 Mon Sep 17 00:00:00 2001 From: sadick254 Date: Fri, 30 Oct 2020 16:24:42 +0300 Subject: [PATCH] Decaf find-options --- lib/find-options.coffee | 83 -------------------------------- lib/find-options.js | 103 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+), 83 deletions(-) delete mode 100644 lib/find-options.coffee create mode 100644 lib/find-options.js diff --git a/lib/find-options.coffee b/lib/find-options.coffee deleted file mode 100644 index 6be07bea..00000000 --- a/lib/find-options.coffee +++ /dev/null @@ -1,83 +0,0 @@ -_ = require 'underscore-plus' -{Emitter} = require 'atom' - -Params = [ - 'findPattern' - 'replacePattern' - 'pathsPattern' - 'useRegex' - 'wholeWord' - 'caseSensitive' - 'inCurrentSelection' - 'leadingContextLineCount' - 'trailingContextLineCount' -] - -module.exports = -class FindOptions - constructor: (state={}) -> - @emitter = new Emitter - - @findPattern = '' - @replacePattern = state.replacePattern ? '' - @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 [0..@findPattern.length] - 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, '\\$&') diff --git a/lib/find-options.js b/lib/find-options.js new file mode 100644 index 00000000..ff7dceff --- /dev/null +++ b/lib/find-options.js @@ -0,0 +1,103 @@ +const _ = require('underscore-plus'); +const {Emitter} = require('atom'); + +const Params = [ + 'findPattern', + 'replacePattern', + 'pathsPattern', + 'useRegex', + 'wholeWord', + 'caseSensitive', + 'inCurrentSelection', + 'leadingContextLineCount', + 'trailingContextLineCount' +]; + +module.exports = class FindOptions { + constructor(state) { + let left, left1, left2, left3, left4, left5; + if (state == null) { state = {}; } + this.emitter = new Emitter; + + this.findPattern = ''; + this.replacePattern = state.replacePattern != null ? state.replacePattern : ''; + this.pathsPattern = state.pathsPattern != null ? state.pathsPattern : ''; + this.useRegex = (left = state.useRegex != null ? state.useRegex : atom.config.get('find-and-replace.useRegex')) != null ? left : false; + this.caseSensitive = (left1 = state.caseSensitive != null ? state.caseSensitive : atom.config.get('find-and-replace.caseSensitive')) != null ? left1 : false; + this.wholeWord = (left2 = state.wholeWord != null ? state.wholeWord : atom.config.get('find-and-replace.wholeWord')) != null ? left2 : false; + this.inCurrentSelection = (left3 = state.inCurrentSelection != null ? state.inCurrentSelection : atom.config.get('find-and-replace.inCurrentSelection')) != null ? left3 : false; + this.leadingContextLineCount = (left4 = state.leadingContextLineCount != null ? state.leadingContextLineCount : atom.config.get('find-and-replace.leadingContextLineCount')) != null ? left4 : 0; + this.trailingContextLineCount = (left5 = state.trailingContextLineCount != null ? state.trailingContextLineCount : atom.config.get('find-and-replace.trailingContextLineCount')) != null ? left5 : 0; + } + + onDidChange(callback) { + this.emitter.on('did-change', callback); + } + + onDidChangeUseRegex(callback) { + this.emitter.on('did-change-useRegex', callback); + } + + onDidChangeReplacePattern(callback) { + this.emitter.on('did-change-replacePattern', callback); + } + + serialize() { + const result = {}; + for (let param of Params) { + result[param] = this[param]; + } + return result; + } + + set(newParams) { + if (newParams == null) { newParams = {}; } + let changedParams = {}; + for (let key of Params) { + if ((newParams[key] != null) && (newParams[key] !== this[key])) { + if (changedParams == null) { changedParams = {}; } + this[key] = (changedParams[key] = newParams[key]); + } + } + + if (Object.keys(changedParams).length) { + for (let param in changedParams) { + const val = changedParams[param]; + this.emitter.emit(`did-change-${param}`); + } + this.emitter.emit('did-change', changedParams); + } + return changedParams; + } + + getFindPatternRegex(forceUnicode) { + let expression; + if (forceUnicode == null) { forceUnicode = false; } + for (let i = 0, end = this.findPattern.length, asc = 0 <= end; asc ? i <= end : i >= end; asc ? i++ : i--) { + if (this.findPattern.charCodeAt(i) > 128) { + forceUnicode = true; + break; + } + } + + let flags = 'gm'; + if (!this.caseSensitive) { flags += 'i'; } + if (forceUnicode) { flags += 'u'; } + + if (this.useRegex) { + expression = this.findPattern; + } else { + expression = escapeRegExp(this.findPattern); + } + + if (this.wholeWord) { expression = `\\b${expression}\\b`; } + + return 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 +var escapeRegExp = string => string.replace(/[\/\\^$*+?.()|[\]{}]/g, '\\$&');