Skip to content

Commit 1ec505d

Browse files
committed
add cpp & c support
1 parent 5f1adb2 commit 1ec505d

4 files changed

Lines changed: 53 additions & 0 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"@codemirror/autocomplete": "^6.18.6",
1515
"@codemirror/basic-setup": "^0.20.0",
1616
"@codemirror/commands": "^6.8.1",
17+
"@codemirror/lang-cpp": "^6.0.3",
1718
"@codemirror/lang-css": "^6.3.1",
1819
"@codemirror/lang-go": "^6.0.1",
1920
"@codemirror/lang-html": "^6.4.9",

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/CodeEditor.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import { sql } from "@codemirror/lang-sql";
3737
import { markdown } from "@codemirror/lang-markdown";
3838
import { xml } from "@codemirror/lang-xml";
3939
import { yaml } from "@codemirror/lang-yaml";
40+
import { cpp } from "@codemirror/lang-cpp";
4041
4142
// Legacy modes for additional languages
4243
import { StreamLanguage } from "@codemirror/language";
@@ -97,6 +98,8 @@ const languageMap: Record<string, any> = {
9798
go: go(),
9899
rust: rust(),
99100
php: php(),
101+
cpp: cpp(),
102+
c: cpp(), // C 语言使用 C++ 的语法高亮
100103
csharp: StreamLanguage.define(csharp),
101104
102105
// 数据库

src/utils/language.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ export const languageIcons: Record<string, string> = {
99
html: "🌐",
1010
go: "🐹",
1111
java: "☕",
12+
cpp: "⚡",
13+
c: "🔵",
1214
php: "🐘",
1315
sql: "🗄️",
1416
shell: "🐚",
@@ -29,6 +31,8 @@ export const languages = [
2931
"html",
3032
"go",
3133
"java",
34+
"cpp",
35+
"c",
3236
"php",
3337
"sql",
3438
"shell",
@@ -165,6 +169,48 @@ export function detectLanguage(content: string): string {
165169
return "java";
166170
}
167171

172+
// 检测 C++
173+
if (
174+
content.includes("#include") ||
175+
content.includes("std::") ||
176+
content.includes("namespace ") ||
177+
content.includes("class ") ||
178+
content.includes("template<") ||
179+
content.includes("cout <<") ||
180+
content.includes("cin >>") ||
181+
content.includes("vector<") ||
182+
content.includes("string ") ||
183+
content.includes("int main(") ||
184+
content.includes("void ") ||
185+
content.includes("const ") ||
186+
content.includes("&") ||
187+
content.includes("->")
188+
) {
189+
return "cpp";
190+
}
191+
192+
// 检测 C
193+
if (
194+
content.includes("#include") ||
195+
content.includes("stdio.h") ||
196+
content.includes("stdlib.h") ||
197+
content.includes("printf(") ||
198+
content.includes("scanf(") ||
199+
content.includes("malloc(") ||
200+
content.includes("free(") ||
201+
content.includes("struct ") ||
202+
content.includes("typedef ") ||
203+
content.includes("int main(") ||
204+
content.includes("void ") ||
205+
content.includes("char ") ||
206+
content.includes("int ") ||
207+
content.includes("float ") ||
208+
content.includes("double ") ||
209+
content.includes("return 0;")
210+
) {
211+
return "c";
212+
}
213+
168214
// 检测 PHP
169215
if (
170216
content.includes("<?php") ||

0 commit comments

Comments
 (0)