@likec4/language-server 1.19.0 → 1.19.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.
Files changed (59) hide show
  1. package/dist/Rpc.js +6 -0
  2. package/dist/browser.d.ts +1 -0
  3. package/dist/documentation/documentation-provider.d.ts +8 -0
  4. package/dist/documentation/documentation-provider.js +46 -0
  5. package/dist/documentation/index.d.ts +1 -0
  6. package/dist/documentation/index.js +1 -0
  7. package/dist/index.d.ts +1 -0
  8. package/dist/index.js +3 -2
  9. package/dist/lsp/DocumentSymbolProvider.d.ts +11 -2
  10. package/dist/lsp/DocumentSymbolProvider.js +78 -6
  11. package/dist/model/fqn-computation.js +2 -2
  12. package/dist/model/model-builder.js +3 -3
  13. package/dist/module.d.ts +9 -1
  14. package/dist/module.js +47 -8
  15. package/dist/protocol.d.ts +19 -1
  16. package/dist/protocol.js +1 -0
  17. package/dist/references/scope-computation.d.ts +2 -2
  18. package/dist/references/scope-computation.js +9 -9
  19. package/dist/validation/dynamic-view-rule.js +3 -2
  20. package/dist/validation/dynamic-view-step.js +23 -27
  21. package/dist/validation/property-checks.js +3 -2
  22. package/dist/validation/specification.js +14 -14
  23. package/dist/validation/view-predicates/element-with.js +3 -2
  24. package/dist/validation/view-predicates/expanded-element.js +3 -2
  25. package/dist/validation/view-predicates/incoming.js +3 -2
  26. package/dist/validation/view-predicates/outgoing.js +3 -2
  27. package/dist/validation/view-predicates/relation-with.js +3 -2
  28. package/dist/validation/view.js +3 -3
  29. package/dist/views/configurable-layouter.d.ts +7 -0
  30. package/dist/views/configurable-layouter.js +55 -0
  31. package/dist/views/index.d.ts +1 -0
  32. package/dist/views/index.js +1 -0
  33. package/dist/views/likec4-views.d.ts +26 -0
  34. package/dist/views/likec4-views.js +113 -0
  35. package/package.json +13 -10
  36. package/src/Rpc.ts +13 -7
  37. package/src/browser.ts +1 -0
  38. package/src/documentation/documentation-provider.ts +52 -0
  39. package/src/documentation/index.ts +1 -0
  40. package/src/index.ts +4 -2
  41. package/src/lsp/DocumentSymbolProvider.ts +110 -28
  42. package/src/model/fqn-computation.ts +8 -8
  43. package/src/model/model-builder.ts +52 -52
  44. package/src/module.ts +56 -9
  45. package/src/protocol.ts +29 -4
  46. package/src/references/scope-computation.ts +35 -35
  47. package/src/validation/dynamic-view-rule.ts +5 -4
  48. package/src/validation/dynamic-view-step.ts +23 -26
  49. package/src/validation/property-checks.ts +11 -10
  50. package/src/validation/specification.ts +38 -38
  51. package/src/validation/view-predicates/element-with.ts +6 -5
  52. package/src/validation/view-predicates/expanded-element.ts +6 -5
  53. package/src/validation/view-predicates/incoming.ts +6 -5
  54. package/src/validation/view-predicates/outgoing.ts +6 -5
  55. package/src/validation/view-predicates/relation-with.ts +6 -5
  56. package/src/validation/view.ts +5 -5
  57. package/src/views/configurable-layouter.ts +65 -0
  58. package/src/views/index.ts +1 -0
  59. package/src/views/likec4-views.ts +139 -0
@@ -1,19 +1,20 @@
1
- import { AstUtils, type ValidationCheck } from 'langium'
1
+ import { type ValidationCheck, AstUtils } from 'langium'
2
2
  import { isNullish } from 'remeda'
3
3
  import { ast } from '../../ast'
4
4
  import type { LikeC4Services } from '../../module'
5
+ import { tryOrLog } from '../_shared'
5
6
 
