-
-
Notifications
You must be signed in to change notification settings - Fork 85
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
Initial work towards next-gen collectionItem #2599
Draft
pokey
wants to merge
8
commits into
main
Choose a base branch
from
pokey/initial-work-towards-next-gen-collectionitem
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.
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
065633a
Initial work towards next-gen collectionItem
pokey b5a8038
Fixed imports
AndreasArvidsson 923311f
Merge remote-tracking branch 'origin/main' into pokey/initial-work-to…
phillco 29fc3d5
import type fixes
phillco b850f34
Merge branch 'main' into pokey/initial-work-towards-next-gen-collecti…
AndreasArvidsson 6e7d574
Added create target scope
AndreasArvidsson 5e1a701
Use collection item scope handler
AndreasArvidsson b86b945
Merge branch 'main' into pokey/initial-work-towards-next-gen-collecti…
AndreasArvidsson 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 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
96 changes: 96 additions & 0 deletions
96
...sTargets/modifiers/scopeHandlers/CollectionItemScopeHandler/CollectionItemScopeHandler.ts
This file contains 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,96 @@ | ||
import type { Direction, Position, TextEditor } from "@cursorless/common"; | ||
import { Range, type ScopeType } from "@cursorless/common"; | ||
import type { LanguageDefinitions } from "../../../../languages/LanguageDefinitions"; | ||
import { ScopeTypeTarget } from "../../../targets"; | ||
import { BaseScopeHandler } from "../BaseScopeHandler"; | ||
import { compareTargetScopes } from "../compareTargetScopes"; | ||
import type { TargetScope } from "../scope.types"; | ||
import type { ScopeIteratorRequirements } from "../scopeHandler.types"; | ||
import { getDelimiterOccurrences } from "../SurroundingPairScopeHandler/getDelimiterOccurrences"; | ||
import { getIndividualDelimiters } from "../SurroundingPairScopeHandler/getIndividualDelimiters"; | ||
import type { | ||
CollectionItemOccurrence, | ||
IndividualDelimiter, | ||
IndividualSeparator, | ||
} from "../SurroundingPairScopeHandler/types"; | ||
import { getCollectionItemOccurrences } from "./getCollectionItemOccurrences"; | ||
|
||
export class CollectionItemScopeHandler extends BaseScopeHandler { | ||
public scopeType: ScopeType = { type: "collectionItem" }; | ||
|
||
public readonly iterationScopeType: ScopeType = { | ||
type: "oneOf", | ||
scopeTypes: [ | ||
{ type: "line" }, | ||
{ | ||
type: "surroundingPairInterior", | ||
delimiter: "any", | ||
}, | ||
], | ||
}; | ||
protected isHierarchical = true; | ||
|
||
constructor( | ||
private languageDefinitions: LanguageDefinitions, | ||
private languageId: string, | ||
) { | ||
super(); | ||
} | ||
|
||
*generateScopeCandidates( | ||
editor: TextEditor, | ||
position: Position, | ||
direction: Direction, | ||
_hints: ScopeIteratorRequirements, | ||
): Iterable<TargetScope> { | ||
const delimiterOccurrences = getDelimiterOccurrences( | ||
this.languageDefinitions.get(this.languageId), | ||
editor.document, | ||
this.getIndividualDelimiters(), | ||
); | ||
|
||
const surroundingPairs = getCollectionItemOccurrences(delimiterOccurrences); | ||
|
||
yield* surroundingPairs | ||
.map((pair) => createTargetScope(editor, pair)) | ||
.sort((a, b) => compareTargetScopes(direction, position, a, b)); | ||
} | ||
|
||
private getIndividualDelimiters(): ( | ||
| IndividualDelimiter | ||
| IndividualSeparator | ||
)[] { | ||
return [ | ||
...getIndividualDelimiters("collectionBoundary", this.languageId), | ||
{ side: "separator", text: "," }, | ||
]; | ||
} | ||
} | ||
|
||
function createTargetScope( | ||
editor: TextEditor, | ||
pair: CollectionItemOccurrence, | ||
): TargetScope { | ||
const contentRange = new Range( | ||
pair.openingDelimiterRange.start, | ||
pair.closingDelimiterRange.end, | ||
); | ||
return { | ||
editor, | ||
domain: contentRange, | ||
getTargets(isReversed) { | ||
return [ | ||
new ScopeTypeTarget({ | ||
scopeTypeType: "collectionItem", | ||
editor, | ||
isReversed, | ||
contentRange, | ||
insertionDelimiter: ", ", | ||
// leadingDelimiterRange: pair.leadingDelimiterRange, | ||
// trailingDelimiterRange: pair.trailingDelimiterRange, | ||
// removalRange: pair.removalRange, | ||
}), | ||
]; | ||
}, | ||
}; | ||
} |
151 changes: 151 additions & 0 deletions
151
...argets/modifiers/scopeHandlers/CollectionItemScopeHandler/getCollectionItemOccurrences.ts
This file contains 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,151 @@ | ||
import type { SimpleSurroundingPairName } from "@cursorless/common"; | ||
import { DefaultMap } from "@cursorless/common"; | ||
import type { | ||
CollectionItemOccurrence, | ||
DelimiterOccurrence, | ||
IndividualDelimiter, | ||
IndividualSeparator, | ||
} from "../SurroundingPairScopeHandler/types"; | ||
|
||
interface DelimiterEntry { | ||
openingDelimiter: DelimiterOccurrence<IndividualDelimiter>; | ||
separator?: DelimiterOccurrence<IndividualSeparator>; | ||
} | ||
|
||
/** | ||
* Given a list of occurrences of delimiters, returns a list of occurrences of | ||
* surrounding pairs by matching opening and closing delimiters. | ||
* | ||
* @param delimiterOccurrences A list of occurrences of delimiters | ||
* @returns A list of occurrences of surrounding pairs | ||
*/ | ||
export function getCollectionItemOccurrences( | ||
delimiterOccurrences: DelimiterOccurrence< | ||
IndividualDelimiter | IndividualSeparator | ||
>[], | ||
): CollectionItemOccurrence[] { | ||
const result: CollectionItemOccurrence[] = []; | ||
|
||
/** | ||
* A map from delimiter names to occurrences of the opening delimiter | ||
*/ | ||
const openingDelimiterOccurrences = new DefaultMap< | ||
SimpleSurroundingPairName, | ||
DelimiterEntry[] | ||
>(() => []); | ||
|
||
for (const occurrence of delimiterOccurrences) { | ||
const { delimiterInfo, isDisqualified, textFragmentRange, range } = | ||
occurrence; | ||
|
||
if (isDisqualified) { | ||
continue; | ||
} | ||
|
||
if (delimiterInfo.side === "separator") { | ||
const openingDelimiters = Array.from( | ||
openingDelimiterOccurrences.values(), | ||
).flat(); | ||
|
||
/** | ||
* A list of opening delimiters that are relevant to the current occurrence. | ||
* We exclude delimiters that are not in the same text fragment range as the | ||
* current occurrence. | ||
*/ | ||
const relevantOpeningDelimiters = openingDelimiters.filter( | ||
({ openingDelimiter }) => | ||
(textFragmentRange == null && | ||
openingDelimiter.textFragmentRange == null) || | ||
(textFragmentRange != null && | ||
openingDelimiter.textFragmentRange != null && | ||
openingDelimiter.textFragmentRange.isRangeEqual(textFragmentRange)), | ||
); | ||
|
||
relevantOpeningDelimiters.sort((a, b) => | ||
a.openingDelimiter.range.start.isBefore(b.openingDelimiter.range.start) | ||
? -1 | ||
: 1, | ||
); | ||
|
||
const lastOpeningDelimiter = relevantOpeningDelimiters.at(-1); | ||
|
||
if (lastOpeningDelimiter == null) { | ||
// TODO: in this case, we are yielding entries on a line with no delimiters | ||
throw new Error("Not implemented"); | ||
} | ||
|
||
result.push({ | ||
openingDelimiterRange: | ||
lastOpeningDelimiter.separator?.range ?? | ||
lastOpeningDelimiter.openingDelimiter.range, | ||
closingDelimiterRange: range, | ||
}); | ||
|
||
lastOpeningDelimiter.separator = | ||
occurrence as DelimiterOccurrence<IndividualSeparator>; | ||
|
||
continue; | ||
} | ||
|
||
const { side, delimiterName, isSingleLine } = delimiterInfo; | ||
|
||
let openingDelimiters = openingDelimiterOccurrences.get(delimiterName); | ||
|
||
if (isSingleLine) { | ||
// If single line, remove all opening delimiters that are not on the same line | ||
// as occurrence | ||
openingDelimiters = openingDelimiters.filter( | ||
(openingDelimiter) => | ||
openingDelimiter.openingDelimiter.range.start.line === | ||
range.start.line, | ||
); | ||
openingDelimiterOccurrences.set(delimiterName, openingDelimiters); | ||
} | ||
|
||
/** | ||
* A list of opening delimiters that are relevant to the current occurrence. | ||
* We exclude delimiters that are not in the same text fragment range as the | ||
* current occurrence. | ||
*/ | ||
const relevantOpeningDelimiters = openingDelimiters.filter( | ||
(openingDelimiter) => | ||
(textFragmentRange == null && | ||
openingDelimiter.openingDelimiter.textFragmentRange == null) || | ||
(textFragmentRange != null && | ||
openingDelimiter.openingDelimiter.textFragmentRange != null && | ||
openingDelimiter.openingDelimiter.textFragmentRange.isRangeEqual( | ||
textFragmentRange, | ||
)), | ||
); | ||
|
||
if ( | ||
side === "left" || | ||
(side === "unknown" && relevantOpeningDelimiters.length % 2 === 0) | ||
) { | ||
openingDelimiters.push({ | ||
openingDelimiter: | ||
occurrence as DelimiterOccurrence<IndividualDelimiter>, | ||
}); | ||
} else { | ||
const openingDelimiter = relevantOpeningDelimiters.at(-1); | ||
|
||
if (openingDelimiter == null) { | ||
continue; | ||
} | ||
|
||
openingDelimiters.splice( | ||
openingDelimiters.lastIndexOf(openingDelimiter), | ||
1, | ||
); | ||
|
||
result.push({ | ||
openingDelimiterRange: | ||
openingDelimiter.separator?.range ?? | ||
openingDelimiter.openingDelimiter.range, | ||
closingDelimiterRange: range, | ||
}); | ||
} | ||
} | ||
|
||
return result; | ||
} |
This file contains 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 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 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 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually this shouldn't be
oneOf
, it should be what I think we callfallback
, where it uses inside if it can find it and only uses line if it finds no containing inside