我有一个JSON
{
"components": [
"component1",
"component2",
"component3"
],
"validFrom": "someDate"
}我想计算VSCode的Range,这样我就可以在我的分机中报告Diagnostics。
我如何计算范围,例如对于"component1"?一些实用函数应该返回类似(startLine: 3, startCharacter: 5, endLine: 3, endCharacter: 5 + component1.length)的内容。
发布于 2021-07-21 14:58:10
使用jsonc-parser,VS Code本身也使用它来解析JSON内容,您可以解析文档并获得元素的范围。
此示例演示如何获取"component1"的范围
import { findNodeAtLocation, parseTree } from "jsonc-parser";
...
//Parse JSON of a TextDocument
const rootNode = parseTree(textDocument.getText());
// Find first element of the "components" array
const firstComponent = findNodeAtLocation(rootNode, ["components", 0]);
const start = textDocument.positionAt(firstComponent.offset);
const end = textDocument.positionAt(firstComponent.offset + firstComponent.length)
const range = new Range(start, end);(请注意:我跳过了对undefined的任何检查)
发布于 2021-07-20 14:48:14
定位信息通常由解析器提供。它确定输入中的各个令牌及其位置。然后,这可以用于生成用于各种目的(代码完成、错误诊断、编辑操作等)的Mongo值范围。
https://stackoverflow.com/questions/68440398
复制相似问题