kotlin 官網(wǎng)提供了如下方式搭建kotlin環(huán)境:
- Getting Started with IntelliJ IDEA
- Getting Started with Eclipse IDE
- Working with the Command Line Compiler
探索用VSCode搭建開發(fā)環(huán)境
外國(guó)友人也有文章介紹VSCode的kotlin開發(fā)環(huán)境肌割,但從其環(huán)境變量看應(yīng)該是基于IntelliJ IDEA搭建的
Visual Studio Code Setup
本文主要參考Working with the Command Line Compiler進(jìn)行探索
安裝
安裝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)
【可選】Kotlin Language (mathiasfrohlich.kotlin)
經(jīng)對(duì)比發(fā)現(xiàn)(fwcd.kotlin)的代碼提示更全因此推薦
【推薦】Code Runner
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
在vscode里面打開設(shè)置頁(yè)面(File->Preference->Settings)
搜索:"Executor Map By File Extension"
點(diǎn)擊:"Edit in settings.json"
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默認(rèn)將結(jié)果輸出到OUTPUT中而不是Terminal吧黄,且OUTPUT中無法編輯輸入馍忽。
解決方法:
CodeRunder設(shè)置中
勾選Code-runner: Run In Terminal
這次終于看到正確結(jié)果了
$ 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檢查
編寫如下錯(cuò)誤代碼:
fun printMessage(message: String): String {
println(message)
// 缺少返回值
}
保存后lint自動(dòng)執(zhí)行提示錯(cuò)誤原因:
我們定義了函數(shù)返回值類型為String但是沒有返回值惜索,vscode可以提示我們錯(cuò)誤的原因和位置曙旭。
formatOnSave
在vscode中編輯
fun fmt() {
var t1="1"
println(t1.javaClass )
}
保存后觸發(fā) formatOnSave
格式化后:
fun fmt() {
var t1 = "1"
println(t1.javaClass)
}
結(jié)果有點(diǎn)讓人失望剃法,僅僅修復(fù)了空格格式唐础,沒有自動(dòng)縮進(jìn)晦款。
jump to
按住cmd鍵點(diǎn)擊變量println 里面的變量t1 规个,光標(biāo)能夠跳轉(zhuǎn)到定義位置
小結(jié)
到目前為止我們能夠基于命令行編寫和執(zhí)行kotlin了凤薛,并且可以通過CodeRunner 免除了輸入命令執(zhí)行的麻煩。
總體來說诞仓,如果是學(xué)習(xí)kotlin或者對(duì)單個(gè)文件進(jìn)行簡(jiǎn)單修改用vscode還是很方便的缤苫。
如果要真刀真槍的開發(fā)項(xiàng)目還是推薦IntelliJ IDEA或者AndroidStudio。