Skip to content

Generic scope handler interfaces #1031

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 72 commits into from
Oct 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
72 commits
Select commit Hold shift + click to select a range
846bd80
Original scope handlers
pokey Oct 11, 2022
ed0d18b
More stuff
pokey Oct 11, 2022
b7a4d73
Initial work
AndreasArvidsson Oct 12, 2022
6d4dbfe
Use proper token stage in sub token stage
AndreasArvidsson Oct 12, 2022
71078ed
Work around for identifier matcher
AndreasArvidsson Oct 12, 2022
07cc996
Use new scope handler in relative scope stage
AndreasArvidsson Oct 12, 2022
298d823
Use new scope handlers in ordinal scope stage
AndreasArvidsson Oct 12, 2022
454e158
Update usage of containing indices
AndreasArvidsson Oct 12, 2022
4c33e8a
Rename
AndreasArvidsson Oct 12, 2022
c8cb599
Refactored create target
AndreasArvidsson Oct 12, 2022
756f9bb
Clean up
AndreasArvidsson Oct 12, 2022
7156a02
clean up
AndreasArvidsson Oct 12, 2022
cc4d38e
Add some tests that should pass
pokey Oct 17, 2022
73bba88
Add a bunch more tests
pokey Oct 18, 2022
b72819f
Attempt at different approach to scope handlers
pokey Oct 17, 2022
1555fe7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 17, 2022
ef0d571
Initial implementation of new idea
pokey Oct 18, 2022
3390b1f
More work on this stuff
pokey Oct 18, 2022
21d75ea
Rename and add some todos
pokey Oct 18, 2022
bde7531
More stuff
pokey Oct 18, 2022
a8104f6
Tweaks
pokey Oct 18, 2022
e24a9e3
Renames and docstrings
pokey Oct 18, 2022
99a5af6
Restructuring
pokey Oct 18, 2022
9513753
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 18, 2022
e410ed5
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 18, 2022
4b2325d
More jsdocs
pokey Oct 18, 2022
e519f0d
Tweaks
pokey Oct 18, 2022
39d6faf
Test fixes
pokey Oct 18, 2022
5ff57d4
Fix error messages
pokey Oct 18, 2022
5aeb2a6
Revert `OrdinalScopeStage`
pokey Oct 18, 2022
ea80465
Tweak
pokey Oct 18, 2022
234811a
jsdocs; fix import
pokey Oct 19, 2022
969bc72
Don't export legacy types
pokey Oct 19, 2022
084824c
Fix import
pokey Oct 19, 2022
b483468
Preparation for surrounding pairs
pokey Oct 19, 2022
5ef5c41
Naming cleanup
pokey Oct 19, 2022
e73c420
Unify `getLegacyScopeStage` functions
pokey Oct 19, 2022
0628ca6
Lots of cleanup
pokey Oct 19, 2022
4b52d3c
Fix regex `lastIndex` issue
pokey Oct 19, 2022
e9b30cc
More cleanup
pokey Oct 19, 2022
4e7920c
More cleanup
pokey Oct 19, 2022
527ad18
More cleanup
pokey Oct 19, 2022
d73fae6
Add `ancestorIndex` in prepartion for #124
pokey Oct 19, 2022
0b082cb
Add more jsdocs
pokey Oct 19, 2022
ed93d3f
More docs
pokey Oct 19, 2022
b1bb34c
More docs
pokey Oct 19, 2022
616e828
docstrings
pokey Oct 19, 2022
eaa3318
Improve hierarchical error type
pokey Oct 19, 2022
62a2e6e
Docs
pokey Oct 19, 2022
f83493c
More minor dog tweaks
pokey Oct 19, 2022
af9c0f7
More docs
pokey Oct 19, 2022
65f8a13
More docs
pokey Oct 19, 2022
ab7feae
Doc strings and a couple tests
pokey Oct 19, 2022
e10a9a8
Remove `isPreferredOver`
pokey Oct 20, 2022
994b364
Support `ancestorIndex` on api surface
pokey Oct 20, 2022
37268e1
Improved jsdocs
pokey Oct 20, 2022
f86b98c
Split and cleanup relative stages
pokey Oct 20, 2022
307fb12
Make scope handler constructor args optional
pokey Oct 20, 2022
f21c8f5
More legacy type fixes
pokey Oct 20, 2022
0818816
Add `"identifier"` scope
pokey Oct 20, 2022
4252fae
Merge branch 'main' into generic-scope-handler-interfaces
pokey Oct 20, 2022
32cb685
Working new code
pokey Oct 21, 2022
4cc83af
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 21, 2022
4d3ed1f
Docs + cheatsheet
pokey Oct 21, 2022
aa2a74f
Update docs
pokey Oct 21, 2022
fe57dd4
Add jsdoc
pokey Oct 21, 2022
11ae8a0
jsdoc
pokey Oct 21, 2022
26438ea
JSDocs
pokey Oct 21, 2022
b4a0967
doc tweaks
pokey Oct 21, 2022
6adadd7
reflow
pokey Oct 21, 2022
eb271cd
Tweaks
pokey Oct 21, 2022
235b05f
Merge branch 'main' into generic-scope-handler-interfaces
AndreasArvidsson Oct 23, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cursorless-talon/src/cheatsheet_html/sections/modifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ def get_modifiers():
"spokenForm": f"{complex_modifiers['previous']} <number> <scope>s",
"description": "previous <number> instances of <scope>",
},
{
"spokenForm": f"<scope> {complex_modifiers['backward']}",
"description": "single instance of <scope> including target, going backwards",
},
{
"spokenForm": f"<number> <scope>s {complex_modifiers['backward']}",
"description": "<number> instances of <scope> including target, going backwards",
Expand Down
14 changes: 13 additions & 1 deletion cursorless-talon/src/modifiers/relative_scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,23 @@ def cursorless_relative_scope_count(m) -> dict[str, Any]:
)


