-
-
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
base: main
Are you sure you want to change the base?
Contiguous scope #2101
Changes from 21 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,209 @@ | ||
import { | ||
ContiguousScopeType, | ||
Direction, | ||
Position, | ||
Range, | ||
ScopeType, | ||
TextEditor, | ||
next, | ||
} from "@cursorless/common"; | ||
import { ScopeHandlerFactory } from "."; | ||
import type { Target } from "../../../typings/target.types"; | ||
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> { | ||
return direction === "backward" | ||
? this.generateScopeCandidatesBackward(editor, position) | ||
: this.generateScopeCandidatesForward(editor, position); | ||
} | ||
|
||
private *generateScopeCandidatesBackward( | ||
editor: TextEditor, | ||
position: Position, | ||
): Iterable<TargetScope> { | ||
let targetRangeForward = next( | ||
generateTargetRangesInDirection( | ||
this.scopeHandler, | ||
editor, | ||
position, | ||
"forward", | ||
), | ||
); | ||
|
||
const targetRangesBackwardIter = generateTargetRangesInDirection( | ||
this.scopeHandler, | ||
editor, | ||
position, | ||
"backward", | ||
); | ||
|
||
for (const targetRange of targetRangesBackwardIter) { | ||
if ( | ||
targetRangeForward != null && | ||
isAdjacent(targetRange[1], targetRangeForward[0]) | ||
) { | ||
yield targetsToScope(targetRange[0], targetRangeForward[1]); | ||
targetRangeForward = undefined; | ||
} else { | ||
yield targetsToScope(...targetRange); | ||
} | ||
} | ||
} | ||
|
||
private *generateScopeCandidatesForward( | ||
editor: TextEditor, | ||
position: Position, | ||
): Iterable<TargetScope> { | ||
let targetRangeBackward = next( | ||
generateTargetRangesInDirection( | ||
this.scopeHandler, | ||
editor, | ||
position, | ||
"backward", | ||
), | ||
); | ||
|
||
const targetRangesForwardIter = generateTargetRangesInDirection( | ||
this.scopeHandler, | ||
editor, | ||
position, | ||
"forward", | ||
); | ||
|
||
for (const targetRange of targetRangesForwardIter) { | ||
if ( | ||
targetRangeBackward != null && | ||
isAdjacent(targetRangeBackward[1], targetRange[0]) | ||
) { | ||
yield targetsToScope(targetRangeBackward[0], targetRange[1]); | ||
targetRangeBackward = undefined; | ||
} else { | ||
yield targetsToScope(...targetRange); | ||
} | ||
} | ||
} | ||
} | ||
|
||
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* 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<[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); | ||
} |
Uh oh!
There was an error while loading. Please reload this page.