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 2 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 @@ -110,6 +110,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
11 changes: 11 additions & 0 deletions client/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export function initializeWorkspace(
try {
const settings = await pickInitWorkspace();
const config = vscode.workspace.getConfiguration(EXTENSION_NS);
const languageSpecificConfigs = await Promise.all(["typescript", "typescriptreact"].map(async (languageId) => vscode.workspace.getConfiguration("", { languageId })))
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: Did this because, since language specifics are not namespaced by the extension settings, we couldn't really rely on the other variable since it would return nothing. So I added it as an array in case we need to add more support (JS, JSX) in the future, so this would be very easy to do.

Also, in case we need to change any other language-specific configs in the future we just need to iterate through it again.

await config.update("enable", true);

const lintInspect = config.inspect("lint");
Expand All @@ -92,6 +93,16 @@ export function initializeWorkspace(
await config.update("unstable", settings.unstable);
}

if (settings.defaultFormatter) {
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
18 changes: 16 additions & 2 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;
defaultFormatter: boolean
}

export function pickInitWorkspace() {
Expand All @@ -25,7 +26,7 @@ export function pickInitWorkspace() {
const pick = await input.showQuickPick({
title,
step: 1,
totalSteps: 2,
totalSteps: 3,
placeholder: "Enable Deno linting?",
items: quickPickYesNo,
shouldResume: () => Promise.resolve(false),
Expand All @@ -38,12 +39,25 @@ export function pickInitWorkspace() {
const pick = await input.showQuickPick({
title,
step: 2,
totalSteps: 2,
totalSteps: 3,
placeholder: "Enable Deno unstable APIs?",
items: quickPickYesNo,
shouldResume: () => Promise.resolve(false),
});
state.unstable = pick.label === "Yes" ? true : false;
return (input: MultiStepInput) => pickDefaultFormatter(input, state)
}

async function pickDefaultFormatter (input: MultiStepInput, state: Partial<State>) {
const pick = await input.showQuickPick({
title,
step: 3,
totalSteps: 3,
placeholder: "Enable default formatter for TypeScript and TSX?",
items: quickPickYesNo,
shouldResume: () => Promise.resolve(false),
})
state.defaultFormatter = pick.label === "Yes" ? true : false
}

async function collectInputs() {
Expand Down