Skip to content

Instantly share code, notes, and snippets.

Created May 16, 2017 07:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/0f3f7cbf7a690e1a1fd5168625fff4c5 to your computer and use it in GitHub Desktop.
Save anonymous/0f3f7cbf7a690e1a1fd5168625fff4c5 to your computer and use it in GitHub Desktop.
Find next
'use babel';
export default class ReplacePackageView {
constructor(serializedState) {
// Create root element
this.element = document.createElement('div');
this.element.classList.add('replace-package');
this.element.classList.add('native-key-bindings');
// Create message element
const message = document.createElement('div');
message.textContent = 'Find/Replace';
message.classList.add('message');
this.element.appendChild(message);
// input box
const searchBox = document.createElement('input');
searchBox.type = "text";
searchBox.classList.add('search-box');
//searchBox.classList.add('native-key-bindings'); doesnt solve backspace problem
this.element.appendChild(searchBox);
// search button
const searchButton = document.createElement('input');
searchButton.type = "button";
searchButton.value = "Find Next";
searchButton.classList.add('search-button');
this.element.appendChild(searchButton);
// replace with
const replaceBox = document.createElement('input');
replaceBox.type = "text";
replaceBox.classList.add('replace-box');
//searchBox.classList.add('native-key-bindings'); doesnt solve backspace problem
this.element.appendChild(replaceBox);
// replace button
const replaceSingleButton = document.createElement('input');
replaceSingleButton.type = "button";
replaceSingleButton.value = "Replace";
replaceSingleButton.classList.add('replace-single-button');
this.element.appendChild(replaceSingleButton);
// replace all button
const replaceButton = document.createElement('input');
replaceButton.type = "button";
replaceButton.value = "Replace All";
replaceButton.classList.add('replace-button');
this.element.appendChild(replaceButton);
}
// Returns an object that can be retrieved when package is activated
serialize() {}
// Tear down any state and detach
destroy() {
this.element.remove();
}
getElement() {
return this.element;
}
}
'use babel';
import ReplacePackageView from './replace-package-view';
import { CompositeDisposable } from 'atom';
import $ from 'jquery';
export default {
replacePackageView: null,
modalPanel: null,
subscriptions: null,
rangeArray: null,
activate(state) {
this.replacePackageView = new ReplacePackageView(state.replacePackageViewState);
this.modalPanel = atom.workspace.addModalPanel({
item: this.replacePackageView.getElement(),
visible: false
});
// Events subscribed to in atom's system can be easily cleaned up with a CompositeDisposable
this.subscriptions = new CompositeDisposable();
// Register command that toggles this view
this.subscriptions.add(atom.commands.add('atom-workspace', {
'replace-package:toggle': () => this.toggle()
}));
},
deactivate() {
this.modalPanel.destroy();
this.subscriptions.dispose();
this.replacePackageView.destroy();
},
serialize() {
return {
replacePackageViewState: this.replacePackageView.serialize()
};
},
toggle() {
this.search();
this.replaceAll();
this.replace();
return (
this.modalPanel.isVisible() ?
this.modalPanel.hide() :
this.modalPanel.show()
);
},
// this selects text one by occurrences
// might need a Marker class (class that outlines the selection)
search() {
var that = this;
$(".search-button").click(function(evt) {
var editor
editor = atom.workspace.getActiveTextEditor();
var text = editor.getText();
// get text value
var textValue = $('.search-box').val();
that.collectRanges(textValue);
// Selects all occurrences of the search term
//editor.setSelectedScreenRanges(arr); Uncomment this
console.log("that.rangeArray: "+that.rangeArray);
editor.scan(new RegExp(textValue, 'ig'), iterator => {
marker = editor.markBufferRange(iterator.range);
decoration = editor.decorateMarker(marker, {type: 'line-number', class: 'my-line-class'});
iterator.stop();
that.rangeArray.splice(0, 1);
});
// show notification warning if no results
if (that.rangeArray.length === 0) {
atom.notifications.addWarning(that.rangeArray.length+ " results found");
}
//that.findNext(that.rangeArray);
evt.stopImmediatePropagation();
return that.rangeArray;
});
},
collectRanges(val) {
let editor
var arr = []
editor = atom.workspace.getActiveTextEditor();
editor.scan(new RegExp(val, 'ig'), iterator => {
arr.push(iterator.range);
});
this.rangeArray = arr;
return arr;
},
findNext(arr) {
editor = atom.workspace.getActiveTextEditor();
marker = editor.markBufferRange(arr[0]);
decoration = editor.decorateMarker(marker, {type: 'line-number', class: 'my-line-class'});
},
replaceAll() {
$(".replace-button").click(function(evt) {
let editor
editor = atom.workspace.getActiveTextEditor();
var text = editor.getText();
// get text value
var findValue = $('.search-box').val();
var replaceValue = $('.replace-box').val();
editor.scan(new RegExp(findValue, 'ig'), iterator => {
editor.insertText(replaceValue);
});
evt.stopImmediatePropagation();
});
},
replace() {
$(".replace-single-button").click(function(evt) {
let editor
editor = atom.workspace.getActiveTextEditor();
var text = editor.getText();
// get text value
var findValue = $('.search-box').val();
var replaceValue = $('.replace-box').val();
editor.scan(new RegExp(findValue, 'ig'), iterator => {
iterator.replace(replaceValue);
iterator.stop();
});
evt.stopImmediatePropagation(); // need this message to avoid firing event twice
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment