Some hacky API tweaks to conform with new stdlib APIs.

This commit is contained in:
Pavel V. Talanov
2012-03-30 16:34:15 +04:00
parent 428c517ee6
commit d378e4bd06
4 changed files with 68 additions and 64 deletions
+5
View File
@@ -0,0 +1,5 @@
package kotlin
import java.util.Map as JMap
/** Provides [] access to maps */
fun <K, V> JMap<K, V>.set(key : K, value : V) = this.put(key, value)
@@ -66,11 +66,11 @@ public final class AnalyzerFacadeForJS {
final ModuleDescriptor owner = new ModuleDescriptor("<module>"); final ModuleDescriptor owner = new ModuleDescriptor("<module>");
TopDownAnalysisParameters topDownAnalysisParameters = new TopDownAnalysisParameters( TopDownAnalysisParameters topDownAnalysisParameters = new TopDownAnalysisParameters(
notLibFiles(config.getLibFiles()), false, false); notLibFiles(config.getLibFiles()), false, false);
InjectorForTopDownAnalyzerForJs injector = new InjectorForTopDownAnalyzerForJs( InjectorForTopDownAnalyzerForJs injector = new InjectorForTopDownAnalyzerForJs(
project, topDownAnalysisParameters, new ObservableBindingTrace(bindingTraceContext), owner, project, topDownAnalysisParameters, new ObservableBindingTrace(bindingTraceContext), owner,
JetControlFlowDataTraceFactory.EMPTY, JsConfiguration.jsLibConfiguration(project)); JetControlFlowDataTraceFactory.EMPTY, JsConfiguration.jsLibConfiguration(project));
injector.getTopDownAnalyzer().analyzeFiles(withJsLibAdded(files, config)); injector.getTopDownAnalyzer().analyzeFiles(withJsLibAdded(files, config));
return bindingTraceContext.getBindingContext(); return bindingTraceContext.getBindingContext();
@@ -110,11 +110,11 @@ public final class AnalyzerFacadeForJS {
final ModuleDescriptor owner = new ModuleDescriptor("<module>"); final ModuleDescriptor owner = new ModuleDescriptor("<module>");
TopDownAnalysisParameters topDownAnalysisParameters = new TopDownAnalysisParameters( TopDownAnalysisParameters topDownAnalysisParameters = new TopDownAnalysisParameters(
Predicates.<PsiFile>alwaysTrue(), false, false); Predicates.<PsiFile>alwaysTrue(), false, false);
InjectorForTopDownAnalyzerForJs injector = new InjectorForTopDownAnalyzerForJs( InjectorForTopDownAnalyzerForJs injector = new InjectorForTopDownAnalyzerForJs(
project, topDownAnalysisParameters, new ObservableBindingTrace(bindingTraceContext), owner, project, topDownAnalysisParameters, new ObservableBindingTrace(bindingTraceContext), owner,
JetControlFlowDataTraceFactory.EMPTY, JsConfiguration.jsLibConfiguration(project)); JetControlFlowDataTraceFactory.EMPTY, JsConfiguration.jsLibConfiguration(project));
injector.getTopDownAnalyzer().analyzeFiles(Collections.singletonList(file)); injector.getTopDownAnalyzer().analyzeFiles(Collections.singletonList(file));
return bindingTraceContext.getBindingContext(); return bindingTraceContext.getBindingContext();
@@ -135,15 +135,17 @@ public final class AnalyzerFacadeForJS {
@Override @Override
public void addDefaultImports(@NotNull Collection<JetImportDirective> directives) { public void addDefaultImports(@NotNull Collection<JetImportDirective> 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("js.*")));
directives.add(JetPsiFactory.createImportDirective(project, new ImportPath(JetStandardClasses.STANDARD_CLASSES_FQNAME, true))); directives.add(JetPsiFactory.createImportDirective(project, new ImportPath(JetStandardClasses.STANDARD_CLASSES_FQNAME, true)));
directives.add(JetPsiFactory.createImportDirective(project, new ImportPath("kotlin.*")));
} }
@Override @Override
public void extendNamespaceScope(@NotNull BindingTrace trace, @NotNull NamespaceDescriptor namespaceDescriptor, public void extendNamespaceScope(@NotNull BindingTrace trace, @NotNull NamespaceDescriptor namespaceDescriptor,
@NotNull WritableScope namespaceMemberScope) { @NotNull WritableScope namespaceMemberScope) {
DefaultModuleConfiguration.createStandardConfiguration(project, true).extendNamespaceScope(trace, namespaceDescriptor, namespaceMemberScope); DefaultModuleConfiguration.createStandardConfiguration(project, true)
.extendNamespaceScope(trace, namespaceDescriptor, namespaceMemberScope);
} }
} }
} }
@@ -44,7 +44,8 @@ public abstract class Config {
"/raphael/raphael.kt", "/raphael/raphael.kt",
"/html5/canvas.kt", "/html5/canvas.kt",
"/html5/files.kt", "/html5/files.kt",
"/html5/image.kt" "/html5/image.kt",
"/stdlib/JUMaps.kt"
); );
protected static final String LIBRARIES_LOCATION = "js/js.libraries/src"; protected static final String LIBRARIES_LOCATION = "js/js.libraries/src";
@@ -7,42 +7,41 @@
* See this page for details: * See this page for details:
* http://confluence.jetbrains.net/display/Kotlin/Type-safe+Groovy-style+builders * http://confluence.jetbrains.net/display/Kotlin/Type-safe+Groovy-style+builders
*/ */
import js.*
import java.util.* import java.util.*
fun main(args : Array<String>) { fun main(args : Array<String>) {
val result = val result =
html { html {
head { head {
title {+"XML encoding with Kotlin"} title {+"XML encoding with Kotlin"}
} }
body { body {
h1 {+"XML encoding with Kotlin"} h1 {+"XML encoding with Kotlin"}
p {+"this format can be used as an alternative markup to XML"} p {+"this format can be used as an alternative markup to XML"}
// an element with attributes and text content // an element with attributes and text content
a(href = "http://jetbrains.com/kotlin") {+"Kotlin"} a(href = "http://jetbrains.com/kotlin") {+"Kotlin"}
// mixed content // mixed content
p { p {
+"This is some" +"This is some"
b {+"mixed"} b {+"mixed"}
+"text. For more see the" +"text. For more see the"
a(href = "http://jetbrains.com/kotlin") {+"Kotlin"} a(href = "http://jetbrains.com/kotlin") {+"Kotlin"}
+"project" +"project"
} }
p {+"some text"} p {+"some text"}
// content generated from command-line arguments // content generated from command-line arguments
p { p {
+"Command line arguments were:" +"Command line arguments were:"
ul { ul {
for (arg in args) for (arg in args)
li {+arg} li {+arg}
}
}
} }
} }
}
}
println(result) println(result)
} }
@@ -50,15 +49,15 @@ trait Element {
fun render(builder : StringBuilder, indent : String) fun render(builder : StringBuilder, indent : String)
fun toString() : String? { fun toString() : String? {
val builder = StringBuilder() val builder = StringBuilder()
render(builder, "") render(builder, "")
return builder.toString() return builder.toString()
} }
} }
class TextElement(val text : String) : Element { class TextElement(val text : String) : Element {
override fun render(builder : StringBuilder, indent : String) { 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<String, String>() val attributes = HashMap<String, String>()
protected fun initTag<T : Element>(tag : T, init : T.() -> Unit) : T { protected fun initTag<T : Element>(tag : T, init : T.() -> Unit) : T {
tag.init() tag.init()
children.add(tag) children.add(tag)
return tag return tag
} }
override fun render(builder : StringBuilder, indent : String) { override fun render(builder : StringBuilder, indent : String) {
builder.append("$indent<$name${renderAttributes()}>\n") builder.append("$indent<$name${renderAttributes()}>\n")
for (c in children) { for (c in children) {
c.render(builder, indent + " ") c?.render(builder, indent + " ")
} }
builder.append("$indent</$name>\n") builder.append("$indent</$name>\n")
} }
private fun renderAttributes() : String? { private fun renderAttributes() : String? {
val builder = StringBuilder() val builder = StringBuilder()
for (a in attributes.keySet()) { for (a in attributes.keySet()) {
builder.append(" $a=\"${attributes[a]}\"") builder.append(" $a=\"${attributes[a]}\"")
} }
return builder.toString() return builder.toString()
} }
} }
abstract class TagWithText(name : String) : Tag(name) { abstract class TagWithText(name : String) : Tag(name) {
fun String.plus() { 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 h1(init : H1.() -> Unit) = initTag(H1(), init)
fun ul(init : UL.() -> Unit) = initTag(UL(), init) fun ul(init : UL.() -> Unit) = initTag(UL(), init)
fun a(href : String, init : A.() -> Unit) { fun a(href : String, init : A.() -> Unit) {
val a = initTag(A(), init) val a = initTag(A(), init)
a.href = href a.href = href
} }
} }
@@ -129,17 +128,14 @@ class P() : BodyTag("p")
class H1() : BodyTag("h1") class H1() : BodyTag("h1")
class A() : BodyTag("a") { class A() : BodyTag("a") {
public var href : String public var href : String
get() = attributes["href"] get() = attributes["href"].sure()
set(value) { set(value) {
attributes["href"] = value attributes["href"] = value
} }
} }
fun html(init : HTML.() -> Unit) : HTML { fun html(init : HTML.() -> Unit) : HTML {
val html = HTML() val html = HTML()
html.init() html.init()
return html return html
} }
// An excerpt from the Standard Library
fun <K, V> Map<K, V>.set(key : K, value : V) = this.put(key, value)