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

Add "format code" support for Python #180

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
25 changes: 25 additions & 0 deletions app/(navigation)/(code)/util/formatCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,25 @@ const parsers = {
CSS: { import: () => import("prettier/plugins/postcss"), name: "css" },
SCSS: { import: () => import("prettier/plugins/postcss"), name: "css" },
YAML: { import: () => import("prettier/plugins/yaml"), name: "yaml" },
Python: { import: () => Promise.resolve({ default: {} }), name: "python" },
};

export const formatterSupportedLanguages: Language["name"][] = Object.keys(parsers);

const ruffConfig = {
preview: false,
builtins: [],
"target-version": "py312",
"line-length": 80,
"indent-width": 4,
format: {
"indent-style": "space",
"quote-style": "double",
"skip-magic-trailing-comma": false,
"line-ending": "auto",
},
};
Copy link

@MichaReiser MichaReiser Feb 14, 2025

Choose a reason for hiding this comment

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

You can use Workspace.defaultSettings() to get the default settings

Copy link
Author

Choose a reason for hiding this comment

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

Thank you for the suggestion @MichaReiser! Addressed.


const prettierConfig = {
singleQuote: false,
printWidth: 80,
Expand All @@ -22,6 +37,16 @@ const formatCode = async (code: string, language: Language | null) => {
if (!language || !formatterSupportedLanguages.includes(language.name)) {
return code;
}

if (language.name === "Python") {
const { default: initRuff, Workspace } = await import("@astral-sh/ruff-wasm-web");
await initRuff();

const workspace = new Workspace(ruffConfig);
const formatted = workspace.format(code);
return formatted.replace(/\n$/, "");
}

const prettier = await import("prettier/standalone");

const parser = parsers[language.name as keyof typeof parsers];
Expand Down
7 changes: 7 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"node": "^18"
},
"dependencies": {
"@astral-sh/ruff-wasm-web": "^0.9.6",
"@next/bundle-analyzer": "^14.2.4",
"@radix-ui/react-collapsible": "^1.0.3",
"@radix-ui/react-context-menu": "^2.1.5",
Expand Down