Skip to content

Commit 95d4e61

Browse files
Show proper error message when trying to visualize pseudo scopes (#3290)
1 parent 3ea81d1 commit 95d4e61

5 files changed

Lines changed: 36 additions & 17 deletions

File tree

packages/app-vscode/src/createScopeVisualizer.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import type {
55
ScopeProvider,
66
ScopeType,
77
} from "@cursorless/lib-common";
8-
import { createVscodeScopeVisualizer } from "./ide/vscode/VSCodeScopeVisualizer";
8+
import { isPseudoScope } from "@cursorless/lib-common";
99
import type { VscodeScopeVisualizer } from "./ide/vscode/VSCodeScopeVisualizer";
10+
import { createVscodeScopeVisualizer } from "./ide/vscode/VSCodeScopeVisualizer";
1011
import type {
1112
ScopeVisualizer,
1213
ScopeVisualizerListener,
@@ -24,6 +25,12 @@ export function createScopeVisualizer(
2425

2526
return {
2627
start(scopeType: ScopeType, visualizationType: VisualizationType) {
28+
if (isPseudoScope(scopeType)) {
29+
throw new Error(
30+
`Can't visualize pseudo scopes like '${scopeType.type}'`,
31+
);
32+
}
33+
2734
scopeVisualizer?.dispose();
2835
scopeVisualizer = createVscodeScopeVisualizer(
2936
ide,

packages/lib-common/src/types/command/PartialTargetDescriptor.types.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -211,11 +211,7 @@ export const simpleScopeTypeTypes = [
211211
"interior",
212212
] as const;
213213

214-
export function isSimpleScopeType(
215-
scopeType: ScopeType,
216-
): scopeType is SimpleScopeType {
217-
return (simpleScopeTypeTypes as readonly string[]).includes(scopeType.type);
218-
}
214+
const simpleScopeTypeTypesSet = new Set(simpleScopeTypeTypes);
219215

220216
export type SimpleScopeTypeType = (typeof simpleScopeTypeTypes)[number];
221217

@@ -226,6 +222,16 @@ export const pseudoScopes = new Set<SimpleScopeTypeType>([
226222
"functionName",
227223
]);
228224

225+
export function isSimpleScopeType(
226+
scopeType: ScopeType,
227+
): scopeType is SimpleScopeType {
228+
return (simpleScopeTypeTypesSet as Set<string>).has(scopeType.type);
229+
}
230+
231+
export function isPseudoScope(scopeType: ScopeType): boolean {
232+
return isSimpleScopeType(scopeType) && pseudoScopes.has(scopeType.type);
233+
}
234+
229235
export interface SimpleScopeType {
230236
type: SimpleScopeTypeType;
231237
}

packages/lib-engine/src/processTargets/modifiers/scopeHandlers/ScopeHandlerFactoryImpl.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { pseudoScopes, UnsupportedScopeError } from "@cursorless/lib-common";
21
import type { IDE, ScopeType } from "@cursorless/lib-common";
2+
import { isPseudoScope, UnsupportedScopeError } from "@cursorless/lib-common";
33
import type { LanguageDefinitions } from "../../../languages/LanguageDefinitions";
44
import {
55
BoundedNonWhitespaceSequenceScopeHandler,
@@ -139,8 +139,8 @@ export class ScopeHandlerFactoryImpl implements ScopeHandlerFactory {
139139
return ConditionalScopeHandler.maybeCreate(this, scopeType, languageId);
140140
default:
141141
// Pseudoscopes are handled separately in their own modifiers.
142-
if (pseudoScopes.has(scopeType.type)) {
143-
throw new Error(`Unexpected scope type '${scopeType.type}'`);
142+
if (isPseudoScope(scopeType)) {
143+
throw new Error(`Unexpected pseudo scope type '${scopeType.type}'`);
144144
}
145145
return this.languageDefinitions
146146
.get(languageId)

packages/lib-engine/src/scopeProviders/ScopeInfoProvider.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type {
77
SurroundingPairScopeType,
88
} from "@cursorless/lib-common";
99
import {
10-
pseudoScopes,
10+
isPseudoScope,
1111
simpleScopeTypeTypes,
1212
surroundingPairNames,
1313
} from "@cursorless/lib-common";
@@ -67,11 +67,10 @@ export class ScopeInfoProvider {
6767
private updateScopeTypeInfos(): void {
6868
const scopeTypes: ScopeType[] = [
6969
...simpleScopeTypeTypes
70-
// Ignore instance pseudo-scope because it's not really a scope
71-
.filter((scopeTypeType) => !pseudoScopes.has(scopeTypeType))
72-
.map((scopeTypeType) => ({
73-
type: scopeTypeType,
74-
})),
70+
// Create simple scope types from simple scope type types
71+
.map((scopeTypeType) => ({ type: scopeTypeType }))
72+
// Ignore pseudo-scope because it's not really a scope
73+
.filter((scopeType) => !isPseudoScope(scopeType)),
7574

7675
...surroundingPairNames.map(
7776
(surroundingPairName): SurroundingPairScopeType => ({

packages/lib-tutorial/src/setupStep.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,15 @@ export async function setupStep(
4949
throw new Error("No current tutorial found");
5050
}
5151

52-
const { initialState: snapshot, languageId = "plaintext" } =
53-
currentTutorial.steps[state.stepNumber];
52+
const step = currentTutorial.steps[state.stepNumber];
53+
54+
if (step == null) {
55+
throw new Error(
56+
`No tutorial step found for step number ${state.stepNumber}`,
57+
);
58+
}
59+
60+
const { initialState: snapshot, languageId = "plaintext" } = step;
5461

5562
if (snapshot == null) {
5663
return { editor, highlightRanges: [] };

0 commit comments

Comments
 (0)