-
-
Notifications
You must be signed in to change notification settings - Fork 195
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/dwim kill region #873
Open
Fuco1
wants to merge
2
commits into
master
Choose a base branch
from
feature/dwim-kill-region
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+65
−12
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1011,6 +1011,34 @@ See also `sp-skip-closing-pair'." | |
:type 'boolean | ||
:group 'smartparens) | ||
|
||
(defcustom sp-auto-narrow-kill-region nil | ||
"If non-nil, `sp-kill-region' will always try to kill as much as possible. | ||
|
||
When user selects a region which deletion would make the buffer | ||
unbalanced, try to find the most permissible | ||
region (i.e. largest) that when removed leaves the buffer | ||
balanced. | ||
|
||
This allows for more sloppy region selection when trying to clean | ||
up some code. | ||
|
||
Example: | ||
|
||
(let (|(foo bar) | ||
(one two)) | ||
body here)M | ||
|
||
With this setting enabled calling `sp-kill-region' will result in | ||
|
||
(let (|M) | ||
body here) | ||
|
||
instead of an error. | ||
|
||
The same applies to `sp-delete-region'." | ||
:type 'boolean | ||
:group 'smartparens) | ||
|
||
(defcustom sp-undo-pairs-separately nil | ||
"If non-nil, put an `undo-boundary' before each inserted pair. | ||
|
||
|
@@ -9030,7 +9058,13 @@ If that text is unbalanced, signal an error instead. | |
With a prefix argument, skip the balance check." | ||
(interactive "r") | ||
(when (or current-prefix-arg | ||
(sp-region-ok-p beg end) | ||
(-let [(&plist :ok ok :last-good-sexp last-good-sexp) (sp-get-region-info beg end)] | ||
(if (and (not ok) | ||
sp-auto-narrow-kill-region) | ||
(when last-good-sexp | ||
(setq end (sp-get last-good-sexp :end-suf)) | ||
(goto-char end)) | ||
ok)) | ||
(user-error (sp-message :unbalanced-region :return))) | ||
(setq this-command 'kill-region) | ||
(kill-region beg end))) | ||
|
@@ -9054,28 +9088,47 @@ of the point." | |
(indent-sexp)) | ||
(sp--back-to-indentation column indentation)))) | ||
|
||
(cl-defun sp-region-ok-p (start end) | ||
"Test if region between START and END is balanced. | ||
(defun sp-get-region-info (start end) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Document this somewhere (check if |
||
"Get information about region between START and END. | ||
|
||
A balanced region is one where all opening delimiters are matched | ||
by closing delimiters. | ||
See `sp-region-ok-p' for explanation of what constitutes a | ||
balanced region. | ||
|
||
This function does *not* check that the delimiters are correctly | ||
ordered, that is [(]) is correct even though it is not logically | ||
properly balanced." | ||
(interactive "r") | ||
The return value is a plist with the following keys: | ||
|
||
:ok - non-nil if the region is balanced | ||
:last-good-sexp - last sexp in the region which is part of a | ||
balanced region starting at START" | ||
(save-excursion | ||
(save-restriction | ||
(when (eq (sp-point-in-string start) (sp-point-in-string end)) | ||
(narrow-to-region start end) | ||
(let ((regex (sp--get-allowed-regexp (-difference sp-pair-list (sp--get-allowed-pair-list))))) | ||
(let ((regex (sp--get-allowed-regexp (-difference sp-pair-list (sp--get-allowed-pair-list)))) | ||
(last-good-sexp)) | ||
(goto-char (point-min)) | ||
(while (or (prog1 (sp-forward-sexp) | ||
(while (or (prog1 (let ((ok (sp-forward-sexp))) | ||
(when ok | ||
(setq last-good-sexp ok)) | ||
ok) | ||
(sp-skip-forward-to-symbol)) | ||
;; skip impossible delimiters | ||
(when (looking-at-p regex) | ||
(goto-char (match-end 0))))) | ||
(looking-at-p "[[:blank:]\n]*\\'")))))) | ||
(list | ||
:ok (looking-at-p "[[:blank:]\n]*\\'") | ||
:last-good-sexp last-good-sexp)))))) | ||
|
||
(defun sp-region-ok-p (start end) | ||
"Test if region between START and END is balanced. | ||
|
||
A balanced region is one where all opening delimiters are matched | ||
by closing delimiters. | ||
|
||
This function does *not* check that the delimiters are correctly | ||
ordered, that is [(]) is correct even though it is not logically | ||
properly balanced." | ||
(interactive "r") | ||
(plist-get (sp-get-region-info start end) :ok)) | ||
|
||
(defun sp-newline () | ||
"Insert a newline and indent it. | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should extract this new block into a separate function as it will be reused multiple times