Need help creating a set of Regexes


#1

Hello everyone!
I’m new here, so I don’t have a lot of experience with atom, but I have serched my question before asking it.
What I’am trying to do but didn’t find out how yet is how to configure a set of regexes (I need about 10 search and replace pairs) to run automatically, a kind of filter.
I would create it, name it “Filter 1”, for exemple, and define all the regexes search and replace pairs it consists of. When I open a file, I would like to be able to open the filter list and chose one to run.
There’s something like this already implemented?
If not, how would you suggest I start do develop a package that would do this?
I have good programming skills but zero experience with atom.

Thank you!


#2

You can take a look at the implementation of my tabs-to-spaces package for an example of how to build a package that searches and replaces.


#3

Thank tou leedohm!
Using your package as example, I was able to create one that does what I need.

The problem is that Atom is throwing an error when I run the search and replace command, but even with the error, the search and replace regex is working perfectly!

Stack Trace

Uncaught TypeError: Cannot read property 'global' of undefined

At C:\Users\Alexandre\AppData\Local\atom\app-1.14.3\resources\app.asar\node_modules\text-buffer\lib\text-buffer.js:824

TypeError: Cannot read property 'global' of undefined
    at TextBuffer.module.exports.TextBuffer.scanInRange (/app.asar/node_modules/text-buffer/lib/text-buffer.js:911:21)
    at TextBuffer.module.exports.TextBuffer.scan (/app.asar/node_modules/text-buffer/lib/text-buffer.js:898:19)
    at /app.asar/node_modules/text-buffer/lib/text-buffer.js:943:24
    at TextBuffer.module.exports.TextBuffer.transact (/app.asar/node_modules/text-buffer/lib/text-buffer.js:819:18)
    at TextBuffer.module.exports.TextBuffer.replace (/app.asar/node_modules/text-buffer/lib/text-buffer.js:941:12)
    at wikiText.replaceAllWiki (/packages/wikiText/lib/wikiText.js:34:12)
    at wikiText.wikiReplace (/packages/wikiText/lib/wikiText.js:20:10)
    at /packages/wikiText/lib/index.js:11:16)
    at CommandRegistry.module.exports.CommandRegistry.handleCommandEvent (/app.asar/src/command-registry.js:259:29)
    at /app.asar/src/command-registry.js:3:59
    at CommandPaletteView.module.exports.CommandPaletteView.confirmed (/app.asar/node_modules/command-palette/lib/command-palette-view.js:210:32)
    at CommandPaletteView.module.exports.SelectListView.confirmSelection (/app.asar/node_modules/atom-space-pen-views/lib/select-list-view.js:338:21)
    at /app.asar/node_modules/atom-space-pen-views/lib/select-list-view.js:109:19)
    at CommandRegistry.module.exports.CommandRegistry.handleCommandEvent (/app.asar/src/command-registry.js:259:29)
    at /app.asar/src/command-registry.js:3:59
    at KeymapManager.module.exports.KeymapManager.dispatchCommandEvent (/app.asar/node_modules/atom-keymap/lib/keymap-manager.js:599:16)
    at KeymapManager.module.exports.KeymapManager.handleKeyboardEvent (/app.asar/node_modules/atom-keymap/lib/keymap-manager.js:390:22)
    at WindowEventHandler.module.exports.WindowEventHandler.handleDocumentKeyEvent (/app.asar/src/window-event-handler.js:106:36)
    at /app.asar/src/window-event-handler.js:3:59)

Code

I have two files in my lib folder, the error is thrown when I try to run the wikiReplace command:

###First file

class wikiText {
  constructor () {
    this.categoria =/\[\[Category\:.*\]\]/g
    this.passos = /==\s*Steps\s*==/g
    this.dicas = /==\s*Tips\s*==/g
    this.fontes = /==\s*Sources\s*and\s*Citations\s*==/g
    this.avisos = /==\s*Warnings\s*==/g
    this.materiais = /==\s*Things\s*You’ll\s*Need\s*==/g
    this.ingredientes = /==\s*Ingredients\s*==/g
    this.relacionados = /==\s*Related\s*wikiHows\s*==/g
    this.relacionados2 = /\*\[\[.*\]\]/g
    this.whiteline = /^(\s*\r\n){2,}/
  }

  wikiReplace (editor = atom.workspace.getActiveTextEditor()) {
    if (!editor) {
      return
    }

    this.replaceAllWiki(editor)
  }

  replaceAllWiki(editor){
    const buffer = editor.getBuffer()
    buffer.replace(this.avisos,"== Avisos ==")
    buffer.replace(this.materiais,"== Materiais Necessários ==")
    buffer.replace(this.categoria, "\[\[Categoria\:\]\]")
    buffer.replace(this.ingredientes, "== Ingredientes ==")
    buffer.replace(this.passos, "== Passos ==")
    buffer.replace(this.fontes, "== Fontes e Citações ==")
    buffer.replace(this.dicas, " == Dicas ==")
    buffer.replace(this.relacionados, '')
    buffer.replace(this.relacionados2, '')
    buffer.replace(this.whitelines, "\r\n")
  }
}

module.exports = new wikiText()

##Second File

/** @babel */

let commands = null
let editorObserver = null
let wikiText = null

export function activate () {
  commands = atom.commands.add('atom-workspace', {
    'wikiText:wikiReplace': () => {
      loadModule()
      wikiText.wikiReplace()
    }
  })
}

export function deactivate () {
  if (editorObserver) {
    editorObserver.dispose()
  }

  if (commands) {
    commands.dispose()
  }
}

function loadModule () {
  if (!wikiText) {
    wikiText = require('./wikiText')
  }
}

--------------------

I have used your package files as a example, so maybe you have an idea about what may be causing the error.
If there’s a way to supress the error message, that would work too.
Thank you!


#4

You have a typo whiteline in one place whitelines in another.


#5

I feel so embarrassed…
I was so sure that the error was something different and more complicated that I didn’t bother checking for typos…
I’m sorry about that!
My package is perfect now!
For the future, I will try to let the user choose what regex expressions he wants to use to search and replace, because right now the expressions are hardcoded to fit my needs.
With that implemented, I will be able to create something that will be helpful for everyone, and I’m looking forward to it!

Thank you very much :slight_smile: