Skip to content

Commit ee69491

Browse files
Refactor utilities
1 parent 40e071a commit ee69491

File tree

3 files changed

+34
-27
lines changed

3 files changed

+34
-27
lines changed

packages/cursorless-org-docs/src/docs/user/languages/components/ScopeSupport.tsx

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import {
22
Range,
33
serializeScopeType,
4-
type PlaintextScopeSupportFacet,
5-
type ScopeSupportFacet,
64
type ScopeSupportFacetInfo,
75
type ScopeTypeType,
86
} from "@cursorless/common";
@@ -11,11 +9,16 @@ import React, { useState } from "react";
119
import { Code, type Highlight } from "./Code";
1210
import { H2, H3, H4 } from "./Header";
1311
import "./ScopeSupport.css";
14-
import type { Fixture, ScopeTests } from "./types";
15-
import { getFacetInfo, prettifyFacet, prettifyScopeType } from "./util";
12+
import type { FacetValue, Fixture, ScopeTests } from "./types";
13+
import {
14+
getFacetInfo,
15+
isScopeInternal,
16+
nameComparator,
17+
prettifyFacet,
18+
prettifyScopeType,
19+
} from "./util";
1620

1721
type RangeType = "content" | "removal";
18-
type FacetValue = ScopeSupportFacet | PlaintextScopeSupportFacet;
1922

2023
interface Scopes {
2124
public: Scope[];
@@ -222,7 +225,7 @@ function getScopeFixtures(scopeTests: ScopeTests, languageId: string): Scopes {
222225
languageIds.has(f.languageId),
223226
);
224227
const scopeMap: Partial<Record<ScopeTypeType, Scope>> = {};
225-
const facetMap: Partial<Record<string, Facet>> = {};
228+
const facetMap: Partial<Record<FacetValue, Facet>> = {};
226229

227230
for (const fixture of fixtures) {
228231
const info = getFacetInfo(fixture.languageId, fixture.facet);
@@ -240,8 +243,6 @@ function getScopeFixtures(scopeTests: ScopeTests, languageId: string): Scopes {
240243
};
241244
}
242245

243-
const facetKey = `${scopeTypeType}.${fixture.facet}`;
244-
245246
if (facetMap[fixture.facet] == null) {
246247
const facet = {
247248
facet: fixture.facet,
@@ -259,10 +260,10 @@ function getScopeFixtures(scopeTests: ScopeTests, languageId: string): Scopes {
259260
const result: Scopes = { public: [], internal: [] };
260261

261262
Object.values(scopeMap)
262-
.sort(comparator)
263+
.sort(nameComparator)
263264
.forEach((scope) => {
264-
scope.facets.sort(comparator);
265-
scope.facets.forEach((f) => f.fixtures.sort(comparator));
265+
scope.facets.sort(nameComparator);
266+
scope.facets.forEach((f) => f.fixtures.sort(nameComparator));
266267
if (isScopeInternal(scope.scopeTypeType)) {
267268
result.internal.push(scope);
268269
} else {
@@ -272,18 +273,3 @@ function getScopeFixtures(scopeTests: ScopeTests, languageId: string): Scopes {
272273

273274
return result;
274275
}
275-
276-
function comparator(a: { name: string }, b: { name: string }): number {
277-
return a.name.localeCompare(b.name);
278-
}
279-
280-
function isScopeInternal(scope: ScopeTypeType): boolean {
281-
switch (scope) {
282-
case "disqualifyDelimiter":
283-
case "pairDelimiter":
284-
case "textFragment":
285-
return true;
286-
default:
287-
return false;
288-
}
289-
}

packages/cursorless-org-docs/src/docs/user/languages/components/types.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@ import type {
33
PlaintextScopeSupportFacet,
44
} from "@cursorless/common";
55

6+
export type FacetValue = ScopeSupportFacet | PlaintextScopeSupportFacet;
7+
68
export interface ScopeTests {
79
imports: Record<string, string[]>;
810
fixtures: Fixture[];
911
}
1012

1113
export interface Fixture {
1214
name: string;
13-
facet: ScopeSupportFacet | PlaintextScopeSupportFacet;
15+
facet: FacetValue;
1416
languageId: string;
1517
code: string;
1618
scopes: Scope[];

packages/cursorless-org-docs/src/docs/user/languages/components/util.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
type ScopeSupportFacet,
99
type ScopeSupportFacetInfo,
1010
type ScopeType,
11+
type ScopeTypeType,
1112
type SimpleScopeTypeType,
1213
} from "@cursorless/common";
1314

@@ -56,3 +57,21 @@ export function getFacetInfo(
5657

5758
return facetInfo;
5859
}
60+
61+
export function nameComparator(
62+
a: { name: string },
63+
b: { name: string },
64+
): number {
65+
return a.name.localeCompare(b.name);
66+
}
67+
68+
export function isScopeInternal(scope: ScopeTypeType): boolean {
69+
switch (scope) {
70+
case "disqualifyDelimiter":
71+
case "pairDelimiter":
72+
case "textFragment":
73+
return true;
74+
default:
75+
return false;
76+
}
77+
}

0 commit comments

Comments
 (0)