-
Notifications
You must be signed in to change notification settings - Fork 219
/
MonacoFEELVariableSuggestions.java
107 lines (91 loc) · 3.78 KB
/
MonacoFEELVariableSuggestions.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
* Copyright 2020 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.kie.workbench.common.dmn.client.widgets.codecompletion;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import org.kie.workbench.common.dmn.api.definition.model.DMNDiagram;
import org.kie.workbench.common.dmn.api.definition.model.NamedElement;
import org.kie.workbench.common.dmn.api.property.dmn.Name;
import org.kie.workbench.common.dmn.client.graph.DMNGraphUtils;
import org.kie.workbench.common.stunner.core.graph.Node;
import org.kie.workbench.common.stunner.core.graph.content.definition.Definition;
@ApplicationScoped
public class MonacoFEELVariableSuggestions {
private final DMNGraphUtils dmnGraphUtils;
@Inject
public MonacoFEELVariableSuggestions(final DMNGraphUtils dmnGraphUtils) {
this.dmnGraphUtils = dmnGraphUtils;
}
public List<String> getSuggestions() {
final List<Object> diagramDefinitions = getDiagramDefinitions();
final List<String> dataTypesSuggestions = getDataTypesSuggestions(diagramDefinitions);
final List<String> namedElementSuggestions = getNamedElementSuggestions(diagramDefinitions);
return Stream
.of(dataTypesSuggestions, namedElementSuggestions)
.flatMap(Collection::stream)
.distinct()
.filter(name -> !name.isEmpty())
.sorted()
.collect(Collectors.toList());
}
private List<String> getNamedElementSuggestions(final List<Object> diagramDefinitions) {
return diagramDefinitions
.stream()
.map(this::getNamedSuggestion)
.collect(Collectors.toList());
}
private List<String> getDataTypesSuggestions(final List<Object> diagramDefinitions) {
return diagramDefinitions
.stream()
.flatMap(this::getDataTypesSuggestions)
.collect(Collectors.toList());
}
private String getNamedSuggestion(final Object definition) {
if (definition instanceof NamedElement) {
final NamedElement namedElement = (NamedElement) definition;
return getName(namedElement);
}
return "";
}
private Stream<? extends String> getDataTypesSuggestions(final Object definition) {
if (definition instanceof DMNDiagram) {
final DMNDiagram dmnDiagram = (DMNDiagram) definition;
return dmnDiagram
.getDefinitions()
.getItemDefinition()
.stream()
.map(this::getName);
}
return Stream.empty();
}
private List<Object> getDiagramDefinitions() {
return dmnGraphUtils
.getNodeStream()
.map(Node::getContent)
.filter(c -> c instanceof Definition)
.map(c -> (Definition) c)
.map(Definition::getDefinition)
.collect(Collectors.toList());
}
private String getName(final NamedElement namedElement) {
final Name name = namedElement.getName();
return name.getValue();
}
}