【问题标题】:Compiling code from within IntelliJ从 IntelliJ 中编译代码
【发布时间】:2017-01-23 17:12:38
【问题描述】:

通过右键单击一个函数并选择“运行函数名”,我已成功编译和执行 'Walkthrough' repository 中的 Cyelon 代码。

我还尝试创建自己的 Ceylon 项目(名为 POSTtoFile)并查看是否可以从那里执行“Hello World”函数。我不能。我想我必须为我的项目创建某种构建配置,但我不知道在这些表单字段中填写什么:

Installing Ceylon IDE for IntelliJ 指南对这个问题完全保持沉默。如果它包含一个名为“制作你的第一个锡兰”项目的部分,那就太好了。令人尴尬的是,该文档的所有以前的读者都必须事先知道这些内容。所以我在这里!唯一的例外。

在“Ceylon 模块”下拉菜单中,有一个名为 module default "unversioned" 的选项。我尝试选择那个,然后单击“运行”->“运行 POSTtoFile”。我得到了这个输出:

"C:\Program Files\Java\jdk1.8.0_121\bin\java" -Dceylon.system.repo=C:\Users\Jon\.IdeaIC2016.3\config\plugins\CeylonIDEA\classes\embeddedDist\repo -Didea.launcher.port=7536 "-Didea.launcher.bin.path=C:\Program Files (x86)\JetBrains\IntelliJ IDEA Community Edition 2016.3.3\bin" -Dfile.encoding=windows-1252 -classpath "C:\Users\Jon\.IdeaIC2016.3\config\plugins\CeylonIDEA\classes\embeddedDist\lib\ceylon-bootstrap.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA Community Edition 2016.3.3\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain com.redhat.ceylon.launcher.Bootstrap run --run "" default/unversioned
ceylon run: Module default/unversioned not found in the following repositories:
 C:\Users\Jon\.IdeaIC2016.3\config\plugins\CeylonIDEA\classes\embeddedDist\repo
 C:\Users\Jon\Auchitect\POSTtoFile\modules
 C:\Users\Jon\.ceylon\cache
 https://modules.ceylon-lang.org/repo/1
 [Maven] Aether
 [NPM] npm

Process finished with exit code 1

编辑:

从下面的屏幕截图中可以看出,我在 IntelliJ 窗口底部没有工具栏,也没有看到名为“Ceylon questions”的工具窗口:

关于您的其他问题:

  • “模块”文件夹为空
  • 当我单击Build>Rebuild Project 时,IntelliJ 似乎可以正常工作。这是输出:

  • 这些是我的版本号:

  • 当我将以下代码放入我的main.ceylonfile 时:

    共享 void hello() { print("你好,世界!"); }

(对不起,堆栈溢出不会将其格式化为代码,即使我在每行前面有 4 个空格)

我看到函数hello 左侧的绿色箭头,但我无法单击它。它说“这里什么都没有”:

编辑 2: 我尝试将源代码文件 (main.ceilon) 从根目录 (POSTtoFile) 移动到源目录 (POSTtoFile/source)。 然后我点击了Run -> Run 'POSTtoFile'

这给了我以下新输出:

"C:\Program Files\Java\jdk1.8.0_121\bin\java" -Dceylon.system.repo=C:\Users\Jon\.IdeaIC2016.3\config\plugins\CeylonIDEA\classes\embeddedDist\repo -Didea.launcher.port=7532 "-Didea.launcher.bin.path=C:\Program Files (x86)\JetBrains\IntelliJ IDEA Community Edition 2016.3.3\bin" -Dfile.encoding=windows-1252 -classpath "C:\Users\Jon\.IdeaIC2016.3\config\plugins\CeylonIDEA\classes\embeddedDist\lib\ceylon-bootstrap.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA Community Edition 2016.3.3\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain com.redhat.ceylon.launcher.Bootstrap run --run "" default/unversioned
ceylon run: String index out of range: 0
java.lang.StringIndexOutOfBoundsException: String index out of range: 0
 at java.lang.String.charAt(String.java:658)
 at ceylon.modules.api.runtime.AbstractRuntime.invokeRun(AbstractRuntime.java:53)
 at ceylon.modules.api.runtime.AbstractRuntime.execute(AbstractRuntime.java:105)
 at ceylon.modules.api.runtime.AbstractRuntime.execute(AbstractRuntime.java:101)
 at ceylon.modules.Main.execute(Main.java:69)
 at ceylon.modules.Main.main(Main.java:42)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java.lang.reflect.Method.invoke(Method.java:498)
 at org.jboss.modules.Module.run(Module.java:308)
 at org.jboss.modules.Main.main(Main.java:487)
 at ceylon.modules.bootstrap.CeylonRunTool.run(CeylonRunTool.java:292)
 at com.redhat.ceylon.common.tools.CeylonTool.run(CeylonTool.java:547)
 at com.redhat.ceylon.common.tools.CeylonTool.execute(CeylonTool.java:423)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java.lang.reflect.Method.invoke(Method.java:498)
 at com.redhat.ceylon.launcher.Launcher.runInJava7Checked(Launcher.java:108)
 at com.redhat.ceylon.launcher.Launcher.run(Launcher.java:38)
 at com.redhat.ceylon.launcher.Launcher.run(Launcher.java:31)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java.lang.reflect.Method.invoke(Method.java:498)
 at com.redhat.ceylon.launcher.Bootstrap.runVersion(Bootstrap.java:162)
 at com.redhat.ceylon.launcher.Bootstrap.runInternal(Bootstrap.java:117)
 at com.redhat.ceylon.launcher.Bootstrap.run(Bootstrap.java:93)
 at com.redhat.ceylon.launcher.Bootstrap.main(Bootstrap.java:85)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java.lang.reflect.Method.invoke(Method.java:498)
 at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

