81 lines
2.0 KiB
JavaScript

import axios from 'axios';
import { globalLookup } from "@/hook/global";
import { pinkLog } from "@/hook/utils";
import { mode, vscode } from ".";
/**
*
* @param {string} definition
*/
export async function gotoDefinition(definition) {
const defs = processDefinition(definition);
if (mode === 'debug') {
const res = await axios.post('http://localhost:3000/netlist/goto-definition', { defs });
} else {
vscode.postMessage({
command: 'goto-definition',
data: { defs }
});
}
}
/**
*
* @typedef FileRange
* @property {string} path
* @property {import("@/hook/jsdoc").Range} [range]
*
* @param {string | undefined} definition
* @returns {FileRange[]}
*/
function processDefinition(definition) {
if (typeof definition !== 'string') {
return [];
}
if (definition.includes('|')) {
const defs = [];
for (const def of definition.split('|')) {
defs.push(...processDefinition(def));
}
return defs;
}
if (definition.includes(':')) {
const [path, rangeString] = definition.split(':');
const [startString, endString] = rangeString.split('-');
const [startLine, startCharacter] = startString.split('.');
const [endLine, endCharacter] = endString.split('.');
const range = {
start: {
line: parseRangeInt(startLine),
character: parseRangeInt(startCharacter)
},
end: {
line: parseRangeInt(endLine),
character: parseRangeInt(endCharacter)
}
};
return [
{ path, range }
];
}
return [
{
path: definition,
range: undefined
}
];
}
function parseRangeInt(s) {
const i = parseInt(s);
if (i > 0) {
// 因为 yosys 是 one index 的,但是 vscode 内部跳转都是 zero index
return i - 1;
}
return i;
}