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

Allow configuring indentation in VSCode extension #291

Merged
merged 1 commit into from
Mar 18, 2024
Merged
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
10 changes: 10 additions & 0 deletions editor-plugins/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@
"type": "number",
"default": 100,
"description": "The maximum size a given line can be before being wrapped."
},
"gdscript_formatter.use_spaces.enabled": {
"type": "boolean",
"default": false,
"description": "Use spaces instead of tabs for indentation"
},
"gdscript_formatter.use_spaces.count": {
"type": "number",
"default": 4,
"description": "How many spaces to use for indenting (only used if use_spaces is enabled)"
}
}
}
Expand Down
8 changes: 7 additions & 1 deletion editor-plugins/vscode/scripts/code_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,11 @@
from gdtoolkit.formatter import format_code

sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8")
formatted_code = format_code(sys.argv[1], int(sys.argv[2]))

formatted_code = ""
if len(sys.argv) == 3:
formatted_code = format_code(sys.argv[1], int(sys.argv[2]))
elif len(sys.argv) == 4:
formatted_code = format_code(sys.argv[1], int(sys.argv[2]), int(sys.argv[3]))

print(formatted_code)
35 changes: 25 additions & 10 deletions editor-plugins/vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,6 @@ const opn = require("opn");
const SCRIPT_PATH = path.resolve(__dirname + "/../scripts/");
const SCRIPT_NAME = "code_formatter.py";

const PY_ARGS = [
"",
`${vscode.workspace
.getConfiguration("gdscript_formatter")
.get("line_size")}`,
];

export function activate(context: vscode.ExtensionContext) {
let organize_command = vscode.commands.registerCommand(
"gdscript-formatter.organize_script",
Expand Down Expand Up @@ -75,6 +68,30 @@ export function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(convert_command);
}

function get_formatter_args(script: string): string[] {
let lineSize = vscode
.workspace
.getConfiguration("gdscript_formatter")
.get("line_size") as number;

let args = [script, String(lineSize)];

let useSpaces = vscode
.workspace
.getConfiguration("gdscript_formatter")
.get("use_spaces.enabled");

if (useSpaces) {
let nSpaces = vscode
.workspace
.getConfiguration("gdscript_formatter")
.get("use_spaces.count") as number;
args.push(String(nSpaces));
}

return args;
}

export async function run_formatter(
script: string,
uri: vscode.Uri
Expand All @@ -91,10 +108,8 @@ export async function run_formatter(
}
}

let input = script;
options.args = get_formatter_args(script);

options.args = PY_ARGS;
options.args[0] = input;
try {
return await runPythonCommand(options);
} catch (error) {
Expand Down
Loading