-
-
Notifications
You must be signed in to change notification settings - Fork 89
Contiguous scope #2101
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
Draft
AndreasArvidsson
wants to merge
41
commits into
main
Choose a base branch
from
contiguous_scope
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+604
−24
Draft
Contiguous scope #2101
Changes from 10 commits
Commits
Show all changes
41 commits
Select commit
Hold shift + click to select a range
fdcd03e
Started working on contiguous scope modifier
AndreasArvidsson f223d84
Merge branch 'main' into contiguous_scope
AndreasArvidsson b549ca5
more work
AndreasArvidsson a71f017
Change continuous scope modifier into continuous scope type
AndreasArvidsson da8d6ec
Added tests
AndreasArvidsson a3fa351
[pre-commit.ci lite] apply automatic fixes
pre-commit-ci-lite[bot] 53351e8
Moved tests
AndreasArvidsson d370690
Added comment
AndreasArvidsson 48f97ee
cleanup
AndreasArvidsson e2ec5c8
more cleanup
AndreasArvidsson d1020a8
Update packages/cursorless-engine/src/processTargets/modifiers/scopeH…
AndreasArvidsson 02a295e
Import
AndreasArvidsson 9d75ead
Update packages/cursorless-engine/src/processTargets/modifiers/scopeH…
AndreasArvidsson 48c7a65
Update packages/cursorless-engine/src/processTargets/modifiers/scopeH…
AndreasArvidsson fa57ab2
Update packages/cursorless-engine/src/processTargets/modifiers/scopeH…
AndreasArvidsson c943542
Update packages/cursorless-engine/src/processTargets/modifiers/scopeH…
AndreasArvidsson c26d704
Update packages/cursorless-engine/src/processTargets/modifiers/scopeH…
AndreasArvidsson e0b6b5b
[pre-commit.ci lite] apply automatic fixes
pre-commit-ci-lite[bot] 2988adb
rename
AndreasArvidsson c2f942b
Merge
AndreasArvidsson 34cbbb1
update
AndreasArvidsson 8b5a95f
Use proximal and distal
AndreasArvidsson bd86a64
Continues target
AndreasArvidsson 5a905ea
Added tests
AndreasArvidsson 6612818
cleanup
AndreasArvidsson 2852e37
Merge branch 'main' into contiguous_scope
AndreasArvidsson d3eb687
clean up
AndreasArvidsson fe7e8e5
Merge branch 'contiguous_scope' of github.com:cursorless-dev/cursorle…
AndreasArvidsson 2f15dc6
Only do contiguous for comments
AndreasArvidsson 0ccdcc6
cleanup
AndreasArvidsson fd93230
Merge branch 'main' into contiguous_scope
AndreasArvidsson d898f26
cleanup
AndreasArvidsson ea5a542
Update test
AndreasArvidsson 66731e4
Added predicate
AndreasArvidsson 58bfb9a
Merge branch 'main' into contiguous_scope
AndreasArvidsson b6ac60f
Added comment facets
AndreasArvidsson 17b0507
Remove pattern argument from contiguous predicate
AndreasArvidsson 6f0de5c
Merge branch 'main' into contiguous_scope
AndreasArvidsson 9f156a6
Update scope fixtures
AndreasArvidsson 38ab206
Merge branch 'main' into contiguous_scope
AndreasArvidsson 883eb1e
[pre-commit.ci lite] apply automatic fixes
pre-commit-ci-lite[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
191 changes: 191 additions & 0 deletions
191
...es/cursorless-engine/src/processTargets/modifiers/scopeHandlers/ContiguousScopeHandler.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
import { | ||
ContiguousScopeType, | ||
Direction, | ||
Position, | ||
Range, | ||
ScopeType, | ||
TextEditor, | ||
next, | ||
} from "@cursorless/common"; | ||
import { ScopeHandlerFactory } from "."; | ||
import { Target } from "../../../typings/target.types"; | ||
import { BaseScopeHandler } from "./BaseScopeHandler"; | ||
import type { TargetScope } from "./scope.types"; | ||
import { CustomScopeType, ScopeHandler } from "./scopeHandler.types"; | ||
|
||
export class ContiguousScopeHandler extends BaseScopeHandler { | ||
protected readonly isHierarchical = false; | ||
private readonly scopeHandler: ScopeHandler; | ||
|
||
constructor( | ||
private scopeHandlerFactory: ScopeHandlerFactory, | ||
public scopeType: ContiguousScopeType, | ||
languageId: string, | ||
) { | ||
super(); | ||
const handler = scopeHandlerFactory.create(scopeType.scopeType, languageId); | ||
if (handler == null) { | ||
throw new Error( | ||
`No available scope handler for '${scopeType.scopeType.type}'`, | ||
); | ||
} | ||
this.scopeHandler = handler; | ||
} | ||
|
||
get iterationScopeType(): ScopeType | CustomScopeType { | ||
return this.scopeHandler.iterationScopeType; | ||
} | ||
|
||
generateScopeCandidates( | ||
editor: TextEditor, | ||
position: Position, | ||
direction: Direction, | ||
): Iterable<TargetScope> { | ||
return direction === "backward" | ||
? this.generateScopeCandidatesBackward(editor, position) | ||
: this.generateScopeCandidatesForward(editor, position); | ||
} | ||
|
||
*generateScopeCandidatesBackward( | ||
AndreasArvidsson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
editor: TextEditor, | ||
position: Position, | ||
): Iterable<TargetScope> { | ||
let targetsForward = next( | ||
getTargetsInDirection(this.scopeHandler, editor, position, "forward"), | ||
); | ||
AndreasArvidsson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const targetsBackward = getTargetsInDirection( | ||
this.scopeHandler, | ||
editor, | ||
position, | ||
"backward", | ||
); | ||
AndreasArvidsson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
for (const targets of targetsBackward) { | ||
AndreasArvidsson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (targetsForward != null && isAdjacent(targets[1], targetsForward[0])) { | ||
AndreasArvidsson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
yield targetsToScope(targets[0], targetsForward[1]); | ||
targetsForward = undefined; | ||
} else { | ||
yield targetsToScope(...targets); | ||
} | ||
} | ||
} | ||
|
||
*generateScopeCandidatesForward( | ||
AndreasArvidsson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
editor: TextEditor, | ||
position: Position, | ||
): Iterable<TargetScope> { | ||
let targetsBackward = next( | ||
getTargetsInDirection(this.scopeHandler, editor, position, "backward"), | ||
); | ||
|
||
const targetsForward = getTargetsInDirection( | ||
this.scopeHandler, | ||
editor, | ||
position, | ||
"forward", | ||
); | ||
|
||
for (const targets of targetsForward) { | ||
if ( | ||
targetsBackward != null && | ||
isAdjacent(targetsBackward[1], targets[0]) | ||
) { | ||
yield targetsToScope(targetsBackward[0], targets[1]); | ||
targetsBackward = undefined; | ||
} else { | ||
yield targetsToScope(...targets); | ||
} | ||
} | ||
} | ||
} | ||
|
||
function targetsToScope( | ||
leadingTarget: Target, | ||
trailingTarget: Target, | ||
): TargetScope { | ||
if (leadingTarget.contentRange.isRangeEqual(trailingTarget.contentRange)) { | ||
return { | ||
editor: leadingTarget.editor, | ||
domain: leadingTarget.contentRange, | ||
getTargets: () => [leadingTarget], | ||
}; | ||
} | ||
AndreasArvidsson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const range = leadingTarget.contentRange.union(trailingTarget.contentRange); | ||
return { | ||
editor: leadingTarget.editor, | ||
domain: range, | ||
getTargets: () => [leadingTarget.withContentRange(range)], | ||
AndreasArvidsson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
} | ||
|
||
function* getTargetsInDirection( | ||
scopeHandler: ScopeHandler, | ||
editor: TextEditor, | ||
position: Position, | ||
direction: Direction, | ||
): Iterable<[Target, Target]> { | ||
const isForward = direction === "forward"; | ||
let first, last: Target | undefined; | ||
AndreasArvidsson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const generator = scopeHandler.generateScopes(editor, position, direction, { | ||
allowAdjacentScopes: true, | ||
skipAncestorScopes: true, | ||
}); | ||
|
||
for (const scope of generator) { | ||
for (const target of scope.getTargets(false)) { | ||
if (first == null) { | ||
first = target; | ||
} | ||
|
||
if (last != null) { | ||
const [leadingTarget, trailingTarget] = isForward | ||
? [last, target] | ||
: [target, last]; | ||
AndreasArvidsson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (!isAdjacent(leadingTarget, trailingTarget)) { | ||
yield isForward ? [first, last] : [last, first]; | ||
first = target; | ||
} | ||
} | ||
|
||
last = target; | ||
} | ||
} | ||
|
||
if (first != null && last != null) { | ||
yield isForward ? [first, last] : [last, first]; | ||
} | ||
} | ||
|
||
function isAdjacent(leadingTarget: Target, trailingTarget: Target): boolean { | ||
AndreasArvidsson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if ( | ||
leadingTarget.contentRange.intersection(trailingTarget.contentRange) != null | ||
) { | ||
return true; | ||
} | ||
|
||
const leadingRange = | ||
leadingTarget.getTrailingDelimiterTarget()?.contentRange ?? | ||
leadingTarget.contentRange; | ||
const trailingRange = | ||
trailingTarget.getLeadingDelimiterTarget()?.contentRange ?? | ||
trailingTarget.contentRange; | ||
|
||
if (leadingRange.intersection(trailingRange) != null) { | ||
return true; | ||
} | ||
|
||
if ( | ||
!leadingTarget.isLine && | ||
trailingRange.start.line - leadingRange.end.line > 1 | ||
) { | ||
return false; | ||
} | ||
|
||
const rangeBetween = new Range(leadingRange.end, trailingRange.start); | ||
const text = leadingTarget.editor.document.getText(rangeBetween); | ||
return /^\s*$/.test(text); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
...ges/cursorless-vscode-e2e/src/suite/fixtures/recorded/contiguousScopes/changeFatBlock.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
languageId: plaintext | ||
command: | ||
version: 6 | ||
spokenForm: change fat block | ||
action: | ||
name: clearAndSetSelection | ||
target: | ||
type: primitive | ||
modifiers: | ||
- type: containingScope | ||
scopeType: | ||
type: contiguous | ||
scopeType: {type: paragraph} | ||
usePrePhraseSnapshot: true | ||
initialState: | ||
documentContents: |- | ||
aaa | ||
aaa | ||
|
||
bbb | ||
selections: | ||
- anchor: {line: 0, character: 0} | ||
active: {line: 0, character: 0} | ||
marks: {} | ||
finalState: | ||
documentContents: "" | ||
selections: | ||
- anchor: {line: 0, character: 0} | ||
active: {line: 0, character: 0} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.