Skip to content

Commit

Permalink
Merge pull request #34 from williamthome/fix/no-active-editor
Browse files Browse the repository at this point in the history
fix: no active editor bug
  • Loading branch information
williamthome authored Jul 16, 2022
2 parents ea58d98 + 3b69a13 commit 7bf3e7f
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 18 deletions.
7 changes: 3 additions & 4 deletions src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -30,7 +29,7 @@ export function buildEnv() {
workspacesRoot,
host,
httpRequest,
editor,
activeEditor,
htmlLanguageService,
});
}
4 changes: 2 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export async function activate(context: ExtensionContext) {
workspacesRoot,
host,
httpRequest,
editor,
activeEditor,
htmlLanguageService,
} = buildEnv();

Expand All @@ -44,7 +44,7 @@ export async function activate(context: ExtensionContext) {
z,
context,
filesByGlobPattern,
editor,
activeEditor,
htmlLanguageService,
}).setup();
}
Expand Down
8 changes: 4 additions & 4 deletions src/infra/vscode/commands/commands.config.ts
Original file line number Diff line number Diff line change
@@ -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(),
];
Expand Down
12 changes: 9 additions & 3 deletions src/infra/vscode/commands/insert-snippet-command.ts
Original file line number Diff line number Diff line change
@@ -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));
};
}
5 changes: 5 additions & 0 deletions src/infra/vscode/protocol.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { TextEditor } from 'vscode';

export type ActiveEditor = TextEditor | undefined;

export type GetActiveEditor = () => ActiveEditor;
18 changes: 13 additions & 5 deletions src/infra/vscode/z-vscode.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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() {
Expand Down Expand Up @@ -52,7 +58,9 @@ export function buildZVSCode(args: {
}),
);

buildCommands({ editor }).forEach(registerCommand({ context }));
buildCommands({ activeEditor }).forEach(
registerCommand({ context }),
);
},
};
}

0 comments on commit 7bf3e7f

Please sign in to comment.