iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 26
0
Software Development

Emacs 來寫程式系列 第 26

[Emacs-26] 用 Emacs 來寫 C# 實戰篇

  • 分享至 

  • twitterImage
  •  

Emacs 寫 C#

前一篇 介紹了 Emacs 編輯 C# 的一些套件及功能,這一篇,我們來看怎樣使用這些功能

建立專案

先建立一個目錄,目錄底下,執行 $dotnet new console,產生一個專案

$ dotnet new console
The template "Console Application" was created successfully.

Processing post-creation actions...
Running 'dotnet restore' on /home/vagrant/dotnet/hello/hello.csproj...
  Restoring packages for /home/vagrant/dotnet/hello/hello.csproj...
  Generating MSBuild file /home/vagrant/dotnet/hello/obj/hello.csproj.nuget.g.props.
  Generating MSBuild file /home/vagrant/dotnet/hello/obj/hello.csproj.nuget.g.targets.
  Restore completed in 275.56 ms for /home/vagrant/dotnet/hello/hello.csproj.

Restore succeeded.

建立一個應用程式 FibonacciGenerator 費氏級數產生器

開啟一個新的檔案 FibonacciGenerator.cs

系統會提示要啟動 omnisharp 後端 Imgur
如果第一次使用,要先用 Meta-x omnisharp-install-server 安裝 omnisharp 後端,這個只要執行一次即可 Imgur
以後只要在專案中執行 Meta-x omnisharp-start-omnisharp-server (C-c o s) 或者按 Ctrl-c o s 來啟動後端,它會問你要執行的目錄 Imgur
選擇完就會有 omnisharp server is ready... Imgur

環境設定檢驗

我們來看一下一些設定,先用 Ctrl-h v company-backends 來看 company 程式補全的後端設定,有三組,第一組就是 company-omnisharp Imgur

語法檢查的部分,使用 Ctrl-c ! v 來看 flycheck 的設定 Imgur

程式片段的部分,使用 Meta-x yas-describe-table 來看,預設的程式片段還蠻多的 Imgur

開始寫程式

先打入 usingtab 鍵,yasnippet 會顯示可以選擇的程式片段,選第三個加入系統 Imgur

再來加入 System.Collections.Generic, 自動補全會依照打入的片段來提示 Imgur

打入 class 會展開程式片段 Imgur

會顯示各種 overloaded 函式定義,例如 Imgur

函式參數的提示 Imgur

flycheck 語法錯誤的提示 FlyC: 2/0 Imgur

Ctrl-c ! l 來看錯誤提示 Imgur

假設故意放一些括號在函式定義後,然後來存檔 Imgur

存檔前會自動排版 Imgur

移動到函式,Eldoc 會在小緩衝區顯示函式的文件 Imgur

修改主程式 Program.cs

先產生一個 FibonacciGenerate 的物件,Company 自動補全會提示可用的類別 Imgur

也會補全類別的函式 (method) Imgur

跳到函式定義 Meta-x omnisharp-go-to-definition (C-c o j) 或者 Meta-x omnishpar-go-to-definition-other-window (C-c o w) Imgur

函式或變數使用情形 Meta-x omnisharp-find-usages(C-c o f) Imgur

編輯完成後,可以直接在 Emacs shell (使用 Meta-x shell 啟動) 執行 $dotnet run
Imgur

相關影片,Yes

相關簡報:簡報

相關程式: Github 請下載後,放在 ~/.emacs.d 下,啟動 Emacs 即可自動安裝相關套件

相關資訊:請參考我的部落格


上一篇
[Emacs-25] 用 Emacs 來寫 C# 設定篇
下一篇
[Emacs-27] 用 Emacs 來寫 Vue.js 設定篇
系列文
Emacs 來寫程式30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言