阅读 1328

用vscode写kotlin是啥感觉

kotlin 官网提供了如下方式搭建kotlin环境:

  1. Getting Started with IntelliJ IDEA
  2. Getting Started with Eclipse IDE
  3. Working with the Command Line Compiler

探索用VSCode搭建开发环境

外国友人也有文章介绍VSCode的kotlin开发环境,但从其环境变量看应该是基于IntelliJ IDEA搭建的 Visual Studio Code Setup

本文主要参考Working with the Command Line Compiler进行探索

安装

安装sdkman

curl -s https://get.sdkman.io | bash
复制代码
$  curl -s https://get.sdkman.io | bash

                                -+syyyyyyys:
                            `/yho:`       -yd.
                         `/yh/`             +m.
                       .oho.                 hy                          .`
                     .sh/`                   :N`                `-/o`  `+dyyo:.
                   .yh:`                     `M-          `-/osysoym  :hs` `-+sys:      hhyssssssssy+
                 .sh:`                       `N:          ms/-``  yy.yh-      -hy.    `.N-````````+N.
               `od/`                         `N-       -/oM-      ddd+`     `sd:     hNNm        -N:
              :do`                           .M.       dMMM-     `ms.      /d+`     `NMMs       `do
            .yy-                             :N`    ```mMMM.      -      -hy.       /MMM:       yh
          `+d+`           `:/oo/`       `-/osyh/ossssssdNMM`           .sh:         yMMN`      /m.
         -dh-           :ymNMMMMy  `-/shmNm-`:N/-.``   `.sN            /N-         `NMMy      .m/
       `oNs`          -hysosmMMMMydmNmds+-.:ohm           :             sd`        :MMM/      yy
      .hN+           /d:    -MMMmhs/-.`   .MMMh   .ss+-                 `yy`       sMMN`     :N.
     :mN/           `N/     `o/-`         :MMMo   +MMMN-         .`      `ds       mMMh      do
    /NN/            `N+....--:/+oooosooo+:sMMM:   hMMMM:        `my       .m+     -MMM+     :N.
   /NMo              -+ooooo+/:-....`...:+hNMN.  `NMMMd`        .MM/       -m:    oMMN.     hs
  -NMd`                                    :mm   -MMMm- .s/     -MMm.       /m-   mMMd     -N.
 `mMM/                                      .-   /MMh. -dMo     -MMMy        od. .MMMs..---yh
 +MMM.                                           sNo`.sNMM+     :MMMM/        sh`+MMMNmNm+++-
 mMMM-                                           /--ohmMMM+     :MMMMm.       `hyymmmdddo
 MMMMh.                  ````                  `-+yy/`yMMM/     :MMMMMy       -sm:.``..-:-.`
 dMMMMmo-.``````..-:/osyhddddho.           `+shdh+.   hMMM:     :MmMMMM/   ./yy/` `:sys+/+sh/
 .dMMMMMMmdddddmmNMMMNNNNNMMMMMs           sNdo-      dMMM-  `-/yd/MMMMm-:sy+.   :hs-      /N`
  `/ymNNNNNNNmmdys+/::----/dMMm:          +m-         mMMM+ohmo/.` sMMMMdo-    .om:       `sh
     `.-----+/.`       `.-+hh/`         `od.          NMMNmds/     `mmy:`     +mMy      `:yy.
           /moyso+//+ossso:.           .yy`          `dy+:`         ..       :MMMN+---/oys:
         /+m:  `.-:::-`               /d+                                    +MMMMMMMNh:`
        +MN/                        -yh.                                     `+hddhy+.
       /MM+                       .sh:
      :NMo                      -sh/
     -NMs                    `/yy:
    .NMy                  `:sh+.
   `mMm`               ./yds-
  `dMMMmyo:-.````.-:oymNy:`
HIST_STAMPS="mm/dd/yyyy"
  +NMMMMMMMMMMMMMMMMms:`
    -+shmNMMMNmdy+:`


                                                                 Now attempting installation...


Looking for a previous installation of SDKMAN...
Looking for unzip...
Looking for zip...
Looking for curl...
Looking for sed...
Installing SDKMAN scripts...
Create distribution directories...
Getting available candidates...
Prime the config file...
Download script archive...
######################################################################## 100.0%  #     #     #     #
Extract script archive...
Install scripts...
Set version to 5.9.1+575 ...
Attempt update of login bash profile on OSX...
Added sdkman init snippet to /Users/caojianfeng/.bash_profile
Attempt update of zsh profile...
Updated existing /Users/caojianfeng/.zshrc



