Compare commits

..

5 Commits

Author SHA1 Message Date
f31b2afcb1 fix
Some checks failed
Deploy / build-and-deploy (push) Has been cancelled
2026-01-14 08:56:22 +08:00
8260aa197d 使用 skulpt 作为 Python 执行器
Some checks failed
Deploy / build-and-deploy (push) Has been cancelled
2026-01-13 20:19:50 +08:00
ff170ae2ce update
Some checks failed
Deploy / build-and-deploy (push) Has been cancelled
2026-01-06 22:32:00 +08:00
fbb4882780 update
Some checks failed
Deploy / build-and-deploy (push) Has been cancelled
2026-01-06 22:29:12 +08:00
dc039bfa16 update
Some checks failed
Deploy / build-and-deploy (push) Has been cancelled
2026-01-06 22:15:58 +08:00
11 changed files with 1426 additions and 968 deletions

View File

@@ -2,15 +2,6 @@ import axios from "axios"
import { languageToId } from "./templates" import { languageToId } from "./templates"
import { Code, Submission } from "./types" import { Code, Submission } from "./types"
// function getChromeVersion() {
// var raw = navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./)
// return raw ? parseInt(raw[2], 10) : 0
// }
// const isLowVersion = getChromeVersion() < 80
// const protocol = isLowVersion ? "http" : "https"
function encode(string?: string) { function encode(string?: string) {
return btoa(String.fromCharCode(...new TextEncoder().encode(string ?? ""))) return btoa(String.fromCharCode(...new TextEncoder().encode(string ?? "")))
} }
@@ -27,7 +18,71 @@ function decode(bytes?: string) {
const judge = axios.create({ baseURL: import.meta.env.PUBLIC_JUDGE0API_URL }) const judge = axios.create({ baseURL: import.meta.env.PUBLIC_JUDGE0API_URL })
const api = axios.create({ baseURL: import.meta.env.PUBLIC_CODEAPI_URL }) const api = axios.create({ baseURL: import.meta.env.PUBLIC_CODEAPI_URL })
type PythonWorkerRequest = {
id: number
source: string
stdin: string
timeoutMs: number
}
type PythonWorkerResponse = {
id: number
status: number
output: string
}
let pythonWorker: Worker | null = null
let pythonWorkerSeq = 0
const pythonPending = new Map<
number,
{ resolve: (v: { status: number; output: string }) => void; timeout: number }
>()
function getPythonWorker() {
if (pythonWorker) return pythonWorker
pythonWorker = new Worker(
new URL("./workers/pythonSkulpt.worker.ts", import.meta.url),
{ type: "module" },
)
pythonWorker.onmessage = (event: MessageEvent<PythonWorkerResponse>) => {
const { id, status, output } = event.data ?? ({} as any)
const pending = pythonPending.get(id)
if (!pending) return
clearTimeout(pending.timeout)
pythonPending.delete(id)
pending.resolve({ status, output })
}
return pythonWorker
}
function restartPythonWorker() {
if (pythonWorker) pythonWorker.terminate()
pythonWorker = null
pythonPending.clear()
}
async function runPythonInWorker(source: string, stdin: string) {
const worker = getPythonWorker()
const id = ++pythonWorkerSeq
const timeoutMs = 5000
return new Promise<{ status: number; output: string }>((resolve) => {
const timeout = window.setTimeout(() => {
restartPythonWorker()
resolve({ status: 11, output: "运行超时" })
}, timeoutMs + 250)
pythonPending.set(id, { resolve, timeout })
const message: PythonWorkerRequest = { id, source, stdin, timeoutMs }
worker.postMessage(message)
})
}
export async function submit(code: Code, input: string) { export async function submit(code: Code, input: string) {
if (code.language === "python") {
return runPythonInWorker(code.value, input)
}
const encodedCode = encode(code.value) const encodedCode = encode(code.value)
const id = languageToId[code.language] const id = languageToId[code.language]

View File

@@ -3,7 +3,7 @@ import { cpp } from "@codemirror/lang-cpp"
import { python } from "@codemirror/lang-python" import { python } from "@codemirror/lang-python"
import { EditorState } from "@codemirror/state" import { EditorState } from "@codemirror/state"
import { EditorView } from "@codemirror/view" import { EditorView } from "@codemirror/view"
import { autocompletion } from "@codemirror/autocomplete" import { autocompletion, completeAnyWord } from "@codemirror/autocomplete"
import { Icon } from "@iconify/vue" import { Icon } from "@iconify/vue"
import { useDark } from "@vueuse/core" import { useDark } from "@vueuse/core"
import { computed, ref, watch } from "vue" import { computed, ref, watch } from "vue"
@@ -62,7 +62,9 @@ const langExtension = computed(() => {
const enhanceAutoCompletion = computed(() => { const enhanceAutoCompletion = computed(() => {
console.log(props.language) console.log(props.language)
return autocompletion({ override: [enhanceCompletion(props.language)] }) return autocompletion({
override: [enhanceCompletion(props.language), completeAnyWord],
})
}) })
function onChange(v: string) { function onChange(v: string) {

View File

@@ -453,7 +453,9 @@ function autoRun() {
<!-- 输出部分 --> <!-- 输出部分 -->
<n-flex v-if="currentOutput" vertical> <n-flex v-if="currentOutput" vertical>
<n-text strong style="margin-bottom: 8px">输出({{ outputLines }})</n-text> <n-text strong style="margin-bottom: 8px">
输出({{ outputLines }})
</n-text>
<n-card size="small" :bordered="true"> <n-card size="small" :bordered="true">
<n-scrollbar style="max-height: 300px"> <n-scrollbar style="max-height: 300px">
<n-text code class="debug-text"> <n-text code class="debug-text">

View File

@@ -25,7 +25,10 @@ function copy() {
/** /**
* 检查调试数据是否需要输入但用户没有提供足够的输入 * 检查调试数据是否需要输入但用户没有提供足够的输入
*/ */
function needsInputButNotProvided(debugData: any, providedInputs: string[]): boolean { function needsInputButNotProvided(
debugData: any,
providedInputs: string[],
): boolean {
if (!debugData?.trace || debugData.trace.length === 0) { if (!debugData?.trace || debugData.trace.length === 0) {
return false return false
} }
@@ -35,7 +38,7 @@ function needsInputButNotProvided(debugData: any, providedInputs: string[]): boo
if (lastStep.event === "raw_input") { if (lastStep.event === "raw_input") {
// 统计 trace 中所有的 raw_input 事件数量(程序需要的输入数量) // 统计 trace 中所有的 raw_input 事件数量(程序需要的输入数量)
const requiredInputCount = debugData.trace.filter( const requiredInputCount = debugData.trace.filter(
(step: any) => step.event === "raw_input" (step: any) => step.event === "raw_input",
).length ).length
// 如果用户提供的输入数量不足,返回 true // 如果用户提供的输入数量不足,返回 true
@@ -93,7 +96,7 @@ async function handleDebug() {
:mask-closable="false" :mask-closable="false"
:auto-focus="false" :auto-focus="false"
@close="closeDebug" @close="closeDebug"
style="width: 80vw; max-width: 1000px;" style="width: 80vw; max-width: 1000px"
> >
<DebugPanel :initial-debug-data="debugData" @close="closeDebug" /> <DebugPanel :initial-debug-data="debugData" @close="closeDebug" />
</n-modal> </n-modal>

View File

@@ -19,6 +19,10 @@ function runSkulptTurtle() {
const canvas = turtleCanvas.value const canvas = turtleCanvas.value
if (!canvas) return if (!canvas) return
canvas.innerHTML = "" canvas.innerHTML = ""
// Prevent UI from being stuck forever on infinite loops (still runs on main thread).
// Skulpt checks this limit periodically and throws a timeout error.
;(Sk as any).execLimit = 5000
;(Sk as any).execStart = new Date()
Sk.configure({ Sk.configure({
output: console.log, output: console.log,
read: builtinRead, read: builtinRead,

View File

@@ -6,6 +6,10 @@ import type {
} from "@codemirror/autocomplete" } from "@codemirror/autocomplete"
import type { EditorView } from "@codemirror/view" import type { EditorView } from "@codemirror/view"
import { LANGUAGE } from "../types" import { LANGUAGE } from "../types"
import { cpp } from "./cpp"
import { c } from "./c"
import { python } from "./python"
import { turtle } from "./turtle"
type ChineseCompletion = Pick< type ChineseCompletion = Pick<
Completion, Completion,
@@ -14,890 +18,10 @@ type ChineseCompletion = Pick<
// 中文注释提示 // 中文注释提示
const chineseAnnotations: Record<string, ChineseCompletion[]> = { const chineseAnnotations: Record<string, ChineseCompletion[]> = {
python: [ python,
{ turtle,
label: "print", c,
detail: "打印输出", cpp,
type: "function",
info: "内置函数,将对象输出到标准输出,可用 sep 和 end 指定分隔符与结尾字符",
boost: 100,
apply: "print()",
},
{
label: "input",
detail: "读取输入",
type: "function",
info: "内置函数,读取一行输入并返回字符串,可传入提示信息",
boost: 95,
apply: "input()",
},
{
label: "len",
detail: "获取长度",
type: "function",
info: "返回对象的长度,常用于列表、字符串、字典等序列或集合类型",
boost: 90,
apply: "len()",
},
{
label: "range",
detail: "生成整数序列",
type: "function",
info: "返回不可变的整数序列,支持 start、stop、step常用于 for 循环",
boost: 85,
apply: "range()",
},
{
label: "enumerate",
detail: "枚举索引与元素",
type: "function",
info: "遍历可迭代对象时同时得到索引和值,可通过 start 指定起始索引",
boost: 82,
apply: "enumerate()",
},
{
label: "zip",
detail: "并行遍历",
type: "function",
info: "将多个可迭代对象聚合为元组迭代器,长度以最短序列为准",
boost: 80,
apply: "zip()",
},
{
label: "map",
detail: "映射函数",
type: "function",
info: "对可迭代对象的每个元素应用函数,返回惰性迭代器",
boost: 78,
apply: "map()",
},
{
label: "filter",
detail: "过滤元素",
type: "function",
info: "保留函数返回真值的元素,返回惰性迭代器",
boost: 76,
apply: "filter()",
},
{
label: "sorted",
detail: "排序",
type: "function",
info: "返回排序后的新列表,支持 key 与 reverse 参数",
boost: 74,
apply: "sorted()",
},
{
label: "sum",
detail: "求和",
type: "function",
info: "对可迭代对象元素求和,可指定起始值",
boost: 72,
apply: "sum()",
},
{
label: "open",
detail: "文件读写",
type: "function",
info: "打开文件并返回文件对象,常与 with 语句搭配确保自动关闭",
boost: 70,
apply: "open()",
},
{
label: "abs",
detail: "绝对值",
type: "function",
info: "返回数字的绝对值",
boost: 68,
apply: "abs()",
},
{
label: "round",
detail: "四舍五入",
type: "function",
info: "按指定精度进行四舍五入,默认到整数",
boost: 66,
apply: "round()",
},
{
label: "isinstance",
detail: "类型检查",
type: "function",
info: "判断对象是否为某个类型或类型元组的实例",
boost: 64,
apply: "isinstance()",
},
{
label: "type",
detail: "获取类型",
type: "function",
info: "返回对象的类型,或在三个参数形式下动态创建类型",
boost: 62,
apply: "type()",
},
{
label: "list",
detail: "列表构造",
type: "function",
info: "将可迭代对象转换为列表,或创建空列表",
boost: 60,
apply: "list()",
},
{
label: "dict",
detail: "字典构造",
type: "function",
info: "根据映射或键值对序列创建字典",
boost: 58,
apply: "dict()",
},
{
label: "set",
detail: "集合构造",
type: "function",
info: "根据可迭代对象创建集合,自动去重",
boost: 56,
apply: "set()",
},
{
label: "tuple",
detail: "元组构造",
type: "function",
info: "将可迭代对象转换为元组,或创建空元组",
boost: 54,
apply: "tuple()",
},
{
label: "int",
detail: "转整数",
type: "function",
info: "将参数转换为整型,支持基数转换",
boost: 74,
apply: "int()",
},
{
label: "float",
detail: "转浮点数",
type: "function",
info: "将参数转换为浮点数,支持字符串与数字",
boost: 72,
apply: "float()",
},
{
label: "str",
detail: "转字符串",
type: "function",
info: "将对象转换为字符串表示,常用于输出",
boost: 70,
apply: "str()",
},
{
label: "bool",
detail: "转布尔值",
type: "function",
info: "根据真值测试转换为 True/False空对象为 False",
boost: 68,
apply: "bool()",
},
{
label: "def",
detail: "定义函数",
type: "keyword",
info: "定义可复用的函数块,支持位置参数、关键字参数与默认值",
boost: 52,
},
{
label: "class",
detail: "定义类",
type: "keyword",
info: "定义自定义类型,支持继承与魔术方法",
boost: 50,
},
{
label: "with",
detail: "上下文管理",
type: "keyword",
info: "进入上下文管理器,自动处理资源的进入与退出",
boost: 48,
},
{
label: "try",
detail: "异常捕获",
type: "keyword",
info: "开始异常处理块,与 except/finally/else 结合使用",
boost: 46,
},
{
label: "except",
detail: "处理异常",
type: "keyword",
info: "捕获特定异常类型并处理,常配合 try 使用",
boost: 44,
},
{
label: "finally",
detail: "清理收尾",
type: "keyword",
info: "无论是否发生异常都执行的收尾代码块",
boost: 42,
},
{
label: "import",
detail: "导入模块",
type: "keyword",
info: "导入模块或包中的名称,可与 as 指定别名",
boost: 40,
},
{
label: "from",
detail: "按需导入",
type: "keyword",
info: "从模块中按名称导入对象,可结合 import 与 as",
boost: 38,
},
{
label: "return",
detail: "返回值",
type: "keyword",
info: "结束函数并返回值,未指定值时返回 None",
boost: 36,
},
{
label: "append",
detail: "列表追加",
type: "method",
info: "在列表尾部添加新元素,等价于 list.append(value)",
boost: 48,
apply: "append()",
},
{
label: "insert",
detail: "列表插入",
type: "method",
info: "在指定位置插入元素list.insert(index, value)",
boost: 46,
apply: "insert()",
},
{
label: "remove",
detail: "删除匹配值",
type: "method",
info: "删除列表中第一次出现的指定值,若不存在将抛出异常",
boost: 44,
apply: "remove()",
},
{
label: "pop",
detail: "弹出元素",
type: "method",
info: "移除并返回列表指定位置(默认尾部)的元素",
boost: 42,
apply: "pop()",
},
{
label: "count",
detail: "统计次数",
type: "method",
info: "返回某个对象在列表中出现的次数",
boost: 40,
apply: "count()",
},
{
label: "reverse",
detail: "反转列表",
type: "method",
info: "原地反转列表中元素的顺序",
boost: 38,
apply: "reverse()",
},
{
label: "sort",
detail: "列表排序",
type: "method",
info: "对列表进行原地排序,可指定 key 与 reverse",
boost: 36,
apply: "sort()",
},
{
label: "add",
detail: "集合添加",
type: "method",
info: "向集合添加单个元素,若已存在则忽略",
boost: 50,
apply: "add()",
},
{
label: "clear",
detail: "清空集合",
type: "method",
info: "移除集合中所有元素,变为空集",
boost: 48,
apply: "clear()",
},
{
label: "keys",
detail: "字典键",
type: "method",
info: "返回字典键的可迭代视图",
boost: 42,
apply: "keys()",
},
{
label: "values",
detail: "字典值",
type: "method",
info: "返回字典值的可迭代视图",
boost: 40,
apply: "values()",
},
{
label: "split",
detail: "字符串切割",
type: "method",
info: "按分隔符切分字符串,返回列表,默认按空白字符",
boost: 52,
apply: "split()",
},
{
label: "replace",
detail: "字符串替换",
type: "method",
info: "将字符串中的子串替换为新内容,可限定次数",
boost: 50,
apply: "replace()",
},
{
label: "format",
detail: "格式化字符串",
type: "method",
info: "使用占位符或命名参数进行字符串格式化",
boost: 48,
apply: "format()",
},
{
label: "strip",
detail: "去首尾指定字符",
type: "method",
info: "移除字符串首尾指定字符,默认移除空白",
boost: 46,
apply: "strip()",
},
{
label: "lower",
detail: "转小写",
type: "method",
info: "将字符串中的字母转换为小写形式",
boost: 44,
apply: "lower()",
},
{
label: "upper",
detail: "转大写",
type: "method",
info: "将字符串中的字母转换为大写形式",
boost: 42,
apply: "upper()",
},
{
label: "swapcase",
detail: "大小写互换",
type: "method",
info: "将字符串中的大小写字母互换",
boost: 40,
apply: "swapcase()",
},
{
label: "find",
detail: "查找子串位置",
type: "method",
info: "返回子串首次出现的索引,未找到返回 -1",
boost: 38,
apply: "find()",
},
{
label: "index",
detail: "查找子串索引",
type: "method",
info: "返回子串首次出现的索引,未找到抛出异常",
boost: 36,
apply: "index()",
},
{
label: "startswith",
detail: "前缀判断",
type: "method",
info: "判断字符串是否以指定前缀开头,可指定范围",
boost: 34,
apply: "startswith()",
},
{
label: "endswith",
detail: "后缀判断",
type: "method",
info: "判断字符串是否以指定后缀结尾,可指定范围",
boost: 32,
apply: "endswith()",
},
{
label: "isalnum",
detail: "是否字母数字",
type: "method",
info: "检测字符串是否只由字母和数字组成",
boost: 30,
apply: "isalnum()",
},
{
label: "isalpha",
detail: "是否字母",
type: "method",
info: "检测字符串是否只由字母组成",
boost: 28,
apply: "isalpha()",
},
{
label: "isdigit",
detail: "是否数字",
type: "method",
info: "检测字符串是否只由数字组成",
boost: 26,
apply: "isdigit()",
},
{
label: "islower",
detail: "是否全小写",
type: "method",
info: "检测字符串是否全部由小写字母组成且至少有一个字母",
boost: 24,
apply: "islower()",
},
{
label: "isupper",
detail: "是否全大写",
type: "method",
info: "检测字符串中所有字母是否都为大写且至少有一个字母",
boost: 22,
apply: "isupper()",
},
],
turtle: [
{
label: "forward",
detail: "向前移动",
type: "function",
info: "让海龟沿当前朝向向前移动指定距离",
boost: 90,
apply: "forward()",
},
{
label: "backward",
detail: "向后移动",
type: "function",
info: "让海龟沿当前朝向向后移动指定距离",
boost: 88,
apply: "backward()",
},
{
label: "left",
detail: "左转",
type: "function",
info: "让海龟左转给定角度(度数)",
boost: 86,
apply: "left()",
},
{
label: "right",
detail: "右转",
type: "function",
info: "让海龟右转给定角度(度数)",
boost: 84,
apply: "right()",
},
{
label: "penup",
detail: "抬笔",
type: "function",
info: "抬起画笔移动,不留下轨迹,也叫 up",
boost: 82,
apply: "penup()",
},
{
label: "pendown",
detail: "落笔",
type: "function",
info: "放下画笔继续绘制轨迹,也叫 down",
boost: 80,
apply: "pendown()",
},
{
label: "goto",
detail: "移动到坐标",
type: "function",
info: "移动到指定坐标,若笔落下则绘制路径",
boost: 78,
apply: "goto()",
},
{
label: "circle",
detail: "画圆/弧",
type: "function",
info: "以当前位置为起点绘制圆或弧",
boost: 76,
apply: "circle()",
},
{
label: "begin_fill",
detail: "开始填充",
type: "function",
info: "开启填充模式,绘制封闭图形后与 end_fill 搭配使用",
boost: 74,
apply: "begin_fill()",
},
{
label: "end_fill",
detail: "结束填充",
type: "function",
info: "结束填充模式,自动填充自 begin_fill 以来的闭合轨迹",
boost: 72,
apply: "end_fill()",
},
{
label: "color",
detail: "设置颜色",
type: "function",
info: "设置画笔与填充颜色,可单独或同时指定",
boost: 70,
apply: "color()",
},
{
label: "speed",
detail: "设置速度",
type: "function",
info: "设置绘制速度0 为最快",
boost: 68,
apply: "speed()",
},
{
label: "setheading",
detail: "设置朝向",
type: "function",
info: "将海龟朝向设置为绝对角度0 度向东",
boost: 66,
apply: "setheading()",
},
{
label: "home",
detail: "回到原点",
type: "function",
info: "将海龟移动回原点并面向东,保持笔状态",
boost: 64,
apply: "home()",
},
{
label: "clear",
detail: "清空画布",
type: "function",
info: "清除轨迹但不移动海龟位置与状态",
boost: 62,
apply: "clear()",
},
{
label: "reset",
detail: "重置画布",
type: "function",
info: "重置画布并将海龟回到初始位置与方向",
boost: 60,
apply: "reset()",
},
{
label: "hideturtle",
detail: "隐藏海龟",
type: "function",
info: "隐藏光标形状,仅绘制轨迹",
boost: 58,
apply: "hideturtle()",
},
{
label: "showturtle",
detail: "显示海龟",
type: "function",
info: "显示光标形状,恢复可见",
boost: 56,
apply: "showturtle()",
},
{
label: "write",
detail: "写文本",
type: "function",
info: "在当前位置写入文本,可指定对齐和字体",
boost: 54,
apply: "write()",
},
{
label: "dot",
detail: "画点",
type: "function",
info: "绘制一个指定直径与颜色的圆点",
boost: 52,
apply: "dot()",
},
{
label: "pensize",
detail: "设置笔粗",
type: "function",
info: "设置或获取画笔粗细",
boost: 50,
apply: "pensize()",
},
{
label: "pencolor",
detail: "设置笔色",
type: "function",
info: "设置或获取画笔颜色",
boost: 48,
apply: "pencolor()",
},
{
label: "fillcolor",
detail: "设置填充色",
type: "function",
info: "设置或获取填充颜色",
boost: 46,
apply: "fillcolor()",
},
{
label: "bgcolor",
detail: "设置背景色",
type: "function",
info: "设置画布背景颜色",
boost: 44,
apply: "bgcolor()",
},
],
c: [
{
label: "printf",
detail: "格式化输出",
type: "function",
info: "标准输出函数,格式化打印字符串,常配合 %d/%s 等占位符",
boost: 90,
apply: "printf();",
},
{
label: "scanf",
detail: "格式化输入",
type: "function",
info: "标准输入函数,按格式读取数据,使用地址符 & 接收变量",
boost: 88,
apply: "scanf();",
},
{
label: "puts",
detail: "输出字符串",
type: "function",
info: "输出以 \\0 结尾的字符串并自动换行,比 printf 简洁",
boost: 84,
apply: "puts();",
},
{
label: "gets",
detail: "读取字符串",
type: "function",
info: "读取一行字符串到缓冲区(不安全,建议使用 fgets",
boost: 60,
apply: "gets();",
},
{
label: "fgets",
detail: "安全读行",
type: "function",
info: "从文件流读取一行到缓冲区,限制读取长度,避免溢出",
boost: 82,
apply: "fgets();",
},
{
label: "memset",
detail: "内存填充",
type: "function",
info: "将一段内存按字节填充为指定值,常用于初始化数组/结构体",
boost: 80,
apply: "memset();",
},
{
label: "memcpy",
detail: "内存拷贝",
type: "function",
info: "从源地址复制指定字节到目标地址,注意避免重叠",
boost: 78,
apply: "memcpy();",
},
{
label: "strlen",
detail: "字符串长度",
type: "function",
info: "计算以 \\0 结尾的字符串长度(不含终止符)",
boost: 76,
apply: "strlen();",
},
{
label: "strcmp",
detail: "字符串比较",
type: "function",
info: "按字典序比较两个字符串,相等返回 0小于返回负数",
boost: 74,
apply: "strcmp();",
},
{
label: "strcpy",
detail: "字符串拷贝",
type: "function",
info: "将源字符串复制到目标(包含终止符),目标需有足够空间",
boost: 72,
apply: "strcpy();",
},
{
label: "int main",
detail: "程序入口",
type: "keyword",
info: "C 程序入口函数,通常返回 0 表示正常退出",
boost: 70,
},
{
label: "for",
detail: "循环语句",
type: "keyword",
info: "for (init; condition; step) 结构,用于固定次数循环",
boost: 68,
},
{
label: "while",
detail: "条件循环",
type: "keyword",
info: "while (condition) 循环,条件为真时重复执行",
boost: 66,
},
{
label: "if",
detail: "条件判断",
type: "keyword",
info: "if (condition) 分支,可搭配 else / else if",
boost: 64,
},
{
label: "struct",
detail: "结构体定义",
type: "keyword",
info: "定义自定义数据结构,可组合不同类型的成员",
boost: 62,
},
{
label: "typedef",
detail: "类型别名",
type: "keyword",
info: "为已有类型定义别名,提升可读性",
boost: 60,
},
{
label: "const",
detail: "只读限定",
type: "keyword",
info: "声明常量或只读指针,防止意外修改",
boost: 58,
},
{
label: "return",
detail: "返回值",
type: "keyword",
info: "结束函数并返回值main 返回 0 表示成功",
boost: 56,
},
],
cpp: [
{
label: "#include <bits/stdc++.h>",
detail: "常用头文件合集",
type: "keyword",
info: "一次性引入常见 STL 头文件,竞赛常用写法",
boost: 95,
},
{
label: "#include <iostream>",
detail: "标准输入输出",
type: "keyword",
info: "引入 iostream提供 std::cin / std::cout 等流式 IO",
boost: 93,
},
{
label: "using namespace std;",
detail: "使用 std 命名空间",
type: "keyword",
info: "简化 std 前缀的引用,头文件引入后常见写法",
boost: 91,
},
{
label: "std::cout",
detail: "标准输出流",
type: "variable",
info: "搭配 << 输出数据,可用 std::endl 或 '\\n' 换行",
boost: 90,
apply: "std::cout << << std::endl;",
},
{
label: "std::cin",
detail: "标准输入流",
type: "variable",
info: "搭配 >> 读取输入,常与 std::cout 配合提示",
boost: 88,
apply: "std::cin >> ;",
},
{
label: "std::vector",
detail: "动态数组",
type: "class",
info: "可变长顺序容器,支持 push_back / size / iter 访问",
boost: 86,
apply: "std::vector<int> v;",
},
{
label: "std::string",
detail: "字符串类",
type: "class",
info: "可变长字符串支持拼接、substr、find 等操作",
boost: 84,
apply: "std::string s;",
},
{
label: "std::sort",
detail: "排序算法",
type: "function",
info: "std::sort(begin, end, cmp):对区间排序,可自定义比较器",
boost: 82,
apply: "std::sort();",
},
{
label: "std::pair",
detail: "二元组",
type: "class",
info: "存储两个元素的容器,可通过 first/second 访问",
boost: 80,
apply: "std::pair<int, int> p;",
},
{
label: "for(auto &x : container)",
detail: "范围 for 循环",
type: "keyword",
info: "现代 C++ 遍历容器写法,可用 const auto& 避免拷贝",
boost: 78,
apply: "for (auto &x : ) {\n \n}",
},
{
label: "int main",
detail: "程序入口",
type: "keyword",
info: "C++ 程序主函数模板,返回 0 表示正常退出",
boost: 76,
apply: "int main() {\n \n return 0;\n}",
},
],
} }
export function enhanceCompletion(language: LANGUAGE): CompletionSource { export function enhanceCompletion(language: LANGUAGE): CompletionSource {
@@ -907,39 +31,39 @@ export function enhanceCompletion(language: LANGUAGE): CompletionSource {
const word = context.matchBefore(/\w+/) const word = context.matchBefore(/\w+/)
if (!word) return null if (!word) return null
const completions: Completion[] = ( const completions: Completion[] = (chineseAnnotations[language] || []).map(
chineseAnnotations[language] || [] (completion) => {
).map((completion) => { const insertText =
const insertText = typeof completion.apply === "string"
typeof completion.apply === "string" ? completion.apply
? completion.apply : completion.label
: completion.label const cursorOffset = insertText.includes("(")
const cursorOffset = insertText.includes("(") ? insertText.indexOf("(") + 1
? insertText.indexOf("(") + 1 : insertText.length
: insertText.length
if ( if (
(completion.type === "function" || completion.type === "method") && (completion.type === "function" || completion.type === "method") &&
insertText.includes(")") insertText.includes(")")
) { ) {
return { return {
...completion, ...completion,
apply: ( apply: (
view: EditorView, view: EditorView,
_c: Completion, _c: Completion,
from: number, from: number,
to: number, to: number,
) => { ) => {
view.dispatch({ view.dispatch({
changes: { from, to, insert: insertText }, changes: { from, to, insert: insertText },
selection: { anchor: from + cursorOffset }, selection: { anchor: from + cursorOffset },
}) })
}, },
}
} }
}
return completion return completion
}) },
)
return { return {
from: word.from, from: word.from,

257
src/extensions/c.ts Normal file
View File

@@ -0,0 +1,257 @@
export const c = [
{
label: "printf",
detail: "格式化输出",
type: "function",
info: "标准输出函数,按格式打印,搭配 %d/%s 等占位符",
boost: 90,
apply: "printf();",
},
{
label: "scanf",
detail: "格式化输入",
type: "function",
info: "标准输入函数,按格式读取,用 & 取地址接收变量",
boost: 88,
apply: "scanf();",
},
{
label: "puts",
detail: "输出字符串",
type: "function",
info: "输出以 \\0 结尾的字符串并自动换行,比 printf 简洁",
boost: 84,
apply: "puts();",
},
{
label: "gets",
detail: "读取字符串",
type: "function",
info: "读取一行字符串到缓冲区(不安全,建议使用 fgets",
boost: 60,
apply: "gets();",
},
{
label: "fgets",
detail: "安全读行",
type: "function",
info: "从文件流读取一行到缓冲区,限制长度,避免溢出",
boost: 82,
apply: "fgets();",
},
{
label: "memset",
detail: "内存填充",
type: "function",
info: "按字节把内存填为指定值,常用于初始化数组或结构体",
boost: 80,
apply: "memset();",
},
{
label: "memcpy",
detail: "内存拷贝",
type: "function",
info: "从源地址复制指定字节到目标地址,避免重叠",
boost: 78,
apply: "memcpy();",
},
{
label: "strlen",
detail: "字符串长度",
type: "function",
info: "计算以 \\0 结尾的字符串长度(不含终止符)",
boost: 76,
apply: "strlen();",
},
{
label: "strcmp",
detail: "字符串比较",
type: "function",
info: "按字典序比较两个字符串,相等为 0小于返回负数",
boost: 74,
apply: "strcmp();",
},
{
label: "strcpy",
detail: "字符串拷贝",
type: "function",
info: "把源字符串复制到目标(含终止符),目标要有足够空间",
boost: 72,
apply: "strcpy();",
},
{
label: "int main",
detail: "程序入口",
type: "keyword",
info: "C 程序入口,通常返回 0 表示正常退出",
boost: 70,
},
{
label: "for",
detail: "循环语句",
type: "keyword",
info: "for (init; condition; step) 结构,用于固定次数循环",
boost: 68,
},
{
label: "while",
detail: "条件循环",
type: "keyword",
info: "while (condition) 条件循环,条件真则执行",
boost: 66,
},
{
label: "if",
detail: "条件判断",
type: "keyword",
info: "if (condition) 条件分支,可配 else/else if",
boost: 64,
},
{
label: "struct",
detail: "结构体定义",
type: "keyword",
info: "定义结构体,可组合不同类型的成员",
boost: 62,
},
{
label: "typedef",
detail: "类型别名",
type: "keyword",
info: "为已有类型起别名,提升可读性",
boost: 60,
},
{
label: "const",
detail: "只读限定",
type: "keyword",
info: "声明常量或只读指针,防止被修改",
boost: 58,
},
{
label: "return",
detail: "返回值",
type: "keyword",
info: "结束函数并返回值main 返回 0 表示成功",
boost: 56,
},
{
label: "break",
detail: "终止循环",
type: "keyword",
info: "立即退出最近的 for/while/do 循环体",
boost: 54,
},
{
label: "continue",
detail: "继续下一次循环",
type: "keyword",
info: "跳过当前循环余下语句,开始下一轮迭代",
boost: 52,
},
{
label: "do",
detail: "do-while 循环",
type: "keyword",
info: "do { ... } while(condition); 后置条件循环,至少执行一次",
boost: 50,
},
{
label: "else",
detail: "分支兜底",
type: "keyword",
info: "if/else if/else 结构中的兜底分支",
boost: 48,
},
{
label: "switch",
detail: "多分支选择",
type: "keyword",
info: "switch (expr) { case ... } 多分支选择结构,常配合 break",
boost: 46,
},
{
label: "case",
detail: "分支标签",
type: "keyword",
info: "switch 中的具体匹配分支,常在末尾使用 break",
boost: 44,
},
{
label: "default",
detail: "默认分支",
type: "keyword",
info: "switch 中未匹配任何 case 时执行的分支",
boost: 42,
},
{
label: "goto",
detail: "无条件跳转",
type: "keyword",
info: "跳转到指定标签位置,需谨慎使用以避免可读性问题",
boost: 40,
},
{
label: "sizeof",
detail: "求字节大小",
type: "keyword",
info: "返回类型或表达式所占字节数,编译期求值",
boost: 38,
},
{
label: "int",
detail: "整数类型",
type: "keyword",
info: "声明整型变量或返回值,通常占 4 字节",
boost: 36,
},
{
label: "char",
detail: "字符类型",
type: "keyword",
info: "声明字符或字节型变量,通常占 1 字节",
boost: 34,
},
{
label: "double",
detail: "双精度类型",
type: "keyword",
info: "声明双精度浮点变量,通常占 8 字节",
boost: 32,
},
{
label: "float",
detail: "单精度类型",
type: "keyword",
info: "声明单精度浮点变量,通常占 4 字节",
boost: 30,
},
{
label: "void",
detail: "空类型",
type: "keyword",
info: "表示无返回值或无类型指针void*",
boost: 28,
},
{
label: "unsigned",
detail: "无符号修饰",
type: "keyword",
info: "与整型/字符型组合表示无符号数,扩大正数范围",
boost: 26,
},
{
label: "long",
detail: "长整型修饰",
type: "keyword",
info: "与整型组合表示更大范围long/long long",
boost: 24,
},
{
label: "short",
detail: "短整型修饰",
type: "keyword",
info: "与整型组合表示较小范围的整数类型",
boost: 22,
},
]

87
src/extensions/cpp.ts Normal file
View File

@@ -0,0 +1,87 @@
export const cpp = [
{
label: "#include <bits/stdc++.h>",
detail: "常用头文件合集",
type: "keyword",
info: "一次性引入常见 STL 头文件,竞赛常用写法",
boost: 95,
},
{
label: "#include <iostream>",
detail: "标准输入输出",
type: "keyword",
info: "引入 iostream提供 std::cin / std::cout 等流式 IO",
boost: 93,
},
{
label: "using namespace std;",
detail: "使用 std 命名空间",
type: "keyword",
info: "简化 std 前缀的引用,头文件引入后常见写法",
boost: 91,
},
{
label: "std::cout",
detail: "标准输出流",
type: "variable",
info: "搭配 << 输出数据,可用 std::endl 或 '\\n' 换行",
boost: 90,
apply: "std::cout << << std::endl;",
},
{
label: "std::cin",
detail: "标准输入流",
type: "variable",
info: "搭配 >> 读取输入,常与 std::cout 配合提示",
boost: 88,
apply: "std::cin >> ;",
},
{
label: "std::vector",
detail: "动态数组",
type: "class",
info: "可变长顺序容器,支持 push_back / size / iter 访问",
boost: 86,
apply: "std::vector<int> v;",
},
{
label: "std::string",
detail: "字符串类",
type: "class",
info: "可变长字符串支持拼接、substr、find 等操作",
boost: 84,
apply: "std::string s;",
},
{
label: "std::sort",
detail: "排序算法",
type: "function",
info: "std::sort(begin, end, cmp):对区间排序,可自定义比较器",
boost: 82,
apply: "std::sort();",
},
{
label: "std::pair",
detail: "二元组",
type: "class",
info: "存储两个元素的容器,可通过 first/second 访问",
boost: 80,
apply: "std::pair<int, int> p;",
},
{
label: "for(auto &x : container)",
detail: "范围 for 循环",
type: "keyword",
info: "现代 C++ 遍历容器写法,可用 const auto& 避免拷贝",
boost: 78,
apply: "for (auto &x : ) {\n \n}",
},
{
label: "int main",
detail: "程序入口",
type: "keyword",
info: "C++ 程序主函数模板,返回 0 表示正常退出",
boost: 76,
apply: "int main() {\n \n return 0;\n}",
},
]

608
src/extensions/python.ts Normal file
View File

@@ -0,0 +1,608 @@
export const python = [
{
label: "print",
detail: "打印输出",
type: "function",
info: "最常用的输出函数把内容打印到屏幕sep 控制分隔符end 控制行尾。",
boost: 100,
apply: "print()",
},
{
label: "input",
detail: "读取输入",
type: "function",
info: "读取一行输入并返回字符串,可以传入提示文字。",
boost: 99,
apply: "input()",
},
{
label: "len",
detail: "获取长度",
type: "function",
info: "返回序列或集合的长度,常用来统计列表、字符串中有多少个元素。",
boost: 90,
apply: "len()",
},
{
label: "range",
detail: "生成整数序列",
type: "function",
info: "生成整数序列,支持起点、终点和步长,常配合 for 循环遍历次数。",
boost: 85,
apply: "range()",
},
{
label: "enumerate",
detail: "枚举索引与值",
type: "function",
info: "遍历时同时得到索引和值,可用 start 指定起始编号,适合需要编号输出的场景。",
boost: 82,
apply: "enumerate()",
},
{
label: "zip",
detail: "并行遍历",
type: "function",
info: "把多个可迭代对象按位置打包成元组并行遍历,长度取最短的序列。",
boost: 80,
apply: "zip()",
},
{
label: "map",
detail: "映射函数",
type: "function",
info: "把函数作用到序列每个元素,返回惰性迭代器,需要 list() 展开后才能看到结果。",
boost: 78,
apply: "map()",
},
{
label: "filter",
detail: "过滤元素",
type: "function",
info: "保留函数返回真值的元素,返回惰性迭代器,常用于筛选出符合条件的数据。",
boost: 76,
apply: "filter()",
},
{
label: "sorted",
detail: "排序",
type: "function",
info: "返回排好序的新列表,支持 key 排序函数和 reverse 逆序,不会修改原序列。",
boost: 74,
apply: "sorted()",
},
{
label: "sum",
detail: "求和",
type: "function",
info: "对可迭代对象求和,可设置初始值,常用于数字累计与前缀和。",
boost: 72,
apply: "sum()",
},
{
label: "open",
detail: "文件读写",
type: "function",
info: "打开文件获得文件对象,常配合 with 自动关闭,支持读写追加等多种模式。",
boost: 70,
apply: "open()",
},
{
label: "abs",
detail: "绝对值",
type: "function",
info: "返回数字的绝对值,把负数变成正数,常用于距离或差值计算。",
boost: 68,
apply: "abs()",
},
{
label: "round",
detail: "四舍五入",
type: "function",
info: "按指定小数位四舍五入,默认保留到整数,适合处理成绩或金额保留位数。",
boost: 66,
apply: "round()",
},
{
label: "isinstance",
detail: "类型检查",
type: "function",
info: "判断对象是否属于某个类型或类型元组,常用于分支处理不同数据。",
boost: 64,
apply: "isinstance()",
},
{
label: "type",
detail: "获取类型",
type: "function",
info: "返回对象的类型,或用三个参数动态创建类型,用来了解变量真实类型。",
boost: 62,
apply: "type()",
},
{
label: "list",
detail: "列表构造",
type: "function",
info: "把可迭代对象转换为列表,或创建空列表存放数据,支持列表推导式。",
boost: 60,
apply: "list()",
},
{
label: "dict",
detail: "字典构造",
type: "function",
info: "根据映射或键值对序列创建字典,用来存储键值对信息。",
boost: 58,
apply: "dict()",
},
{
label: "set",
detail: "集合构造",
type: "function",
info: "把可迭代对象转换为集合,自动去重,适合判重和集合运算。",
boost: 56,
apply: "set()",
},
{
label: "tuple",
detail: "元组构造",
type: "function",
info: "把可迭代对象转换为元组,或创建不可变的序列,用作安全的组合数据。",
boost: 54,
apply: "tuple()",
},
{
label: "int",
detail: "转整数",
type: "function",
info: "把参数转为整数,支持进制转换,如 int('101', 2) 表示二进制转十进制。",
boost: 74,
apply: "int()",
},
{
label: "float",
detail: "转浮点数",
type: "function",
info: "把参数转为浮点数,接受字符串或数字,常用于保留小数的计算。",
boost: 72,
apply: "float()",
},
{
label: "str",
detail: "转字符串",
type: "function",
info: "把对象转为字符串,常用于输出、拼接或写入文件。",
boost: 70,
apply: "str()",
},
{
label: "bool",
detail: "转布尔值",
type: "function",
info: "按真值规则转为 True/False空对象一般为 False常用于条件判断。",
boost: 68,
apply: "bool()",
},
{
label: "def",
detail: "定义函数",
type: "keyword",
info: "定义函数,可写位置参数、关键字参数和默认值,是封装和复用代码的基础。",
boost: 52,
},
{
label: "class",
detail: "定义类",
type: "keyword",
info: "定义类,支持继承和魔术方法,用来创建自定义数据类型。",
boost: 50,
},
{
label: "with",
detail: "上下文管理",
type: "keyword",
info: "进入上下文管理器,自动处理进入与退出,常用于文件、锁等需要收尾的资源。",
boost: 48,
},
{
label: "try",
detail: "异常捕获",
type: "keyword",
info: "开始异常处理代码块,后面接 except/finally/else防止程序因错误直接退出。",
boost: 46,
},
{
label: "except",
detail: "处理异常",
type: "keyword",
info: "捕获并处理指定异常,与 try 连用,让程序能优雅处理输入或运行时错误。",
boost: 44,
},
{
label: "finally",
detail: "收尾",
type: "keyword",
info: "无论是否出现异常都会执行的收尾代码块,常用于关闭文件或释放资源。",
boost: 42,
},
{
label: "import",
detail: "导入模块",
type: "keyword",
info: "导入模块或包中的名称,可用 as 取别名,便于使用标准库或第三方库。",
boost: 40,
},
{
label: "from",
detail: "按需导入",
type: "keyword",
info: "从模块中按名称导入对象,可与 import/as 组合,减少书写模块前缀。",
boost: 38,
},
{
label: "return",
detail: "返回值",
type: "keyword",
info: "结束函数并返回一个值,不写时默认返回 None。",
boost: 36,
},
{
label: "for",
detail: "for 循环",
type: "keyword",
info: "逐项遍历,可以配合 range 使用。",
boost: 45,
},
{
label: "while",
detail: "while 循环",
type: "keyword",
info: "条件循环,条件为真时执行,支持 break/continue 和 else 分支。",
boost: 43,
},
{
label: "if",
detail: "条件分支",
type: "keyword",
info: "if 判断,条件成立执行对应语句,可与 elif/else 组成多分支。",
boost: 41,
apply: "if ",
},
{
label: "elif",
detail: "多分支判断",
type: "keyword",
info: "否则如果,用来依次判断多个条件,让逻辑更清晰。",
boost: 39,
apply: "elif ",
},
{
label: "else",
detail: "否则",
type: "keyword",
info: "否则,在前面条件不满足或循环未提前退出时执行。",
boost: 37,
apply: "else:",
},
{
label: "break",
detail: "退出循环",
type: "keyword",
info: "立即终止最近的循环for 或 while跳出当前循环体。",
boost: 35,
},
{
label: "continue",
detail: "跳过本次",
type: "keyword",
info: "结束本轮循环迭代,直接开始下一轮条件判断。",
boost: 33,
},
{
label: "pass",
detail: "空操作占位",
type: "keyword",
info: "占位语句,不执行任何操作,常用于占位或编写空的函数/类体。",
boost: 31,
},
{
label: "lambda",
detail: "匿名函数",
type: "keyword",
info: "定义轻量级匿名函数,语法 lambda 参数: 表达式,返回表达式结果,适合简短逻辑。",
boost: 29,
},
{
label: "yield",
detail: "生成器产出",
type: "keyword",
info: "在函数中产出一个值并暂停状态,把函数变成生成器,可配合 yield from 继续产出。",
boost: 27,
},
{
label: "global",
detail: "声明全局变量",
type: "keyword",
info: "在函数内部声明写入模块级变量的权限,用于修改外层的全局变量。",
boost: 25,
},
{
label: "nonlocal",
detail: "声明外层变量",
type: "keyword",
info: "在嵌套函数中声明使用最近外层作用域的变量,便于修改闭包变量。",
boost: 23,
},
{
label: "True",
detail: "布尔真",
type: "keyword",
info: "布尔常量 True与 False/None 一起常用于条件表达式和逻辑判断。",
boost: 21,
},
{
label: "False",
detail: "布尔假",
type: "keyword",
info: "布尔常量 False在逻辑判断中表示假值常作为条件的否定结果。",
boost: 19,
},
{
label: "None",
detail: "空值对象",
type: "keyword",
info: "表示空值或缺失值的单例对象,常用于默认参数或占位。",
boost: 17,
},
{
label: "and",
detail: "逻辑与",
type: "keyword",
info: "逻辑与运算符,短路求值,返回最后被求值的操作数,常用于多条件同时成立。",
boost: 15,
},
{
label: "or",
detail: "逻辑或",
type: "keyword",
info: "逻辑或运算符,短路求值,返回第一个真值或最后一个操作数,常用于设默认值。",
boost: 14,
},
{
label: "not",
detail: "逻辑非",
type: "keyword",
info: "逻辑非运算符,返回布尔取反结果,把真变假、假变真。",
boost: 13,
},
{
label: "in",
detail: "成员测试",
type: "keyword",
info: "判断元素是否在序列、集合或字典中,返回布尔结果,常用于查找。",
boost: 12,
},
{
label: "is",
detail: "身份比较",
type: "keyword",
info: "比较两个对象是否是同一个对象,常用于和 None 比较以避免误判。",
boost: 11,
},
{
label: "append",
detail: "列表追加",
type: "method",
info: "在列表尾部添加一个新元素,等价于 list.append(value)。",
boost: 48,
apply: "append()",
},
{
label: "insert",
detail: "列表插入",
type: "method",
info: "在指定位置插入元素,语法 list.insert(index, value)。",
boost: 46,
apply: "insert()",
},
{
label: "remove",
detail: "删除匹配值",
type: "method",
info: "删除列表中第一次出现的指定值,不存在会抛出异常 ValueError。",
boost: 44,
apply: "remove()",
},
{
label: "pop",
detail: "弹出元素",
type: "method",
info: "移除并返回列表指定位置(默认尾部)的元素,用于栈或队列操作。",
boost: 42,
apply: "pop()",
},
{
label: "count",
detail: "统计次数",
type: "method",
info: "返回某个对象在列表中出现的次数,常用于频次统计。",
boost: 40,
apply: "count()",
},
{
label: "reverse",
detail: "反转列表",
type: "method",
info: "原地反转列表中元素的顺序,常与切片 [::-1] 效果类似。",
boost: 38,
apply: "reverse()",
},
{
label: "sort",
detail: "列表排序",
type: "method",
info: "对列表进行原地排序,可指定 key 排序函数和 reverse 是否倒序。",
boost: 36,
apply: "sort()",
},
{
label: "add",
detail: "集合添加",
type: "method",
info: "向集合添加单个元素,若元素已存在则忽略,保持集合去重特性。",
boost: 50,
apply: "add()",
},
{
label: "clear",
detail: "清空集合",
type: "method",
info: "移除集合中所有元素,变成一个空集合。",
boost: 48,
apply: "clear()",
},
{
label: "keys",
detail: "字典键",
type: "method",
info: "返回字典键的可迭代视图,用于遍历所有键。",
boost: 42,
apply: "keys()",
},
{
label: "values",
detail: "字典值",
type: "method",
info: "返回字典值的可迭代视图,用于遍历所有值。",
boost: 40,
apply: "values()",
},
{
label: "split",
detail: "字符串切分",
type: "method",
info: "按分隔符切分字符串,返回列表,默认按空白字符分割。",
boost: 52,
apply: "split()",
},
{
label: "replace",
detail: "字符串替换",
type: "method",
info: "把字符串中的子串替换为新内容,可限制替换次数。",
boost: 50,
apply: "replace()",
},
{
label: "format",
detail: "格式化字符串",
type: "method",
info: "使用占位符或命名参数进行字符串格式化,便于拼接变量输出。",
boost: 48,
apply: "format()",
},
{
label: "strip",
detail: "去首尾字符",
type: "method",
info: "移除字符串首尾指定字符,默认移除空白符,常用于清理输入。",
boost: 46,
apply: "strip()",
},
{
label: "lower",
detail: "转小写",
type: "method",
info: "把字符串中的字母转换为小写形式,常用于不区分大小写比较。",
boost: 44,
apply: "lower()",
},
{
label: "upper",
detail: "转大写",
type: "method",
info: "把字符串中的字母转换为大写形式。",
boost: 42,
apply: "upper()",
},
{
label: "swapcase",
detail: "大小写互换",
type: "method",
info: "把字符串中的大小写字母互换,便于切换展示风格。",
boost: 40,
apply: "swapcase()",
},
{
label: "find",
detail: "查找子串位置",
type: "method",
info: "返回子串首次出现的索引,未找到返回 -1适合安全查找。",
boost: 38,
apply: "find()",
},
{
label: "index",
detail: "查找子串索引",
type: "method",
info: "返回子串首次出现的索引,未找到会抛出异常 ValueError。",
boost: 36,
apply: "index()",
},
{
label: "startswith",
detail: "前缀判断",
type: "method",
info: "判断字符串是否以指定前缀开头,可指定检查的范围切片。",
boost: 34,
apply: "startswith()",
},
{
label: "endswith",
detail: "后缀判断",
type: "method",
info: "判断字符串是否以指定后缀结尾,可指定范围,常用于文件名处理。",
boost: 32,
apply: "endswith()",
},
{
label: "isalnum",
detail: "是否字母数字",
type: "method",
info: "检测字符串是否只由字母和数字组成,常用于基础输入校验。",
boost: 30,
apply: "isalnum()",
},
{
label: "isalpha",
detail: "是否字母",
type: "method",
info: "检测字符串是否只由字母组成,用来判断名字等只含字母的场景。",
boost: 28,
apply: "isalpha()",
},
{
label: "isdigit",
detail: "是否数字",
type: "method",
info: "检测字符串是否只由数字组成,用来判断输入是否为纯数字。",
boost: 26,
apply: "isdigit()",
},
{
label: "islower",
detail: "是否全小写",
type: "method",
info: "检测字符串是否全部由小写字母组成且至少有一个字母。",
boost: 24,
apply: "islower()",
},
{
label: "isupper",
detail: "是否全大写",
type: "method",
info: "检测字符串中所有字母是否都是大写且至少有一个字母。",
boost: 22,
apply: "isupper()",
},
]

194
src/extensions/turtle.ts Normal file
View File

@@ -0,0 +1,194 @@
export const turtle = [
{
label: "forward",
detail: "向前移动",
type: "function",
info: "让海龟沿当前朝向向前移动指定距离",
boost: 90,
apply: "forward()",
},
{
label: "backward",
detail: "向后移动",
type: "function",
info: "让海龟沿当前朝向向后移动指定距离",
boost: 88,
apply: "backward()",
},
{
label: "left",
detail: "左转",
type: "function",
info: "让海龟左转指定角度(度数)",
boost: 86,
apply: "left()",
},
{
label: "right",
detail: "右转",
type: "function",
info: "让海龟右转指定角度(度数)",
boost: 84,
apply: "right()",
},
{
label: "penup",
detail: "抬笔",
type: "function",
info: "抬起画笔移动,不留下轨迹(也叫 up",
boost: 82,
apply: "penup()",
},
{
label: "pendown",
detail: "落笔",
type: "function",
info: "放下画笔继续绘制轨迹(也叫 down",
boost: 80,
apply: "pendown()",
},
{
label: "goto",
detail: "移动到坐标",
type: "function",
info: "移动到指定坐标,若画笔放下则绘制路径",
boost: 78,
apply: "goto()",
},
{
label: "circle",
detail: "画圆/弧",
type: "function",
info: "以当前位置为起点绘制圆或弧",
boost: 76,
apply: "circle()",
},
{
label: "begin_fill",
detail: "开始填充",
type: "function",
info: "开启填充模式,绘制封闭图形后与 end_fill 配合使用",
boost: 74,
apply: "begin_fill()",
},
{
label: "end_fill",
detail: "结束填充",
type: "function",
info: "结束填充模式,自动填充自 begin_fill 以来的闭合轨迹",
boost: 72,
apply: "end_fill()",
},
{
label: "color",
detail: "设置颜色",
type: "function",
info: "设置画笔与填充颜色,可单独或同时指定",
boost: 70,
apply: "color()",
},
{
label: "speed",
detail: "设置速度",
type: "function",
info: "设置绘制速度1 最慢10 最快",
boost: 68,
apply: "speed()",
},
{
label: "setheading",
detail: "设置朝向",
type: "function",
info: "将海龟朝向设置为绝对角度0 度向东90 度向北",
boost: 66,
apply: "setheading()",
},
{
label: "home",
detail: "回到原点",
type: "function",
info: "将海龟移动回原点并面向东,保持画笔状态",
boost: 64,
apply: "home()",
},
{
label: "clear",
detail: "清空画布",
type: "function",
info: "清除轨迹但不移动海龟位置与状态",
boost: 62,
apply: "clear()",
},
{
label: "reset",
detail: "重置画布",
type: "function",
info: "重置画布并将海龟回到初始位置与方向",
boost: 60,
apply: "reset()",
},
{
label: "hideturtle",
detail: "隐藏海龟",
type: "function",
info: "隐藏光标形状,仅绘制轨迹",
boost: 58,
apply: "hideturtle()",
},
{
label: "showturtle",
detail: "显示海龟",
type: "function",
info: "显示光标形状,恢复可见",
boost: 56,
apply: "showturtle()",
},
{
label: "write",
detail: "写文本",
type: "function",
info: "在当前位置写入文本,可指定对齐和字体",
boost: 54,
apply: "write()",
},
{
label: "dot",
detail: "画点",
type: "function",
info: "绘制一个指定直径与颜色的圆点",
boost: 52,
apply: "dot()",
},
{
label: "pensize",
detail: "设置笔粗",
type: "function",
info: "设置或获取画笔粗细",
boost: 50,
apply: "pensize()",
},
{
label: "pencolor",
detail: "设置笔色",
type: "function",
info: "设置或获取画笔颜色",
boost: 48,
apply: "pencolor()",
},
{
label: "fillcolor",
detail: "设置填充色",
type: "function",
info: "设置或获取填充颜色",
boost: 46,
apply: "fillcolor()",
},
{
label: "bgcolor",
detail: "设置背景色",
type: "function",
info: "设置画布背景颜色",
boost: 44,
apply: "bgcolor()",
},
]

View File

@@ -0,0 +1,122 @@
/// <reference lib="webworker" />
// @ts-ignore
import * as Sk from "skulpt"
type RunRequest = {
id: number
source: string
stdin: string
timeoutMs: number
}
type RunResponse = {
id: number
status: number
output: string
}
const exceptionNameToCn: Record<string, string> = {
SyntaxError: "格式错误",
IndentationError: "格式错误",
TabError: "格式错误",
NameError: "变量命名错误",
TypeError: "类型错误",
ValueError: "值错误",
IndexError: "索引错误",
KeyError: "键错误",
ZeroDivisionError: "除零错误",
AttributeError: "属性错误",
ImportError: "导入错误",
ModuleNotFoundError: "模块未找到",
RuntimeError: "运行错误",
}
function translateSkulptError(
name: string,
message: string,
isCompileError: boolean,
) {
if (isCompileError) return "代码格式错误"
if (/exceeded run time limit/i.test(message)) return "运行超时"
const cnName = exceptionNameToCn[name] ?? ""
const translatedMessage = String(message ?? "")
.replace(/No module named ([^\s]+)/gi, "没有名为 $1 的模块")
.replace(/integer division or modulo by zero/gi, "不能除以零")
.replace(/name '([^']+)' is not defined/gi, "变量 $1 未定义")
.replace(/list index out of range/gi, "列表下标越界")
.replace(/index out of range/gi, "索引越界")
.trim()
if (cnName)
return translatedMessage ? `${cnName}${translatedMessage}` : cnName
return translatedMessage || "运行错误"
}
function skulptRead(path: string) {
const builtinFiles = (Sk as any).builtinFiles
const files = builtinFiles?.files ?? builtinFiles?.["files"]
if (!files) throw new Error("skulpt-stdlib.js has not been loaded")
if (files[path] === undefined) throw new Error(`File not found: '${path}'`)
return files[path]
}
async function runPythonWithSkulpt(
source: string,
stdin: string,
timeoutMs: number,
) {
const stdout: string[] = []
const inputLines = (stdin ?? "").split("\n")
let inputIndex = 0
const normalizedSource = String(source ?? "").replace(/\r\n/g, "\n")
;(Sk as any).configure({
output: (text: string) => stdout.push(String(text)),
read: skulptRead,
inputfun: () => String(inputLines[inputIndex++] ?? ""),
inputfunTakesPrompt: true,
__future__: (Sk as any).python3,
})
;(Sk as any).execLimit = Math.max(1, Number(timeoutMs) || 1)
;(Sk as any).execStart = new Date()
try {
await (Sk as any).misceval.asyncToPromise(() =>
(Sk as any).importMainWithBody("<stdin>", false, normalizedSource, true),
)
return { status: 3, output: stdout.join("").trimEnd() }
} catch (err: any) {
const name = String(err?.tp$name ?? err?.name ?? "")
const message = String(
err?.tp$str?.()?.v ?? err?.message ?? err?.toString?.() ?? err ?? "",
)
const isCompileError =
name === "SyntaxError" ||
name === "IndentationError" ||
name === "TabError" ||
/SyntaxError|IndentationError|TabError/i.test(message)
const formattedError = translateSkulptError(name, message, isCompileError)
return {
status: isCompileError ? 6 : 11,
output: (stdout.join("") + (stdout.length ? "\n" : "") + formattedError)
.trim()
.replace(/\r\n/g, "\n"),
}
}
}
self.onmessage = async (event: MessageEvent<RunRequest>) => {
const { id, source, stdin, timeoutMs } = event.data
try {
const result = await runPythonWithSkulpt(source, stdin, timeoutMs)
;(self as any).postMessage({ id, ...result } satisfies RunResponse)
} catch (err: any) {
const output = String(err?.message ?? err?.toString?.() ?? err ?? "")
;(self as any).postMessage({ id, status: 11, output } satisfies RunResponse)
}
}