diff --git a/js/js.libraries/src/stdlib/JUMaps.kt b/js/js.libraries/src/stdlib/JUMaps.kt new file mode 100644 index 00000000000..2958fb676b1 --- /dev/null +++ b/js/js.libraries/src/stdlib/JUMaps.kt @@ -0,0 +1,5 @@ +package kotlin + +import java.util.Map as JMap +/** Provides [] access to maps */ +fun JMap.set(key : K, value : V) = this.put(key, value) diff --git a/js/js.translator/src/org/jetbrains/k2js/analyze/AnalyzerFacadeForJS.java b/js/js.translator/src/org/jetbrains/k2js/analyze/AnalyzerFacadeForJS.java index 49a049bdbad..ace8c2fc424 100644 --- a/js/js.translator/src/org/jetbrains/k2js/analyze/AnalyzerFacadeForJS.java +++ b/js/js.translator/src/org/jetbrains/k2js/analyze/AnalyzerFacadeForJS.java @@ -66,11 +66,11 @@ public final class AnalyzerFacadeForJS { final ModuleDescriptor owner = new ModuleDescriptor(""); TopDownAnalysisParameters topDownAnalysisParameters = new TopDownAnalysisParameters( - notLibFiles(config.getLibFiles()), false, false); + notLibFiles(config.getLibFiles()), false, false); InjectorForTopDownAnalyzerForJs injector = new InjectorForTopDownAnalyzerForJs( - project, topDownAnalysisParameters, new ObservableBindingTrace(bindingTraceContext), owner, - JetControlFlowDataTraceFactory.EMPTY, JsConfiguration.jsLibConfiguration(project)); + project, topDownAnalysisParameters, new ObservableBindingTrace(bindingTraceContext), owner, + JetControlFlowDataTraceFactory.EMPTY, JsConfiguration.jsLibConfiguration(project)); injector.getTopDownAnalyzer().analyzeFiles(withJsLibAdded(files, config)); return bindingTraceContext.getBindingContext(); @@ -110,11 +110,11 @@ public final class AnalyzerFacadeForJS { final ModuleDescriptor owner = new ModuleDescriptor(""); TopDownAnalysisParameters topDownAnalysisParameters = new TopDownAnalysisParameters( - Predicates.alwaysTrue(), false, false); + Predicates.alwaysTrue(), false, false); InjectorForTopDownAnalyzerForJs injector = new InjectorForTopDownAnalyzerForJs( - project, topDownAnalysisParameters, new ObservableBindingTrace(bindingTraceContext), owner, - JetControlFlowDataTraceFactory.EMPTY, JsConfiguration.jsLibConfiguration(project)); + project, topDownAnalysisParameters, new ObservableBindingTrace(bindingTraceContext), owner, + JetControlFlowDataTraceFactory.EMPTY, JsConfiguration.jsLibConfiguration(project)); injector.getTopDownAnalyzer().analyzeFiles(Collections.singletonList(file)); return bindingTraceContext.getBindingContext(); @@ -135,15 +135,17 @@ public final class AnalyzerFacadeForJS { @Override public void addDefaultImports(@NotNull Collection directives) { + //TODO: these thing should not be hard-coded like that directives.add(JetPsiFactory.createImportDirective(project, new ImportPath("js.*"))); directives.add(JetPsiFactory.createImportDirective(project, new ImportPath(JetStandardClasses.STANDARD_CLASSES_FQNAME, true))); + directives.add(JetPsiFactory.createImportDirective(project, new ImportPath("kotlin.*"))); } @Override public void extendNamespaceScope(@NotNull BindingTrace trace, @NotNull NamespaceDescriptor namespaceDescriptor, @NotNull WritableScope namespaceMemberScope) { - DefaultModuleConfiguration.createStandardConfiguration(project, true).extendNamespaceScope(trace, namespaceDescriptor, namespaceMemberScope); + DefaultModuleConfiguration.createStandardConfiguration(project, true) + .extendNamespaceScope(trace, namespaceDescriptor, namespaceMemberScope); } - } } diff --git a/js/js.translator/src/org/jetbrains/k2js/config/Config.java b/js/js.translator/src/org/jetbrains/k2js/config/Config.java index 3ccbb8d307f..af75c6d253e 100644 --- a/js/js.translator/src/org/jetbrains/k2js/config/Config.java +++ b/js/js.translator/src/org/jetbrains/k2js/config/Config.java @@ -44,7 +44,8 @@ public abstract class Config { "/raphael/raphael.kt", "/html5/canvas.kt", "/html5/files.kt", - "/html5/image.kt" + "/html5/image.kt", + "/stdlib/JUMaps.kt" ); protected static final String LIBRARIES_LOCATION = "js/js.libraries/src"; diff --git a/js/js.translator/testFiles/webDemoExamples2/cases/builder.kt b/js/js.translator/testFiles/webDemoExamples2/cases/builder.kt index 5655c7d2ff0..5abd98a999e 100644 --- a/js/js.translator/testFiles/webDemoExamples2/cases/builder.kt +++ b/js/js.translator/testFiles/webDemoExamples2/cases/builder.kt @@ -7,42 +7,41 @@ * See this page for details: * http://confluence.jetbrains.net/display/Kotlin/Type-safe+Groovy-style+builders */ -import js.* import java.util.* fun main(args : Array) { val result = - html { - head { - title {+"XML encoding with Kotlin"} - } - body { - h1 {+"XML encoding with Kotlin"} - p {+"this format can be used as an alternative markup to XML"} + html { + head { + title {+"XML encoding with Kotlin"} + } + body { + h1 {+"XML encoding with Kotlin"} + p {+"this format can be used as an alternative markup to XML"} - // an element with attributes and text content - a(href = "http://jetbrains.com/kotlin") {+"Kotlin"} + // an element with attributes and text content + a(href = "http://jetbrains.com/kotlin") {+"Kotlin"} - // mixed content - p { - +"This is some" - b {+"mixed"} - +"text. For more see the" - a(href = "http://jetbrains.com/kotlin") {+"Kotlin"} - +"project" - } - p {+"some text"} + // mixed content + p { + +"This is some" + b {+"mixed"} + +"text. For more see the" + a(href = "http://jetbrains.com/kotlin") {+"Kotlin"} + +"project" + } + p {+"some text"} - // content generated from command-line arguments - p { - +"Command line arguments were:" - ul { - for (arg in args) - li {+arg} - } - } + // content generated from command-line arguments + p { + +"Command line arguments were:" + ul { + for (arg in args) + li {+arg} } } + } + } println(result) } @@ -50,15 +49,15 @@ trait Element { fun render(builder : StringBuilder, indent : String) fun toString() : String? { - val builder = StringBuilder() - render(builder, "") - return builder.toString() + val builder = StringBuilder() + render(builder, "") + return builder.toString() } } class TextElement(val text : String) : Element { override fun render(builder : StringBuilder, indent : String) { - builder.append("$indent$text\n") + builder.append("$indent$text\n") } } @@ -67,31 +66,31 @@ abstract class Tag(val name : String) : Element { val attributes = HashMap() protected fun initTag(tag : T, init : T.() -> Unit) : T { - tag.init() - children.add(tag) - return tag + tag.init() + children.add(tag) + return tag } override fun render(builder : StringBuilder, indent : String) { - builder.append("$indent<$name${renderAttributes()}>\n") - for (c in children) { - c.render(builder, indent + " ") - } - builder.append("$indent\n") + builder.append("$indent<$name${renderAttributes()}>\n") + for (c in children) { + c?.render(builder, indent + " ") + } + builder.append("$indent\n") } private fun renderAttributes() : String? { - val builder = StringBuilder() - for (a in attributes.keySet()) { - builder.append(" $a=\"${attributes[a]}\"") - } - return builder.toString() + val builder = StringBuilder() + for (a in attributes.keySet()) { + builder.append(" $a=\"${attributes[a]}\"") + } + return builder.toString() } } abstract class TagWithText(name : String) : Tag(name) { fun String.plus() { - children.add(TextElement(this)) + children.add(TextElement(this)) } } @@ -113,8 +112,8 @@ abstract class BodyTag(name : String) : TagWithText(name) { fun h1(init : H1.() -> Unit) = initTag(H1(), init) fun ul(init : UL.() -> Unit) = initTag(UL(), init) fun a(href : String, init : A.() -> Unit) { - val a = initTag(A(), init) - a.href = href + val a = initTag(A(), init) + a.href = href } } @@ -129,17 +128,14 @@ class P() : BodyTag("p") class H1() : BodyTag("h1") class A() : BodyTag("a") { public var href : String - get() = attributes["href"] - set(value) { - attributes["href"] = value - } + get() = attributes["href"].sure() + set(value) { + attributes["href"] = value + } } fun html(init : HTML.() -> Unit) : HTML { val html = HTML() html.init() return html -} - -// An excerpt from the Standard Library -fun Map.set(key : K, value : V) = this.put(key, value) +} \ No newline at end of file