Skip to content

Commit 7bf3e7f

Browse files
authored
Merge pull request #34 from williamthome/fix/no-active-editor
fix: no active editor bug
2 parents ea58d98 + 3b69a13 commit 7bf3e7f

File tree

6 files changed

+36
-18
lines changed

6 files changed

+36
-18
lines changed

src/env.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ export function buildEnv() {
1818
const httpRequest = buildHttpRequest();
1919

2020
// Infra
21-
const editor = window.activeTextEditor;
22-
if (!editor) {
23-
throw new Error('No editor active');
21+
function activeEditor() {
22+
return window.activeTextEditor;
2423
}
2524
const htmlLanguageService = getHtmlLanguageService();
2625

@@ -30,7 +29,7 @@ export function buildEnv() {
3029
workspacesRoot,
3130
host,
3231
httpRequest,
33-
editor,
32+
activeEditor,
3433
htmlLanguageService,
3534
});
3635
}

src/extension.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export async function activate(context: ExtensionContext) {
2121
workspacesRoot,
2222
host,
2323
httpRequest,
24-
editor,
24+
activeEditor,
2525
htmlLanguageService,
2626
} = buildEnv();
2727

@@ -44,7 +44,7 @@ export async function activate(context: ExtensionContext) {
4444
z,
4545
context,
4646
filesByGlobPattern,
47-
editor,
47+
activeEditor,
4848
htmlLanguageService,
4949
}).setup();
5050
}

src/infra/vscode/commands/commands.config.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
import { TextEditor } from 'vscode';
1+
import { GetActiveEditor } from '../protocol';
22
import { buildGetUserChoiceCommand } from './get-user-choice-command';
33
import { buildGrowlErrorCommand } from './growl-error-command';
44
import { buildInsertSnippetCommand } from './insert-snippet-command';
55
import { buildMainCommand } from './main-command';
66
import { buildPopUpSnippets } from './pop-up-snippets-command';
77

8-
export function buildCommands(args: { editor: TextEditor }) {
9-
const { editor } = args;
8+
export function buildCommands(args: { activeEditor: GetActiveEditor }) {
9+
const { activeEditor } = args;
1010

1111
const generalCommands = [
1212
buildGetUserChoiceCommand(),
13-
buildInsertSnippetCommand({ editor }),
13+
buildInsertSnippetCommand({ activeEditor }),
1414
buildPopUpSnippets(),
1515
buildGrowlErrorCommand(),
1616
];
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
import { InsertSnippetCommand } from '../../../domain/commands';
2-
import { SnippetString, TextEditor } from 'vscode';
2+
import { SnippetString } from 'vscode';
3+
import { GetActiveEditor } from '../protocol';
34

45
export function buildInsertSnippetCommand(args: {
5-
editor: TextEditor;
6+
activeEditor: GetActiveEditor;
67
}): InsertSnippetCommand {
78
return async function insertSnippet({ snippet }) {
8-
await args.editor.insertSnippet(new SnippetString(snippet));
9+
const editor = args.activeEditor();
10+
if (!editor) {
11+
return;
12+
}
13+
14+
await editor.insertSnippet(new SnippetString(snippet));
915
};
1016
}

src/infra/vscode/protocol.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { TextEditor } from 'vscode';
2+
3+
export type ActiveEditor = TextEditor | undefined;
4+
5+
export type GetActiveEditor = () => ActiveEditor;

src/infra/vscode/z-vscode.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ExtensionContext, TextEditor } from 'vscode';
1+
import { ExtensionContext } from 'vscode';
22
import { Z } from '../../domain/z';
33
import { FilesByGlobPattern } from '../../domain/files';
44
import { registerSnippetProvider } from './completion-item-provider';
@@ -8,16 +8,22 @@ import { registerCommand } from './command';
88
import { buildCommands } from './commands';
99
import { buildHtmlSnippetProvider } from './embedded';
1010
import { LanguageService } from 'vscode-html-languageservice';
11+
import { GetActiveEditor } from './protocol';
1112

1213
export function buildZVSCode(args: {
1314
z: Z;
1415
context: ExtensionContext;
1516
filesByGlobPattern: FilesByGlobPattern;
16-
editor: TextEditor;
17+
activeEditor: GetActiveEditor;
1718
htmlLanguageService: LanguageService;
1819
}) {
19-
const { z, context, filesByGlobPattern, editor, htmlLanguageService } =
20-
args;
20+
const {
21+
z,
22+
context,
23+
filesByGlobPattern,
24+
activeEditor,
25+
htmlLanguageService,
26+
} = args;
2127

2228
return {
2329
setup() {
@@ -52,7 +58,9 @@ export function buildZVSCode(args: {
5258
}),
5359
);
5460

55-
buildCommands({ editor }).forEach(registerCommand({ context }));
61+
buildCommands({ activeEditor }).forEach(
62+
registerCommand({ context }),
63+
);
5664
},
5765
};
5866
}

0 commit comments

Comments
 (0)