@mod.capture(rule="<user.cursorless_scope_type> {user.cursorless_backward_modifier}")
def cursorless_relative_scope_one_backward(m) -> dict[str, Any]:
Comment on lines +59 to +60
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I introduced this one because otherwise you need to say "one tokens backward", which is kinda awkward 😅. Now you just say "token backward". Cheatsheet updated

"""Take scope backward, eg `funk backward`"""
return create_relative_scope_modifier(
m.cursorless_scope_type,
0,
1,
m.cursorless_backward_modifier,
)


@mod.capture(
rule=(
"<user.cursorless_relative_scope_singular> | "
"<user.cursorless_relative_scope_plural> | "
"<user.cursorless_relative_scope_count>"
"<user.cursorless_relative_scope_count> | "
"<user.cursorless_relative_scope_one_backward>"
)
)
def cursorless_relative_scope(m) -> dict[str, Any]:
Expand Down
1 change: 1 addition & 0 deletions cursorless-talon/src/modifiers/scopes.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
# Text-based scope types
"char": "character",
"word": "word",
"identifier": "identifier",
"block": "paragraph",
"cell": "notebookCell",
"file": "document",
Expand Down
10 changes: 10 additions & 0 deletions docs/user/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,16 @@ The `"token"` modifier expands its input to the nearest containing token. This m
- `"take token"`
- `"chuck token"`

##### `"identifier`

The `"identifier"` modifier behaves like `"token"`, but only considers tokens that are viable identifiers. For example `"identifier"` could be used to select `foo`, `fooBar`, or `foo_bar`, but not `.`, `=`, `+=`, etc. For example:

- `"copy identifier"`
- `"take identifier"`
- `"chuck identifier"`

This scope type is useful with ordinals, allowing you to say eg `"last identifier"` to refer to the last identifier on the current line.

##### `"paint"`

Both of the commands below will expand from the mark forward and backward to include all adjacent non-whitespace characters.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,28 @@
import { HatStyleName } from "../../hatStyles";