All done!


Please open a new terminal, or run the following in the existing one:

    source "/Users/caojianfeng/.sdkman/bin/sdkman-init.sh"

Then issue the following command:

    sdk help

Enjoy!!!
复制代码

安装kotlin

$ sdk install kotlin
复制代码
==== BROADCAST =================================================================
* 2020-12-11: springboot 2.4.1 available on SDKMAN!
* 2020-12-11: springboot 2.3.7.RELEASE available on SDKMAN!
* 2020-12-10: springboot 2.2.12.RELEASE available on SDKMAN!
================================================================================

Downloading: kotlin 1.4.21

In progress...

#################################################################################################################### 100.0%#################################################################################################################### 100.0%

Installing: kotlin 1.4.21
Done installing!


Setting kotlin 1.4.21 as default.
复制代码

安装插件

【推荐】Kotlin(fwcd.kotlin)

Screen Shot 2020-12-12 at 11.18.00 AM.png

【可选】Kotlin Language (mathiasfrohlich.kotlin)

Screen Shot 2020-12-12 at 8.13.41 AM.png

经对比发现(fwcd.kotlin)的代码提示更全因此推荐

【推荐】Code Runner

Screen Shot 2020-12-12 at 8.12.48 AM.png

Code Runner插件问题

[Running] cd "/Volumes/user/cjf/p/kotlin/hello/" && kotlinc main.kt -include-runtime -d main.jar && java -jar main.jar
/bin/sh: kotlinc: command not found

[Done] exited with code=127 in 0.007 seconds

复制代码

解决方法:

参考:[# How to run javascript code in Visual studio code? /bin/sh: 1: node: not found ](stackoverflow.com/questions/4…)

在vscode里面打开设置页面(File->Preference->Settings) 搜索:"Executor Map By File Extension" 点击:"Edit in settings.json"

Screen Shot 2020-12-12 at 8.16.54 AM.png

code-runner.executorMapByFileExtension 中 修改

kotlinc -> ~/.sdkman/candidates/kotlin/current/bin/kotlinc

    "code-runner.executorMapByFileExtension": {
        
        ...
        ".kt": "cd $dir && kotlinc $fileName -include-runtime -d $fileNameWithoutExt.jar && java -jar $fileNameWithoutExt.jar",
        ...
    }
复制代码

为:

    "code-runner.executorMapByFileExtension": {
        
        ...
        ".kt": "cd $dir && ~/.sdkman/candidates/kotlin/current/bin/kotlinc $fileName -include-runtime -d $fileNameWithoutExt.jar && java -jar $fileNameWithoutExt.jar",
        ...
    }
复制代码

新问题:无法使用readLine

CodeRuner默认将结果输出到OUTPUT中而不是Terminal,且OUTPUT中无法编辑输入。

Screen Shot 2020-12-12 at 8.05.54 AM.png

解决方法:

CodeRunder设置中 勾选Code-runner: Run In Terminal Screen Shot 2020-12-12 at 8.00.34 AM.png

这次终于看到正确结果了

$ cd "/Volumes/user/cjf/p/kotlin/hello/" && ~/.sdkman/candidates/kotlin/current/bin/kotlinc main.kt -include-runtime -d main.jar && java -jar main.jar
What's your name?
Jeffrey
Hello Jeffrey!
复制代码

lint检查

编写如下错误代码:

fun printMessage(message: String): String { 
    println(message)
    // 缺少返回值
}
复制代码

保存后lint自动执行提示错误原因: 我们定义了函数返回值类型为String但是没有返回值,vscode可以提示我们错误的原因和位置。 Screen Shot 2020-12-12 at 9.57.56 AM.png

formatOnSave

在vscode中编辑

  fun fmt() {
        var t1="1"
    println(t1.javaClass  )
  }

复制代码

保存后触发 formatOnSave 格式化后:

  fun fmt() {
        var t1 = "1"
    println(t1.javaClass)
  }

复制代码

结果有点让人失望,仅仅修复了空格格式,没有自动缩进。

jump to

按住cmd键点击变量println 里面的变量t1 ,光标能够跳转到定义位置

小结

到目前为止我们能够基于命令行编写和执行kotlin了,并且可以通过CodeRunner 免除了输入命令执行的麻烦。 总体来说,如果是学习kotlin或者对单个文件进行简单修改用vscode还是很方便的。 如果要真刀真枪的开发项目还是推荐IntelliJ IDEA或者AndroidStudio。

文章分类
Android
文章标签