代码编辑器:代码补全、悬停提示、参数提示、边写边检查

- 运行器新增 language-server 模式:常驻语言服务,和编译服务分开;用 Roslyn 的
  CompletionService、QuickInfoService(和 VS 的 IntelliSense 同一套引擎),参数提示用语义模型实现;
  每个浏览器标签页一份工程,只更新有变化的文件;去掉仿真类库的可空注解(string? → string)
- 编译和语言服务共用同一套编译选项和引用;检查结果带上禁止 API 的报错和中文提示
- 服务端 POST /api/language 转发请求(按用户区分会话,10 秒超时)
- 编辑器:补全("." 触发,选中时取说明)、悬停、参数提示("(" "," 触发);停止输入 1 秒后检查整个工程,
  和"运行"时的编译结果以较新的为准
- 实测(2 核):50 个标签页同时检查 1.2 秒内完成,同时补全 0.7 秒;内存约 260 MB

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
xuyueandClaude Opus 5.5 committed 2026-10-22 19:23:55 +08:00
1 parent 89e9956a22
commit 302eae4f13
12 files changed
+667 -20

No files matched your search

+40 -4
View File
@@ -1,8 +1,10 @@
<script setup lang="ts">
// 代码编辑器:每个文件一个 Monaco 模型(各自保留撤销记录),切换标签页时换模型。
// 编译错误以波浪线标在代码里。F5 / Shift+F5 由页面统一处理(见 App.vue),这里不再单独绑定。
// 编译错误以波浪线标在代码里:点"运行"时的编译结果,以及停止输入一会儿后的自动检查结果,以较新的为准。
// 代码补全、悬停提示、参数提示见 language.ts。F5 / Shift+F5 由页面统一处理,这里不再单独绑定。
import { onBeforeUnmount, onMounted, ref, shallowRef, watch } from 'vue'
import type { Diagnostic, SourceFile } from '@iot/shared'
import { checkProject, registerLanguageFeatures, setLanguageFiles } from './language'
import { monaco } from './monaco'
const props = defineProps<{
@@ -25,7 +27,10 @@ function modelFor(file: SourceFile) {
let model = models.get(file.path)
if (!model) {
model = monaco.editor.createModel(file.content, 'csharp', monaco.Uri.file(file.path))
model.onDidChangeContent(() => emit('change', file.path, model!.getValue()))
model.onDidChangeContent(() => {
emit('change', file.path, model!.getValue())
scheduleCheck()
})
models.set(file.path, model)
} else if (model.getValue() !== file.content) {
// 外部换了内容(切换示例、恢复原始代码)
@@ -53,9 +58,12 @@ function showActive() {
if (editor.value.getModel() !== model) editor.value.setModel(model)
}
/** 当前显示的错误:新的编译结果或自动检查结果到来时替换 */
let shown: Diagnostic[] = props.diagnostics
function applyMarkers() {
for (const [path, model] of models) {
const markers = props.diagnostics
const markers = shown
.filter((d) => d.file === path)
.map((d) => ({
severity: d.severity === 'error' ? monaco.MarkerSeverity.Error : monaco.MarkerSeverity.Warning,
@@ -83,7 +91,28 @@ function reveal(path: string, line: number, column: number) {
defineExpose({ reveal })
// ---------- 边写边检查:停止输入 1 秒后检查整个工程 ----------
const CHECK_DELAY_MS = 1000
let checkTimer: ReturnType<typeof setTimeout> | undefined
function scheduleCheck() {
if (props.readOnly) return
clearTimeout(checkTimer)
checkTimer = setTimeout(() => void check(), CHECK_DELAY_MS)
}
async function check() {
const versions = new Map([...models].map(([path, model]) => [path, model.getVersionId()]))
const result = await checkProject()
// 检查期间又改了代码:这次的结果作废,等下一次
if (!result || [...models].some(([path, model]) => versions.get(path) !== model.getVersionId())) return
shown = result
applyMarkers()
}
onMounted(() => {
registerLanguageFeatures()
setLanguageFiles(() => props.files)
editor.value = monaco.editor.create(container.value!, {
language: 'csharp',
theme: 'vs',
@@ -111,9 +140,16 @@ watch(() => props.files, () => {
applyMarkers()
})
watch(() => props.activeFile, showActive)
watch(() => props.diagnostics, applyMarkers)
watch(
() => props.diagnostics,
(diagnostics) => {
shown = diagnostics
applyMarkers()
},
)
onBeforeUnmount(() => {
clearTimeout(checkTimer)
editor.value?.dispose()
for (const model of models.values()) model.dispose()
models.clear()