Skip to content

Added member scope type #954

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

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions cursorless-talon/src/modifiers/containing_scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"short paint": "boundedNonWhitespaceSequence",
"link": "url",
"token": "token",
"member": "member",
# LaTeX
"part": "part",
"chapter": "chapter",
Expand Down
7 changes: 5 additions & 2 deletions src/processTargets/getModifierStage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
EveryScopeModifier,
Modifier,
} from "../typings/targetDescriptor.types";
import BoundedNonWhitespaceSequenceStage from "./modifiers/BoundedNonWhitespaceStage";
import CascadingStage from "./modifiers/CascadingStage";
import { HeadStage, TailStage } from "./modifiers/HeadTailStage";
import {
Expand All @@ -19,17 +18,19 @@ import OrdinalRangeSubTokenStage, {
} from "./modifiers/OrdinalRangeSubTokenStage";
import PositionStage from "./modifiers/PositionStage";
import RawSelectionStage from "./modifiers/RawSelectionStage";
import BoundedNonWhitespaceSequenceStage from "./modifiers/scopeTypeStages/BoundedNonWhitespaceStage";
import ContainingSyntaxScopeStage, {
SimpleContainingScopeModifier,
} from "./modifiers/scopeTypeStages/ContainingSyntaxScopeStage";
import DocumentStage from "./modifiers/scopeTypeStages/DocumentStage";
import LineStage from "./modifiers/scopeTypeStages/LineStage";
import MemberStage from "./modifiers/scopeTypeStages/MemberStage";
import NotebookCellStage from "./modifiers/scopeTypeStages/NotebookCellStage";
import ParagraphStage from "./modifiers/scopeTypeStages/ParagraphStage";
import {
NonWhitespaceSequenceStage,
CustomRegexModifier,
CustomRegexStage,
NonWhitespaceSequenceStage,
UrlStage,
} from "./modifiers/scopeTypeStages/RegexStage";
import TokenStage from "./modifiers/scopeTypeStages/TokenStage";
Expand Down Expand Up @@ -95,6 +96,8 @@ const getContainingScopeStage = (
return new UrlStage(modifier);
case "collectionItem":
return new ItemStage(modifier);
case "member":
return new MemberStage(modifier);
case "customRegex":
return new CustomRegexStage(modifier as CustomRegexModifier);
case "surroundingPair":
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Target } from "../../typings/target.types";
import { Target } from "../../../typings/target.types";
import {
ContainingScopeModifier,
EveryScopeModifier,
} from "../../typings/targetDescriptor.types";
import { ProcessedTargetsContext } from "../../typings/Types";
import { ModifierStage } from "../PipelineStages.types";
import { TokenTarget } from "../targets";
import getModifierStage from "../getModifierStage";
import { processSurroundingPair } from "./surroundingPair";
import { NoContainingScopeError } from "../../errors";
} from "../../../typings/targetDescriptor.types";
import { ProcessedTargetsContext } from "../../../typings/Types";
import { ModifierStage } from "../../PipelineStages.types";
import { TokenTarget } from "../../targets";
import getModifierStage from "../../getModifierStage";
import { processSurroundingPair } from "../surroundingPair";
import { NoContainingScopeError } from "../../../errors";

