Skip to content

Commit 201c4a2

Browse files
committed
feat (language): 支持 Go 语言
1 parent 2c8b0a5 commit 201c4a2

7 files changed

Lines changed: 277 additions & 1 deletion

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@ pnpm-lock.yaml
2626
*.csv
2727
*.json
2828
!config*.json
29+
go.mod

public/icons/go.svg

Lines changed: 41 additions & 0 deletions
Loading

src-tauri/src/plugins/go.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
use super::{LanguagePlugin, PluginConfig};
2+
3+
pub struct GoPlugin;
4+
5+
impl LanguagePlugin for GoPlugin {
6+
fn get_order(&self) -> i32 {
7+
4
8+
}
9+
10+
fn get_language_name(&self) -> &'static str {
11+
"Go"
12+
}
13+
14+
fn get_language_key(&self) -> &'static str {
15+
"go"
16+
}
17+
18+
fn get_version_args(&self) -> Vec<&'static str> {
19+
vec!["version"]
20+
}
21+
22+
fn get_path_command(&self) -> String {
23+
"package main\nimport (\"fmt\"\n\"runtime\")\nfunc main() { fmt.Println(runtime.GOROOT()) }"
24+
.to_string()
25+
}
26+
27+
fn get_default_config(&self) -> PluginConfig {
28+
PluginConfig {
29+
enabled: true,
30+
language: String::from("go"),
31+
before_compile: Some(String::from("go mod init temp 2>/dev/null || true")),
32+
extension: String::from("go"),
33+
execute_home: None,
34+
run_command: Some(String::from("go run $filename")),
35+
after_compile: None,
36+
template: Some(String::from(
37+
"package main\n\nimport \"fmt\"\n\nfunc main() {\n\t// Your code here\n\tfmt.Println(\"Hello, Go!\")\n}\n",
38+
)),
39+
timeout: Some(30),
40+
}
41+
}
42+
43+
fn get_default_command(&self) -> String {
44+
self.get_config()
45+
.and_then(|config| config.run_command)
46+
.unwrap_or_else(|| "go run".to_string())
47+
}
48+
49+
fn get_file_extension(&self) -> String {
50+
self.get_config()
51+
.map(|config| config.extension.clone())
52+
.unwrap_or_else(|| "go".to_string())
53+
}
54+
}

src-tauri/src/plugins/manager.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use super::{
2-
LanguagePlugin, PluginConfig, nodejs::NodeJSPlugin, python2::Python2Plugin,
2+
LanguagePlugin, PluginConfig, go::GoPlugin, nodejs::NodeJSPlugin, python2::Python2Plugin,
33
python3::Python3Plugin,
44
};
55
use std::collections::HashMap;
@@ -15,6 +15,7 @@ impl PluginManager {
1515
plugins.insert("python2".to_string(), Box::new(Python2Plugin));
1616
plugins.insert("python3".to_string(), Box::new(Python3Plugin));
1717
plugins.insert("nodejs".to_string(), Box::new(NodeJSPlugin));
18+
plugins.insert("go".to_string(), Box::new(GoPlugin));
1819

1920
Self { plugins }
2021
}

src-tauri/src/plugins/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,5 +316,6 @@ pub mod manager;
316316
pub mod nodejs;
317317
pub mod python2;
318318
pub mod python3;
319+
pub mod go;
319320

