Skip to content

Commit

Permalink
Allow configuring indentation in VSCode extension (#291)
Browse files Browse the repository at this point in the history
  • Loading branch information
nonolai authored Mar 18, 2024
1 parent 320df8a commit 73c9dca
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 11 deletions.
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

0 comments on commit 73c9dca

Please sign in to comment.