Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add default formatter question on initialize #790

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ Or if you wanted to have Deno be your default formatter overall:
}
```

By default, when initializing a new workspace, the extension will ask if you want
to set the default formatter for these types of files to be Deno. If you choose
to do so, the extension will set the `editor.defaultFormatter` setting to
`denoland.vscode-deno` for these types of files.

The formatter will respect the settings in your Deno configuration file, which
can be explicitly set via `deno.config` or automatically detected in the
workspace. You can find more information about formatter settings at
Expand Down
26 changes: 26 additions & 0 deletions client/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@ export function initializeWorkspace(
try {
const settings = await pickInitWorkspace();
const config = vscode.workspace.getConfiguration(EXTENSION_NS);
const SUPPORTED_LANGUAGES = [
"javascript",
"typescript",
"javascriptreact",
"typescriptreact",
];
const languageSpecificConfigs = SUPPORTED_LANGUAGES.map((languageId) =>
vscode.workspace.getConfiguration("", { languageId })
);
await config.update("enable", true);

const lintInspect = config.inspect("lint");
Expand All @@ -88,6 +97,23 @@ export function initializeWorkspace(
await config.update("lint", settings.lint);
await config.update("unstable", settings.unstable);

if (settings.fmt) {
for (const languageSpecificConfig of languageSpecificConfigs) {
const defaultFormatterInspect = languageSpecificConfig.inspect(
"editor.defaultFormatter",
);
assert(defaultFormatterInspect);
if (defaultFormatterInspect.defaultValue != "denoland.vscode-deno") {
await languageSpecificConfig.update(
"editor.defaultFormatter",
"denoland.vscode-deno",
false,
true,
);
}
}
}

await vscode.window.showInformationMessage(
"Deno is now setup in this workspace.",
);
Expand Down
27 changes: 22 additions & 5 deletions client/src/initialize_project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const quickPickYesNo = [
export interface InitWorkspaceSettings {
lint: boolean;
unstable: boolean;
fmt: boolean;
}

export function pickInitWorkspace() {
Expand All @@ -21,11 +22,27 @@ export function pickInitWorkspace() {

const title = "Initialize Project";

async function pickLint(input: MultiStepInput, state: Partial<State>) {
async function pickFmt(
input: MultiStepInput,
state: Partial<State>,
) {
const pick = await input.showQuickPick({
title,
step: 1,
totalSteps: 2,
totalSteps: 3,
placeholder: "Enable Deno formatting?",
items: quickPickYesNo,
shouldResume: () => Promise.resolve(false),
});
state.fmt = pick.label === "Yes" ? true : false;
return (input: MultiStepInput) => pickLint(input, state);
}

async function pickLint(input: MultiStepInput, state: Partial<State>) {
const pick = await input.showQuickPick({
title,
step: 2,
totalSteps: 3,
placeholder: "Enable Deno linting?",
items: quickPickYesNo,
shouldResume: () => Promise.resolve(false),
Expand All @@ -37,8 +54,8 @@ export function pickInitWorkspace() {
async function pickUnstable(input: MultiStepInput, state: Partial<State>) {
const pick = await input.showQuickPick({
title,
step: 2,
totalSteps: 2,
step: 3,
totalSteps: 3,
placeholder: "Enable Deno unstable APIs?",
items: quickPickYesNo,
shouldResume: () => Promise.resolve(false),
Expand All @@ -48,7 +65,7 @@ export function pickInitWorkspace() {

async function collectInputs() {
const state: Partial<State> = {};
await MultiStepInput.run((input) => pickLint(input, state));
await MultiStepInput.run((input) => pickFmt(input, state));
return state as InitWorkspaceSettings;
}

Expand Down