Skip to content

Filter modifiers #1008

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

Merged
merged 11 commits into from
Oct 6, 2022
2 changes: 2 additions & 0 deletions cursorless-talon/src/modifiers/modifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
"just": "toRawSelection",
"leading": "leading",
"trailing": "trailing",
"content": "keepContentFilter",
"empty": "keepEmptyFilter",
}

mod.list(
Expand Down
8 changes: 8 additions & 0 deletions src/processTargets/getModifierStage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import {
Modifier,
} from "../typings/targetDescriptor.types";
import CascadingStage from "./modifiers/CascadingStage";
import {
KeepContentFilterStage,
KeepEmptyFilterStage,
} from "./modifiers/FilterStages";
import { HeadStage, TailStage } from "./modifiers/HeadTailStage";
import {
ExcludeInteriorStage,
Expand Down Expand Up @@ -65,6 +69,10 @@ export default (modifier: Modifier): ModifierStage => {
return new OrdinalScopeStage(modifier);
case "relativeScope":
return new RelativeScopeStage(modifier);
case "keepContentFilter":
return new KeepContentFilterStage(modifier);
case "keepEmptyFilter":
return new KeepEmptyFilterStage(modifier);
case "cascading":
return new CascadingStage(modifier);
case "modifyIfUntyped":
Expand Down
23 changes: 23 additions & 0 deletions src/processTargets/modifiers/FilterStages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { Target } from "../../typings/target.types";
import type {
KeepContentFilterModifier,
KeepEmptyFilterModifier,
} from "../../typings/targetDescriptor.types";
import type { ProcessedTargetsContext } from "../../typings/Types";
import type { ModifierStage } from "../PipelineStages.types";

export class KeepContentFilterStage implements ModifierStage {
constructor(private modifier: KeepContentFilterModifier) {}

run(context: ProcessedTargetsContext, target: Target): Target[] {
return target.contentText.trim() !== "" ? [target] : [];
}
}

export class KeepEmptyFilterStage implements ModifierStage {
constructor(private modifier: KeepEmptyFilterModifier) {}

run(context: ProcessedTargetsContext, target: Target): Target[] {
return target.contentText.trim() === "" ? [target] : [];
}
}
13 changes: 7 additions & 6 deletions src/processTargets/modifiers/scopeTypeStages/LineStage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ export default class implements ModifierStage {
const targets: LineTarget[] = [];

for (let i = startLine; i <= endLine; ++i) {
const line = editor.document.lineAt(i);
if (!line.isEmptyOrWhitespace) {
targets.push(
createLineTarget(target.editor, target.isReversed, line.range)
);
}
targets.push(
createLineTarget(
target.editor,
target.isReversed,
editor.document.lineAt(i).range
)
);
}

if (targets.length === 0) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
languageId: markdown
command:
spokenForm: clear content
version: 3
targets:
- type: primitive
modifiers:
- {type: keepContentFilter}
usePrePhraseSnapshot: true
action: {name: clearAndSetSelection}
initialState:
documentContents: |-

a

b
selections:
- anchor: {line: 0, character: 0}
active: {line: 0, character: 0}
- anchor: {line: 1, character: 0}
active: {line: 1, character: 1}
- anchor: {line: 2, character: 0}
active: {line: 2, character: 1}
- anchor: {line: 3, character: 0}
active: {line: 3, character: 1}
marks: {}
finalState:
documentContents: |2



selections:
- anchor: {line: 1, character: 0}
active: {line: 1, character: 0}
- anchor: {line: 3, character: 0}
active: {line: 3, character: 0}
fullTargets: [{type: primitive, mark: {type: cursor}, modifiers: [{type: keepContentFilter}]}]
38 changes: 38 additions & 0 deletions src/test/suite/fixtures/recorded/selectionTypes/clearEmpty.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
languageId: markdown
command:
spokenForm: clear empty
version: 3
targets:
- type: primitive
modifiers:
- {type: keepEmptyFilter}
usePrePhraseSnapshot: true
action: {name: clearAndSetSelection}
initialState:
documentContents: |-

a

b
selections:
- anchor: {line: 0, character: 0}
active: {line: 0, character: 0}
- anchor: {line: 1, character: 0}
active: {line: 1, character: 1}
- anchor: {line: 2, character: 0}
active: {line: 2, character: 1}
- anchor: {line: 3, character: 0}
active: {line: 3, character: 1}
marks: {}
finalState:
documentContents: |-

a

b
selections:
- anchor: {line: 0, character: 0}
active: {line: 0, character: 0}
- anchor: {line: 2, character: 0}
active: {line: 2, character: 0}
fullTargets: [{type: primitive, mark: {type: cursor}, modifiers: [{type: keepEmptyFilter}]}]
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,18 @@ finalState:
d e
f g
selections:
- anchor: {line: 0, character: 0}
active: {line: 0, character: 0}
- anchor: {line: 1, character: 0}
active: {line: 1, character: 1}
- anchor: {line: 2, character: 0}
active: {line: 2, character: 0}
- anchor: {line: 3, character: 0}
active: {line: 3, character: 5}
- anchor: {line: 4, character: 0}
active: {line: 4, character: 5}
- anchor: {line: 5, character: 0}
active: {line: 5, character: 5}
- anchor: {line: 6, character: 0}
active: {line: 6, character: 0}
fullTargets: [{type: primitive, mark: {type: cursor}, modifiers: [{type: everyScope, scopeType: line}]}]
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ finalState:
selections:
- anchor: {line: 1, character: 1}
active: {line: 1, character: 0}
- anchor: {line: 2, character: 0}
active: {line: 2, character: 0}
- anchor: {line: 3, character: 5}
active: {line: 3, character: 0}
- anchor: {line: 4, character: 5}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ finalState:
selections:
- anchor: {line: 1, character: 0}
active: {line: 1, character: 1}
- anchor: {line: 2, character: 0}
active: {line: 2, character: 0}
- anchor: {line: 3, character: 0}
active: {line: 3, character: 5}
- anchor: {line: 4, character: 0}
Expand Down
12 changes: 11 additions & 1 deletion src/typings/targetDescriptor.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,14 @@ export interface TrailingModifier {
type: "trailing";
}

export interface KeepContentFilterModifier {
type: "keepContentFilter";
}

export interface KeepEmptyFilterModifier {
type: "keepEmptyFilter";
}

export type Position = "before" | "after" | "start" | "end";

export interface PositionModifier {
Expand Down Expand Up @@ -310,7 +318,9 @@ export type Modifier =
| RawSelectionModifier
| ModifyIfUntypedModifier
| CascadingModifier
| RangeModifier;
| RangeModifier
| KeepContentFilterModifier
| KeepEmptyFilterModifier;

export interface PartialRangeTargetDescriptor {
type: "range";
Expand Down