/**
* Intersection of NonWhitespaceSequenceStage and a surrounding pair
Expand Down
35 changes: 35 additions & 0 deletions src/processTargets/modifiers/scopeTypeStages/MemberStage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { NoContainingScopeError } from "../../../errors";
import { Target } from "../../../typings/target.types";
import {
ContainingScopeModifier,
EveryScopeModifier,
} from "../../../typings/targetDescriptor.types";
import { ProcessedTargetsContext } from "../../../typings/Types";
import { ModifierStage } from "../../PipelineStages.types";
import { processSurroundingPair } from "../surroundingPair";
import { RegexStageBase } from "./RegexStage";

export default class MemberStage implements ModifierStage {
constructor(private modifier: ContainingScopeModifier | EveryScopeModifier) {}

run(context: ProcessedTargetsContext, target: Target): Target[] {
const pairInfo = processSurroundingPair(
context,
target.editor,
target.contentRange,
{
type: "surroundingPair",
delimiter: "parentheses",
requireStrongContainment: true,
}
);

const regexStage = new RegexStageBase(
this.modifier,
/([\w.](\(.*\))?)+/g,
pairInfo != null ? pairInfo.interiorRange : undefined
);

return regexStage.run(context, target);
}
}
60 changes: 31 additions & 29 deletions src/processTargets/modifiers/scopeTypeStages/RegexStage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import { ProcessedTargetsContext } from "../../../typings/Types";
import { ModifierStage } from "../../PipelineStages.types";
import { TokenTarget } from "../../targets";

class RegexStageBase implements ModifierStage {
export class RegexStageBase implements ModifierStage {
constructor(
private modifier: ContainingScopeModifier | EveryScopeModifier,
protected regex: RegExp
protected regex: RegExp,
private textRange?: Range
) {}

run(context: ProcessedTargetsContext, target: Target): Target[] {
Expand All @@ -30,19 +31,20 @@ class RegexStageBase implements ModifierStage {
const end = target.hasExplicitRange
? contentRange.end
: editor.document.lineAt(contentRange.end).range.end;
const targets: Target[] = [];

for (let i = start.line; i <= end.line; ++i) {
this.getMatchesForLine(editor, i).forEach((range) => {
// Regex match and selection intersects
if (
range.end.isAfterOrEqual(start) &&
range.start.isBeforeOrEqual(end)
) {
targets.push(this.getTargetFromRange(target, range));
}
});
}
const textRange =
this.textRange ??
new Range(
editor.document.lineAt(contentRange.start).range.start,
editor.document.lineAt(contentRange.end).range.end
);

const targets = this.getMatchesForRange(editor, textRange)
.filter(
(contentRange) =>
contentRange.end.isAfterOrEqual(start) &&
contentRange.start.isBeforeOrEqual(end)
)
.map((contentRange) => this.getTargetFromRange(target, contentRange));

if (targets.length === 0) {
throw new NoContainingScopeError(this.modifier.scopeType.type);
Expand All @@ -52,11 +54,10 @@ class RegexStageBase implements ModifierStage {
}

private getSingleTarget(target: Target): Target {
const { editor } = target;
const start = this.getMatchForPos(editor, target.contentRange.start).start;
const end = this.getMatchForPos(editor, target.contentRange.end).end;
const contentRange = new Range(start, end);
return this.getTargetFromRange(target, contentRange);
const { editor, contentRange } = target;
const start = this.getMatchForPos(editor, contentRange.start).start;
const end = this.getMatchForPos(editor, contentRange.end).end;
return this.getTargetFromRange(target, new Range(start, end));
}

private getTargetFromRange(target: Target, contentRange: Range): Target {
Expand All @@ -68,24 +69,25 @@ class RegexStageBase implements ModifierStage {
}

private getMatchForPos(editor: TextEditor, position: Position) {
const match = this.getMatchesForLine(editor, position.line).find((range) =>
range.contains(position)
const textRange =
this.textRange ?? editor.document.lineAt(position.line).range;
const match = this.getMatchesForRange(editor, textRange).find(
(contentRange) => contentRange.contains(position)
);
if (match == null) {
throw new NoContainingScopeError(this.modifier.scopeType.type);
}
return match;
}

private getMatchesForLine(editor: TextEditor, lineNum: number) {
const line = editor.document.lineAt(lineNum);
const result = [...line.text.matchAll(this.regex)].map(
private getMatchesForRange(editor: TextEditor, range: Range) {
const offset = editor.document.offsetAt(range.start);
const text = editor.document.getText(range);
const result = [...text.matchAll(this.regex)].map(
(match) =>
new Range(
lineNum,
match.index!,
lineNum,
match.index! + match[0].length
editor.document.positionAt(offset + match.index!),
editor.document.positionAt(offset + match.index! + match[0].length)
)
);
if (result == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
languageId: plaintext
command:
spokenForm: clear every member
version: 2
targets:
- type: primitive
modifiers:
- type: everyScope
scopeType: {type: member}
usePrePhraseSnapshot: true
action: {name: clearAndSetSelection}
initialState:
documentContents: aaa.bbb(ccc.ddd("hello"), eee).fff()
selections:
- anchor: {line: 0, character: 8}
active: {line: 0, character: 8}
marks: {}
finalState:
documentContents: aaa.bbb(, ).fff()
selections:
- anchor: {line: 0, character: 8}
active: {line: 0, character: 8}
- anchor: {line: 0, character: 10}
active: {line: 0, character: 10}
thatMark:
- anchor: {line: 0, character: 8}
active: {line: 0, character: 8}
- anchor: {line: 0, character: 10}
active: {line: 0, character: 10}
fullTargets: [{type: primitive, mark: {type: cursor}, modifiers: [{type: everyScope, scopeType: {type: member}}]}]
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
languageId: plaintext
command:
spokenForm: clear member air
version: 2
targets:
- type: primitive
modifiers:
- type: containingScope
scopeType: {type: member}
mark: {type: decoratedSymbol, symbolColor: default, character: a}
usePrePhraseSnapshot: true
action: {name: clearAndSetSelection}
initialState:
documentContents: aaa.bbb(ccc.ddd("hello"), eee).fff()
selections:
- anchor: {line: 0, character: 0}
active: {line: 0, character: 0}
marks:
default.a:
start: {line: 0, character: 0}
end: {line: 0, character: 3}
finalState:
documentContents: ""
selections:
- anchor: {line: 0, character: 0}
active: {line: 0, character: 0}
thatMark:
- anchor: {line: 0, character: 0}
active: {line: 0, character: 0}
fullTargets: [{type: primitive, mark: {type: decoratedSymbol, symbolColor: default, character: a}, modifiers: [{type: containingScope, scopeType: {type: member}}]}]
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
languageId: plaintext
command:
spokenForm: clear member cap
version: 2
targets:
- type: primitive
modifiers:
- type: containingScope
scopeType: {type: member}
mark: {type: decoratedSymbol, symbolColor: default, character: c}
usePrePhraseSnapshot: true
action: {name: clearAndSetSelection}
initialState:
documentContents: aaa.bbb(ccc.ddd("hello"), eee).fff()
selections:
- anchor: {line: 0, character: 0}
active: {line: 0, character: 0}
marks:
default.c:
start: {line: 0, character: 8}
end: {line: 0, character: 11}
finalState:
documentContents: aaa.bbb(, eee).fff()
selections:
- anchor: {line: 0, character: 8}
active: {line: 0, character: 8}
thatMark:
- anchor: {line: 0, character: 8}
active: {line: 0, character: 8}
fullTargets: [{type: primitive, mark: {type: decoratedSymbol, symbolColor: default, character: c}, modifiers: [{type: containingScope, scopeType: {type: member}}]}]
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
languageId: plaintext
command:
spokenForm: clear member each
version: 2
targets:
- type: primitive
modifiers:
- type: containingScope
scopeType: {type: member}
mark: {type: decoratedSymbol, symbolColor: default, character: e}
usePrePhraseSnapshot: true
action: {name: clearAndSetSelection}
initialState:
documentContents: aaa.bbb(ccc.ddd("hello"), eee).fff()
selections:
- anchor: {line: 0, character: 0}
active: {line: 0, character: 0}
marks:
default.e:
start: {line: 0, character: 26}
end: {line: 0, character: 29}
finalState:
documentContents: aaa.bbb(ccc.ddd("hello"), ).fff()
selections:
- anchor: {line: 0, character: 26}
active: {line: 0, character: 26}
thatMark:
- anchor: {line: 0, character: 26}
active: {line: 0, character: 26}
fullTargets: [{type: primitive, mark: {type: decoratedSymbol, symbolColor: default, character: e}, modifiers: [{type: containingScope, scopeType: {type: member}}]}]
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
languageId: plaintext
command:
spokenForm: clear member fine
version: 2
targets:
- type: primitive
modifiers:
- type: containingScope
scopeType: {type: member}
mark: {type: decoratedSymbol, symbolColor: default, character: f}
usePrePhraseSnapshot: true
action: {name: clearAndSetSelection}
initialState:
documentContents: aaa.bbb(ccc.ddd("hello"), eee).fff()
selections:
- anchor: {line: 0, character: 0}
active: {line: 0, character: 0}
marks:
default.f:
start: {line: 0, character: 31}
end: {line: 0, character: 34}
finalState:
documentContents: ""
selections:
- anchor: {line: 0, character: 0}
active: {line: 0, character: 0}
thatMark:
- anchor: {line: 0, character: 0}
active: {line: 0, character: 0}
fullTargets: [{type: primitive, mark: {type: decoratedSymbol, symbolColor: default, character: f}, modifiers: [{type: containingScope, scopeType: {type: member}}]}]
1 change: 1 addition & 0 deletions src/typings/targetDescriptor.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export type SimpleScopeTypeType =
| "ifStatement"
| "list"
| "map"
| "member"
| "name"
| "namedFunction"
| "regularExpression"
Expand Down