320321
pub use manager::PluginManager;
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
import type { HighlightRule, LanguageHighlighter } from '../types'
2+
3+
export class GoHighlighter
4+
implements LanguageHighlighter
5+
{
6+
getLanguageName(): string
7+
{
8+
return 'go'
9+
}
10+
11+
getDisplayName(): string
12+
{
13+
return 'Go'
14+
}
15+
16+
getOrder(): number
17+
{
18+
return 4
19+
}
20+
21+
getRules(): HighlightRule[]
22+
{
23+
return [
24+
// 1. 注释 (最高优先级)
25+
{
26+
pattern: /\/\/.*$/gm,
27+
className: 'text-gray-500 italic',
28+
priority: 1
29+
},
30+
{
31+
pattern: /\/\*[\s\S]*?\*\//g,
32+
className: 'text-gray-500 italic',
33+
priority: 1
34+
},
35+
36+
// 2. 字符串 (高优先级)
37+
{
38+
pattern: /`(?:[^`\\]|\\.)*`/g,
39+
className: 'text-green-600',
40+
priority: 2
41+
},
42+
{
43+
pattern: /"(?:[^"\\]|\\.)*"/g,
44+
className: 'text-green-600',
45+
priority: 2
46+
},
47+
{
48+
pattern: /'(?:[^'\\]|\\.)*'/g,
49+
className: 'text-green-600',
50+
priority: 2
51+
},
52+
53+
// 3. Go 关键字
54+
{
55+
pattern: /\b(package|import|func|var|const|type|struct|interface|map|chan|select|go|defer|return|break|continue|fallthrough|if|else|switch|case|default|for|range|goto|true|false|nil|iota)\b/g,
56+
className: 'text-purple-600 font-semibold',
57+
priority: 3
58+
},
59+
60+
// 4. Go 内置类型
61+
{
62+
pattern: /\b(bool|byte|complex64|complex128|error|float32|float64|int|int8|int16|int32|int64|rune|string|uint|uint8|uint16|uint32|uint64|uintptr)\b/g,
63+
className: 'text-blue-600 font-semibold',
64+
priority: 4
65+
},
66+
67+
// 5. 数字
68+
{
69+
pattern: /\b\d+(?:\.\d+)?(?:[eE][+-]?\d+)?[fFlL]?\b/g,
70+
className: 'text-blue-600',
71+
priority: 5
72+
},
73+
74+
// 6. 十六进制、八进制、二进制数字
75+
{
76+
pattern: /\b0[xX][0-9a-fA-F]+\b|\b0[0-7]+\b|\b0[bB][01]+\b/g,
77+
className: 'text-blue-600',
78+
priority: 5
79+
},
80+
81+
// 7. 函数定义
82+
{
83+
pattern: /\bfunc\s+([a-zA-Z_]\w*)/g,
84+
className: 'text-orange-600 font-semibold',
85+
priority: 6
86+
},
87+
88+
// 8. 函数调用
89+
{
90+
pattern: /\b([a-zA-Z_]\w*)(?=\s*\()/g,
91+
className: 'text-orange-600',
92+
priority: 7
93+
},
94+
95+
// 9. 类型定义
96+
{
97+
pattern: /\btype\s+([A-Z]\w*)/g,
98+
className: 'text-yellow-600 font-semibold',
99+
priority: 8
100+
},
101+
102+
// 10. 结构体字段
103+
{
104+
pattern: /\b([a-zA-Z_]\w*)\s*:/g,
105+
className: 'text-cyan-600',
106+
priority: 9
107+
},
108+
109+
// 11. 包名
110+
{
111+
pattern: /\bpackage\s+([a-zA-Z_]\w*)/g,
112+
className: 'text-indigo-600 font-semibold',
113+
priority: 10
114+
},
115+
116+
// 12. import 语句
117+
{
118+
pattern: /\bimport\s*\(\s*[\s\S]*?\)/g,
119+
className: 'text-emerald-600',
120+
priority: 11
121+
},
122+
{
123+
pattern: /\bimport\s+"[^"]+"/g,
124+
className: 'text-emerald-600',
125+
priority: 11
126+
},
127+
128+
// 13. 常见的 Go 内置函数
129+
{
130+
pattern: /\b(make|new|len|cap|append|copy|delete|close|panic|recover|print|println)\b/g,
131+
className: 'text-pink-600 font-medium',
132+
priority: 12
133+
},
134+
135+
// 14. 指针操作符
136+
{
137+
pattern: /[*&]/g,
138+
className: 'text-red-600 font-bold',
139+
priority: 13
140+
},
141+
142+
// 15. 通道操作符
143+
{
144+
pattern: /<-/g,
145+
className: 'text-violet-600 font-bold',
146+
priority: 14
147+
},
148+
149+
// 16. 结构体标签
150+
{
151+
pattern: /`[^`]*`/g,
152+
className: 'text-teal-600 bg-gray-100',
153+
priority: 15
154+
},
155+
156+
// 17. 大写开头的导出标识符
157+
{
158+
pattern: /\b[A-Z]\w*/g,
159+
className: 'text-amber-600',
160+
priority: 16
161+
}
162+
]
163+
}
164+
165+
preProcess(code: string): string
166+
{
167+
// Go 特定的预处理
168+
return code
169+
}
170+
171+
postProcess(highlighted: string): string
172+
{
173+
// Go 特定的后处理
174+
return highlighted
175+
}
176+
}

src/utils/highlighter/manager.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { HighlightMatch, LanguageHighlighter } from './types'
22
import { Python2Highlighter } from './languages/python2'
33
import { Python3Highlighter } from './languages/python3'
44
import { NodeJSHighlighter } from './languages/nodejs.ts'
5+
import { GoHighlighter } from './languages/go.ts'
56

67
export class HighlightManager
78
{
@@ -20,6 +21,7 @@ export class HighlightManager
2021
this.register(new Python2Highlighter())
2122
this.register(new Python3Highlighter())
2223
this.register(new NodeJSHighlighter())
24+
this.register(new GoHighlighter())
2325
}
2426

2527
/**

0 commit comments

Comments
 (0)