|
| 1 | +import { maxBy, minBy } from "lodash"; |
| 2 | +import { Position, Range, TextEditor } from "vscode"; |
| 3 | +import { getScopeHandler } from "."; |
| 4 | +import { |
| 5 | + Direction, |
| 6 | + OneOfScopeType, |
| 7 | +} from "../../../typings/targetDescriptor.types"; |
| 8 | +import { OutOfRangeError } from "../targetSequenceUtils"; |
| 9 | +import type { TargetScope } from "./scope.types"; |
| 10 | +import { ScopeHandler } from "./scopeHandler.types"; |
| 11 | + |
| 12 | +export default class OneOfScopeHandler implements ScopeHandler { |
| 13 | + private scopeHandlers: ScopeHandler[] = this.scopeType.scopeTypes.map( |
| 14 | + (scopeType) => { |
| 15 | + const handler = getScopeHandler(scopeType, this.languageId); |
| 16 | + if (handler == null) { |
| 17 | + throw new Error(`No available scope handler for '${scopeType.type}'`); |
| 18 | + } |
| 19 | + return handler; |
| 20 | + }, |
| 21 | + ); |
| 22 | + |
| 23 | + public iterationScopeType: OneOfScopeType = { |
| 24 | + type: "oneOf", |
| 25 | + scopeTypes: this.scopeHandlers.map( |
| 26 | + ({ iterationScopeType }) => iterationScopeType, |
| 27 | + ), |
| 28 | + }; |
| 29 | + |
| 30 | + constructor( |
| 31 | + public readonly scopeType: OneOfScopeType, |
| 32 | + private languageId: string, |
| 33 | + ) {} |
| 34 | + |
| 35 | + getScopesTouchingPosition( |
| 36 | + editor: TextEditor, |
| 37 | + position: Position, |
| 38 | + ancestorIndex?: number, |
| 39 | + ): TargetScope[] { |
| 40 | + if (ancestorIndex !== 0) { |
| 41 | + // FIXME: We could support this one, but it will be a bit of work. |
| 42 | + throw new Error("`grand` not yet supported for compound scopes."); |
| 43 | + } |
| 44 | + |
| 45 | + return keepOnlyBottomLevelScopes( |
| 46 | + this.scopeHandlers.flatMap((scopeHandler) => |
| 47 | + scopeHandler.getScopesTouchingPosition(editor, position, ancestorIndex), |
| 48 | + ), |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * We proceed as follows: |
| 54 | + * |
| 55 | + * 1. Get all scopes returned by |
| 56 | + * {@link ScopeHandler.getScopesOverlappingRange} from each of |
| 57 | + * {@link scopeHandlers}. |
| 58 | + * 2. If any of these scopes has a {@link TargetScope.domain|domain} that |
| 59 | + * terminates within {@link range}, return all such maximal scopes. |
| 60 | + * 3. Otherwise, return a list containing just the minimal scope containing |
| 61 | + * {@link range}. |
| 62 | + */ |
| 63 | + getScopesOverlappingRange(editor: TextEditor, range: Range): TargetScope[] { |
| 64 | + const candidateScopes = this.scopeHandlers.flatMap((scopeHandler) => |
| 65 | + scopeHandler.getScopesOverlappingRange(editor, range), |
| 66 | + ); |
| 67 | + |
| 68 | + const scopesTerminatingInRange = candidateScopes.filter( |
| 69 | + ({ domain }) => !domain.contains(range), |
| 70 | + ); |
| 71 | + |
| 72 | + return scopesTerminatingInRange.length > 0 |
| 73 | + ? keepOnlyTopLevelScopes(scopesTerminatingInRange) |
| 74 | + : keepOnlyBottomLevelScopes(candidateScopes); |
| 75 | + } |
| 76 | + |
| 77 | + getScopeRelativeToPosition( |
| 78 | + editor: TextEditor, |
| 79 | + position: Position, |
| 80 | + offset: number, |
| 81 | + direction: Direction, |
| 82 | + ): TargetScope { |
| 83 | + let currentPosition = position; |
| 84 | + let currentScope: TargetScope; |
| 85 | + |
| 86 | + if (this.scopeHandlers.length === 0) { |
| 87 | + throw new OutOfRangeError(); |
| 88 | + } |
| 89 | + |
| 90 | + for (let i = 0; i < offset; i++) { |
| 91 | + const candidateScopes = this.scopeHandlers.map((scopeHandler) => |
| 92 | + scopeHandler.getScopeRelativeToPosition( |
| 93 | + editor, |
| 94 | + currentPosition, |
| 95 | + 1, |
| 96 | + direction, |
| 97 | + ), |
| 98 | + ); |
| 99 | + |
| 100 | + currentScope = |
| 101 | + direction === "forward" |
| 102 | + ? minBy(candidateScopes, ({ domain: start }) => start)! |
| 103 | + : maxBy(candidateScopes, ({ domain: end }) => end)!; |
| 104 | + |
| 105 | + currentPosition = |
| 106 | + direction === "forward" |
| 107 | + ? currentScope.domain.end |
| 108 | + : currentScope.domain.start; |
| 109 | + } |
| 110 | + |
| 111 | + return currentScope!; |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +function keepOnlyTopLevelScopes(candidateScopes: TargetScope[]): TargetScope[] { |
| 116 | + return candidateScopes.filter( |
| 117 | + ({ domain }) => |
| 118 | + !candidateScopes.some(({ domain: otherDomain }) => |
| 119 | + otherDomain.contains(domain), |
| 120 | + ), |
| 121 | + ); |
| 122 | +} |
| 123 | + |
| 124 | +function keepOnlyBottomLevelScopes( |
| 125 | + candidateScopes: TargetScope[], |
| 126 | +): TargetScope[] { |
| 127 | + return candidateScopes.filter( |
| 128 | + ({ domain }) => |
| 129 | + !candidateScopes.some(({ domain: otherDomain }) => |
| 130 | + domain.contains(otherDomain), |
| 131 | + ), |
| 132 | + ); |
| 133 | +} |
0 commit comments