@theia/typehierarchy 1.53.0-next.4 → 1.53.0-next.55

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.
@@ -1,190 +1,190 @@
1
- // *****************************************************************************
2
- // Copyright (C) 2019 TypeFox and others.
3
- //
4
- // This program and the accompanying materials are made available under the
5
- // terms of the Eclipse Public License v. 2.0 which is available at
6
- // http://www.eclipse.org/legal/epl-2.0.
7
- //
8
- // This Source Code may also be made available under the following Secondary
9
- // Licenses when the conditions for such availability set forth in the Eclipse
10
- // Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
- // with the GNU Classpath Exception which is available at
12
- // https://www.gnu.org/software/classpath/license.html.
13
- //
14
- // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
15
- // *****************************************************************************
16
-
17
- /* eslint-disable @typescript-eslint/no-explicit-any */
18
-
19
- import { injectable } from '@theia/core/shared/inversify';
20
- import { generateUuid } from '@theia/core/lib/common/uuid';
21
- import URI from '@theia/core/lib/common/uri';
22
- import { Location } from '@theia/editor/lib/browser/editor';
23
- import { TreeDecoration, DecoratedTreeNode } from '@theia/core/lib/browser/tree/tree-decorator';
24
- import { TreeImpl, TreeNode, CompositeTreeNode, ExpandableTreeNode, SelectableTreeNode } from '@theia/core/lib/browser/tree';
25
- import { TypeHierarchyProvider, TypeHierarchyDirection, ResolveTypeHierarchyItemParams, TypeHierarchyItem } from '../typehierarchy-provider';
26
-
27
- @injectable()
28
- export class TypeHierarchyTree extends TreeImpl {
29
-
30
- provider: TypeHierarchyProvider | undefined;
31
-
32
- override async resolveChildren(parent: CompositeTreeNode): Promise<TreeNode[]> {
33
- if (TypeHierarchyTree.Node.is(parent)) {
34
- await this.ensureResolved(parent);
35
- if (parent.children.length === 0) {
36
- delete (parent as any).children;
37
- delete (parent as any).expanded;
38
- return [];
39
- }
40
- return parent.children.slice();
41
- }
42
- return [];
43
- }
44
-
45
- /**
46
- * Returns with the direction of the type hierarchy attached to the root node. `undefined` if the root is not set.
47
- */
48
- protected get direction(): TypeHierarchyDirection | undefined {
49
- if (TypeHierarchyTree.RootNode.is(this.root)) {
50
- return this.root.direction;
51
- }
52
- return undefined;
53
- }
54
-
55
- /**
56
- * Makes sure, the node and its children are resolved. Resolves it on demand.
57
- */
58
- protected async ensureResolved(node: TypeHierarchyTree.Node): Promise<void> {
59
- if (!node.resolved) {
60
- const { provider, direction } = this;
61
- if (provider && direction !== undefined) {
62
- const { item } = node;
63
- const param: ResolveTypeHierarchyItemParams = {
64
- item,
65
- direction,
66
- resolve: 1
67
- };
68
- const resolvedItem = await provider.resolve(param);
69
- if (resolvedItem) {
70
- node.resolved = true;
71
- const items = TypeHierarchyDirection.Children === direction ? resolvedItem.children : resolvedItem.parents;
72
- if (items) {
73
- node.children = items.map(child => TypeHierarchyTree.Node.create(child, direction, false));
74
- } else {
75
- node.children = [];
76
- }
77
- }
78
- }
79
- }
80
- }
81
-
82
- }
83
-
84
- export namespace TypeHierarchyTree {
85
-
86
- export interface InitOptions {
87
- readonly direction: TypeHierarchyDirection;
88
- readonly location: Location | undefined;
89
- readonly languageId: string | undefined;
90
- }
91
-
92
- export interface RootNode extends Node {
93
- readonly direction: TypeHierarchyDirection;
94
- }
95
-
96
- export namespace RootNode {
97
-
98
- export function is(node: TreeNode | undefined): node is RootNode {
99
- if (Node.is(node) && 'direction' in node) {
100
- const { direction } = (node as RootNode);
101
- return direction === TypeHierarchyDirection.Children || direction === TypeHierarchyDirection.Parents;
102
- }
103
- return false;
104
- }
105
-
106
- export function create(item: TypeHierarchyItem, direction: TypeHierarchyDirection): RootNode {
107
- return {
108
- ...Node.create(item, direction, true),
109
- direction
110
- };
111
- }
112
-
113
- }
114
-
115
- export interface Node extends CompositeTreeNode, ExpandableTreeNode, SelectableTreeNode, DecoratedTreeNode {
116
- readonly item: TypeHierarchyItem;
117
- resolved: boolean;
118
- }
119
-
120
- export namespace Node {
121
-
122
- export function is(node: TreeNode | undefined): node is Node {
123
- if (!!node && 'resolved' in node && 'item' in node) {
124
- const { resolved, item } = (node as Node);
125
- return typeof resolved === 'boolean' && !!item;
126
- }
127
- return false;
128
- }
129
-
130
- export function create(item: TypeHierarchyItem, direction: TypeHierarchyDirection, resolved: boolean = true): Node {
131
- const items = TypeHierarchyDirection.Children === direction ? item.children : item.parents;
132
- if (items && items.length > 0) {
133
- // If the server sent more levels than requested, use them.
134
- resolved = true;
135
- }
136
- const node = {
137
- id: generateUuid(),
138
- name: item.name,
139
- description: item.detail,
140
- parent: undefined,
141
- location: Location.create(item.uri, item.selectionRange),
142
- resolved,
143
- children: items ? items.map(child => create(child, direction, false)) : [],
144
- expanded: false,
145
- visible: true,
146
- selected: false,
147
- kind: item.kind,
148
- decorationData: decorationData(item, direction),
149
- item
150
- };
151
- // Trick: if the node is `resolved` and have zero `children`, make the node non-expandable.
152
- if (resolved && node.children.length === 0) {
153
- delete (node as any).expanded;
154
- }
155
- return node;
156
- }
157
-
158
- function decorationData(item: TypeHierarchyItem, direction: TypeHierarchyDirection): TreeDecoration.Data {
159
- const captionSuffixes: TreeDecoration.CaptionAffix[] = [{
160
- data: new URI(item.uri).displayName,
161
- fontData: {
162
- color: 'var(--theia-descriptionForeground)',
163
- }
164
- }];
165
- if (item.detail) {
166
- captionSuffixes.unshift({
167
- data: item.detail,
168
- fontData: {
169
- color: 'var(--theia-list-highlightForeground)',
170
- style: 'italic'
171
- }
172
- });
173
- }
174
- const data = `${TypeHierarchyDirection.Children === direction ? '▼' : '▲'}`;
175
- const color = `var(${TypeHierarchyDirection.Children === direction ? '--theia-errorForeground' : '--theia-successBackground'})`;
176
- return {
177
- captionSuffixes,
178
- captionPrefixes: [{
179
- data,
180
- fontData: {
181
- color,
182
- style: 'bold'
183
- }
184
- }]
185
- };
186
- }
187
-
188
- }
189
-
190
- }
1
+ // *****************************************************************************
2
+ // Copyright (C) 2019 TypeFox and others.
3
+ //
4
+ // This program and the accompanying materials are made available under the
5
+ // terms of the Eclipse Public License v. 2.0 which is available at
6
+ // http://www.eclipse.org/legal/epl-2.0.
7
+ //
8
+ // This Source Code may also be made available under the following Secondary
9
+ // Licenses when the conditions for such availability set forth in the Eclipse
10
+ // Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
+ // with the GNU Classpath Exception which is available at
12
+ // https://www.gnu.org/software/classpath/license.html.
13
+ //
14
+ // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
15
+ // *****************************************************************************
16
+
17
+ /* eslint-disable @typescript-eslint/no-explicit-any */
18
+
19
+ import { injectable } from '@theia/core/shared/inversify';
20
+ import { generateUuid } from '@theia/core/lib/common/uuid';
21
+ import URI from '@theia/core/lib/common/uri';
22
+ import { Location } from '@theia/editor/lib/browser/editor';
23
+ import { TreeDecoration, DecoratedTreeNode } from '@theia/core/lib/browser/tree/tree-decorator';
24
+ import { TreeImpl, TreeNode, CompositeTreeNode, ExpandableTreeNode, SelectableTreeNode } from '@theia/core/lib/browser/tree';
25
+ import { TypeHierarchyProvider, TypeHierarchyDirection, ResolveTypeHierarchyItemParams, TypeHierarchyItem } from '../typehierarchy-provider';
26
+
27
+ @injectable()
28
+ export class TypeHierarchyTree extends TreeImpl {
29
+
30
+ provider: TypeHierarchyProvider | undefined;
31
+
32
+ override async resolveChildren(parent: CompositeTreeNode): Promise<TreeNode[]> {
33
+ if (TypeHierarchyTree.Node.is(parent)) {
34
+ await this.ensureResolved(parent);
35
+ if (parent.children.length === 0) {
36
+ delete (parent as any).children;
37
+ delete (parent as any).expanded;
38
+ return [];
39
+ }
40
+ return parent.children.slice();
41
+ }
42
+ return [];
43
+ }
44
+
45
+ /**
46
+ * Returns with the direction of the type hierarchy attached to the root node. `undefined` if the root is not set.
47
+ */
48
+ protected get direction(): TypeHierarchyDirection | undefined {
49
+ if (TypeHierarchyTree.RootNode.is(this.root)) {
50
+ return this.root.direction;
51
+ }
52
+ return undefined;
53
+ }
54
+
55
+ /**
56
+ * Makes sure, the node and its children are resolved. Resolves it on demand.
57
+ */
58
+ protected async ensureResolved(node: TypeHierarchyTree.Node): Promise<void> {
59
+ if (!node.resolved) {
60
+ const { provider, direction } = this;
61
+ if (provider && direction !== undefined) {
62
+ const { item } = node;
63
+ const param: ResolveTypeHierarchyItemParams = {
64
+ item,
65
+ direction,
66
+ resolve: 1
67
+ };
68
+ const resolvedItem = await provider.resolve(param);
69
+ if (resolvedItem) {
70
+ node.resolved = true;
71
+ const items = TypeHierarchyDirection.Children === direction ? resolvedItem.children : resolvedItem.parents;
72
+ if (items) {
73
+ node.children = items.map(child => TypeHierarchyTree.Node.create(child, direction, false));
74
+ } else {
75
+ node.children = [];
76
+ }
77
+ }
78
+ }
79
+ }
80
+ }
81
+
82
+ }
83
+
84
+ export namespace TypeHierarchyTree {
85
+
86
+ export interface InitOptions {
87
+ readonly direction: TypeHierarchyDirection;
88
+ readonly location: Location | undefined;
89
+ readonly languageId: string | undefined;
90
+ }
91
+
92
+ export interface RootNode extends Node {
93
+ readonly direction: TypeHierarchyDirection;
94
+ }
95
+
96
+ export namespace RootNode {
97
+
98
+ export function is(node: TreeNode | undefined): node is RootNode {
99
+ if (Node.is(node) && 'direction' in node) {
100
+ const { direction } = (node as RootNode);
101
+ return direction === TypeHierarchyDirection.Children || direction === TypeHierarchyDirection.Parents;
102
+ }
103
+ return false;
104
+ }
105
+
106
+ export function create(item: TypeHierarchyItem, direction: TypeHierarchyDirection): RootNode {
107
+ return {
108
+ ...Node.create(item, direction, true),
109
+ direction
110
+ };
111
+ }
112
+
113
+ }
114
+
115
+ export interface Node extends CompositeTreeNode, ExpandableTreeNode, SelectableTreeNode, DecoratedTreeNode {
116
+ readonly item: TypeHierarchyItem;
117
+ resolved: boolean;
118
+ }
119
+
120
+ export namespace Node {
121
+
122
+ export function is(node: TreeNode | undefined): node is Node {
123
+ if (!!node && 'resolved' in node && 'item' in node) {
124
+ const { resolved, item } = (node as Node);
125
+ return typeof resolved === 'boolean' && !!item;
126
+ }
127
+ return false;
128
+ }
129
+
130
+ export function create(item: TypeHierarchyItem, direction: TypeHierarchyDirection, resolved: boolean = true): Node {
131
+ const items = TypeHierarchyDirection.Children === direction ? item.children : item.parents;
132
+ if (items && items.length > 0) {
133
+ // If the server sent more levels than requested, use them.
134
+ resolved = true;
135
+ }
136
+ const node = {
137
+ id: generateUuid(),
138
+ name: item.name,
139
+ description: item.detail,
140
+ parent: undefined,
141
+ location: Location.create(item.uri, item.selectionRange),
142
+ resolved,
143
+ children: items ? items.map(child => create(child, direction, false)) : [],
144
+ expanded: false,
145
+ visible: true,
146
+ selected: false,
147
+ kind: item.kind,
148
+ decorationData: decorationData(item, direction),
149
+ item
150
+ };
151
+ // Trick: if the node is `resolved` and have zero `children`, make the node non-expandable.
152
+ if (resolved && node.children.length === 0) {
153
+ delete (node as any).expanded;
154
+ }
155
+ return node;
156
+ }
157
+
158
+ function decorationData(item: TypeHierarchyItem, direction: TypeHierarchyDirection): TreeDecoration.Data {
159
+ const captionSuffixes: TreeDecoration.CaptionAffix[] = [{
160
+ data: new URI(item.uri).displayName,
161
+ fontData: {
162
+ color: 'var(--theia-descriptionForeground)',
163
+ }
164
+ }];
165
+ if (item.detail) {
166
+ captionSuffixes.unshift({
167
+ data: item.detail,
168
+ fontData: {
169
+ color: 'var(--theia-list-highlightForeground)',
170
+ style: 'italic'
171
+ }
172
+ });
173
+ }
174
+ const data = `${TypeHierarchyDirection.Children === direction ? '▼' : '▲'}`;
175
+ const color = `var(${TypeHierarchyDirection.Children === direction ? '--theia-errorForeground' : '--theia-successBackground'})`;
176
+ return {
177
+ captionSuffixes,
178
+ captionPrefixes: [{
179
+ data,
180
+ fontData: {
181
+ color,
182
+ style: 'bold'
183
+ }
184
+ }]
185
+ };
186
+ }
187
+
188
+ }
189
+
190
+ }