Skip to content

Commit

Permalink
(feat) only show SvelteKit files context menu in kit project (#1771)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonlyu123 committed Dec 11, 2022
1 parent c7fe395 commit c366e2f
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 2 deletions.
12 changes: 11 additions & 1 deletion packages/svelte-vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,16 @@
"default": "off",
"description": "Traces the communication between VS Code and the Svelte Language Server."
},
"svelte.ui.svelteKitFilesContextMenu.enable": {
"type": "string",
"default": "auto",
"enum": [
"auto",
"never",
"always"
],
"description": "Show a context menu to generate SvelteKit files. \"always\" to always show it. \"never\" to always disable it. \"auto\" to show it when in a SvelteKit project. "
},
"svelte.plugin.typescript.enable": {
"type": "boolean",
"default": true,
Expand Down Expand Up @@ -637,7 +647,7 @@
"group": "4_search"
},
{
"when": "explorerResourceIsFolder",
"when": "explorerResourceIsFolder && config.svelte.ui.svelteKitFilesContextMenu.enable == always || explorerResourceIsFolder && svelte.uiContext.svelteKitFilesContextMenu.enable",
"submenu": "sveltekit2files",
"group": "1_SvelteKit2files"
}
Expand Down
79 changes: 78 additions & 1 deletion packages/svelte-vscode/src/sveltekit/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,83 @@
import { ExtensionContext } from 'vscode';
import { TextDecoder } from 'util';
import { ExtensionContext, commands, workspace } from 'vscode';
import { addGenerateKitRouteFilesCommand } from './generateFiles';

type ShowSvelteKitFilesContextMenuConfig = 'auto' | 'always' | 'never';

export function setupSvelteKit(context: ExtensionContext) {
let contextMenuEnabled = false;
context.subscriptions.push(
workspace.onDidChangeConfiguration(() => {
enableContextMenu();
})
);

addGenerateKitRouteFilesCommand(context);
enableContextMenu();

async function enableContextMenu() {
const config = getConfig();
if (config === 'never') {
if (contextMenuEnabled) {
setEnableContext(false);
}
return;
}

if (config === 'always') {
// Force on. The condition is defined in the extension manifest
return;
}

const enabled = await detect(20);
if (enabled !== contextMenuEnabled) {
setEnableContext(enabled);
contextMenuEnabled = enabled;
}
}
}

function getConfig() {
return (
workspace
.getConfiguration('svelte.ui.svelteKitFilesContextMenu')
.get<ShowSvelteKitFilesContextMenuConfig>('enable') ?? 'auto'
);
}

async function detect(nrRetries: number): Promise<boolean> {
const packageJsonList = await workspace.findFiles('**/package.json', '**​/node_modules/**');

if (packageJsonList.length === 0 && nrRetries > 0) {
// We assume that the user has not setup their project yet, so try again after a while
await new Promise((resolve) => setTimeout(resolve, 10000));
return detect(nrRetries - 1);
}

for (const fileUri of packageJsonList) {
try {
const text = new TextDecoder().decode(await workspace.fs.readFile(fileUri));
const pkg = JSON.parse(text);
const hasKit = Object.keys(pkg.devDependencies ?? {})
.concat(Object.keys(pkg.dependencies ?? {}))
.includes('@sveltejs/kit');

if (hasKit) {
return true;
}
} catch (error) {
console.error(error);
}
}

return false;
}

function setEnableContext(enable: boolean) {
// https://code.visualstudio.com/api/references/when-clause-contexts#add-a-custom-when-clause-context
commands.executeCommand(
'setContext',
'svelte.uiContext.svelteKitFilesContextMenu.enable',
enable
);
}

0 comments on commit c366e2f

Please sign in to comment.