-
-
Notifications
You must be signed in to change notification settings - Fork 85
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
base: main
Are you sure you want to change the base?
Contiguous scope #2101
Changes from 28 commits
fdcd03e
f223d84
b549ca5
a71f017
da8d6ec
a3fa351
53351e8
d370690
48f97ee
e2ec5c8
d1020a8
02a295e
9d75ead
48c7a65
fa57ab2
c943542
c26d704
e0b6b5b
2988adb
c2f942b
34cbbb1
8b5a95f
bd86a64
5a905ea
6612818
2852e37
d3eb687
fe7e8e5
2f15dc6
0ccdcc6
fd93230
d898f26
ea5a542
66731e4
58bfb9a
b6ac60f
17b0507
6f0de5c
9f156a6
38ab206
883eb1e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
import { | ||
ContiguousScopeType, | ||
Direction, | ||
Position, | ||
Range, | ||
ScopeType, | ||
TextEditor, | ||
next, | ||
} from "@cursorless/common"; | ||
import { ScopeHandlerFactory } from "."; | ||
import { Target } from "../../../typings/target.types"; | ||
import { constructScopeRangeTarget } from "../constructScopeRangeTarget"; | ||
import { BaseScopeHandler } from "./BaseScopeHandler"; | ||
import type { TargetScope } from "./scope.types"; | ||
import type { | ||
CustomScopeType, | ||
ScopeHandler, | ||
ScopeIteratorRequirements, | ||
} 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, | ||
_hints: ScopeIteratorRequirements, | ||
): Iterable<TargetScope> { | ||
let targetRangeOpposite = next( | ||
generateTargetRangesInDirection( | ||
this.scopeHandler, | ||
editor, | ||
position, | ||
direction === "forward" ? "backward" : "forward", | ||
), | ||
); | ||
|
||
const targetRangesIter = generateTargetRangesInDirection( | ||
this.scopeHandler, | ||
editor, | ||
position, | ||
direction, | ||
); | ||
|
||
for (const targetRange of targetRangesIter) { | ||
if ( | ||
targetRangeOpposite != null && | ||
isAdjacent(targetRangeOpposite.proximal, targetRange.proximal) | ||
) { | ||
yield combineScopes(targetRangeOpposite.distal, targetRange.distal); | ||
targetRangeOpposite = undefined; | ||
} else { | ||
yield combineScopes(targetRange.proximal, targetRange.distal); | ||
} | ||
} | ||
} | ||
} | ||
|
||
function combineScopes(scope1: TargetScope, scope2: TargetScope): TargetScope { | ||
if (scope1.domain.isRangeEqual(scope2.domain)) { | ||
return scope1; | ||
} | ||
|
||
return { | ||
editor: scope1.editor, | ||
domain: scope1.domain.union(scope2.domain), | ||
getTargets: (isReversed) => { | ||
return constructScopeRangeTarget(isReversed, scope1, scope2); | ||
}, | ||
}; | ||
} | ||
|
||
function* generateTargetRangesInDirection( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not convinced this function is correct. Eg corner cases like single scope not adjacent to others that's last in the file. To me it looks like if you've already yielded anything, then the final yield after the loop body won't fire. Some proper unit tests for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually might make sense to hold off on unit tests for the following reasons:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update from discussion today:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
looking at these test cases, i think it should die There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds good. Any opinion on making single-line comments behave this way by default? (Ie auto expand to all comments that aren't separated by an empty line) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update from meet-up: let's do the following: (
(comment) @comment
(#match? @comment "^//")
(#contiguous! @comment)
)
(comment) @comment We both don't love this option or the option in this PR, but it's the best we can do with what we have today. Fwiw we would prefer something like (
(comment) @comment
($if
(#match? @comment "^//")
(#contiguous! @comment)
)
) but that's a bit out of scope here 😅 |
||
scopeHandler: ScopeHandler, | ||
editor: TextEditor, | ||
position: Position, | ||
direction: Direction, | ||
): Iterable<{ proximal: TargetScope; distal: TargetScope }> { | ||
let proximal, distal: TargetScope | undefined; | ||
|
||
const generator = scopeHandler.generateScopes(editor, position, direction, { | ||
allowAdjacentScopes: true, | ||
skipAncestorScopes: true, | ||
}); | ||
|
||
for (const scope of generator) { | ||
if (proximal == null) { | ||
proximal = scope; | ||
} | ||
|
||
if (distal != null) { | ||
if (!isAdjacent(distal, scope)) { | ||
yield { proximal, distal }; | ||
proximal = scope; | ||
} | ||
} | ||
|
||
distal = scope; | ||
} | ||
|
||
if (proximal != null && distal != null) { | ||
yield { proximal, distal }; | ||
} | ||
} | ||
|
||
function isAdjacent(scope1: TargetScope, scope2: TargetScope): boolean { | ||
if (scope1.domain.isRangeEqual(scope2.domain)) { | ||
return true; | ||
} | ||
|
||
const [startTarget, endTarget] = getTargetsInDocumentOrder( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This file is now general enough to be used with most scopes. If we wanted to we could remove a bunch of code here and rely on the fact that we only use it for comments. I could go either way. |
||
scope1.getTargets(false)[0], | ||
scope2.getTargets(false)[0], | ||
); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we want to throw an error if there's more than one target? Should we have a utility function There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reused our existing and shore single target. They should really never trigger with our current implementation so am fine with using an error message that might not be perfect. |
||
|
||
const leadingRange = | ||
startTarget.getTrailingDelimiterTarget()?.contentRange ?? | ||
startTarget.contentRange; | ||
const trailingRange = | ||
endTarget.getLeadingDelimiterTarget()?.contentRange ?? | ||
endTarget.contentRange; | ||
|
||
if (leadingRange.intersection(trailingRange) != null) { | ||
return true; | ||
} | ||
|
||
// Non line targets are excluded if they are separated by more than one line | ||
if ( | ||
!startTarget.isLine && | ||
trailingRange.start.line - leadingRange.end.line > 1 | ||
) { | ||
return false; | ||
} | ||
|
||
// Finally targets are excluded if there is non whitespace text between them | ||
const rangeBetween = new Range(leadingRange.end, trailingRange.start); | ||
const text = startTarget.editor.document.getText(rangeBetween); | ||
return /^\s*$/.test(text); | ||
} | ||
|
||
function getTargetsInDocumentOrder( | ||
target1: Target, | ||
target2: Target, | ||
): [Target, Target] { | ||
return target1.contentRange.start.isBefore(target2.contentRange.start) | ||
? [target1, target2] | ||
: [target2, target1]; | ||
} |
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} |
Uh oh!
There was an error while loading. Please reload this page.