Skip to content

Commit

Permalink
feat: symmetric editing/selection
Browse files Browse the repository at this point in the history
  • Loading branch information
haberdashPI committed Jun 10, 2021
1 parent 0a800e3 commit fbf2479
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1 @@
nodejs 14.9.0
nodejs 16.1.0
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Change Log

## [0.1.8]
- **Feature**: New symmetric (start/end of selection) commands:
- Add a character before/after selections
- Remove characters before/after selections
- Adjust selection ends both inwards/outwards

## [0.1.7]
- **Bugfix**: downstream vulnerabilities fixed

Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ do not contain a match of the given string.
These commands modify the start and end of selections.

- "Trim selection to exclude whitespace at start/end" (`selection-utilities.trimSelectionWhitespace`): Self explanatory
- "Adjust selection boundaries inwards or outwards" (`selection-utilities.adjustSelections`): Expands or shrinks the selection by individual characters. Takes two arguments, dir ("forward" or "backward") and count (how many characters to adjust).

### Editing Text by Selection

Expand All @@ -297,6 +298,8 @@ These commands modify selected text in various ways
- "Right align selections (using spaces)" (`selection-utilities.alignSelectionsRight`):
Insert spaces to the left of a selection so that the right side of the selections align.
- "Trim whitespace at start/end of selection" (`selection-utilities.trimWhitespace`)
- "Delete characters at start end of selections" (`selection-utilities.deleteAround).
- "Insert characters around selections" (`selection-utilities.insertAround`), takes two arguments: `before` and `after` which should include the characters that should be placed before and after each selection

## Related projects

Expand Down
22 changes: 20 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"publisher": "haberdashPI",
"repository": "https://github.com/haberdashPI/vscode-selection-utilities",
"description": "Kakaune-inspired collection of useful commands for manipulating selections.",
"version": "0.1.7",
"version": "0.1.8",
"engines": {
"vscode": "^1.55.0"
},
Expand Down Expand Up @@ -54,7 +54,10 @@
"onCommand:selection-utilities.selectBySubsection",
"onCommand:selection-utilities.moveBySection",
"onCommand:selection-utilities.selectBySection",
"onCommand:selection-utilities.narrowTo"
"onCommand:selection-utilities.narrowTo",
"onCommand:selection-utilities.insertAround",
"onCommand:selection-utilities.deleteAround",
"onCommand:selection-utilities.adjustSelections"
],
"main": "./out/extension.js",
"contributes": {
Expand Down Expand Up @@ -397,6 +400,21 @@
"command": "selection-utilities.excludeByRegex",
"title": "Exclude selections by... (regex)",
"category": "Selcetion Utilities"
},
{
"command": "selection-utilities.insertAround",
"title": "Insert characters around selections",
"category": "Selection Utilities"
},
{
"command": "selection-utilities.deleteAround",
"title": "Delete characters at start end of selections",
"category": "Selection Utilities"
},
{
"command": "selection-utilities.adjustSelections",
"title": "Adjust selections inwards or outwards",
"category": "Selection Utilities"
}
]
},
Expand Down
2 changes: 2 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { registerActiveMotions } from './activeMotions';
import { registerSelectionModifiers } from './selectionModifiers';
import { registerSelectionFilters } from './selectionFilters';
import { registerSelectionAlignments } from './selectionAlignment';
import { registerSymmetricModifiers } from "./symmetricModifiers";

// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
Expand All @@ -26,6 +27,7 @@ export function activate(context: vscode.ExtensionContext) {
registerSelectionModifiers(context);
registerSelectionFilters(context);
registerSelectionAlignments(context);
registerSymmetricModifiers(context);
}

// this method is called when your extension is deactivated
Expand Down
77 changes: 77 additions & 0 deletions src/symmetricModifiers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import * as vscode from 'vscode';

export function registerSymmetricModifiers(context: vscode.ExtensionContext){
context.subscriptions.push(vscode.commands.
registerCommand('selection-utilities.insertAround', insertAround));
context.subscriptions.push(vscode.commands.
registerCommand('selection-utilities.deleteAround', deleteAround));
context.subscriptions.push(vscode.commands.
registerCommand('selection-utilities.adjustSelections', adjustSelections));
}

interface InsertAroundArgs{
before: string,
after: string
}
async function insertAround(args: InsertAroundArgs){
let editor = vscode.window.activeTextEditor;
if(editor){
let ed = editor;
let ranges = editor.selections.map(sel =>
new vscode.Range(sel.start, sel.end));

await editor.edit(builder => {
for(const r of ranges){
builder.insert(r.start, args.before);
builder.insert(r.end, args.after);
}
});

editor.selections = editor.selections.map(sel => {
if(!sel.isReversed){
return new vscode.Selection(
sel.anchor,
sel.active.translate(0, -1),
);
}else{
return new vscode.Selection(
sel.anchor.translate(0, -1),
sel.active
);
}
});
}
}

function deleteAround(args: {count?: number }){
let editor = vscode.window.activeTextEditor;
if(editor){
let ed = editor;
editor.edit(builder => {
for(const sel of ed.selections){
builder.delete(new vscode.Range(
sel.start.translate(0, -(args.count || 1)),
sel.start)
);
builder.delete(new vscode.Range(
sel.end,
sel.end.translate(0, (args.count || 1)))
);
}
});
}
}

function adjustSelections(args: {dir: string, count: number}){
let editor = vscode.window.activeTextEditor;
let step = args.dir === 'forward' ? (args.count || 1) : -(args.count || 1);
if(editor){
editor.selections = editor.selections.map(sel => {
let sign = sel.isReversed ? -1 : 1;
return new vscode.Selection(
sel.anchor.translate(0, -sign*step),
sel.active.translate(0, sign*step)
);
});
}
}

0 comments on commit fbf2479

Please sign in to comment.