Skip to content

Commit

Permalink
Merge pull request #14 from winebarrel/add_exclude
Browse files Browse the repository at this point in the history
Add "exclude"
  • Loading branch information
winebarrel committed Feb 7, 2024
2 parents b009919 + 39dd746 commit 9ce6ec5
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -5,6 +5,6 @@

VS Code extension to filter text by selected text.

![](https://github.com/winebarrel/vscode-right-click-filter/assets/117768/cbcb26b3-e955-4000-8186-29e90b641edf)
![](https://github.com/winebarrel/vscode-right-click-filter/assets/117768/cf6ee89c-ff7f-446d-b0e0-06a7fcbcc4f7)

![](https://github.com/winebarrel/vscode-right-click-filter/assets/117768/2dfcd892-7463-4192-a367-301f1d46e386)
9 changes: 9 additions & 0 deletions package.json
Expand Up @@ -26,6 +26,10 @@
{
"command": "right-click-filter.filterTextBySelection",
"title": "Filter Text by Selection"
},
{
"command": "right-click-filter.excludeTextBySelection",
"title": "Exclude Text by Selection"
}
],
"menus": {
Expand All @@ -34,6 +38,11 @@
"command": "right-click-filter.filterTextBySelection",
"when": "editorHasSelection",
"group": "right-click-filter@1"
},
{
"command": "right-click-filter.excludeTextBySelection",
"when": "editorHasSelection",
"group": "right-click-filter@2"
}
]
}
Expand Down
35 changes: 25 additions & 10 deletions src/extension.ts
@@ -1,6 +1,6 @@
import * as vscode from "vscode";

async function filterTextBySelection() {
async function filterTextBySelection(options: { exclude: boolean }) {
const activeTextEditor = vscode.window.activeTextEditor;

if (!activeTextEditor) {
Expand All @@ -21,11 +21,11 @@ async function filterTextBySelection() {
return;
}

const content =
text
.split(/\r?\n/)
.filter((line) => line.includes(selectedText))
.join("\n") + "\n";
const f = options.exclude
? (l: string) => !l.includes(selectedText)
: (l: string) => l.includes(selectedText);

const content = text.split(/\r?\n/).filter(f).join("\n") + "\n";

if (!content.trim()) {
return;
Expand All @@ -35,13 +35,28 @@ async function filterTextBySelection() {
vscode.window.showTextDocument(document);
}

async function filterTextBySelectionCmd() {
await filterTextBySelection({ exclude: false });
}

async function excludeTextBySelectionCmd() {
await filterTextBySelection({ exclude: true });
}

export function activate(context: vscode.ExtensionContext) {
let disposable = vscode.commands.registerCommand(
"right-click-filter.filterTextBySelection",
filterTextBySelection
context.subscriptions.push(
vscode.commands.registerCommand(
"right-click-filter.filterTextBySelection",
filterTextBySelectionCmd
)
);

context.subscriptions.push(disposable);
context.subscriptions.push(
vscode.commands.registerCommand(
"right-click-filter.excludeTextBySelection",
excludeTextBySelectionCmd
)
);
}

export function deactivate() {}

0 comments on commit 9ce6ec5

Please sign in to comment.