clang-format 不认中文分号、缺了分号的语句,会把它和下一行并起来(int n; 和 scanf 成了 一行),报错的行号也对不上学生记得的位置。现在 C / C++ 格式化之前先套上题目模板、在 判题机上编一次,编不过就原样返回、不格式化,代码照常提交、照常判成编译错误。 api 容器里没有 gcc,判题机的编译器和参数本来就是判的标准,所以借它编。这一下不进判题 队列、不建提交记录;判题机连不上时照旧格式化。代价是每次交 C 多一次编译,零点几秒。 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
fa092d334c
commit
c2ef55b10b
5 files changed
+51
-3
No files matched your search
@@ -134,7 +134,7 @@ export function submitCode(data: SubmitCodePayload) {
|
||||
return api.post<CreateSubmissionResponse>("submissions", data)
|
||||
}
|
||||
|
||||
export function formatCode(data: { code: string; language: string }) {
|
||||
export function formatCode(data: { code: string; language: string; problemId: number }) {
|
||||
const languages: Record<string, string> = {
|
||||
Python: "python",
|
||||
C: "c",
|
||||
@@ -144,6 +144,7 @@ export function formatCode(data: { code: string; language: string }) {
|
||||
return api.post<FormatCodeResponse>("code/format", {
|
||||
code: data.code,
|
||||
language: languages[data.language] ?? data.language.toLowerCase(),
|
||||
problemId: data.problemId,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -103,7 +103,8 @@ export const useSubmissionStore = defineStore("submission", () => {
|
||||
formattedBeforeSubmit.value = false
|
||||
|
||||
// 0. 提交前自动格式化(Python 用 ruff,C/C++ 用 clang-format,SQL 用 sqlparse)。
|
||||
// Python 在格式化之前先由服务端的 CPython 查一遍语法,有错就不提交
|
||||
// Python 在格式化之前先由服务端的 CPython 查一遍语法,有错就不提交;
|
||||
// C / C++ 先在判题机上编一遍,编不过就原样交(格式化会把错的几行并成一行)
|
||||
const formatLang = LANGUAGE_FORMAT_VALUE[codeStore.code.language]
|
||||
if (["python", "c", "cpp", "sql"].includes(formatLang)) {
|
||||
isFormatting.value = true
|
||||
@@ -111,6 +112,7 @@ export const useSubmissionStore = defineStore("submission", () => {
|
||||
const res = await formatCode({
|
||||
code: codeStore.code.value,
|
||||
language: formatLang,
|
||||
problemId: problem.id,
|
||||
})
|
||||
if (stale()) return
|
||||
if (res.code !== codeStore.code.value) {
|
||||
|
||||
Reference in new issue
Block a user