dot-language-support 4.2.0 → 4.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # dot-language-support [![CD](https://github.com/nikeee/dot-language-support/actions/workflows/CD.yml/badge.svg)](https://github.com/nikeee/dot-language-support/actions/workflows/CD.yml) [![CI](https://github.com/nikeee/dot-language-support/actions/workflows/CI.yml/badge.svg)](https://github.com/nikeee/dot-language-support/actions/workflows/CI.yml) ![Dependencies](https://img.shields.io/librariesio/release/npm/dot-language-support) [![npm version](https://img.shields.io/npm/v/dot-language-support)](https://www.npmjs.com/package/dot-language-support)
1
+ # dot-language-support [![CD](https://github.com/nikeee/dot-language-support/actions/workflows/CD.yaml/badge.svg)](https://github.com/nikeee/dot-language-support/actions/workflows/CD.yaml) [![CI](https://github.com/nikeee/dot-language-support/actions/workflows/CI.yaml/badge.svg)](https://github.com/nikeee/dot-language-support/actions/workflows/CI.yaml) ![Dependencies](https://img.shields.io/librariesio/release/npm/dot-language-support) [![npm version](https://img.shields.io/npm/v/dot-language-support)](https://www.npmjs.com/package/dot-language-support)
2
2
 
3
3
  A language service library, written in TypeScript. Used by [dot-language-server](https://github.com/nikeee/dot-language-server) and [edotor.net](https://edotor.net).
4
4
 
package/dist/index.mjs CHANGED
@@ -295,6 +295,13 @@ const characterCodes = {
295
295
  };
296
296
  //#endregion
297
297
  //#region src/service/util.ts
298
+ function posRangeToRange(doc, sourceFile, pos, end) {
299
+ const start = pos === end ? pos : skipTrivia(sourceFile.content, pos);
300
+ return {
301
+ start: doc.positionAt(start),
302
+ end: doc.positionAt(end)
303
+ };
304
+ }
298
305
  function getStart(sourceFile, node) {
299
306
  return getTokenPosOfNode(sourceFile, node);
300
307
  }
@@ -3268,11 +3275,17 @@ function hover(doc, sourceFile, position) {
3268
3275
  return getNodeHover(doc, sourceFile, node);
3269
3276
  }
3270
3277
  function getNodeHover(doc, sf, n) {
3271
- const contents = getHoverContents(n);
3272
- if (contents) return {
3273
- contents,
3274
- range: syntaxNodeToRange(doc, sf, n)
3275
- };
3278
+ const result = getHoverContents(n);
3279
+ if (result) {
3280
+ const { pos, end } = result.range ?? {
3281
+ pos: n.pos,
3282
+ end: n.end
3283
+ };
3284
+ return {
3285
+ contents: result.contents,
3286
+ range: posRangeToRange(doc, sf, pos, end)
3287
+ };
3288
+ }
3276
3289
  }
3277
3290
  function getAssignedLabel(statement) {
3278
3291
  const assignedLabel = statement.attributes.flatMap((a) => a.assignments)?.find((a) => getIdentifierText(a.leftId) === "label");
@@ -3288,37 +3301,63 @@ function getHoverContents(n) {
3288
3301
  const labelMentions = (n.symbol?.references).map((e) => e.symbol?.members?.get("label")?.firstMention.parent);
3289
3302
  for (let i = labelMentions.length; i >= 0; i--) {
3290
3303
  const s = labelMentions[i];
3291
- if (s?.rightId) return `(node) ${getIdentifierText(n)}: ${getIdentifierText(s.rightId)}`;
3304
+ if (s?.rightId) return { contents: `(node) ${getIdentifierText(n)}: ${getIdentifierText(s.rightId)}` };
3292
3305
  }
3293
3306
  } else if (parent.parent?.kind === syntaxKind.NodeStatement) {
3294
3307
  const label = getAssignedLabel(parent.parent);
3295
- if (label) return `(node) ${getIdentifierText(n)}: ${label}`;
3308
+ if (label) return { contents: `(node) ${getIdentifierText(n)}: ${label}` };
3296
3309
  }
3297
- return `(node) ${getIdentifierText(n)}`;
3310
+ return { contents: `(node) ${getIdentifierText(n)}` };
3298
3311
  case syntaxKind.Assignment: {
3299
3312
  const assignment = parent;
3300
- return `(assignment) \`${getIdentifierText(assignment.leftId)}\` = \`${getIdentifierText(assignment.rightId)}\``;
3313
+ return {
3314
+ contents: `(assignment) \`${getIdentifierText(assignment.leftId)}\` = \`${getIdentifierText(assignment.rightId)}\``,
3315
+ range: {
3316
+ pos: assignment.pos,
3317
+ end: assignment.end
3318
+ }
3319
+ };
3301
3320
  }
3302
3321
  case syntaxKind.DirectedGraph: return getGraphHover(parent);
3303
3322
  case syntaxKind.UndirectedGraph: return getGraphHover(parent);
3304
3323
  case syntaxKind.SubGraphStatement: {
3305
- const sg = parent.subgraph;
3306
- return sg.id ? `(sub graph) ${getIdentifierText(sg.id)}` : "(sub graph)";
3324
+ const sgs = parent;
3325
+ const sg = sgs.subgraph;
3326
+ return {
3327
+ contents: sg.id ? `(sub graph) ${getIdentifierText(sg.id)}` : "(sub graph)",
3328
+ range: {
3329
+ pos: sgs.pos,
3330
+ end: sgs.end
3331
+ }
3332
+ };
3307
3333
  }
3308
3334
  case syntaxKind.SubGraph: {
3309
3335
  const sg = parent;
3310
- return sg.id ? `(sub graph) ${getIdentifierText(sg.id)}` : "(sub graph)";
3336
+ return {
3337
+ contents: sg.id ? `(sub graph) ${getIdentifierText(sg.id)}` : "(sub graph)",
3338
+ range: {
3339
+ pos: sg.pos,
3340
+ end: sg.end
3341
+ }
3342
+ };
3311
3343
  }
3312
3344
  case syntaxKind.IdEqualsIdStatement: {
3313
3345
  const idEqId = parent;
3314
- return `(graph property) \`${getIdentifierText(idEqId.leftId)}\` = \`${getIdentifierText(idEqId.rightId)}\``;
3346
+ return {
3347
+ contents: `(graph property) \`${getIdentifierText(idEqId.leftId)}\` = \`${getIdentifierText(idEqId.rightId)}\``,
3348
+ range: {
3349
+ pos: idEqId.pos,
3350
+ end: idEqId.end
3351
+ }
3352
+ };
3315
3353
  }
3316
3354
  case syntaxKind.EdgeRhs: return getEdgeHover(parent);
3317
3355
  }
3318
- return syntaxKindNames[parent.kind];
3356
+ const fallbackParent = syntaxKindNames[parent.kind];
3357
+ return fallbackParent ? { contents: fallbackParent } : void 0;
3319
3358
  }
