Dever v0.0.2

This commit is contained in:
Codex committed 2026-11-27 06:03:56 +00:00
commit e6e175ebcf
816 files changed
+120349

No files matched your search

+176
View File
@@ -0,0 +1,176 @@
const vscode = require('vscode');
const path = require('node:path');
const fs = require('node:fs/promises');
const { createHash } = require('node:crypto');
const { execFile } = require('node:child_process');
const { promisify } = require('node:util');
const execute = promisify(execFile);
function isSource(document) {
return document.languageId === 'dever';
}
async function projectRoot(document) {
const folder = vscode.workspace.getWorkspaceFolder(document.uri)?.uri.fsPath;
for (let directory = path.dirname(document.uri.fsPath); ; directory = path.dirname(directory)) {
try {
await fs.stat(path.join(directory, 'module'));
return directory;
} catch (error) {
if (error.code !== 'ENOENT') throw error;
}
if (directory === folder || directory === path.dirname(directory)) break;
}
return folder || document.uri.fsPath;
}
function uri(index, file) { return vscode.Uri.file(index.files[file].path); }
function range(location) {
return new vscode.Range(location.start.line, location.start.character, location.end.line, location.end.character);
}
function expandIndex(index) {
const location = ([file, line, character, endLine, endCharacter]) => ({
file, start: { line, character }, end: { line: endLine, character: endCharacter }
});
const symbols = new Map(index.symbols.map(symbol => [symbol.id, symbol]));
for (const symbol of index.symbols) {
if (symbol.template === undefined) {
symbol.source_name ??= symbol.name;
symbol.declarations = symbol.declarations.map(location);
}
}
for (const symbol of index.symbols) {
if (symbol.template !== undefined) {
const template = symbols.get(symbol.template);
symbol.source_name = template.name;
symbol.declarations = template.declarations;
symbol.signatures = template.signatures.map(signature => signature.replace(template.name, () => symbol.name));
delete symbol.template;
}
}
for (const call of index.calls) call.site = location(call.site);
for (const issue of index.diagnostics) {
issue.location = location(issue.location);
issue.related = issue.related.map(([site, reason]) => [location(site), reason]);
}
for (const flow of index.flows || []) {
for (const node of flow.nodes) if (node.site) node.site = location(node.site);
}
return index;
}
function createInspection(context) {
const cache = new Map();
// 随包源码沿用最近一次实际索引它的项目,打开库文件后仍能继续读调用链。
const sourceOwners = new Map();
const changed = new vscode.EventEmitter();
const output = vscode.window.createOutputChannel('Dever');
const diagnostics = vscode.languages.createDiagnosticCollection('dever');
const status = vscode.window.createStatusBarItem();
status.text = 'Dever:有未保存修改,导航和流程暂不可用';
const savedState = () => {
if (vscode.workspace.textDocuments.some(document => document.isDirty && isSource(document))) status.show();
else status.hide();
};
context.subscriptions.push(changed, output, diagnostics, status);
const invalidate = () => { cache.clear(); diagnostics.clear(); savedState(); changed.fire(); };
context.subscriptions.push(vscode.workspace.onDidChangeTextDocument(event => {
if (isSource(event.document)) { diagnostics.clear(); savedState(); changed.fire(); }
}));
context.subscriptions.push(vscode.workspace.onDidCloseTextDocument(savedState));
const watcher = vscode.workspace.createFileSystemWatcher('**/{*.dv,*.dever,README.md,doc/*.md,dever.lock,dever.api,setting.json}');
context.subscriptions.push(watcher, watcher.onDidChange(invalidate), watcher.onDidCreate(invalidate), watcher.onDidDelete(invalidate));
context.subscriptions.push(vscode.workspace.onDidSaveTextDocument(document => {
if (isSource(document) || /[/\\]doc[/\\][^/\\]+\.md$/.test(document.uri.fsPath)) invalidate();
}));
context.subscriptions.push(vscode.workspace.onDidChangeConfiguration(event => {
if (event.affectsConfiguration('dever.command')) invalidate();
}));
async function load(root, symbol) {
const key = JSON.stringify([root, symbol]);
if (!vscode.workspace.isTrusted) throw new Error('请信任工作区后再运行 Dever 源码检查。');
if (!cache.has(key)) {
const command = vscode.workspace.getConfiguration('dever', vscode.Uri.file(root)).get('command');
const arguments_ = [...command.slice(1), 'inspect', root, '--json'];
if (symbol) arguments_.push('--symbol', symbol, '--depth', '0');
const promise = execute(command[0], arguments_, {
maxBuffer: 32 * 1024 * 1024, windowsHide: true
}).then(({ stdout }) => {
if (cache.get(key) !== promise) throw new Error('检查期间源码已变化,请重新查询调用关系。');
const index = expandIndex(JSON.parse(stdout));
for (const file of index.files) sourceOwners.set(file.path, root);
for (const error of index.documentation_errors) output.appendLine(error);
if (!symbol) {
const byFile = new Map(index.files.map((file, id) => [id, []]));
for (const issue of index.diagnostics) {
const diagnostic = new vscode.Diagnostic(range(issue.location), issue.message, vscode.DiagnosticSeverity.Error);
diagnostic.code = issue.code;
diagnostic.relatedInformation = issue.related.map(([site, reason]) => new vscode.DiagnosticRelatedInformation(
new vscode.Location(uri(index, site.file, root), range(site)), reason));
byFile.get(issue.location.file).push(diagnostic);
}
for (const [file, issues] of byFile) diagnostics.set(uri(index, file, root), issues);
}
return index;
}).catch(error => {
if (cache.get(key) === promise) cache.delete(key);
const message = error.stderr?.trim() || error.message;
output.appendLine(message);
throw new Error(message);
});
cache.set(key, promise);
}
const pending = cache.get(key);
const index = await pending;
if (cache.get(key) !== pending) throw new Error('检查期间源码已变化,请重新查询调用关系。');
return index;
}
async function verify(index, file, root) {
const document = await vscode.workspace.openTextDocument(uri(index, file, root));
if (document.isDirty) throw new Error('调用关系基于已保存源码,请先保存文件。');
const digest = createHash('sha256').update(document.getText()).digest('hex');
if (digest !== index.files[file].sha256) {
invalidate();
throw new Error('源码已变化,请刷新调用关系。');
}
return document;
}
async function current(document) {
if (document.isDirty) return undefined;
const root = sourceOwners.get(document.uri.fsPath) || await projectRoot(document);
if (vscode.workspace.textDocuments.some(value => value.isDirty && isSource(value)
&& (value.uri.fsPath === root || value.uri.fsPath.startsWith(`${root}${path.sep}`)))) return undefined;
const index = await load(root);
const sourcePath = document.uri.fsPath;
const file = index.files.findIndex(source => source.path === sourcePath);
if (file < 0) return undefined;
await verify(index, file, root);
return { root, index, file };
}
async function location(index, site, root) {
await verify(index, site.file, root);
return new vscode.Location(uri(index, site.file, root), range(site));
}
async function checkLinks(root) {
if (!vscode.workspace.isTrusted) throw new Error('请信任工作区后再检查领域流程。');
if (vscode.workspace.textDocuments.some(document => document.isDirty && isSource(document)
&& document.uri.fsPath.startsWith(`${root}${path.sep}`))) throw new Error('请先保存源码,再查看领域流程。');
const command = vscode.workspace.getConfiguration('dever', vscode.Uri.file(root)).get('command');
try {
await execute(command[0], [...command.slice(1), 'inspect', root, '--links', '--check'], { windowsHide: true });
} catch (error) {
throw new Error(error.stderr?.trim() || error.stdout?.trim() || error.message);
}
}
return { load, current, verify, location, checkLinks, invalidate, changed };
}
module.exports = { createInspection, expandIndex, range, uri, isSource, projectRoot };