6
7
  export const incomingExpressionChecks = (
7
- _services: LikeC4Services
8
+ _services: LikeC4Services,
8
9
  ): ValidationCheck<ast.IncomingRelationExpression> => {
9
- return (el, accept) => {
10
+ return tryOrLog((el, accept) => {
10
11
  if (ast.isWildcardExpression(el.to) && !ast.isInOutRelationExpression(el.$container)) {
11
12
  const view = AstUtils.getContainerOfType(el, ast.isElementView)
12
13
  if (isNullish(view?.viewOf)) {
13
14
  accept('warning', 'Predicate is ignored as it concerns all relationships', {
14
- node: el
15
+ node: el,
15
16
  })
16
17
  }
17
18
  }
18
- }
19
+ })
19
20
  }
@@ -1,19 +1,20 @@
1
- import { AstUtils, type ValidationCheck } from 'langium'
1
+ import { type ValidationCheck, AstUtils } from 'langium'
2
2
  import { isNullish } from 'remeda'
3
3
  import { ast } from '../../ast'
4
4
  import type { LikeC4Services } from '../../module'
5
+ import { tryOrLog } from '../_shared'
5
6
 
6
7
  export const outgoingExpressionChecks = (
7
- _services: LikeC4Services
8
+ _services: LikeC4Services,
8
9
  ): ValidationCheck<ast.OutgoingRelationExpression> => {
9
- return (el, accept) => {
10
+ return tryOrLog((el, accept) => {
10
11
  if (ast.isWildcardExpression(el.from) && !ast.isDirectedRelationExpression(el.$container)) {
11
12
  const view = AstUtils.getContainerOfType(el, ast.isElementView)
12
13
  if (isNullish(view?.viewOf)) {
13
14
  accept('warning', 'Predicate is ignored as it concerns all relationships', {
14
- node: el
15
+ node: el,
15
16
  })
16
17
  }
17
18
  }
18
- }
19
+ })
19
20
  }
@@ -1,16 +1,17 @@
1
- import { AstUtils, type ValidationCheck } from 'langium'
1
+ import { type ValidationCheck, AstUtils } from 'langium'
2
2
  import { ast } from '../../ast'
3
3
  import type { LikeC4Services } from '../../module'
4
+ import { tryOrLog } from '../_shared'
4
5
 
5
6
  export const relationPredicateWithChecks = (
6
- _services: LikeC4Services
7
+ _services: LikeC4Services,
7
8
  ): ValidationCheck<ast.RelationPredicateWith> => {
8
- return (el, accept) => {
9
+ return tryOrLog((el, accept) => {
9
10
  const container = AstUtils.getContainerOfType(el, ast.isViewRulePredicate)
10
11
  if (ast.isExcludePredicate(container)) {
11
12
  accept('error', 'Invalid usage inside "exclude"', {
12
- node: el
13
+ node: el,
13
14
  })
14
15
  }
15
- }
16
+ })
16
17
  }
@@ -1,11 +1,11 @@
1
1
  import { type ValidationCheck } from 'langium'
2
2
  import { ast } from '../ast'
3
3
  import type { LikeC4Services } from '../module'
4
- import { RESERVED_WORDS } from './_shared'
4
+ import { RESERVED_WORDS, tryOrLog } from './_shared'
5
5
 
6
6
  export const viewChecks = (services: LikeC4Services): ValidationCheck<ast.LikeC4View> => {
7
7
  const index = services.shared.workspace.IndexManager
8
- return (el, accept) => {
8
+ return tryOrLog((el, accept) => {
9
9
  // const commentNode = CstUtils.findCommentNode(el.$cstNode, ['BLOCK_COMMENT'])
10
10
  // if (commentNode && hasManualLayout(commentNode.text) && !deserializeFromComment(commentNode.text)) {
11
11
  // accept('warning', `Malformed @likec4-generated (ignored)`, {
@@ -19,7 +19,7 @@ export const viewChecks = (services: LikeC4Services): ValidationCheck<ast.LikeC4
19
19
  if (RESERVED_WORDS.includes(el.name)) {
20
20
  accept('error', `Reserved word: ${el.name}`, {
21
21
  node: el,
22
- property: 'name'
22
+ property: 'name',
23
23
  })
24
24
  }
25
25
  const anotherViews = index
@@ -30,8 +30,8 @@ export const viewChecks = (services: LikeC4Services): ValidationCheck<ast.LikeC4
30
30
  if (anotherViews > 1) {
31
31
  accept('error', `Duplicate view '${el.name}'`, {
32
32
  node: el,
33
- property: 'name'
33
+ property: 'name',
34
34
  })
35
35
  }
36
- }
36
+ })
37
37
  }
