Skip to content

Add ScopeSupportChecker #1652

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

Merged
merged 2 commits into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions packages/common/src/util/itertools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,18 @@ export function partition<T, U>(
}
return [first, second];
}

/**
* Returns `true` if the given iterable is empty, `false` otherwise
*
* From https://github.com/sindresorhus/is-empty-iterable/blob/12d3b4f966170d9d85a2067f5326668d5bb910a0/index.js
* @param iterable The iterable to check
* @returns `true` if the iterable is empty, `false` otherwise
*/
export function isEmptyIterable(iterable: Iterable<unknown>): boolean {
for (const _ of iterable) {
return false;
}

return true;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import {
Position,
ScopeType,
SimpleScopeTypeType,
TextEditor,
isEmptyIterable,
} from "@cursorless/common";
import { LegacyLanguageId } from "../languages/LegacyLanguageId";
import { languageMatchers } from "../languages/getNodeMatcher";
import { ScopeHandlerFactory } from "../processTargets/modifiers/scopeHandlers/ScopeHandlerFactory";
import { ScopeHandler } from "../processTargets/modifiers/scopeHandlers/scopeHandler.types";
import { ScopeSupport } from "../api/ScopeProvider";

/**
* Determines the level of support for a given scope type in a given editor.
* This is primarily determined by the language id of the editor, though some
* scopes are supported in all languages.
*/
export class ScopeSupportChecker {
constructor(private scopeHandlerFactory: ScopeHandlerFactory) {
this.getScopeSupport = this.getScopeSupport.bind(this);
this.getIterationScopeSupport = this.getIterationScopeSupport.bind(this);
}

/**
* Determine the level of support for {@link scopeType} in {@link editor}, as
* determined by its language id.
* @param editor The editor to check
* @param scopeType The scope type to check
* @returns The level of support for {@link scopeType} in {@link editor}
*/
getScopeSupport(editor: TextEditor, scopeType: ScopeType): ScopeSupport {
const { languageId } = editor.document;
const scopeHandler = this.scopeHandlerFactory.create(scopeType, languageId);

if (scopeHandler == null) {
return getLegacyScopeSupport(languageId, scopeType);
}

return editorContainsScope(editor, scopeHandler)
? ScopeSupport.supportedAndPresentInEditor
: ScopeSupport.supportedButNotPresentInEditor;
}

/**
* Determine the level of support for the iteration scope of {@link scopeType}
* in {@link editor}, as determined by its language id.
* @param editor The editor to check
* @param scopeType The scope type to check
* @returns The level of support for the iteration scope of {@link scopeType}
* in {@link editor}
*/
getIterationScopeSupport(
editor: TextEditor,
scopeType: ScopeType,
): ScopeSupport {
const { languageId } = editor.document;
const scopeHandler = this.scopeHandlerFactory.create(scopeType, languageId);

if (scopeHandler == null) {
return getLegacyScopeSupport(languageId, scopeType);
}

const iterationScopeHandler = this.scopeHandlerFactory.create(
scopeHandler.iterationScopeType,
languageId,
);

if (iterationScopeHandler == null) {
return ScopeSupport.unsupported;
}

return editorContainsScope(editor, iterationScopeHandler)
? ScopeSupport.supportedAndPresentInEditor
: ScopeSupport.supportedButNotPresentInEditor;
}
}

function editorContainsScope(
editor: TextEditor,
scopeHandler: ScopeHandler,
): boolean {
return !isEmptyIterable(
scopeHandler.generateScopes(editor, new Position(0, 0), "forward"),
);
}

function getLegacyScopeSupport(
languageId: string,
scopeType: ScopeType,
): ScopeSupport {
switch (scopeType.type) {
case "boundedNonWhitespaceSequence":
case "surroundingPair":
return ScopeSupport.supportedLegacy;
case "notebookCell":
// FIXME: What to do here
return ScopeSupport.unsupported;
default:
if (
languageMatchers[languageId as LegacyLanguageId]?.[
scopeType.type as SimpleScopeTypeType
] != null
) {
return ScopeSupport.supportedLegacy;
}

return ScopeSupport.unsupported;
}
}
29 changes: 29 additions & 0 deletions packages/cursorless-engine/src/api/ScopeProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,28 @@ export interface ScopeProvider {
callback: IterationScopeChangeEventCallback,
config: IterationScopeRangeConfig,
) => Disposable;

/**
* Determine the level of support for {@link scopeType} in {@link editor}, as
* determined by its language id.
* @param editor The editor to check
* @param scopeType The scope type to check
* @returns The level of support for {@link scopeType} in {@link editor}
*/
getScopeSupport: (editor: TextEditor, scopeType: ScopeType) => ScopeSupport;

/**
* Determine the level of support for the iteration scope of {@link scopeType}
* in {@link editor}, as determined by its language id.
* @param editor The editor to check
* @param scopeType The scope type to check
* @returns The level of support for the iteration scope of {@link scopeType}
* in {@link editor}
*/
getIterationScopeSupport: (
editor: TextEditor,
scopeType: ScopeType,
) => ScopeSupport;
}

interface ScopeRangeConfigBase {
Expand Down Expand Up @@ -131,3 +153,10 @@ export interface IterationScopeRanges {
targets?: TargetRanges[];
}[];
}

export enum ScopeSupport {
supportedAndPresentInEditor,
supportedButNotPresentInEditor,
supportedLegacy,
unsupported,
}
4 changes: 4 additions & 0 deletions packages/cursorless-engine/src/cursorlessEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { StoredTargetMap, TestCaseRecorder, TreeSitter } from ".";
import { CursorlessEngine } from "./api/CursorlessEngineApi";
import { ScopeProvider } from "./api/ScopeProvider";
import { ScopeRangeProvider } from "./ScopeVisualizer/ScopeRangeProvider";
import { ScopeSupportChecker } from "./ScopeVisualizer/ScopeSupportChecker";
import { Debug } from "./core/Debug";
import { HatTokenMapImpl } from "./core/HatTokenMapImpl";
import { Snippets } from "./core/Snippets";
Expand Down Expand Up @@ -101,12 +102,15 @@ function createScopeProvider(
);

const rangeWatcher = new ScopeRangeWatcher(rangeProvider);
const supportChecker = new ScopeSupportChecker(scopeHandlerFactory);

return {
provideScopeRanges: rangeProvider.provideScopeRanges,
provideIterationScopeRanges: rangeProvider.provideIterationScopeRanges,
onDidChangeScopeRanges: rangeWatcher.onDidChangeScopeRanges,
onDidChangeIterationScopeRanges:
rangeWatcher.onDidChangeIterationScopeRanges,
getScopeSupport: supportChecker.getScopeSupport,
getIterationScopeSupport: supportChecker.getIterationScopeSupport,
};
}