@likec4/language-server 1.1.0 → 1.2.0

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.
Files changed (42) hide show
  1. package/contrib/likec4.monarch.ts +4 -4
  2. package/contrib/likec4.tmLanguage.json +1 -1
  3. package/dist/ast.d.ts +28 -11
  4. package/dist/ast.js +14 -13
  5. package/dist/generated/ast.d.ts +75 -19
  6. package/dist/generated/ast.js +96 -7
  7. package/dist/generated/grammar.js +1 -1
  8. package/dist/lsp/CodeLensProvider.js +5 -2
  9. package/dist/lsp/DocumentSymbolProvider.d.ts +1 -1
  10. package/dist/lsp/DocumentSymbolProvider.js +14 -9
  11. package/dist/lsp/SemanticTokenProvider.js +1 -1
  12. package/dist/model/model-builder.js +68 -45
  13. package/dist/model/model-locator.d.ts +2 -2
  14. package/dist/model/model-locator.js +12 -16
  15. package/dist/model/model-parser.d.ts +2 -0
  16. package/dist/model/model-parser.js +140 -30
  17. package/dist/model-change/ModelChanges.d.ts +2 -1
  18. package/dist/model-change/ModelChanges.js +39 -35
  19. package/dist/model-change/changeElementStyle.d.ts +18 -0
  20. package/dist/model-change/changeElementStyle.js +141 -0
  21. package/dist/model-change/changeViewLayout.d.ts +4 -4
  22. package/dist/model-change/changeViewLayout.js +4 -5
  23. package/dist/protocol.d.ts +12 -12
  24. package/dist/references/scope-computation.js +36 -31
  25. package/dist/shared/NodeKindProvider.js +4 -2
  26. package/dist/validation/dynamic-view-rule.d.ts +5 -0
  27. package/dist/validation/dynamic-view-rule.js +32 -0
  28. package/dist/validation/dynamic-view-step.d.ts +5 -0
  29. package/dist/validation/dynamic-view-step.js +33 -0
  30. package/dist/validation/index.js +5 -1
  31. package/dist/validation/view-predicates/expanded-element.js +1 -0
  32. package/dist/validation/view-predicates/outgoing.js +2 -2
  33. package/dist/validation/view.d.ts +1 -1
  34. package/dist/validation/view.js +1 -3
  35. package/dist/view-utils/assignNavigateTo.d.ts +1 -1
  36. package/dist/view-utils/assignNavigateTo.js +2 -1
  37. package/dist/view-utils/resolve-extended-views.d.ts +2 -2
  38. package/dist/view-utils/resolve-relative-paths.d.ts +2 -2
  39. package/dist/view-utils/resolve-relative-paths.js +2 -3
  40. package/package.json +6 -6
  41. package/dist/model-change/changeViewStyle.d.ts +0 -15
  42. package/dist/model-change/changeViewStyle.js +0 -123
@@ -1,123 +0,0 @@
1
- import { invariant, isAncestor, nonNullable } from "@likec4/core";
2
- import { GrammarUtils } from "langium";
3
- import { findLast, last, partition } from "remeda";
4
- import { TextEdit } from "vscode-languageserver-protocol";
5
- import { ast } from "../ast.js";
6
- const { findNodeForKeyword, findNodeForProperty } = GrammarUtils;
7
- const asViewStyleRule = (target, key, value, indent = 0) => {
8
- const indentStr = indent > 0 ? " ".repeat(indent) : "";
9
- return [
10
- indentStr + `style ${target} {`,
11
- indentStr + ` ${key} ${value}`,
12
- indentStr + `}`
13
- ].join("\n");
14
- };
15
- const isMatchingViewRule = (fqn, index) => (rule) => {
16
- if (!ast.isViewRuleStyle(rule)) {
17
- return false;
18
- }
19
- const [target, ...rest] = rule.targets;
20
- if (!target || rest.length > 0 || !ast.isElementRef(target)) {
21
- return false;
22
- }
23
- const ref = target.el.ref;
24
- const _fqn = ref ? index.getFqn(ref) : null;
25
- return _fqn === fqn;
26
- };
27
- export function changeViewStyle(services, {
28
- view,
29
- viewAst,
30
- targets,
31
- key,
32
- value
33
- }) {
34
- const viewCstNode = viewAst.$cstNode;
35
- invariant(viewCstNode, "viewCstNode");
36
- const insertPos = last(viewAst.body.rules)?.$cstNode?.range.end ?? last(viewAst.body.props)?.$cstNode?.range.end ?? viewAst.body.$cstNode?.range.start;
37
- invariant(insertPos, "insertPos is not defined");
38
- const indent = viewCstNode.range.start.character + 2;
39
- const fqnIndex = services.likec4.FqnIndex;
40
- const styleRules = viewAst.body.rules.filter(ast.isViewRuleStyle);
41
- const viewOf = view.viewOf;
42
- const targetsWithRules = targets.map((target) => {
43
- const rule = findLast(styleRules, isMatchingViewRule(target, fqnIndex));
44
- const fqn = viewOf && isAncestor(viewOf, target) ? target.substring(viewOf.length + 1) : target;
45
- if (rule) {
46
- return {
47
- fqn,
48
- rule
49
- };
50
- } else {
51
- return {
52
- fqn
53
- };
54
- }
55
- });
56
- const [existing, insert] = partition(
57
- targetsWithRules,
58
- (a) => !!a.rule
59
- );
60
- const modifiedRange = {
61
- start: insertPos,
62
- end: insertPos
63
- };
64
- const includeRange = (range) => {
65
- if (range.start.line < modifiedRange.start.line) {
66
- modifiedRange.start = range.start;
67
- }
68
- if (range.end.line > modifiedRange.end.line) {
69
- modifiedRange.end = range.end;
70
- }
71
- };
72
- const edits = [];
73
- if (insert.length > 0) {
74
- const linesToInsert = [
75
- "",
76
- ...insert.map(({ fqn }) => asViewStyleRule(fqn, key, value, indent))
77
- ];
78
- edits.push(
79
- TextEdit.insert(
80
- insertPos,
81
- linesToInsert.join("\n")
82
- )
83
- );
84
- modifiedRange.end = {
85
- line: modifiedRange.end.line + linesToInsert.length,
86
- character: last(linesToInsert)?.length ?? insertPos.character
87
- };
88
- }
89
- if (existing.length > 0) {
90
- for (const { rule } of existing) {
91
- const ruleCstNode = rule.$cstNode;
92
- invariant(ruleCstNode, "RuleCstNode not found");
93
- const ruleProp = rule.styleprops.find((p) => p.key === key);
94
- if (ruleProp && ruleProp.$cstNode) {
95
- const { range: { start, end } } = nonNullable(
96
- findNodeForProperty(ruleProp.$cstNode, "value"),
97
- "cant find value cst node"
98
- );
99
- includeRange(ruleProp.$cstNode.range);
100
- edits.push(TextEdit.replace({ start, end }, value));
101
- continue;
102
- }
103
- const insertPos2 = findNodeForKeyword(ruleCstNode, "{")?.range.end;
104
- invariant(insertPos2, "Opening brace not found");
105
- const indentStr = " ".repeat(2 + ruleCstNode.range.start.character);
106
- const insertKeyValue = indentStr + key + " " + value;
107
- edits.push(
108
- TextEdit.insert(
109
- insertPos2,
110
- "\n" + insertKeyValue
111
- )
112
- );
113
- includeRange({
114
- start: insertPos2,
115
- end: {
116
- line: insertPos2.line + 1,
117
- character: insertKeyValue.length
118
- }
119
- });
120
- }
121
- }
122
- return edits;
123
- }