3320
3359
  const fallback = syntaxKindNames[n.kind];
3321
- return fallback ? `(${fallback.toLowerCase()})` : void 0;
3360
+ return fallback ? { contents: `(${fallback.toLowerCase()})` } : void 0;
3322
3361
  }
3323
3362
  switch (n.kind) {
3324
3363
  case syntaxKind.GraphKeyword:
@@ -3335,7 +3374,13 @@ function getGraphHover(g) {
3335
3374
  const direction = g.kind === syntaxKind.DirectedGraph ? "directed" : "undirected";
3336
3375
  const graphId = g.id;
3337
3376
  const strict = g.strict ? "strict " : "";
3338
- return graphId ? `(${strict}${direction} graph) ${getIdentifierText(graphId)}` : `(${strict}${direction} graph)`;
3377
+ return {
3378
+ contents: graphId ? `(${strict}${direction} graph) ${getIdentifierText(graphId)}` : `(${strict}${direction} graph)`,
3379
+ range: {
3380
+ pos: g.pos,
3381
+ end: g.end
3382
+ }
3383
+ };
3339
3384
  }
3340
3385
  function getEdgeHover(n) {
3341
3386
  const p = n.parent;
@@ -3346,8 +3391,15 @@ function getEdgeHover(n) {
3346
3391
  source = curr.target;
3347
3392
  }
3348
3393
  if (source === void 0) source = p.source;
3394
+ if (source === void 0) return void 0;
3349
3395
  const edgeOpStr = getEdgeStr(n.operation.kind);
3350
- return source === void 0 ? void 0 : `(edge) ${getEdgeSourceOrTargetText(source)} ${edgeOpStr} ${getEdgeSourceOrTargetText(n.target)}`;
3396
+ return {
3397
+ contents: `(edge) ${getEdgeSourceOrTargetText(source)} ${edgeOpStr} ${getEdgeSourceOrTargetText(n.target)}`,
3398
+ range: {
3399
+ pos: source.pos,
3400
+ end: n.target.end
3401
+ }
3402
+ };
3351
3403
  }
3352
3404
  function getEdgeSourceOrTargetText(n) {
3353
3405
  return n.kind === syntaxKind.NodeId ? getIdentifierText(n.id) : n.id !== void 0 ? `${getIdentifierText(n.id)}` : "sub graph";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dot-language-support",
3
- "version": "4.2.0",
3
+ "version": "4.2.1",
4
4
  "description": "Parser and language service for graphviz (dot) files",
5
5
  "keywords": [
6
6
  "dot",