@@ -0,0 +1,65 @@
1
+ import { GraphvizLayouter, GraphvizWasmAdapter } from '@likec4/layouts'
2
+ import { GraphvizBinaryAdapter } from '@likec4/layouts/graphviz/binary'
3
+ import { isEmpty } from 'remeda'
4
+ import which from 'which'
5
+ import { logger } from '../logger'
6
+ import type { LikeC4Services } from '../module'
7
+
8
+ function graphvizBinPath() {
9
+ try {
10
+ return which.sync('dot')
11
+ } catch (error) {
12
+ logger.error('Error checking for native Graphviz:', error)
13
+ return null
14
+ }
15
+ }
16
+
17
+ export const ConfigurableLayouter = {
18
+ likec4: {
19
+ Layouter(services: LikeC4Services): GraphvizLayouter {
20
+ logger.debug('Creating ConfigurableLayouter')
21
+ const wasmAdapter = new GraphvizWasmAdapter()
22
+ const layouter = new GraphvizLayouter(wasmAdapter)
23
+ const langId = services.LanguageMetaData.languageId
24
+ services.shared.workspace.ConfigurationProvider.onConfigurationSectionUpdate((update) => {
25
+ logger.debug('Configuration update', update)
26
+ if (update.section === langId) {
27
+ try {
28
+ const { mode, path } = update.configuration.graphviz ?? {
29
+ mode: 'wasm',
30
+ path: '',
31
+ }
32
+
33
+ if (mode === 'wasm') {
34
+ layouter.changePort(wasmAdapter)
35
+ logger.info('use graphviz wasm')
36
+ return
37
+ }
38
+
39
+ let binaryPath = isEmpty(path) ? graphvizBinPath() : path
40
+
41
+ if (binaryPath === null) {
42
+ layouter.changePort(wasmAdapter)
43
+ logger.warn(`No Graphviz binaries found on PATH, use graphviz wasm`)
44
+ services.shared.lsp.Connection?.window.showWarningMessage(
45
+ 'No Graphviz binaries found on PATH, set path to binaries in settings.',
46
+ )
47
+ return
48
+ }
49
+
50
+ layouter.changePort(new GraphvizBinaryAdapter(binaryPath))
51
+
52
+ logger.info(`use graphviz binary: ${binaryPath}`)
53
+ } catch (error) {
54
+ logger.error('Failed to update configuration', error)
55
+ }
56
+ return
57
+ }
58
+
59
+ logger.warn('Unexpected configuration update', update)
60
+ })
61
+
62
+ return layouter
63
+ },
64
+ },
65
+ }
@@ -0,0 +1 @@
1
+ export { LikeC4Views } from './likec4-views'
@@ -0,0 +1,139 @@
1
+ import type { ComputedView, DiagramView, OverviewGraph, ViewId } from '@likec4/core'
2
+ import { GraphvizLayouter } from '@likec4/layouts'
3
+ import { type Cancellation, type WorkspaceCache } from 'langium'
4
+ import { values } from 'remeda'
5
+ import { logger, logWarnError } from '../logger'
6
+ import type { LikeC4Services } from '../module'
7
+
8
+ export type GraphvizOut = {
9
+ dot: string
10
+ diagram: DiagramView
11
+ }
12
+
13
+ type GraphvizSvgOut = {
14
+ id: ViewId
15
+ dot: string
16
+ svg: string
17
+ }
18
+
19
+ export class LikeC4Views {
20
+ private cache = new WeakMap<ComputedView, GraphvizOut>()
21
+
22
+ private viewsWithReportedErrors = new Set<ViewId>()
23
+
24
+ constructor(private services: LikeC4Services) {
25
+ }
26
+
27
+ private get layouter(): GraphvizLayouter {
28
+ return this.services.likec4.Layouter
29
+ }
30
+
31
+ async computedViews(cancelToken?: Cancellation.CancellationToken): Promise<ComputedView[]> {
32
+ const model = await this.services.likec4.ModelBuilder.buildComputedModel(cancelToken)
33
+ return model ? values(model.views) : []
34
+ }
35
+
36
+ async layoutAllViews(cancelToken?: Cancellation.CancellationToken): Promise<Array<Readonly<GraphvizOut>>> {
37
+ const views = await this.computedViews(cancelToken)
38
+ if (views.length === 0) {
39
+ return []
40
+ }
41
+ const results = [] as GraphvizOut[]
42
+ const tasks = [] as Promise<GraphvizOut>[]
43
+ for (const view of views) {
44
+ this.viewsWithReportedErrors.delete(view.id)
45
+ tasks.push(
46
+ this.layouter.layout(view)
47
+ .then(result => {
48
+ this.cache.set(view, result)
49
+ return result
50
+ })
51
+ .catch(e => {
52
+ this.cache.delete(view)
53
+ logWarnError(e)
54
+ return Promise.reject(e)
55
+ }),
56
+ )
57
+ }
58
+ for (const task of await Promise.allSettled(tasks)) {
59
+ if (task.status === 'fulfilled') {
60
+ results.push(task.value)
61
+ }
62
+ }
63
+ return results
64
+ }
65
+
66
+ async layoutView(viewId: ViewId, cancelToken?: Cancellation.CancellationToken): Promise<GraphvizOut | null> {
67
+ const model = await this.services.likec4.ModelBuilder.buildComputedModel(cancelToken)
68
+ if (!model) {
69
+ return null
70
+ }
71
+ const view = model.views[viewId]
72
+ if (!view) {
73
+ return null
74
+ }
75
+ let cached = this.cache.get(view)
76
+ if (cached) {
77
+ return cached
78
+ }
79
+ try {
80
+ const result = await this.layouter.layout(view)
81
+ this.viewsWithReportedErrors.delete(viewId)
82
+ this.cache.set(view, result)
83
+ return result
84
+ } catch (e) {
85
+ if (!this.viewsWithReportedErrors.has(viewId)) {
86
+ const errMessage = e instanceof Error ? e.message : '' + e
87
+ this.services.shared.lsp.Connection?.window.showErrorMessage(`LikeC4: ${errMessage}`)
88
+ this.viewsWithReportedErrors.add(viewId)
89
+ }
90
+ logger.error(e)
91
+ return Promise.reject(e)
92
+ }
93
+ }
94
+
95
+ async diagrams(): Promise<Array<DiagramView>> {
96
+ const layouted = await this.layoutAllViews()
97
+ return layouted.map(l => l.diagram)
98
+ }
99
+
100
+ async viewsAsGraphvizOut(): Promise<Array<GraphvizSvgOut>> {
101
+ const KEY = 'All-LayoutedViews-DotWithSvg'
102
+ const cache = this.services.WorkspaceCache as WorkspaceCache<string, GraphvizSvgOut[]>
103
+ if (cache.has(KEY)) {
104
+ return await Promise.resolve(cache.get(KEY)!)
105
+ }
106
+ const views = await this.computedViews()
107
+ const tasks = views.map(async view => {
108
+ const { dot, svg } = await this.layouter.svg(view)
109
+ return {
110
+ id: view.id,
111
+ dot,
112
+ svg,
113
+ }
114
+ })
115
+ const succeed = [] as GraphvizSvgOut[]
116
+ const settledResult = await Promise.allSettled(tasks)
117
+ for (const result of settledResult) {
118
+ if (result.status === 'fulfilled') {
119
+ succeed.push(result.value)
120
+ } else {
121
+ logWarnError(result.reason)
122
+ }
123
+ }
124
+ cache.set(KEY, succeed)
125
+ return succeed
126
+ }
127
+
128
+ async overviewGraph(): Promise<OverviewGraph> {
129
+ const KEY = 'OverviewGraph'
130
+ const cache = this.services.WorkspaceCache as WorkspaceCache<string, OverviewGraph>
131
+ if (cache.has(KEY)) {
132
+ return await Promise.resolve(cache.get(KEY)!)
133
+ }
134
+ const views = await this.computedViews()
135
+ const overviewGraph = await this.layouter.layoutOverviewGraph(views)
136
+ cache.set(KEY, overviewGraph)
137
+ return overviewGraph
138
+ }
139
+ }