firefly-compiler 0.4.4
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/.firefly-workspace +1 -0
- package/.vscode/settings.json +5 -0
- package/LICENSE.txt +21 -0
- package/README.md +96 -0
- package/bin/firefly.mjs +2 -0
- package/compiler/.firefly/package.ff +1 -0
- package/compiler/Builder.ff +218 -0
- package/compiler/Compiler.ff +241 -0
- package/compiler/Dependencies.ff +179 -0
- package/compiler/Deriver.ff +647 -0
- package/compiler/Dictionaries.ff +205 -0
- package/compiler/Environment.ff +166 -0
- package/compiler/Inference.ff +1117 -0
- package/compiler/JsEmitter.ff +861 -0
- package/compiler/JsImporter.ff +56 -0
- package/compiler/LspHook.ff +188 -0
- package/compiler/Main.ff +237 -0
- package/compiler/Parser.ff +1383 -0
- package/compiler/Patterns.ff +111 -0
- package/compiler/Resolver.ff +620 -0
- package/compiler/Substitution.ff +178 -0
- package/compiler/Syntax.ff +299 -0
- package/compiler/Token.ff +180 -0
- package/compiler/Tokenizer.ff +278 -0
- package/compiler/Unification.ff +220 -0
- package/compiler/Wildcards.ff +50 -0
- package/compiler/Workspace.ff +88 -0
- package/core/.firefly/package.ff +2 -0
- package/core/Any.ff +30 -0
- package/core/Array.ff +249 -0
- package/core/AssetSystem.ff +61 -0
- package/core/Atomic.ff +64 -0
- package/core/Bool.ff +13 -0
- package/core/BrowserSystem.ff +14 -0
- package/core/Buffer.ff +211 -0
- package/core/BuildSystem.ff +144 -0
- package/core/Channel.ff +131 -0
- package/core/Char.ff +18 -0
- package/core/Core.ff +58 -0
- package/core/Duration.ff +15 -0
- package/core/Equal.ff +52 -0
- package/core/Error.ff +20 -0
- package/core/FileHandle.ff +41 -0
- package/core/Float.ff +41 -0
- package/core/HttpClient.ff +84 -0
- package/core/Instant.ff +9 -0
- package/core/Int.ff +61 -0
- package/core/IntMap.ff +85 -0
- package/core/JsSystem.ff +66 -0
- package/core/JsValue.ff +240 -0
- package/core/List.ff +440 -0
- package/core/Lock.ff +144 -0
- package/core/Log.ff +24 -0
- package/core/Map.ff +126 -0
- package/core/NodeSystem.ff +88 -0
- package/core/Nothing.ff +1 -0
- package/core/Option.ff +133 -0
- package/core/Ordering.ff +157 -0
- package/core/Pair.ff +55 -0
- package/core/Path.ff +393 -0
- package/core/RbMap.ff +216 -0
- package/core/Serializable.ff +173 -0
- package/core/Set.ff +38 -0
- package/core/Show.ff +43 -0
- package/core/Stack.ff +263 -0
- package/core/Stream.ff +406 -0
- package/core/String.ff +175 -0
- package/core/StringMap.ff +85 -0
- package/core/Task.ff +138 -0
- package/core/Try.ff +81 -0
- package/core/Unit.ff +3 -0
- package/experimental/random/AltGeneric.ff +44 -0
- package/experimental/random/Async.ff +68 -0
- package/experimental/random/Buffer2.ff +77 -0
- package/experimental/random/Cat.ff +12 -0
- package/experimental/random/Dictionary.ff +52 -0
- package/experimental/random/Example.ff +46 -0
- package/experimental/random/Generic.ff +102 -0
- package/experimental/random/HappyEyeballs.ff +40 -0
- package/experimental/random/HashMap.ff +72 -0
- package/experimental/random/IfElseUnit.ff +9 -0
- package/experimental/random/InputOutput.ff +23 -0
- package/experimental/random/ListVsArray.ff +45 -0
- package/experimental/random/Main.ff +44 -0
- package/experimental/random/MapTest.ff +67 -0
- package/experimental/random/OldTaskSystem.ff +210 -0
- package/experimental/random/PatternTest.ff +39 -0
- package/experimental/random/Patterns.ff +226 -0
- package/experimental/random/ReadBytesTest.ff +10 -0
- package/experimental/random/RunLength.ff +65 -0
- package/experimental/random/Scrape.ff +51 -0
- package/experimental/random/Serialization.ff +217 -0
- package/experimental/random/SerializationTest.ff +46 -0
- package/experimental/random/Serializer.ff +36 -0
- package/experimental/random/StdInOutErr.ff +4 -0
- package/experimental/random/Symbols.ff +74 -0
- package/experimental/random/Tag.ff +49 -0
- package/experimental/random/Tensor.ff +52 -0
- package/experimental/random/Try.ff +56 -0
- package/experimental/random/Tsv.ff +9 -0
- package/experimental/random/TypesAreModules.ff +87 -0
- package/experimental/random/blueprints/Blueprint.ff +52 -0
- package/experimental/random/blueprints/Main.ff +11 -0
- package/experimental/random/blueprints/Pretty.ff +58 -0
- package/experimental/random/blueprints/User.ff +64 -0
- package/experimental/random/blueprintsystem/BlueprintSystem.ff +48 -0
- package/experimental/random/blueprintsystem/Deserialize.ff +53 -0
- package/experimental/random/blueprintsystem/ReadJs.ff +13 -0
- package/experimental/random/blueprintsystem/User.ff +2 -0
- package/experimental/random/kahrs/Kahrs.ff +112 -0
- package/experimental/random/kahrs/TestKahrs.ff +22 -0
- package/experimental/random/kahrs/TestMap.ff +18 -0
- package/experimental/random/streaming/Gzip.ff +3 -0
- package/experimental/random/streaming/Main.ff +34 -0
- package/experimental/random/streaming/S3Bucket.ff +11 -0
- package/experimental/random/streaming/Tar.ff +5 -0
- package/experimental/rhymeapp/Main.ff +81 -0
- package/experimental/rhymeapp/index.html +14 -0
- package/firefly.sh +5 -0
- package/fireflysite/Main.ff +13 -0
- package/httpserver/.firefly/package.ff +1 -0
- package/httpserver/HttpServer.ff +184 -0
- package/lsp/.firefly/package.ff +1 -0
- package/lsp/CompletionHandler.ff +814 -0
- package/lsp/Handler.ff +551 -0
- package/lsp/HoverHandler.ff +82 -0
- package/lsp/LanguageServer.ff +229 -0
- package/lsp/SignatureHelpHandler.ff +55 -0
- package/lsp/SymbolHandler.ff +167 -0
- package/output/js/ff/compiler/Builder.mjs +483 -0
- package/output/js/ff/compiler/Compiler.mjs +410 -0
- package/output/js/ff/compiler/Dependencies.mjs +388 -0
- package/output/js/ff/compiler/Deriver.mjs +1166 -0
- package/output/js/ff/compiler/Dictionaries.mjs +1305 -0
- package/output/js/ff/compiler/Environment.mjs +1005 -0
- package/output/js/ff/compiler/Inference.mjs +4264 -0
- package/output/js/ff/compiler/JsEmitter.mjs +5353 -0
- package/output/js/ff/compiler/JsImporter.mjs +262 -0
- package/output/js/ff/compiler/LspHook.mjs +789 -0
- package/output/js/ff/compiler/Main.mjs +1695 -0
- package/output/js/ff/compiler/Parser.mjs +4004 -0
- package/output/js/ff/compiler/Patterns.mjs +923 -0
- package/output/js/ff/compiler/Resolver.mjs +2303 -0
- package/output/js/ff/compiler/Substitution.mjs +1146 -0
- package/output/js/ff/compiler/Syntax.mjs +12430 -0
- package/output/js/ff/compiler/Token.mjs +3092 -0
- package/output/js/ff/compiler/Tokenizer.mjs +589 -0
- package/output/js/ff/compiler/Unification.mjs +1748 -0
- package/output/js/ff/compiler/Wildcards.mjs +604 -0
- package/output/js/ff/compiler/Workspace.mjs +683 -0
- package/output/js/ff/core/Any.mjs +139 -0
- package/output/js/ff/core/Array.mjs +594 -0
- package/output/js/ff/core/AssetSystem.mjs +270 -0
- package/output/js/ff/core/Atomic.mjs +186 -0
- package/output/js/ff/core/Bool.mjs +141 -0
- package/output/js/ff/core/BrowserSystem.mjs +122 -0
- package/output/js/ff/core/Buffer.mjs +467 -0
- package/output/js/ff/core/BuildSystem.mjs +320 -0
- package/output/js/ff/core/Channel.mjs +268 -0
- package/output/js/ff/core/Char.mjs +145 -0
- package/output/js/ff/core/Core.mjs +300 -0
- package/output/js/ff/core/Duration.mjs +112 -0
- package/output/js/ff/core/Equal.mjs +175 -0
- package/output/js/ff/core/Error.mjs +138 -0
- package/output/js/ff/core/FileHandle.mjs +164 -0
- package/output/js/ff/core/Float.mjs +214 -0
- package/output/js/ff/core/HttpClient.mjs +210 -0
- package/output/js/ff/core/Instant.mjs +105 -0
- package/output/js/ff/core/Int.mjs +254 -0
- package/output/js/ff/core/IntMap.mjs +282 -0
- package/output/js/ff/core/JsSystem.mjs +234 -0
- package/output/js/ff/core/JsValue.mjs +678 -0
- package/output/js/ff/core/List.mjs +2335 -0
- package/output/js/ff/core/Lock.mjs +322 -0
- package/output/js/ff/core/Log.mjs +159 -0
- package/output/js/ff/core/Map.mjs +358 -0
- package/output/js/ff/core/NodeSystem.mjs +288 -0
- package/output/js/ff/core/Nothing.mjs +100 -0
- package/output/js/ff/core/Option.mjs +1002 -0
- package/output/js/ff/core/Ordering.mjs +734 -0
- package/output/js/ff/core/Pair.mjs +318 -0
- package/output/js/ff/core/Path.mjs +768 -0
- package/output/js/ff/core/RbMap.mjs +1936 -0
- package/output/js/ff/core/Serializable.mjs +434 -0
- package/output/js/ff/core/Set.mjs +250 -0
- package/output/js/ff/core/Show.mjs +201 -0
- package/output/js/ff/core/Stack.mjs +595 -0
- package/output/js/ff/core/Stream.mjs +1300 -0
- package/output/js/ff/core/String.mjs +433 -0
- package/output/js/ff/core/StringMap.mjs +282 -0
- package/output/js/ff/core/Task.mjs +345 -0
- package/output/js/ff/core/Try.mjs +503 -0
- package/output/js/ff/core/Unit.mjs +103 -0
- package/package.json +29 -0
- package/postgresql/.firefly/include/package-lock.json +250 -0
- package/postgresql/.firefly/include/package.json +5 -0
- package/postgresql/.firefly/include/prepare.sh +2 -0
- package/postgresql/.firefly/package.ff +3 -0
- package/postgresql/Pg.ff +530 -0
- package/unsafejs/.firefly/package.ff +1 -0
- package/unsafejs/UnsafeJs.ff +19 -0
- package/vscode/.vscode/launch.json +18 -0
- package/vscode/.vscode/tasks.json +33 -0
- package/vscode/LICENSE.txt +21 -0
- package/vscode/Prepublish.ff +15 -0
- package/vscode/README.md +17 -0
- package/vscode/client/package-lock.json +544 -0
- package/vscode/client/package.json +22 -0
- package/vscode/client/src/extension.ts +64 -0
- package/vscode/client/tsconfig.json +12 -0
- package/vscode/icons/firefly-icon.png +0 -0
- package/vscode/icons/firefly-icon.svg +10 -0
- package/vscode/icons/firefly-logo-notext.png +0 -0
- package/vscode/icons/firefly-logo.png +0 -0
- package/vscode/language-configuration.json +39 -0
- package/vscode/package-lock.json +3623 -0
- package/vscode/package.json +144 -0
- package/vscode/snippets-none.json +1 -0
- package/vscode/snippets.json +241 -0
- package/vscode/syntaxes/firefly.tmLanguage.json +294 -0
- package/vscode/tsconfig.json +20 -0
|
@@ -0,0 +1,814 @@
|
|
|
1
|
+
import LspHook from ff:compiler
|
|
2
|
+
import Unification from ff:compiler
|
|
3
|
+
import Environment from ff:compiler
|
|
4
|
+
import Syntax from ff:compiler
|
|
5
|
+
import Handler
|
|
6
|
+
|
|
7
|
+
data CompletionInfo(
|
|
8
|
+
label: String
|
|
9
|
+
extra: String
|
|
10
|
+
snippet: String
|
|
11
|
+
member: Bool
|
|
12
|
+
type: Type
|
|
13
|
+
documentation: String
|
|
14
|
+
expectedType: Option[Type]
|
|
15
|
+
secondarySort: Int = 5
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
handleCompletion(system: NodeSystem, lspHook: LspHook, toplevel: Bool, followedByOpenBracket: Bool): JsValue {
|
|
19
|
+
let topLevelCompletions = if(!toplevel) {[]} else {toplevelCompletion(lspHook)}
|
|
20
|
+
let patternCompletions = lspHook.results().collectFirst {
|
|
21
|
+
| InferPatternHook h =>
|
|
22
|
+
let expected = h.unification.substitute(h.expected)
|
|
23
|
+
Some(patternCompletion(h.unification, h.environment, expected))
|
|
24
|
+
| _ =>
|
|
25
|
+
None
|
|
26
|
+
}
|
|
27
|
+
mutable sawLookupHook = False // Only consider the first lookup
|
|
28
|
+
mutable sawParameterOrVariantFieldHook = False // Rewrite the type completions to parameter completions
|
|
29
|
+
mutable typeCompletions = []
|
|
30
|
+
let completions = patternCompletions.else {lspHook.results().flatMap {
|
|
31
|
+
| ResolveTypeHook h =>
|
|
32
|
+
typeCompletions = [...typeCompletions, ...typeCompletion(h.types, h.typeGenerics)]
|
|
33
|
+
[]
|
|
34
|
+
| ResolveVariantFieldHook _ =>
|
|
35
|
+
sawParameterOrVariantFieldHook = True
|
|
36
|
+
[]
|
|
37
|
+
| InferLambdaStartHook h =>
|
|
38
|
+
h.unification.substitute(h.lambdaType).{
|
|
39
|
+
| TConstructor(_, "Function$1", [_, TConstructor(_, prefix, _), _]) =>
|
|
40
|
+
exhaustiveMatchCompletion(h.environment, prefix + "_", True)
|
|
41
|
+
| _ =>
|
|
42
|
+
[]
|
|
43
|
+
}
|
|
44
|
+
| InferLookupHook h {!sawLookupHook && !h.symbol.value.qualifiedName.startsWith("_")} =>
|
|
45
|
+
sawLookupHook = True
|
|
46
|
+
let typePrefix = if(h.symbol.value.qualifiedName.contains("_")) {
|
|
47
|
+
h.symbol.value.qualifiedName.reverse().dropWhile {_ != '_'}.reverse()
|
|
48
|
+
} else {
|
|
49
|
+
""
|
|
50
|
+
}
|
|
51
|
+
let expected = h.unification.substitute(h.expected)
|
|
52
|
+
let qualifiedByAlias = h.symbol.value.qualifiedName.{n =>
|
|
53
|
+
if(n.contains(".") && !n.contains("/") && !n.contains(":")) {n.takeWhile {_ != '.'}}
|
|
54
|
+
}
|
|
55
|
+
h.selfVariable.{
|
|
56
|
+
| Some(selfName) {typePrefix == "" && qualifiedByAlias == None} {
|
|
57
|
+
h.environment.symbols.get(selfName) | Some(scheme)
|
|
58
|
+
} {
|
|
59
|
+
scheme.signature.returnType | TConstructor(_, selfTypeName, _)
|
|
60
|
+
} =>
|
|
61
|
+
let otherCompletions = completion(h.unification, h.environment, "", None, expected)
|
|
62
|
+
let selfCompletions = h.environment.symbols.get(selfName).toList().flatMap {scheme =>
|
|
63
|
+
let prefix = selfTypeName + "_"
|
|
64
|
+
completion(h.unification, h.environment, prefix, None, expected).filter {
|
|
65
|
+
_.label.first().any {_.isAsciiLower()}
|
|
66
|
+
}.map {c =>
|
|
67
|
+
c.CompletionInfo(label = selfName + "." + c.label, snippet = selfName + "." + c.snippet)
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
[...otherCompletions, ...selfCompletions]
|
|
71
|
+
| _ =>
|
|
72
|
+
completion(h.unification, h.environment, typePrefix, qualifiedByAlias, expected)
|
|
73
|
+
}
|
|
74
|
+
| InferRecordFieldHook h {h.unification.substitute(h.recordType) | TConstructor(_, n, ts)} =>
|
|
75
|
+
let fieldNames = n.split('$').toList().dropFirst(1)
|
|
76
|
+
let fieldCompletions = fieldNames.zip(ts).map {| Pair(name, t) =>
|
|
77
|
+
let t2 = h.unification.substitute(t)
|
|
78
|
+
CompletionInfo(name, "", name, True, t2, "(\n ...\n " + name + ": " + t2.show([]) + "\n ...\n)", Some(h.expected))
|
|
79
|
+
}
|
|
80
|
+
[...fieldCompletions, ...completion(h.unification, h.environment, n, None, h.expected)]
|
|
81
|
+
| InferArgumentHook h {
|
|
82
|
+
![ // Avoids named argument suggestions for operators that become trait method calls, such as ==
|
|
83
|
+
"ff:core/Equal.equals", "ff:core/Equal.notEquals"
|
|
84
|
+
"ff:core/Ordering.before", "ff:core/Ordering.notBefore"
|
|
85
|
+
"ff:core/Ordering.after", "ff:core/Ordering.notAfter"
|
|
86
|
+
].any {_ == h.callName}
|
|
87
|
+
} =>
|
|
88
|
+
let usedNames = h.arguments.pairs().filter {| Pair(i, a) => i != h.argumentIndex}.collect {_.second.name}
|
|
89
|
+
let usedPlaces = h.arguments.takeFirst(h.argumentIndex).filter {_.name.isEmpty()}.size()
|
|
90
|
+
let remainingParameters = h.parameters.filter {p => !usedNames.any {_ == p.name}}.dropFirst(usedPlaces)
|
|
91
|
+
let preselect = h.isCopy || usedNames.size() != 0
|
|
92
|
+
remainingParameters.pairs().map {| Pair(i, p) =>
|
|
93
|
+
namedParameterCompletion(p, i, preselect)
|
|
94
|
+
}
|
|
95
|
+
| InferTermHook h {h.term | ELet _} =>
|
|
96
|
+
h.missing.toList().filter {_.first.all {_.isAsciiLetterOrDigit()}}.flatMap {
|
|
97
|
+
| Pair(x, Pair(instantiated, None)) =>
|
|
98
|
+
missingCompletion(False, False, h.unification, x, instantiated, None)
|
|
99
|
+
| _ =>
|
|
100
|
+
[]
|
|
101
|
+
}
|
|
102
|
+
| InferSequentialStartHook h {h.term | ESequential(_, before, _)} =>
|
|
103
|
+
function lastIsVariable(term: Term): Bool {
|
|
104
|
+
| ESequential(_, _, e) => lastIsVariable(e)
|
|
105
|
+
| EVariable _ => True
|
|
106
|
+
| _ => False
|
|
107
|
+
}
|
|
108
|
+
if(!lastIsVariable(before)) {[]} else:
|
|
109
|
+
h.missing.toList().filter {| Pair(x, _) =>
|
|
110
|
+
x.size() != 0 && x.all {_.isAsciiLetterOrDigit()}
|
|
111
|
+
}.flatMap {| Pair(x, Pair(instantiated, arguments)) =>
|
|
112
|
+
missingCompletion(True, False, h.unification, x, instantiated, arguments)
|
|
113
|
+
}
|
|
114
|
+
| InferParameterHook h =>
|
|
115
|
+
sawParameterOrVariantFieldHook = True
|
|
116
|
+
h.missing.toList().filter {| Pair(x, _) =>
|
|
117
|
+
x.size() != 0 && x.all {_.isAsciiLetterOrDigit()}
|
|
118
|
+
}.flatMap {| Pair(x, Pair(instantiated, arguments)) =>
|
|
119
|
+
missingCompletion(False, True, h.unification, x, instantiated, arguments)
|
|
120
|
+
}
|
|
121
|
+
| InferFunctionDefinitionHook h {!h.definition.signature.member} =>
|
|
122
|
+
h.missing.toList().filter {| Pair(x, _) =>
|
|
123
|
+
x.size() != 0 && x.all {_.isAsciiLetterOrDigit()}
|
|
124
|
+
}.flatMap {| Pair(x, Pair(instantiated, arguments)) =>
|
|
125
|
+
arguments.toList().flatMap {as =>
|
|
126
|
+
missingCompletion(False, False, h.unification, x, instantiated, Some(as))
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
| InferFunctionDefinitionHook h {h.definition.signature.member} =>
|
|
130
|
+
h.missing.toList().filter {_.first.contains("_")}.map {
|
|
131
|
+
_.mapFirst {_.reverse().takeWhile {_ != '_'}.reverse()}
|
|
132
|
+
}.filter {| Pair(x, _) =>
|
|
133
|
+
x.size() != 0 && x.all {_.isAsciiLetterOrDigit()}
|
|
134
|
+
}.flatMap {| Pair(x, Pair(instantiated, arguments)) =>
|
|
135
|
+
arguments.toList().flatMap {as =>
|
|
136
|
+
let instantiated2 = instantiated.Instantiated(scheme = instantiated.scheme.Scheme(
|
|
137
|
+
signature = instantiated.scheme.signature.Signature(
|
|
138
|
+
parameters = instantiated.scheme.signature.parameters.dropFirst()
|
|
139
|
+
)
|
|
140
|
+
))
|
|
141
|
+
missingCompletion(False, False, h.unification, x, instantiated2, Some(as.dropFirst()))
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
| _ =>
|
|
145
|
+
[]
|
|
146
|
+
}}
|
|
147
|
+
let fixedTypeCompletions = if(!sawParameterOrVariantFieldHook) {typeCompletions} else {
|
|
148
|
+
typeCompletions.map {c => c.CompletionInfo(snippet =
|
|
149
|
+
(c.snippet.slice(0, 1).lower() + c.snippet.dropFirst()).takeWhile {_ != '['} + ": " + c.snippet
|
|
150
|
+
)}
|
|
151
|
+
}
|
|
152
|
+
let fixedCompletions = if(!followedByOpenBracket) {completions} else {
|
|
153
|
+
completions.map {c =>
|
|
154
|
+
if(c.snippet.dropFirst(1).any {c => c != '(' && c != '[' && c != '{'}) {
|
|
155
|
+
c.CompletionInfo(snippet = c.snippet.takeWhile {c => c != ' ' && c != '(' && c != '[' && c != '{'})
|
|
156
|
+
} else {c}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
completionsToJson(system.js(), [...fixedCompletions, ...fixedTypeCompletions, ...topLevelCompletions])
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
completionsToJson(js: JsSystem, completions: List[CompletionInfo]): JsValue {
|
|
163
|
+
js.object()
|
|
164
|
+
.with("isIncomplete", False)
|
|
165
|
+
.with("items", completions.distinct().toArray().map {| CompletionInfo i =>
|
|
166
|
+
let shownType = i.type.show([])
|
|
167
|
+
let isAlpha = i.label.first().any {_.isAsciiLetter()}
|
|
168
|
+
let isLower = i.label.first().any {_.isAsciiLower()}
|
|
169
|
+
let isUnqualified = !i.label.contains(".")
|
|
170
|
+
let isMember = isLower && !isUnqualified
|
|
171
|
+
let isVariable = isLower && !i.extra.contains("(") && !i.extra.contains("{")
|
|
172
|
+
let isDefinition = ["let ", "mutable ", "function "].any {i.snippet.startsWith(_)}
|
|
173
|
+
let isParameter = i.snippet.endsWith(" = ")
|
|
174
|
+
let sortText = if(isParameter || isDefinition) {0} else {9 - (
|
|
175
|
+
isVariable.toInt() + isUnqualified.toInt() +
|
|
176
|
+
isAlpha.toInt() + isLower.toInt() + isMember.toInt()
|
|
177
|
+
)} + if(isMember) {"1"} else {"0"} + i.secondarySort + i.label.lower()
|
|
178
|
+
let preselect = !isDefinition && Pair(i.type, i.expectedType).{
|
|
179
|
+
| Pair(TConstructor(_, n1, _), Some(TConstructor(_, n2, _))) => n1 == n2
|
|
180
|
+
| _ => False
|
|
181
|
+
}
|
|
182
|
+
js.object()
|
|
183
|
+
// Namespace or Property or Constructor or EnumMember or Constructor or Method/Function or Field/Variable
|
|
184
|
+
.with("kind"
|
|
185
|
+
if(shownType == "") {3} else:
|
|
186
|
+
if(isParameter) {10} else:
|
|
187
|
+
if(i.type.{| TConstructor(_, "type", _) => True | _ => False}) {9} else:
|
|
188
|
+
if(i.label.first().any {c => c == '{' || c == '|'}) {22} else:
|
|
189
|
+
if(i.label.first().any {_.isAsciiUpper()}) {4} else:
|
|
190
|
+
if(i.extra.any {c => c == '(' || c == '{'}) {2} else {5} + if(i.member) {0} else {1}
|
|
191
|
+
)
|
|
192
|
+
.with("sortText", sortText)
|
|
193
|
+
.with("preselect", preselect)
|
|
194
|
+
.with("label", i.label)
|
|
195
|
+
.with("labelDetails", js.object()
|
|
196
|
+
.with("detail", i.extra + if(shownType == "") {""} else {": " + shownType})
|
|
197
|
+
)
|
|
198
|
+
.with("insertText", i.snippet)
|
|
199
|
+
.with("insertTextFormat", 2 /* Snippet */)
|
|
200
|
+
.with("documentation", js.object()
|
|
201
|
+
.with("kind", "markdown")
|
|
202
|
+
.with("value", "```\n" + i.documentation + "\n```")
|
|
203
|
+
)
|
|
204
|
+
})
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
typeCompletion(types: Map[String, String], typeGenerics: Map[String, List[String]]): List[CompletionInfo] {
|
|
208
|
+
let completions = types.toList().filter {| Pair(n, full) =>
|
|
209
|
+
n.all {_.isAsciiLetterOrDigit()}
|
|
210
|
+
}.map {| Pair(typeName, full) =>
|
|
211
|
+
let generics = typeGenerics.find {k, _ => k == typeName}.toList().flatMap {_.second}
|
|
212
|
+
let realGenerics = generics.filter {_ != "Q$"}
|
|
213
|
+
let label = typeName
|
|
214
|
+
let extra = if(realGenerics.isEmpty()) {""} else {"[" + realGenerics.join(", ") + "]"}
|
|
215
|
+
let snippet = typeName + if(realGenerics.isEmpty()) {""} else {"[$0]"}
|
|
216
|
+
CompletionInfo(label, extra, snippet, False, TConstructor(Location("", 0, 0), "type", []), full, None)
|
|
217
|
+
}
|
|
218
|
+
completions
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
completion(
|
|
222
|
+
unification: Unification
|
|
223
|
+
environment: Environment
|
|
224
|
+
prefix: String
|
|
225
|
+
qualifiedByAlias: Option[String]
|
|
226
|
+
expected: Type
|
|
227
|
+
): List[CompletionInfo] {
|
|
228
|
+
|
|
229
|
+
let member = prefix.contains("_")
|
|
230
|
+
|
|
231
|
+
let members = Stack.make()
|
|
232
|
+
|
|
233
|
+
let symbols = if(prefix == "") {
|
|
234
|
+
environment.symbols.toList().collect {
|
|
235
|
+
| Pair(name, _)@pair {name.all {_.isAsciiLetterOrDigit()}} =>
|
|
236
|
+
Some(pair)
|
|
237
|
+
| Pair(name, _)@pair {name.startsWith("ff:core/Core.")} =>
|
|
238
|
+
Some(pair.Pair(first = name.dropFirst("ff:core/Core.".size())))
|
|
239
|
+
| Pair(name, _)@pair {name.startsWith(environment.modulePrefix)} {
|
|
240
|
+
name.dropFirst(environment.modulePrefix.size()) | n
|
|
241
|
+
} {n.all {_.isAsciiLetterOrDigit()}} =>
|
|
242
|
+
Some(pair.Pair(first = n))
|
|
243
|
+
| Pair(name, scheme) {name.dropWhile {_ != '/'}.dropFirst() | short} {
|
|
244
|
+
short.reverse().takeWhile {_ != '.'}.reverse() | shorter
|
|
245
|
+
} {shorter.all {_.isAsciiLetterOrDigit()}} =>
|
|
246
|
+
if(shorter.first().any {_.isAsciiUpper()}) {
|
|
247
|
+
Some(Pair(shorter, scheme))
|
|
248
|
+
} else {
|
|
249
|
+
let module = name.dropLast(shorter.size() + 1)
|
|
250
|
+
let alias = environment.imports.find {_, i =>
|
|
251
|
+
module == i.package.groupName() + "/" + i.directory.map {_ + "/"}.join() + i.file
|
|
252
|
+
}
|
|
253
|
+
alias.map {| Pair(alias, i) =>
|
|
254
|
+
Pair(alias + short.dropFirst(i.file.size()), scheme)
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
| _ =>
|
|
258
|
+
None
|
|
259
|
+
}.toMap()
|
|
260
|
+
} else {
|
|
261
|
+
members.pushAll(exhaustiveMatchCompletion(environment, prefix, False).toStack())
|
|
262
|
+
let shorterPrefix = prefix.dropLast()
|
|
263
|
+
let recordFields = shorterPrefix.split('$').dropFirst().toSet()
|
|
264
|
+
environment.symbols.each {
|
|
265
|
+
| shortName, scheme {shortName.dropWhile {_ != '/'}.dropFirst() | short} {
|
|
266
|
+
short.reverse().takeWhile {_ != '.'}.reverse() | shorter
|
|
267
|
+
} {shorter.all {_.isAsciiLetterOrDigit()} && shorter.first().any {_.isAsciiUpper()}} =>
|
|
268
|
+
scheme.signature.returnType.{
|
|
269
|
+
| _ {prefix.startsWith("Record$")} =>
|
|
270
|
+
if(scheme.signature.parameters.any {recordFields.contains(_.name)}) {
|
|
271
|
+
members.push(makeCompletion(unification, expected, "", shorter, scheme, True, member))
|
|
272
|
+
}
|
|
273
|
+
| TConstructor(_, name, _) {name.startsWith(shorterPrefix)} =>
|
|
274
|
+
if(!scheme.signature.parameters.isEmpty()) {
|
|
275
|
+
members.push(makeCompletion(unification, expected, "", shorter, scheme, True, member))
|
|
276
|
+
}
|
|
277
|
+
| _ =>
|
|
278
|
+
}
|
|
279
|
+
| _, _ =>
|
|
280
|
+
}
|
|
281
|
+
environment.symbols
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
let filteredSymbols = qualifiedByAlias.{
|
|
285
|
+
| None => symbols
|
|
286
|
+
| Some(alias) =>
|
|
287
|
+
let aliasPrefix = alias + "."
|
|
288
|
+
symbols.toList().filter {
|
|
289
|
+
_.first.startsWith(aliasPrefix)
|
|
290
|
+
}.map {
|
|
291
|
+
_.mapFirst {_.dropFirst(aliasPrefix.size())}
|
|
292
|
+
}.toMap()
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
filteredSymbols.each {memberName, memberScheme =>
|
|
296
|
+
if(memberName.startsWith(prefix)) {
|
|
297
|
+
members.push(makeCompletion(unification, expected, prefix, memberName, memberScheme, False, member))
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
members.toList()
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
makeCompletion(
|
|
304
|
+
unification: Unification
|
|
305
|
+
expected: Type
|
|
306
|
+
prefix: String
|
|
307
|
+
memberName: String
|
|
308
|
+
memberScheme: Scheme
|
|
309
|
+
copy: Bool
|
|
310
|
+
member: Bool
|
|
311
|
+
): CompletionInfo {
|
|
312
|
+
let shortName = memberName.dropFirst(prefix.size())
|
|
313
|
+
let unqualifiedName = shortName.reverse().takeWhile {_ != '.'}.reverse()
|
|
314
|
+
let upper = unqualifiedName.first().any {_.isAsciiUpper()}
|
|
315
|
+
let variantWithoutParameters = upper && memberScheme.signature.parameters.isEmpty()
|
|
316
|
+
let realParameters = memberScheme.signature.parameters.dropFirst(if(member && !copy) {1} else {0})
|
|
317
|
+
let pair = if(!memberScheme.isVariable && !variantWithoutParameters) {
|
|
318
|
+
let trailing = realParameters.pairs().reverse().map {| Pair(index, p) =>
|
|
319
|
+
p.valueType.{
|
|
320
|
+
| TConstructor(_, name, _) {name.startsWith("Function$")} =>
|
|
321
|
+
Some(Pair(
|
|
322
|
+
" {...}"
|
|
323
|
+
if(index == 0) {" {$0}"} else {" {}"}
|
|
324
|
+
))
|
|
325
|
+
| _ =>
|
|
326
|
+
None
|
|
327
|
+
}
|
|
328
|
+
}.toStream().takeWhile {_ != None}.collect {_}.toList().reverse()
|
|
329
|
+
let allRequired = realParameters.filter {_.default.isEmpty()}
|
|
330
|
+
let required = allRequired.dropLast(trailing.size()).map {_.name}
|
|
331
|
+
let optional = if(allRequired.size() != realParameters.size()) {"..."}
|
|
332
|
+
Pair(
|
|
333
|
+
if(trailing.isEmpty() || !required.isEmpty()) {
|
|
334
|
+
"(" + [...required, ...optional.toList()].join(", ") + ")"
|
|
335
|
+
} else {
|
|
336
|
+
""
|
|
337
|
+
} + trailing.map {_.first}.join()
|
|
338
|
+
if(copy) {
|
|
339
|
+
"(${1|" + realParameters.map {_.name}.map {f => f + " = "}.join(",") + "|}$0)"
|
|
340
|
+
} else {
|
|
341
|
+
if(trailing.isEmpty() || !required.isEmpty()) {
|
|
342
|
+
if(required.isEmpty()) {"()"} else {"($0)"}
|
|
343
|
+
} else {
|
|
344
|
+
""
|
|
345
|
+
} + if(trailing.isEmpty()) {""} else {trailing.map {_.second}.join()}
|
|
346
|
+
}
|
|
347
|
+
)
|
|
348
|
+
} else {Pair("", "")}
|
|
349
|
+
let returnType = unification.substitute(memberScheme.signature.returnType)
|
|
350
|
+
let documentation = if(memberScheme.isVariable || variantWithoutParameters) {
|
|
351
|
+
let methodGenerics = memberScheme.signature.generics
|
|
352
|
+
let generics = if(member || methodGenerics.isEmpty()) {""} else {"[" + methodGenerics.join(", ") + "]"}
|
|
353
|
+
let beforeAfter = memberScheme.signature.parameters.first().map {
|
|
354
|
+
Pair(unification.substitute(_.valueType).show([]) + "(\n ...\n ", "\n ...\n)")
|
|
355
|
+
}.else {Pair("", "")}
|
|
356
|
+
beforeAfter.first +
|
|
357
|
+
if(memberScheme.isMutable) {"mutable "} else {""} +
|
|
358
|
+
unqualifiedName + generics +
|
|
359
|
+
": " + returnType.show([]) +
|
|
360
|
+
beforeAfter.second
|
|
361
|
+
} else {
|
|
362
|
+
let selfType = memberScheme.signature.parameters.first().filter {_ => member && !copy}.map {_.valueType}
|
|
363
|
+
let generics = selfType.map {
|
|
364
|
+
| TConstructor(_, _, gs) =>
|
|
365
|
+
// TODO: Needs constraints as well
|
|
366
|
+
// TODO: This drops the wrong number of type parameters - more environment needed
|
|
367
|
+
let methodGenerics = memberScheme.signature.generics.dropFirst(gs.size() + 1)
|
|
368
|
+
if(methodGenerics.isEmpty()) {""} else {"[" + methodGenerics.join(", ") + "]"}
|
|
369
|
+
| _ =>
|
|
370
|
+
""
|
|
371
|
+
}.else {
|
|
372
|
+
let methodGenerics = memberScheme.signature.generics.filter {_ != "Q$"}
|
|
373
|
+
if(methodGenerics.isEmpty()) {""} else {"[" + methodGenerics.join(", ") + "]"}
|
|
374
|
+
}
|
|
375
|
+
let selfIndent = selfType.map {_ => " "}.else {""}
|
|
376
|
+
let parameters = if(realParameters.isEmpty()) {""} else {
|
|
377
|
+
"\n" + realParameters.map {p =>
|
|
378
|
+
showCompletionParameter(selfIndent + " ", p)
|
|
379
|
+
}.join("\n") + "\n" + selfIndent
|
|
380
|
+
}
|
|
381
|
+
selfType.map {_.show([])}.map {"extend " + _ + " {\n"}.else {""} +
|
|
382
|
+
selfIndent + unqualifiedName +
|
|
383
|
+
generics + "(" + parameters + "): " +
|
|
384
|
+
returnType.show([]) +
|
|
385
|
+
selfType.map {_ => "\n}"}.else {""}
|
|
386
|
+
}
|
|
387
|
+
CompletionInfo(
|
|
388
|
+
label = shortName
|
|
389
|
+
extra = pair.first
|
|
390
|
+
snippet = shortName + pair.second
|
|
391
|
+
member = member && !copy
|
|
392
|
+
type = returnType
|
|
393
|
+
documentation = documentation
|
|
394
|
+
expectedType = Some(expected)
|
|
395
|
+
)
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
exhaustiveMatchCompletion(environment: Environment, prefix: String, inside: Bool): List[CompletionInfo] {
|
|
399
|
+
if(prefix == "ff:core/List.List_") {
|
|
400
|
+
let curly = if(inside) {Pair("", "")} else {Pair("{", "}")}
|
|
401
|
+
[CompletionInfo(
|
|
402
|
+
label = curly.first + "| "
|
|
403
|
+
extra = "[] => ... | [first, ...rest] => ..." + curly.second
|
|
404
|
+
snippet = curly.first + "\n | [] => $0\n | [first, ...rest] =>\n" + curly.second
|
|
405
|
+
member = True
|
|
406
|
+
type = TConstructor(Location("", 0, 0), "exhaustive match", [])
|
|
407
|
+
documentation = "// Exhaustive list match"
|
|
408
|
+
expectedType = None
|
|
409
|
+
)]
|
|
410
|
+
} else:
|
|
411
|
+
let shorterPrefix = prefix.dropLast()
|
|
412
|
+
let variants = environment.symbols.toList().filter {s =>
|
|
413
|
+
!s.first.contains("_") &&
|
|
414
|
+
s.first.reverse().takeWhile {_ != '.'}.reverse().first().any {_.isAsciiUpper()} &&
|
|
415
|
+
s.second.signature.returnType.{
|
|
416
|
+
| TConstructor(_, n, _) => n == shorterPrefix
|
|
417
|
+
| _ => False
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
if(variants.isEmpty()) {[]} else:
|
|
421
|
+
let label = if(inside) {""} else {"{"} +
|
|
422
|
+
variants.map {v =>
|
|
423
|
+
"| " + v.first.reverse().takeWhile {_ != '.'}.reverse() +
|
|
424
|
+
if(v.second.signature.parameters.isEmpty()) {""} else {"(...)"} + " => ..."
|
|
425
|
+
}.join(" ") +
|
|
426
|
+
if(inside) {""} else {"}"}
|
|
427
|
+
let snippetParts = variants.pairs().map {| Pair(index, Pair(name, scheme)) =>
|
|
428
|
+
"| " + name.reverse().takeWhile {_ != '.'}.reverse() +
|
|
429
|
+
if(scheme.signature.parameters.isEmpty()) {""} else {
|
|
430
|
+
"(" + scheme.signature.parameters.map {_.name}.join(", ") + ")"
|
|
431
|
+
} +
|
|
432
|
+
" => " + if(index == 0) {"$0"} else {""}
|
|
433
|
+
}
|
|
434
|
+
let snippet =
|
|
435
|
+
if(inside) {""} else {"{"} +
|
|
436
|
+
if(snippetParts.size() != 1) {
|
|
437
|
+
"\n" + snippetParts.map {" " + _}.join("\n") + "\n"
|
|
438
|
+
} else {
|
|
439
|
+
snippetParts.join(" ")
|
|
440
|
+
} +
|
|
441
|
+
if(inside) {""} else {"}"}
|
|
442
|
+
[CompletionInfo(
|
|
443
|
+
label = label.slice(0, 2)
|
|
444
|
+
extra = label.dropFirst(2)
|
|
445
|
+
snippet = snippet
|
|
446
|
+
member = True
|
|
447
|
+
type = TConstructor(Location("", 0, 0), "exhaustive match", [])
|
|
448
|
+
documentation = "// Exhaustive match:\n" + snippetParts.join("\n").replace("$0", "")
|
|
449
|
+
expectedType = None
|
|
450
|
+
)]
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
patternCompletion(unification: Unification, environment: Environment, expected: Type): List[CompletionInfo] {
|
|
454
|
+
let typeName = unification.substitute(expected).{
|
|
455
|
+
| TConstructor(_, name, _) => name
|
|
456
|
+
| _ => ""
|
|
457
|
+
}
|
|
458
|
+
if(typeName == "") {[]} else:
|
|
459
|
+
if(typeName == "ff:core/List.List") {
|
|
460
|
+
[CompletionInfo("[...]", "", "[$0]", False, expected, "// List pattern", Some(expected))]
|
|
461
|
+
} else:
|
|
462
|
+
if(typeName == "ff:core/String.String") {
|
|
463
|
+
[CompletionInfo("\"...\"", "", "\"$0\"", False, expected, "// String pattern", Some(expected))]
|
|
464
|
+
} else:
|
|
465
|
+
if(typeName == "ff:core/Char.Char") {
|
|
466
|
+
[CompletionInfo("'...'", "", "'$0'", False, expected, "// Char pattern", Some(expected))]
|
|
467
|
+
} else:
|
|
468
|
+
if(typeName == "ff:core/Int.Int") {
|
|
469
|
+
[CompletionInfo("0", "", "0", False, expected, "// Int pattern", Some(expected))]
|
|
470
|
+
} else:
|
|
471
|
+
let variants = do {
|
|
472
|
+
environment.symbols.toList().filter {s =>
|
|
473
|
+
!s.first.contains("_") &&
|
|
474
|
+
s.first.reverse().takeWhile {_ != '.'}.reverse().first().any {_.isAsciiUpper()} &&
|
|
475
|
+
s.second.signature.returnType.{
|
|
476
|
+
| TConstructor(_, n, _) => n == typeName
|
|
477
|
+
| _ => False
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
let completions = variants.map {| Pair(name, scheme) =>
|
|
482
|
+
let shortName = name.reverse().takeWhile {_ != '.'}.reverse()
|
|
483
|
+
let extra = if(scheme.signature.parameters.isEmpty()) {""} else {
|
|
484
|
+
"(" + scheme.signature.parameters.map {_.name}.join(", ") + ")"
|
|
485
|
+
}
|
|
486
|
+
let generics = scheme.signature.generics.filter {_ != "Q$"}
|
|
487
|
+
let documentation =
|
|
488
|
+
shortName +
|
|
489
|
+
if(generics.isEmpty()) {""} else {"[" + generics.join(", ") + "]"} +
|
|
490
|
+
if(scheme.signature.parameters.isEmpty()) {""} else {
|
|
491
|
+
"(\n" + scheme.signature.parameters.map {
|
|
492
|
+
showCompletionParameter(" ", _)
|
|
493
|
+
}.join("\n") + "\n)"
|
|
494
|
+
} + ": " + scheme.signature.returnType.show([])
|
|
495
|
+
CompletionInfo(
|
|
496
|
+
label = shortName
|
|
497
|
+
extra = extra
|
|
498
|
+
snippet = shortName + if(scheme.signature.parameters.isEmpty()) {""} else {"($0)"}
|
|
499
|
+
member = False
|
|
500
|
+
type = expected
|
|
501
|
+
documentation = documentation
|
|
502
|
+
expectedType = Some(expected)
|
|
503
|
+
)
|
|
504
|
+
}
|
|
505
|
+
completions
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
missingCompletion(
|
|
509
|
+
keyword: Bool
|
|
510
|
+
isParameter: Bool
|
|
511
|
+
unification: Unification
|
|
512
|
+
name: String
|
|
513
|
+
instantiated: Instantiated
|
|
514
|
+
arguments: Option[List[Argument]]
|
|
515
|
+
): List[CompletionInfo] {
|
|
516
|
+
function findTermName(term: Term): Option[String] {
|
|
517
|
+
| ECall e {e.target | DynamicCall c} {
|
|
518
|
+
e.arguments.last() | Some(Argument(_, _, e2))
|
|
519
|
+
} {e2 | ELambda(_, Lambda(_, _, cases))} =>
|
|
520
|
+
cases.collectFirst {findTermName(_.body)}
|
|
521
|
+
| ECall e {e.target | DynamicCall c} => findTermName(c.function)
|
|
522
|
+
| ECopy e => Some(e.name.slice(0, 1).lower() + e.name.dropFirst())
|
|
523
|
+
| EField e => Some(e.field)
|
|
524
|
+
| ELambda e {e.lambda.cases | [MatchCase c]} => findTermName(c.body)
|
|
525
|
+
| EList e {e.items | []} => Some("list")
|
|
526
|
+
| EList e {e.items | [e1, ...]} => findTermName(e1.first)
|
|
527
|
+
| EPipe e => findTermName(e.function)
|
|
528
|
+
| ERecord _ => Some("record")
|
|
529
|
+
| EVariable e {e.name != "" && e.name.all {_.isAsciiLetterOrDigit()}} => Some(e.name)
|
|
530
|
+
| EVariant e => Some(e.name.slice(0, 1).lower() + e.name.dropFirst())
|
|
531
|
+
| EVariantIs e => Some(e.name.slice(0, 1).lower() + e.name.dropFirst())
|
|
532
|
+
| _ => None
|
|
533
|
+
}
|
|
534
|
+
function findTypeName(type: Type): String {
|
|
535
|
+
| TConstructor(_, name, _) {name.startsWith("Function$")} =>
|
|
536
|
+
"lambda"
|
|
537
|
+
| TConstructor(_, name, _) =>
|
|
538
|
+
let n = name.reverse().takeWhile {_.isAsciiLetterOrDigit()}.reverse()
|
|
539
|
+
if(!n.first().all {_.isAsciiLetter()}) {"p"} else {n.slice(0, 1).lower() + n.dropFirst()}
|
|
540
|
+
| TVariable(_, index) => "p"
|
|
541
|
+
}
|
|
542
|
+
let completions = arguments.{
|
|
543
|
+
| None {isParameter} =>
|
|
544
|
+
let t = unification.substitute(instantiated.scheme.signature.returnType)
|
|
545
|
+
let snippet = t.{
|
|
546
|
+
| TConstructor _ => name + ": " + t.show([])
|
|
547
|
+
| TVariable _ => name + ": "
|
|
548
|
+
}
|
|
549
|
+
let documentation = "// Add missing parameter\n" + name + ": " + t.show([])
|
|
550
|
+
[CompletionInfo(name, "", snippet, False, t, documentation, Some(t))]
|
|
551
|
+
| Some(as) {isParameter} =>
|
|
552
|
+
let noEffect = TConstructor(instantiated.scheme.signature.at, "ff:core/Nothing.Nothing", [])
|
|
553
|
+
let t = unification.substitute(TConstructor(
|
|
554
|
+
instantiated.scheme.signature.at
|
|
555
|
+
"Function$" + instantiated.scheme.signature.parameters.size()
|
|
556
|
+
[
|
|
557
|
+
noEffect
|
|
558
|
+
...instantiated.scheme.signature.parameters.map {_.valueType}
|
|
559
|
+
instantiated.scheme.signature.returnType
|
|
560
|
+
]
|
|
561
|
+
))
|
|
562
|
+
let documentation = "// Add missing parameter\n" + name + ": " + t.show([])
|
|
563
|
+
[CompletionInfo(name, "", name + ": " + t.show([]), False, t, documentation, Some(t))]
|
|
564
|
+
| None =>
|
|
565
|
+
let t = unification.substitute(instantiated.scheme.signature.returnType)
|
|
566
|
+
let snippet = name + " = "
|
|
567
|
+
let documentation = "// Define missing variable\n" + name + ": " + t.show([])
|
|
568
|
+
[CompletionInfo(snippet, "...", snippet, False, t, documentation, Some(t))]
|
|
569
|
+
| Some(as) =>
|
|
570
|
+
mutable remainingParameters = instantiated.scheme.signature.parameters
|
|
571
|
+
mutable usedNames = [].toSet()
|
|
572
|
+
function uniqueName(name: String): String {
|
|
573
|
+
let alternativeName = name + (usedNames.size() + 1)
|
|
574
|
+
if(!usedNames.contains(name)) {
|
|
575
|
+
usedNames = usedNames.add(name)
|
|
576
|
+
name
|
|
577
|
+
} elseIf {!usedNames.contains(alternativeName)} {
|
|
578
|
+
usedNames = usedNames.add(alternativeName)
|
|
579
|
+
alternativeName
|
|
580
|
+
} else {
|
|
581
|
+
usedNames = usedNames.add(alternativeName + "_" + (usedNames.size() + 1))
|
|
582
|
+
uniqueName(name)
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
let parameters = as.collect {a =>
|
|
586
|
+
let found = a.name.{
|
|
587
|
+
| Some(n) => remainingParameters.find {_.name == n}
|
|
588
|
+
| None => remainingParameters.first()
|
|
589
|
+
}
|
|
590
|
+
found.map {p =>
|
|
591
|
+
remainingParameters = remainingParameters.filter {_.name != p.name}
|
|
592
|
+
let t = unification.substitute(p.valueType)
|
|
593
|
+
let x = a.name.orElse {findTermName(a.value)}.else {findTypeName(t)}
|
|
594
|
+
Pair(uniqueName(x), t)
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
let returnType = unification.substitute(instantiated.scheme.signature.returnType)
|
|
598
|
+
let types = [...parameters.map {_.second}, returnType]
|
|
599
|
+
let returnTypeCode = returnType.{| TVariable _ => "" | _ => ": " + returnType.show(types)}
|
|
600
|
+
let parameterSnippets = parameters.map {| Pair(x, t) =>
|
|
601
|
+
x + ": " + t.show(types)
|
|
602
|
+
}
|
|
603
|
+
let extra = "(" + parameterSnippets.join(", ") + ") {...}"
|
|
604
|
+
let shortSnippet = name + "(" + parameterSnippets.join(", ") + ")" + returnTypeCode
|
|
605
|
+
let longSnippet = name + "(\n " + parameterSnippets.join("\n ") + "\n)" + returnTypeCode
|
|
606
|
+
let snippet = if(shortSnippet.size() <= 100) {shortSnippet} else {longSnippet}
|
|
607
|
+
let documentation = "// Define missing function\n" +
|
|
608
|
+
if(shortSnippet.size() < 30) {shortSnippet} else {longSnippet}
|
|
609
|
+
[CompletionInfo(name, extra, snippet + " {\n $0\n}", False, returnType, documentation, None)]
|
|
610
|
+
}
|
|
611
|
+
if(keyword && arguments.isEmpty()) {
|
|
612
|
+
["let ", "mutable "].flatMap {k => completions.map {c =>
|
|
613
|
+
c.CompletionInfo(label = k + c.label, snippet = k + c.snippet)
|
|
614
|
+
}}
|
|
615
|
+
} elseIf {keyword} {
|
|
616
|
+
["function "].flatMap {k => completions.map {c =>
|
|
617
|
+
c.CompletionInfo(label = k + c.label, snippet = k + c.snippet)
|
|
618
|
+
}}
|
|
619
|
+
} else {
|
|
620
|
+
completions
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
namedParameterCompletion(parameter: Parameter, index: Int, preselect: Bool): CompletionInfo {
|
|
625
|
+
CompletionInfo(
|
|
626
|
+
label = parameter.name + " = "
|
|
627
|
+
extra = "..."
|
|
628
|
+
snippet = parameter.name + " = "
|
|
629
|
+
member = False
|
|
630
|
+
type = parameter.valueType
|
|
631
|
+
documentation = showCompletionParameter("", parameter)
|
|
632
|
+
expectedType = if(preselect) {parameter.valueType}
|
|
633
|
+
secondarySort = (1000 + index).clamp(1000, 9999)
|
|
634
|
+
)
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
toplevelCompletion(lspHook: LspHook): List[CompletionInfo] {
|
|
638
|
+
[
|
|
639
|
+
CompletionInfo(
|
|
640
|
+
label = "package",
|
|
641
|
+
extra = " some:package:0.0.0",
|
|
642
|
+
snippet = "package ",
|
|
643
|
+
member = False,
|
|
644
|
+
type = TConstructor(lspHook.at, "", []),
|
|
645
|
+
documentation = "// Declares the group, name and version of this package",
|
|
646
|
+
expectedType = None
|
|
647
|
+
)
|
|
648
|
+
CompletionInfo(
|
|
649
|
+
label = "dependency",
|
|
650
|
+
extra = " some:package:0.0.0",
|
|
651
|
+
snippet = "dependency ",
|
|
652
|
+
member = False,
|
|
653
|
+
type = TConstructor(lspHook.at, "", []),
|
|
654
|
+
documentation = "// Declares a dependency on the specified version of the package",
|
|
655
|
+
expectedType = None
|
|
656
|
+
)
|
|
657
|
+
CompletionInfo(
|
|
658
|
+
label = "import",
|
|
659
|
+
extra = " Module from some:package",
|
|
660
|
+
snippet = "import ",
|
|
661
|
+
member = False,
|
|
662
|
+
type = TConstructor(lspHook.at, "", []),
|
|
663
|
+
documentation = "// Imports a module from a package",
|
|
664
|
+
expectedType = None
|
|
665
|
+
)
|
|
666
|
+
CompletionInfo(
|
|
667
|
+
label = "extend",
|
|
668
|
+
extra = " self[T]: SomeType[T] {...}",
|
|
669
|
+
snippet = "extend ",
|
|
670
|
+
member = False,
|
|
671
|
+
type = TConstructor(lspHook.at, "", []),
|
|
672
|
+
documentation = "// Add methods to a type defined in this module",
|
|
673
|
+
expectedType = None
|
|
674
|
+
)
|
|
675
|
+
CompletionInfo(
|
|
676
|
+
label = "data",
|
|
677
|
+
extra = " SomeType[T](...) {...}",
|
|
678
|
+
snippet = "data ",
|
|
679
|
+
member = False,
|
|
680
|
+
type = TConstructor(lspHook.at, "", []),
|
|
681
|
+
documentation = "// Defines an immutable type",
|
|
682
|
+
expectedType = None
|
|
683
|
+
)
|
|
684
|
+
CompletionInfo(
|
|
685
|
+
label = "class",
|
|
686
|
+
extra = " SomeType[T](...) {...}",
|
|
687
|
+
snippet = "class ",
|
|
688
|
+
member = False,
|
|
689
|
+
type = TConstructor(lspHook.at, "", []),
|
|
690
|
+
documentation = "// Defines a mutable type",
|
|
691
|
+
expectedType = None
|
|
692
|
+
)
|
|
693
|
+
CompletionInfo(
|
|
694
|
+
label = "capability",
|
|
695
|
+
extra = " SomeType[T](...) {...}",
|
|
696
|
+
snippet = "capability ",
|
|
697
|
+
member = False,
|
|
698
|
+
type = TConstructor(lspHook.at, "", []),
|
|
699
|
+
documentation = "// Defines a type with capabilities and functions",
|
|
700
|
+
expectedType = None
|
|
701
|
+
)
|
|
702
|
+
CompletionInfo(
|
|
703
|
+
label = "trait",
|
|
704
|
+
extra = " T: SomeTrait {...}",
|
|
705
|
+
snippet = "trait ",
|
|
706
|
+
member = False,
|
|
707
|
+
type = TConstructor(lspHook.at, "", []),
|
|
708
|
+
documentation = "// Defines a trait",
|
|
709
|
+
expectedType = None
|
|
710
|
+
)
|
|
711
|
+
CompletionInfo(
|
|
712
|
+
label = "instance",
|
|
713
|
+
extra = " SomeType: SomeTrait {...}",
|
|
714
|
+
snippet = "instance ",
|
|
715
|
+
member = False,
|
|
716
|
+
type = TConstructor(lspHook.at, "", []),
|
|
717
|
+
documentation = "// Defines an instance of a trait",
|
|
718
|
+
expectedType = None
|
|
719
|
+
)
|
|
720
|
+
CompletionInfo(
|
|
721
|
+
label = "nodeMain",
|
|
722
|
+
extra = "(system: NodeSystem) {...}",
|
|
723
|
+
snippet = "nodeMain(system: NodeSystem) {\n $0\n}",
|
|
724
|
+
member = False,
|
|
725
|
+
type = TConstructor(lspHook.at, "", []),
|
|
726
|
+
documentation = "// Main function for targetting Node.js",
|
|
727
|
+
expectedType = None
|
|
728
|
+
)
|
|
729
|
+
CompletionInfo(
|
|
730
|
+
label = "browserMain",
|
|
731
|
+
extra = "(system: BrowserSystem) {...}",
|
|
732
|
+
snippet = "browserMain(system: BrowserSystem) {\n $0\n}",
|
|
733
|
+
member = False,
|
|
734
|
+
type = TConstructor(lspHook.at, "", []),
|
|
735
|
+
documentation = "// Main function for targetting the browser",
|
|
736
|
+
expectedType = None
|
|
737
|
+
)
|
|
738
|
+
CompletionInfo(
|
|
739
|
+
label = "buildMain",
|
|
740
|
+
extra = "(system: BuildSystem) {...}",
|
|
741
|
+
snippet = "buildMain(system: NodeSystem) {\n $0\n}",
|
|
742
|
+
member = False,
|
|
743
|
+
type = TConstructor(lspHook.at, "", []),
|
|
744
|
+
documentation = "// Main function for building assets",
|
|
745
|
+
expectedType = None
|
|
746
|
+
)
|
|
747
|
+
CompletionInfo(
|
|
748
|
+
label = "webapp",
|
|
749
|
+
extra = " with frontend and backend",
|
|
750
|
+
snippet = [
|
|
751
|
+
"dependency ff:httpserver:0.0.0"
|
|
752
|
+
"import HttpServer from ff:httpserver"
|
|
753
|
+
""
|
|
754
|
+
"browserMain(system: BrowserSystem): Unit {"
|
|
755
|
+
" let response = system.httpClient().fetch(\"http://localhost:8080/hello\")"
|
|
756
|
+
" let window = system.js().global().get(\"window\")"
|
|
757
|
+
" window.call1(\"alert\", response.readText())"
|
|
758
|
+
"}"
|
|
759
|
+
""
|
|
760
|
+
"nodeMain(system: NodeSystem): Unit {"
|
|
761
|
+
" HttpServer.listen(system, \"localhost\", 8080) {request, response =>"
|
|
762
|
+
" if(request.path() == \"/\") {"
|
|
763
|
+
" response.setHeader(\"Content-Type\", [\"text/html; charset=UTF-8\"])"
|
|
764
|
+
" response.writeText(\"<!doctype html>\")"
|
|
765
|
+
" response.writeText(\"<script type='module' src='/js/script/script/WebApp.mjs'></script>\")"
|
|
766
|
+
" } elseIf {request.path() == \"/hello\"} {"
|
|
767
|
+
" response.setHeader(\"Content-Type\", [\"text/plain; charset=UTF-8\"])"
|
|
768
|
+
" response.writeText(\"Hello from server!\")"
|
|
769
|
+
" } elseIf {request.path().startsWith(\"/js/\") && !request.path().contains(\"..\")} {"
|
|
770
|
+
" response.setHeader(\"Content-Type\", [\"text/javascript; charset=UTF-8\"])"
|
|
771
|
+
" response.writeText(system.assets().readText(request.path()))"
|
|
772
|
+
" } else {"
|
|
773
|
+
" response.writeStatus(404, Some(\"Not found\"))"
|
|
774
|
+
" }"
|
|
775
|
+
" }"
|
|
776
|
+
"}"
|
|
777
|
+
""
|
|
778
|
+
"buildMain(system: BuildSystem) {"
|
|
779
|
+
" let browser = system.compileForBrowser(\"WebApp.ff\")"
|
|
780
|
+
" let assets = AssetSystem.create().addAssets(\"/js\", browser.assets())"
|
|
781
|
+
" system.setAssets(assets)"
|
|
782
|
+
"}"
|
|
783
|
+
""
|
|
784
|
+
"// To run this, name your file WebApp.ff and type:"
|
|
785
|
+
"// firefly WebApp.ff"
|
|
786
|
+
].join("\n"),
|
|
787
|
+
member = False,
|
|
788
|
+
type = TConstructor(lspHook.at, "", []),
|
|
789
|
+
documentation = "// Example webapp with frontend and backend",
|
|
790
|
+
expectedType = None
|
|
791
|
+
)
|
|
792
|
+
]
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
showCompletionParameter(indentation: String, parameter: Parameter): String {
|
|
796
|
+
if(parameter.mutable) {indentation + "mutable "} else {indentation} +
|
|
797
|
+
parameter.name + ": " + parameter.valueType.show([]) +
|
|
798
|
+
parameter.default.map {
|
|
799
|
+
| EVariant(_, n, _, None) =>
|
|
800
|
+
n.reverse().takeWhile {_.isAsciiLetterOrDigit()}.reverse()
|
|
801
|
+
| EVariant(_, n, _, Some([])) =>
|
|
802
|
+
n.reverse().takeWhile {_.isAsciiLetterOrDigit()}.reverse()
|
|
803
|
+
| EVariant(_, n, _, _) =>
|
|
804
|
+
n.reverse().takeWhile {_.isAsciiLetterOrDigit()}.reverse() + "(...)"
|
|
805
|
+
| EChar(_, v) => v
|
|
806
|
+
| EInt(_, v) => v
|
|
807
|
+
| EFloat(_, v) => v
|
|
808
|
+
| EString(_, v) => v.replace("```", "'''")
|
|
809
|
+
| ELambda(_, _) => "{...}"
|
|
810
|
+
| EList(_, _, []) => "[]"
|
|
811
|
+
| EList(_, _, _) => "[...]"
|
|
812
|
+
| _ => "..."
|
|
813
|
+
}.map {" = " + _}.else {""}
|
|
814
|
+
}
|