Skip to content

Implemented head tail modifier that works on multiple scope types #708

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 23 commits into from
Jun 20, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
206d882
Implemented head tail modifier that works on multiple scope types
pokey Jun 7, 2022
e99e1c9
Merge branch 'main' into headTail
pokey Jun 7, 2022
6451364
Merge branch 'main' into headTail
AndreasArvidsson Jun 13, 2022
2d0fe2b
Fixed test that broke in merge
AndreasArvidsson Jun 13, 2022
148b435
Merge branch 'main' into headTail
AndreasArvidsson Jun 15, 2022
4c3d73f
Updated head tail modifier
AndreasArvidsson Jun 16, 2022
372952e
Cleanup
AndreasArvidsson Jun 16, 2022
fa30879
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 16, 2022
406c5f9
More cleanup
AndreasArvidsson Jun 16, 2022
2fbc676
Merge branch 'headTail' of github.com:pokey/cursorless-vscode into he…
AndreasArvidsson Jun 16, 2022
2aa8222
More cleanup
AndreasArvidsson Jun 16, 2022
00d0ed5
More cleanup
AndreasArvidsson Jun 16, 2022
9277be8
Fixed line endings
AndreasArvidsson Jun 16, 2022
aaf3f89
Updated tests
AndreasArvidsson Jun 16, 2022
9f8eac1
Updated tests
AndreasArvidsson Jun 16, 2022
0124280
Head and tail modifiers swallow all following modifiers
AndreasArvidsson Jun 17, 2022
db90689
Updated tests
AndreasArvidsson Jun 17, 2022
55d641e
Merge branch 'main' into headTail
AndreasArvidsson Jun 17, 2022
31a77b7
Cleanup
AndreasArvidsson Jun 18, 2022
a97be06
Cleanup
AndreasArvidsson Jun 18, 2022
2f04384
Switch head tail stage to use token target instead of plane target
pokey Jun 20, 2022
075db77
Cleanup head tail stage
pokey Jun 20, 2022
fc8550f
Reinstate doc string
pokey Jun 20, 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
36 changes: 27 additions & 9 deletions src/processTargets/modifiers/HeadTailStage.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,40 @@
import { Position, Range, TextEditor } from "vscode";
import { Range, TextEditor } from "vscode";
import { Target } from "../../typings/target.types";
import {
HeadModifier,
TailModifier,
} from "../../typings/targetDescriptor.types";
import { ProcessedTargetsContext } from "../../typings/Types";
import { ModifierStage } from "../PipelineStages.types";
import TokenTarget from "../targets/TokenTarget";
import PlainTarget from "../targets/PlainTarget";
import { toLineTarget } from "./scopeTypeStages/LineStage";

abstract class HeadTailStage implements ModifierStage {
abstract update(editor: TextEditor, range: Range): Range;
abstract update(
editor: TextEditor,
previousRange: Range,
nextRange: Range
): Range;

constructor(private isReversed: boolean) {}

run(context: ProcessedTargetsContext, target: Target): Target[] {
const contentRange = this.update(target.editor, target.contentRange);
const { previousRange, nextRange } = (() => {
if (target.previousTarget != null) {
return {
previousRange: target.previousTarget.contentRange,
nextRange: target.contentRange,
};
}
return {
previousRange: target.contentRange,
nextRange: toLineTarget(target).contentRange,
};
})();

const contentRange = this.update(target.editor, previousRange, nextRange);
return [
new TokenTarget({
new PlainTarget({
editor: target.editor,
isReversed: this.isReversed,
contentRange,
Expand All @@ -30,8 +48,8 @@ export class HeadStage extends HeadTailStage {
super(true);
}

update(editor: TextEditor, range: Range) {
return new Range(new Position(range.start.line, 0), range.end);
update(editor: TextEditor, previousRange: Range, nextRange: Range) {
return new Range(nextRange.start, previousRange.end);
}
}

Expand All @@ -40,7 +58,7 @@ export class TailStage extends HeadTailStage {
super(false);
}

update(editor: TextEditor, range: Range) {
return new Range(range.start, editor.document.lineAt(range.end).range.end);
update(editor: TextEditor, previousRange: Range, nextRange: Range) {
return new Range(previousRange.start, nextRange.end);
}
}
1 change: 1 addition & 0 deletions src/processTargets/modifiers/OrdinalRangeSubTokenStage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ export default class OrdinalRangeSubTokenStage implements ModifierStage {
insertionDelimiter,
leadingDelimiterRange,
trailingDelimiterRange,
previousTarget: target,
}),
];
}
Expand Down
1 change: 1 addition & 0 deletions src/processTargets/modifiers/SurroundingPairStage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ function processedSurroundingPairTarget(
...pairInfo,
editor: target.editor,
isReversed: target.isReversed,
previousTarget: target,
}),
];
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ export default class implements ModifierStage {
) {}

run(context: ProcessedTargetsContext, target: Target): ScopeTypeTarget[] {
const isEveryScope = this.modifier.type === "everyScope";

const nodeMatcher = getNodeMatcher(
target.editor.document.languageId,
this.modifier.scopeType.type,
this.modifier.type === "everyScope"
isEveryScope
);

const node: SyntaxNode | null = context.getNodeAtLocation(
Expand Down Expand Up @@ -82,6 +84,7 @@ export default class implements ModifierStage {
delimiter: containingListDelimiter,
leadingDelimiterRange,
trailingDelimiterRange,
previousTarget: isEveryScope ? undefined : target,
});
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export default class implements ModifierStage {
editor: target.editor,
isReversed: target.isReversed,
contentRange: getDocumentRange(target.editor),
previousTarget: target,
}),
];
}
Expand Down
11 changes: 6 additions & 5 deletions src/processTargets/modifiers/scopeTypeStages/LineStage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,12 @@ export default class implements ModifierStage {
}

