From a6a01e8b6bcdcba428d0f8397174b737fb023193 Mon Sep 17 00:00:00 2001 From: ax487 Date: Wed, 19 Jul 2017 18:55:07 +0200 Subject: [PATCH] Added preliminary support to mark previous / next --- lib/multi-cursor-plus.coffee | 76 +++++++++++++++++++++++++++++++++++- package.json | 8 +++- 2 files changed, 81 insertions(+), 3 deletions(-) diff --git a/lib/multi-cursor-plus.coffee b/lib/multi-cursor-plus.coffee index 665583a..be8350b 100644 --- a/lib/multi-cursor-plus.coffee +++ b/lib/multi-cursor-plus.coffee @@ -1,4 +1,5 @@ -{CompositeDisposable} = require 'atom' +{CompositeDisposable, Point, Range} = require 'atom' +_ = require 'underscore-plus' module.exports = @@ -76,6 +77,79 @@ module.exports = @selectToBottom() + @subscriptions.add atom.commands.add 'atom-text-editor', + 'multi-cursor-plus:mark-previous-like-this': => + @markPreviousLikeThis() + @subscriptions.add atom.commands.add 'atom-text-editor', + 'multi-cursor-plus:mark-next-like-this': => + @markNextLikeThis() + + markPreviousLikeThis: -> + editor = atom.workspace.getActiveTextEditor() + selections = editor?.getSelections() + bufferRange = editor?.getBuffer()?.getRange() + + return unless selections? + + if selections.length == 0 + return + + firstSelection = selections[0] + + start = (selection) -> selection.getBufferRange().start + + for selection in selections + if start(selection).isLessThan(start(firstSelection)) + firstSelection = selection + + currentText = firstSelection.getText() + + return unless currentText?.length + + selectionRange = firstSelection.getBufferRange() + + searchRange = (new Range(bufferRange.start, selectionRange.start)) + .translate(new Point(0, 0), new Point(0, -1)) + + editor.backwardsScanInBufferRange new RegExp(_.escapeRegExp(currentText)), + searchRange, + ({match, range, stop}) => + editor.addSelectionForBufferRange range + stop() + + markNextLikeThis: -> + editor = atom.workspace.getActiveTextEditor() + selections = editor?.getSelections() + bufferRange = editor?.getBuffer()?.getRange() + + return unless selections? + + if selections.length == 0 + return + + lastSelection = selections[0] + + end = (selection) -> selection.getBufferRange().end + + for selection in selections + if end(lastSelection).isLessThan(end(selection)) + lastSelection = selection + + currentText = lastSelection.getText() + + return unless currentText?.length + + selectionRange = lastSelection.getBufferRange() + + searchRange = (new Range(selectionRange.end, bufferRange.end)) + .translate new Point(0, 1) + + editor.scanInBufferRange new RegExp(_.escapeRegExp(currentText)), + searchRange, + ({match, range, stop}) => + editor.addSelectionForBufferRange range + stop() + # # Deactivate the package. # diff --git a/package.json b/package.json index 72f2223..6c2eade 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,9 @@ "multi-cursor-plus:select-to-first-character-of-line", "multi-cursor-plus:select-to-end-of-line", "multi-cursor-plus:select-to-top", - "multi-cursor-plus:select-to-bottom" + "multi-cursor-plus:select-to-bottom", + "multi-cursor-plus:mark-previous-like-this", + "multi-cursor-plus:mark-next-like-this" ] }, "repository": "https://github.com/kankaristo/atom-multi-cursor-plus", @@ -34,5 +36,7 @@ "engines": { "atom": ">=0.174.0 <2.0.0" }, - "dependencies": {} + "dependencies": { + "underscore-plus": "^1.6.6" + } }