diff --git a/src/env.ts b/src/env.ts index a52d1417..ced102fa 100644 --- a/src/env.ts +++ b/src/env.ts @@ -18,9 +18,8 @@ export function buildEnv() { const httpRequest = buildHttpRequest(); // Infra - const editor = window.activeTextEditor; - if (!editor) { - throw new Error('No editor active'); + function activeEditor() { + return window.activeTextEditor; } const htmlLanguageService = getHtmlLanguageService(); @@ -30,7 +29,7 @@ export function buildEnv() { workspacesRoot, host, httpRequest, - editor, + activeEditor, htmlLanguageService, }); } diff --git a/src/extension.ts b/src/extension.ts index ebcac3c8..c50992b8 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -21,7 +21,7 @@ export async function activate(context: ExtensionContext) { workspacesRoot, host, httpRequest, - editor, + activeEditor, htmlLanguageService, } = buildEnv(); @@ -44,7 +44,7 @@ export async function activate(context: ExtensionContext) { z, context, filesByGlobPattern, - editor, + activeEditor, htmlLanguageService, }).setup(); } diff --git a/src/infra/vscode/commands/commands.config.ts b/src/infra/vscode/commands/commands.config.ts index 75355fdd..e8cc311f 100644 --- a/src/infra/vscode/commands/commands.config.ts +++ b/src/infra/vscode/commands/commands.config.ts @@ -1,16 +1,16 @@ -import { TextEditor } from 'vscode'; +import { GetActiveEditor } from '../protocol'; import { buildGetUserChoiceCommand } from './get-user-choice-command'; import { buildGrowlErrorCommand } from './growl-error-command'; import { buildInsertSnippetCommand } from './insert-snippet-command'; import { buildMainCommand } from './main-command'; import { buildPopUpSnippets } from './pop-up-snippets-command'; -export function buildCommands(args: { editor: TextEditor }) { - const { editor } = args; +export function buildCommands(args: { activeEditor: GetActiveEditor }) { + const { activeEditor } = args; const generalCommands = [ buildGetUserChoiceCommand(), - buildInsertSnippetCommand({ editor }), + buildInsertSnippetCommand({ activeEditor }), buildPopUpSnippets(), buildGrowlErrorCommand(), ]; diff --git a/src/infra/vscode/commands/insert-snippet-command.ts b/src/infra/vscode/commands/insert-snippet-command.ts index d07f5d35..27021ada 100644 --- a/src/infra/vscode/commands/insert-snippet-command.ts +++ b/src/infra/vscode/commands/insert-snippet-command.ts @@ -1,10 +1,16 @@ import { InsertSnippetCommand } from '../../../domain/commands'; -import { SnippetString, TextEditor } from 'vscode'; +import { SnippetString } from 'vscode'; +import { GetActiveEditor } from '../protocol'; export function buildInsertSnippetCommand(args: { - editor: TextEditor; + activeEditor: GetActiveEditor; }): InsertSnippetCommand { return async function insertSnippet({ snippet }) { - await args.editor.insertSnippet(new SnippetString(snippet)); + const editor = args.activeEditor(); + if (!editor) { + return; + } + + await editor.insertSnippet(new SnippetString(snippet)); }; } diff --git a/src/infra/vscode/protocol.ts b/src/infra/vscode/protocol.ts new file mode 100644 index 00000000..f53bc25d --- /dev/null +++ b/src/infra/vscode/protocol.ts @@ -0,0 +1,5 @@ +import { TextEditor } from 'vscode'; + +export type ActiveEditor = TextEditor | undefined; + +export type GetActiveEditor = () => ActiveEditor; diff --git a/src/infra/vscode/z-vscode.ts b/src/infra/vscode/z-vscode.ts index 0d589cfd..042ac863 100644 --- a/src/infra/vscode/z-vscode.ts +++ b/src/infra/vscode/z-vscode.ts @@ -1,4 +1,4 @@ -import { ExtensionContext, TextEditor } from 'vscode'; +import { ExtensionContext } from 'vscode'; import { Z } from '../../domain/z'; import { FilesByGlobPattern } from '../../domain/files'; import { registerSnippetProvider } from './completion-item-provider'; @@ -8,16 +8,22 @@ import { registerCommand } from './command'; import { buildCommands } from './commands'; import { buildHtmlSnippetProvider } from './embedded'; import { LanguageService } from 'vscode-html-languageservice'; +import { GetActiveEditor } from './protocol'; export function buildZVSCode(args: { z: Z; context: ExtensionContext; filesByGlobPattern: FilesByGlobPattern; - editor: TextEditor; + activeEditor: GetActiveEditor; htmlLanguageService: LanguageService; }) { - const { z, context, filesByGlobPattern, editor, htmlLanguageService } = - args; + const { + z, + context, + filesByGlobPattern, + activeEditor, + htmlLanguageService, + } = args; return { setup() { @@ -52,7 +58,9 @@ export function buildZVSCode(args: { }), ); - buildCommands({ editor }).forEach(registerCommand({ context })); + buildCommands({ activeEditor }).forEach( + registerCommand({ context }), + ); }, }; }