export function toLineTarget(target: Target): LineTarget {
return createLineTarget(
target.editor,
target.contentRange,
target.isReversed
);
return new LineTarget({
editor: target.editor,
isReversed: target.isReversed,
contentRange: fitRangeToLineContent(target.editor, target.contentRange),
previousTarget: target,
});
}

export function createLineTarget(
Expand Down
14 changes: 8 additions & 6 deletions src/processTargets/modifiers/scopeTypeStages/ParagraphStage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,16 @@ export default class implements ModifierStage {
}

getSingleTarget(target: Target): ParagraphTarget {
return this.getTargetFromRange(target);
const range = calculateRange(target);
return new ParagraphTarget({
editor: target.editor,
isReversed: target.isReversed,
contentRange: fitRangeToLineContent(target.editor, range),
previousTarget: target,
});
}

getTargetFromRange(target: Target, range?: Range): ParagraphTarget {
if (range == null) {
range = calculateRange(target);
}

getTargetFromRange(target: Target, range: Range): ParagraphTarget {
return new ParagraphTarget({
editor: target.editor,
isReversed: target.isReversed,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,13 @@ class RegexStage implements ModifierStage {
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);
return new ScopeTypeTarget({
scopeTypeType: this.modifier.scopeType.type,
editor: target.editor,
isReversed: target.isReversed,
contentRange,
previousTarget: target,
});
}

getTargetFromRange(target: Target, range: Range): ScopeTypeTarget {
Expand Down
6 changes: 6 additions & 0 deletions src/processTargets/targets/BaseTarget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface CommonTargetParameters {
readonly isReversed: boolean;
readonly contentRange: Range;
readonly thatTarget?: Target;
readonly previousTarget?: Target;
}

export interface CloneWithParameters {
Expand All @@ -35,6 +36,7 @@ export default abstract class BaseTarget implements Target {
isReversed: parameters.isReversed,
contentRange: parameters.contentRange,
thatTarget: parameters.thatTarget,
previousTarget: parameters.previousTarget,
};
}

Expand All @@ -51,6 +53,10 @@ export default abstract class BaseTarget implements Target {
: this;
}

get previousTarget(): Target | undefined {
return this.state.previousTarget;
}

get contentText(): string {
return this.editor.document.getText(this.contentRange);
}
Expand Down
1 change: 1 addition & 0 deletions src/processTargets/targets/SurroundingPairTarget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export default class SurroundingPairTarget extends BaseTarget {
editor: this.editor,
isReversed: this.isReversed,
contentRange: this.interiorRange_,
previousTarget: this.previousTarget,
}),
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@ finalState:
thatMark:
- anchor: {line: 0, character: 4}
active: {line: 0, character: 17}
- anchor: {line: 0, character: 4}
active: {line: 0, character: 17}
fullTargets: [{type: list, elements: [{type: primitive, mark: {type: decoratedSymbol, symbolColor: default, character: v}, selectionType: line, position: contents, insideOutsideType: inside, modifier: {type: identity}}, {type: primitive, mark: {type: decoratedSymbol, symbolColor: default, character: a}, selectionType: line, position: contents, insideOutsideType: inside, modifier: {type: identity}}]}]
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
languageId: plaintext
command:
spokenForm: clear head core
version: 2
targets:
- type: primitive
modifiers:
- {type: head}
- {type: interiorOnly}
usePrePhraseSnapshot: true
action: {name: clearAndSetSelection}
initialState:
documentContents: foo(bar baz)
selections:
- anchor: {line: 0, character: 10}
active: {line: 0, character: 10}
marks: {}
finalState:
documentContents: foo(z)
selections:
- anchor: {line: 0, character: 4}
active: {line: 0, character: 4}
thatMark:
- anchor: {line: 0, character: 4}
active: {line: 0, character: 4}
fullTargets: [{type: primitive, mark: {type: cursor}, modifiers: [{type: head}, {type: interiorOnly}]}]
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
languageId: typescript
command:
spokenForm: clear head funk
version: 2
targets:
- type: primitive
modifiers:
- {type: head}
- type: containingScope
scopeType: {type: namedFunction}
usePrePhraseSnapshot: true
action: {name: clearAndSetSelection}
initialState:
documentContents: |-
function whatever() {

}
selections:
- anchor: {line: 1, character: 4}
active: {line: 1, character: 4}
marks: {}
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: cursor}, modifiers: [{type: head}, {type: containingScope, scopeType: {type: namedFunction}}]}]
2 changes: 2 additions & 0 deletions src/typings/target.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ export interface Target {
/** Internal target that should be used for the that mark */
readonly thatTarget: Target;

readonly previousTarget: Target | undefined;

getInteriorStrict(): Target[];
getBoundaryStrict(): Target[];
/** The range of the delimiter before the content selection */
Expand Down