|
| 1 | +import { |
| 2 | + Position, |
| 3 | + ScopeType, |
| 4 | + SimpleScopeTypeType, |
| 5 | + TextEditor, |
| 6 | + isEmptyIterable, |
| 7 | +} from "@cursorless/common"; |
| 8 | +import { LegacyLanguageId } from "../languages/LegacyLanguageId"; |
| 9 | +import { languageMatchers } from "../languages/getNodeMatcher"; |
| 10 | +import { ScopeHandlerFactory } from "../processTargets/modifiers/scopeHandlers/ScopeHandlerFactory"; |
| 11 | +import { ScopeHandler } from "../processTargets/modifiers/scopeHandlers/scopeHandler.types"; |
| 12 | +import { ScopeSupport } from "../api/ScopeProvider"; |
| 13 | + |
| 14 | +/** |
| 15 | + * Determines the level of support for a given scope type in a given editor. |
| 16 | + * This is primarily determined by the language id of the editor, though some |
| 17 | + * scopes are supported in all languages. |
| 18 | + */ |
| 19 | +export class ScopeSupportChecker { |
| 20 | + constructor(private scopeHandlerFactory: ScopeHandlerFactory) { |
| 21 | + this.getScopeSupport = this.getScopeSupport.bind(this); |
| 22 | + this.getIterationScopeSupport = this.getIterationScopeSupport.bind(this); |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * Determine the level of support for {@link scopeType} in {@link editor}, as |
| 27 | + * determined by its language id. |
| 28 | + * @param editor The editor to check |
| 29 | + * @param scopeType The scope type to check |
| 30 | + * @returns The level of support for {@link scopeType} in {@link editor} |
| 31 | + */ |
| 32 | + getScopeSupport(editor: TextEditor, scopeType: ScopeType): ScopeSupport { |
| 33 | + const { languageId } = editor.document; |
| 34 | + const scopeHandler = this.scopeHandlerFactory.create(scopeType, languageId); |
| 35 | + |
| 36 | + if (scopeHandler == null) { |
| 37 | + return getLegacyScopeSupport(languageId, scopeType); |
| 38 | + } |
| 39 | + |
| 40 | + return editorContainsScope(editor, scopeHandler) |
| 41 | + ? ScopeSupport.supportedAndPresentInEditor |
| 42 | + : ScopeSupport.supportedButNotPresentInEditor; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Determine the level of support for the iteration scope of {@link scopeType} |
| 47 | + * in {@link editor}, as determined by its language id. |
| 48 | + * @param editor The editor to check |
| 49 | + * @param scopeType The scope type to check |
| 50 | + * @returns The level of support for the iteration scope of {@link scopeType} |
| 51 | + * in {@link editor} |
| 52 | + */ |
| 53 | + getIterationScopeSupport( |
| 54 | + editor: TextEditor, |
| 55 | + scopeType: ScopeType, |
| 56 | + ): ScopeSupport { |
| 57 | + const { languageId } = editor.document; |
| 58 | + const scopeHandler = this.scopeHandlerFactory.create(scopeType, languageId); |
| 59 | + |
| 60 | + if (scopeHandler == null) { |
| 61 | + return getLegacyScopeSupport(languageId, scopeType); |
| 62 | + } |
| 63 | + |
| 64 | + const iterationScopeHandler = this.scopeHandlerFactory.create( |
| 65 | + scopeHandler.iterationScopeType, |
| 66 | + languageId, |
| 67 | + ); |
| 68 | + |
| 69 | + if (iterationScopeHandler == null) { |
| 70 | + return ScopeSupport.unsupported; |
| 71 | + } |
| 72 | + |
| 73 | + return editorContainsScope(editor, iterationScopeHandler) |
| 74 | + ? ScopeSupport.supportedAndPresentInEditor |
| 75 | + : ScopeSupport.supportedButNotPresentInEditor; |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +function editorContainsScope( |
| 80 | + editor: TextEditor, |
| 81 | + scopeHandler: ScopeHandler, |
| 82 | +): boolean { |
| 83 | + return !isEmptyIterable( |
| 84 | + scopeHandler.generateScopes(editor, new Position(0, 0), "forward"), |
| 85 | + ); |
| 86 | +} |
| 87 | + |
| 88 | +function getLegacyScopeSupport( |
| 89 | + languageId: string, |
| 90 | + scopeType: ScopeType, |
| 91 | +): ScopeSupport { |
| 92 | + switch (scopeType.type) { |
| 93 | + case "boundedNonWhitespaceSequence": |
| 94 | + case "surroundingPair": |
| 95 | + return ScopeSupport.supportedLegacy; |
| 96 | + case "notebookCell": |
| 97 | + // FIXME: What to do here |
| 98 | + return ScopeSupport.unsupported; |
| 99 | + default: |
| 100 | + if ( |
| 101 | + languageMatchers[languageId as LegacyLanguageId]?.[ |
| 102 | + scopeType.type as SimpleScopeTypeType |
| 103 | + ] != null |
| 104 | + ) { |
| 105 | + return ScopeSupport.supportedLegacy; |
| 106 | + } |
| 107 | + |
| 108 | + return ScopeSupport.unsupported; |
| 109 | + } |
| 110 | +} |
0 commit comments