Process finished with exit code 2

这是main.ceilon的内容:

shared void hello() {
    print("Hello , World!");
}

编辑 3:

main.ceilon的内容改成这样后:

void hello() {
    print("Hello, World!");
}

hello();

我在做Run -> Run 'POSTtoFile' 时得到这个输出:

现在我突然在底部工具栏中有了Ceylon problems 按钮!它说我有三个问题:

incorrect syntax: no viable alternative at token end of file.
missing declaration or argument name.
not an annotation constructor: hello

不过,我不明白为什么会出现错误,因为我的代码直接来自 Tour of Ceylon

【问题讨论】:

  • 几个问题可以帮助我了解发生了什么:您是否在底部工具栏中看到名为“锡兰问题”的工具窗口?如果是,则表示在您的项目中正确配置了 Ceylon。你在modules 目录中看到了什么吗? (它应该被标记为输出目录)。如果您执行 Build>Rebuild Project 有什么改变吗?您使用的是什么版本的 IntelliJ 和 Ceylon IDE?
  • 如果一切配置正确,一旦你创建了一个文件并放入了一个没有参数的shared顶级函数,左边应该会出现一个绿色箭头。您可以使用它来自动构建和运行代码。
  • @BastienJansen 我已经更新了我的问题并回答了您的问题。谢谢你的帮助:)

标签: ceylon


【解决方案1】:

同意,文档缺少“IntelliJ 入门”部分,我将在本周解决这个问题。

以下是在 IntelliJ 中运行简单项目所需的步骤。

通过File > New > Project 创建一个新项目并选择Ceylon 类别。

单击Next 并选择是否要在JVM、JS 或两者上编译/运行您的项目:

单击Next 两次,输入项目名称并单击Finish。您的项目现在已准备好托管新的 Ceylon 代码:

在项目视图中,您可以看到一些现有的文件和文件夹:

  • .ceylon 包含可被 CLI 工具重用的内部配置,大部分时间您不必直接修改它。
  • modules 是编译 Ceylon 模块的输出目录。
  • resource 包含 resources
  • source 包含 source modules

要将 Ceylon 代码添加到此项目,您可以将文件直接放在 source 文件夹下以将它们添加到 default 模块,或者创建一个新模块并将源文件添加到此模块。在这两种情况下,.ceylon 文件必须source 文件夹中,而不是直接在项目文件夹中。

  • 要将文件添加到default 模块,请右键单击source 文件夹并选择New > Ceylon File/Declaration,输入名称(例如run)并单击OK
  • 要创建一个新模块,在source文件夹上右击选择New > Ceylon Module,输入类似my.ceylon.example的名称,如果不喜欢1.0.0,请调整版本,设置可运行单元名称到run 之类的内容,然后单击OK。将创建新文件夹 (my/ceylon/example),以及模块描述符 (module.ceylon)、包描述符 (package.ceylon) 和可运行单元 (run.ceylon)。

此时,标题为Ceylon Problems 的工具窗口应该出现在底部工具栏中,表明IDE 现在知道该项目包含Ceylon 代码并且已正确配置:

现在项目已经建立,是时候实际添加代码了:)。打开run.ceylon,添加如下代码:

shared void run() {
    print("hello");
}

左侧装订线会出现一个绿色箭头,点击该箭头运行或调试run函数:

点击Run 'run',这将自动创建运行配置,构建项目并打印hello

关于你的最后一个错误:

语法不正确:在文件的标记末尾没有可行的替代方案。

就像Java 及其main() 方法一样,当您在Ceylon 中声明一个可运行函数时,您不必实际手动调用它。 Ceylon runner 将为您完成(IDE 或ceylon run,当您使用 CLI 时)。 Ceylon 只接受顶级声明,而不接受语句,因此当在文件根目录中找到此代码时会触发解析错误:

hello();

如果您已经尝试过 web IDE,这有点误导,因为 Web IDE 会自动将您的代码包装在底层的 run 函数中,因此上述语句永远不会是 toplevel em> 声明。

【讨论】:

  • 现在它可以工作了,但前提是我明确运行 hello 函数。如果我只是执行 Run -> Run -> POSTtoFile,我会收到很长的错误消息,您可以在上面的帖子中看到。因此,如果我没有任何模块,我该如何为我的项目或我的 main.ceylon 文件指定一个“可运行单元名称”?
  • 找到了 :) 它是“运行中的可运行声明->“构建配置”窗口
猜你喜欢
  • 1970-01-01
  • 2017-06-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-27
  • 2017-05-18
  • 2015-08-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多