Skip to content

Commit afab431

Browse files
Replace ide.singleton with constructor injection (#3212)
1 parent 00704cf commit afab431

127 files changed

Lines changed: 923 additions & 773 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

packages/common/src/ide/PassthroughIDEBase.ts renamed to packages/common/src/ide/PassthroughIDE.ts

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import type { KeyValueStore } from "./types/KeyValueStore";
2222
import type { Messages } from "./types/Messages";
2323
import type { QuickPickOptions } from "./types/QuickPickOptions";
2424

25-
export default class PassthroughIDEBase implements IDE {
25+
export class PassthroughIDE implements IDE {
2626
configuration: Configuration;
2727
keyValueStore: KeyValueStore;
2828
clipboard: Clipboard;
@@ -37,6 +37,15 @@ export default class PassthroughIDEBase implements IDE {
3737
this.capabilities = original.capabilities;
3838
}
3939

40+
setIde(original: IDE) {
41+
this.original = original;
42+
this.configuration = original.configuration;
43+
this.keyValueStore = original.keyValueStore;
44+
this.clipboard = original.clipboard;
45+
this.messages = original.messages;
46+
this.capabilities = original.capabilities;
47+
}
48+
4049
flashRanges(flashDescriptors: FlashDescriptor[]): Promise<void> {
4150
return this.original.flashRanges(flashDescriptors);
4251
}
@@ -112,76 +121,76 @@ export default class PassthroughIDEBase implements IDE {
112121
);
113122
}
114123

115-
public get activeTextEditor(): TextEditor | undefined {
124+
get activeTextEditor(): TextEditor | undefined {
116125
return this.original.activeTextEditor;
117126
}
118127

119-
public get activeEditableTextEditor(): EditableTextEditor | undefined {
128+
get activeEditableTextEditor(): EditableTextEditor | undefined {
120129
return this.original.activeEditableTextEditor;
121130
}
122131

123-
public get visibleTextEditors(): TextEditor[] {
132+
get visibleTextEditors(): TextEditor[] {
124133
return this.original.visibleTextEditors;
125134
}
126135

127-
public get visibleNotebookEditors(): NotebookEditor[] {
136+
get visibleNotebookEditors(): NotebookEditor[] {
128137
return this.original.visibleNotebookEditors;
129138
}
130139

131-
public get cursorlessVersion(): string {
140+
get cursorlessVersion(): string {
132141
return this.original.cursorlessVersion;
133142
}
134143

135-
public get assetsRoot(): string {
144+
get assetsRoot(): string {
136145
return this.original.assetsRoot;
137146
}
138147

139-
public get runMode(): RunMode {
148+
get runMode(): RunMode {
140149
return this.original.runMode;
141150
}
142151

143-
public get workspaceFolders(): readonly WorkspaceFolder[] | undefined {
152+
get workspaceFolders(): readonly WorkspaceFolder[] | undefined {
144153
return this.original.workspaceFolders;
145154
}
146155

147-
public findInDocument(query: string, editor?: TextEditor): Promise<void> {
156+
findInDocument(query: string, editor?: TextEditor): Promise<void> {
148157
return this.original.findInDocument(query, editor);
149158
}
150159

151-
public findInWorkspace(query: string): Promise<void> {
160+
findInWorkspace(query: string): Promise<void> {
152161
return this.original.findInWorkspace(query);
153162
}
154163

155-
public openTextDocument(path: string): Promise<TextEditor> {
164+
openTextDocument(path: string): Promise<TextEditor> {
156165
return this.original.openTextDocument(path);
157166
}
158167

159-
public openUntitledTextDocument(
168+
openUntitledTextDocument(
160169
options?: OpenUntitledTextDocumentOptions,
161170
): Promise<TextEditor> {
162171
return this.original.openUntitledTextDocument(options);
163172
}
164173

165-
public showQuickPick(
174+
showQuickPick(
166175
items: readonly string[],
167176
options?: QuickPickOptions,
168177
): Promise<string | undefined> {
169178
return this.original.showQuickPick(items, options);
170179
}
171180

172-
public showInputBox(options?: any): Promise<string | undefined> {
181+
showInputBox(options?: any): Promise<string | undefined> {
173182
return this.original.showInputBox(options);
174183
}
175184

176-
public getEditableTextEditor(editor: TextEditor): EditableTextEditor {
185+
getEditableTextEditor(editor: TextEditor): EditableTextEditor {
177186
return this.original.getEditableTextEditor(editor);
178187
}
179188

180189
executeCommand<T>(command: string, ...args: any[]): Promise<T | undefined> {
181190
return this.original.executeCommand(command, ...args);
182191
}
183192

184-
public onDidChangeTextDocument(
193+
onDidChangeTextDocument(
185194
listener: (event: TextDocumentChangeEvent) => void,
186195
): Disposable {
187196
return this.original.onDidChangeTextDocument(listener);

packages/common/src/ide/normalized/NormalizedIDE.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import type { TextEditor } from "../../types/TextEditor";
33
import type FakeConfiguration from "../fake/FakeConfiguration";
44
import type FakeKeyValueStore from "../fake/FakeKeyValueStore";
55
import type { FakeIDE } from "../fake/FakeIDE";
6-
import PassthroughIDEBase from "../PassthroughIDEBase";
6+
import { PassthroughIDE } from "../PassthroughIDE";
77
import type { FlashDescriptor } from "../types/FlashDescriptor";
88
import type { IDE } from "../types/ide.types";
99
import type { QuickPickOptions } from "../types/QuickPickOptions";
1010

11-
export class NormalizedIDE extends PassthroughIDEBase {
11+
export class NormalizedIDE extends PassthroughIDE {
1212
configuration: FakeConfiguration;
1313
keyValueStore: FakeKeyValueStore;
1414

packages/common/src/ide/spy/SpyIDE.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { pickBy, values } from "lodash-es";
22
import type { GeneralizedRange } from "../../types/GeneralizedRange";
33
import type { TextEditor } from "../../types/TextEditor";
4-
import PassthroughIDEBase from "../PassthroughIDEBase";
4+
import { PassthroughIDE } from "../PassthroughIDE";
55
import type { FlashDescriptor } from "../types/FlashDescriptor";
66
import type { HighlightId, IDE } from "../types/ide.types";
77
import type { Message } from "./SpyMessages";
@@ -18,7 +18,7 @@ export interface SpyIDERecordedValues {
1818
highlights?: Highlight[];
1919
}
2020

21-
export class SpyIDE extends PassthroughIDEBase {
21+
export class SpyIDE extends PassthroughIDE {
2222
messages: SpyMessages;
2323
private flashes: FlashDescriptor[] = [];
2424
private highlights: Highlight[] = [];

packages/common/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export * from "./FakeCommandServerApi";
88
export * from "./ide/fake/FakeIDE";
99
export * from "./ide/inMemoryTextDocument/InMemoryTextDocument";
1010
export * from "./ide/normalized/NormalizedIDE";
11+
export * from "./ide/PassthroughIDE";
1112
export * from "./ide/spy/SpyIDE";
1213
export * from "./ide/spy/SpyMessages";
1314
export * from "./ide/types/Capabilities";

packages/common/src/scopeVisualizerUtil/generateDecorationsForLineRange.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Range } from "../types/Range";
2-
import { BorderStyle, type StyledRange } from "./decorationStyle.types";
2+
import type { StyledRange } from "./decorationStyle.types";
3+
import { BorderStyle } from "./decorationStyle.types";
34

45
export function* generateDecorationsForLineRange(
56
startLine: number,

packages/cursorless-engine/src/CommandHistoryAnalyzer.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import type {
22
CommandHistoryEntry,
33
CommandHistoryStorage,
4+
IDE,
45
Modifier,
56
PartialPrimitiveTargetDescriptor,
67
ScopeType,
78
} from "@cursorless/common";
89
import { showWarning } from "@cursorless/common";
910
import { groupBy, map, sum } from "lodash-es";
1011
import { canonicalizeAndValidateCommand } from "./core/commandVersionUpgrades/canonicalizeAndValidateCommand";
11-
import { ide } from "./singletons/ide.singleton";
1212
import { getPartialTargetDescriptors } from "./util/getPartialTargetDescriptors";
1313
import { getPartialPrimitiveTargets } from "./util/getPrimitiveTargets";
1414
import { getScopeType } from "./util/getScopeType";
@@ -116,22 +116,23 @@ function getMonth(entry: CommandHistoryEntry): string {
116116
}
117117

118118
export async function analyzeCommandHistory(
119+
ide: IDE,
119120
commandHistoryStorage: CommandHistoryStorage,
120121
) {
121122
const entries = await commandHistoryStorage.getEntries();
122123

123124
if (entries.length === 0) {
124125
const TAKE_ME_THERE = "Show me";
125126
const result = await showWarning(
126-
ide().messages,
127+
ide.messages,
127128
"noHistory",
128129
"No command history entries found. Please enable the command history in the settings.",
129130
TAKE_ME_THERE,
130131
);
131132

132133
if (result === TAKE_ME_THERE) {
133134
// FIXME: This is VSCode-specific
134-
await ide().executeCommand(
135+
await ide.executeCommand(
135136
"workbench.action.openSettings",
136137
"cursorless.commandHistory",
137138
);
@@ -148,7 +149,7 @@ export async function analyzeCommandHistory(
148149
),
149150
].join("\n\n\n");
150151

151-
await ide().openUntitledTextDocument({ content });
152+
await ide.openUntitledTextDocument({ content });
152153
}
153154

154155
function toPercent(value: number) {

packages/cursorless-engine/src/KeyboardTargetUpdater.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export class KeyboardTargetUpdater {
5252
return;
5353
}
5454

55-
this.storedTargets.set("keyboard", new CursorStage().run());
55+
this.storedTargets.set("keyboard", new CursorStage(this.ide).run());
5656
}
5757

5858
dispose() {

0 commit comments

Comments
 (0)