export interface CursorMark {
interface CursorMark {
type: "cursor";
}

export interface ThatMark {
interface ThatMark {
type: "that";
}

export interface SourceMark {
interface SourceMark {
type: "source";
}

export interface NothingMark {
interface NothingMark {
type: "nothing";
}

export interface LastCursorPositionMark {
type: "lastCursorPosition";
}

export interface DecoratedSymbolMark {
interface DecoratedSymbolMark {
type: "decoratedSymbol";
symbolColor: HatStyleName;
character: string;
}

export type LineNumberType = "absolute" | "relative" | "modulo100";
type LineNumberType = "absolute" | "relative" | "modulo100";

export interface LineNumberPositionV2 {
type: LineNumberType;
Expand All @@ -48,7 +44,7 @@ export type MarkV2 =
| NothingMark
| LineNumberMarkV2;

export type SimpleSurroundingPairName =
type SimpleSurroundingPairName =
| "angleBrackets"
| "backtickQuotes"
| "curlyBrackets"
Expand All @@ -60,11 +56,8 @@ export type SimpleSurroundingPairName =
| "parentheses"
| "singleQuotes"
| "squareBrackets";
export type ComplexSurroundingPairName =
| "string"
| "any"
| "collectionBoundary";
export type SurroundingPairName =
type ComplexSurroundingPairName = "string" | "any" | "collectionBoundary";
type SurroundingPairName =
| SimpleSurroundingPairName
| ComplexSurroundingPairName;

Expand Down Expand Up @@ -123,17 +116,17 @@ export type SimpleScopeTypeTypeV2 =
| "boundedNonWhitespaceSequence"
| "url";

export interface SimpleScopeType {
interface SimpleScopeType {
type: SimpleScopeTypeTypeV2;
}

export interface CustomRegexScopeType {
interface CustomRegexScopeType {
type: "customRegex";
regex: string;
}

export type SurroundingPairDirection = "left" | "right";
export interface SurroundingPairScopeType {
type SurroundingPairDirection = "left" | "right";
interface SurroundingPairScopeType {
type: "surroundingPair";
delimiter: SurroundingPairName;
forceDirection?: SurroundingPairDirection;
Expand All @@ -145,37 +138,32 @@ export interface SurroundingPairScopeType {
requireStrongContainment?: boolean;
}

export type ScopeType =
export type ScopeTypeV2 =
| SimpleScopeType
| SurroundingPairScopeType
| CustomRegexScopeType;

export interface ContainingSurroundingPairModifier
extends ContainingScopeModifier {
scopeType: SurroundingPairScopeType;
}

export interface InteriorOnlyModifier {
interface InteriorOnlyModifier {
type: "interiorOnly";
}

export interface ExcludeInteriorModifier {
interface ExcludeInteriorModifier {
type: "excludeInterior";
}

export interface ContainingScopeModifier {
interface ContainingScopeModifier {
type: "containingScope";
scopeType: ScopeType;
scopeType: ScopeTypeV2;
}

export interface EveryScopeModifier {
interface EveryScopeModifier {
type: "everyScope";
scopeType: ScopeType;
scopeType: ScopeTypeV2;
}

export interface OrdinalRangeModifier {
export interface OrdinalRangeModifierV2 {
type: "ordinalRange";
scopeType: ScopeType;
scopeType: ScopeTypeV2;
anchor: number;
active: number;
excludeAnchor?: boolean;
Expand All @@ -187,21 +175,21 @@ export interface OrdinalRangeModifier {
* example if it is the destination of a bring or move it should inherit the
* type information such as delimiters from its source.
*/
export interface RawSelectionModifier {
interface RawSelectionModifier {
type: "toRawSelection";
}

export interface LeadingModifier {
interface LeadingModifier {
type: "leading";
}

export interface TrailingModifier {
interface TrailingModifier {
type: "trailing";
}

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

export interface PositionModifier {
interface PositionModifier {
type: "position";
position: Position;
}
Expand All @@ -213,7 +201,7 @@ export interface PartialPrimitiveTargetDescriptorV2 {
isImplicit?: boolean;
}

export interface HeadTailModifier {
interface HeadTailModifier {
type: "extendThroughStartOf" | "extendThroughEndOf";
modifiers?: ModifierV2[];
}
Expand All @@ -222,7 +210,7 @@ export interface HeadTailModifier {
* Runs {@link modifier} if the target has no explicit scope type, ie if
* {@link Target.hasExplicitScopeType} is `false`.
*/
export interface ModifyIfUntypedModifier {
interface ModifyIfUntypedModifier {
type: "modifyIfUntyped";

/**
Expand All @@ -236,7 +224,7 @@ export interface ModifyIfUntypedModifier {
* doesn't throw an error, returning the output from the first modifier not
* throwing an error.
*/
export interface CascadingModifier {
interface CascadingModifier {
type: "cascading";

/**
Expand All @@ -251,7 +239,7 @@ export type ModifierV2 =
| ExcludeInteriorModifier
| ContainingScopeModifier
| EveryScopeModifier
| OrdinalRangeModifier
| OrdinalRangeModifierV2
| HeadTailModifier
| LeadingModifier
| TrailingModifier
Expand Down Expand Up @@ -281,50 +269,7 @@ export type PartialTargetDescriptorV2 =
| PartialRangeTargetDescriptorV2
| PartialListTargetDescriptorV2;

export interface PrimitiveTargetDescriptor
extends PartialPrimitiveTargetDescriptorV2 {
/**
* The mark, eg "air", "this", "that", etc
*/
mark: MarkV2;

/**
* Zero or more modifiers that will be applied in sequence to the output from
* the mark. Note that the modifiers will be applied in reverse order. For
* example, if the user says "take first char name air", then we will apply
* "name" to the output of "air" to select the name of the function or
* statement containing "air", then apply "first char" to select the first
* character of the name.
*/
modifiers: ModifierV2[];

/**
* We separate the positional modifier from the other modifiers because it
* behaves differently and and makes the target behave like a destination for
* example for bring. This change is the first step toward #803
*/
positionModifier?: PositionModifier;
}

export interface RangeTargetDescriptor {
type: "range";
anchor: PrimitiveTargetDescriptor;
active: PrimitiveTargetDescriptor;
excludeAnchor: boolean;
excludeActive: boolean;
rangeType: RangeType;
}
// continuous is one single continuous selection between the two targets
// vertical puts a selection on each line vertically between the two targets

export type RangeType = "continuous" | "vertical";

export interface ListTargetDescriptor {
type: "list";
elements: (PrimitiveTargetDescriptor | RangeTargetDescriptor)[];
}

export type TargetDescriptor =
| PrimitiveTargetDescriptor
| RangeTargetDescriptor
| ListTargetDescriptor;
type RangeType = "continuous" | "vertical";
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import {
LineNumberPositionV2,
MarkV2,
ModifierV2,
OrdinalRangeModifier,
OrdinalRangeModifierV2,
PartialPrimitiveTargetDescriptorV2,
PartialTargetDescriptorV2,
ScopeType,
ScopeTypeV2,
} from "./targetDescriptorV2.types";

export function upgradeV2ToV3(command: CommandV2): CommandV3 {
Expand Down Expand Up @@ -104,7 +104,7 @@ function createLineNumberMark(
}

function createOrdinalModifier(
modifier: OrdinalRangeModifier
modifier: OrdinalRangeModifierV2
): OrdinalScopeModifier | RangeModifier {
if (modifier.anchor === modifier.active) {
return createAbsoluteOrdinalModifier(modifier.scopeType, modifier.anchor);
Expand All @@ -130,7 +130,7 @@ function createLineNumberMarkFromPos(
}

function createAbsoluteOrdinalModifier(
scopeType: ScopeType,
scopeType: ScopeTypeV2,
start: number
): OrdinalScopeModifier {
return {
Expand Down
Loading