Sublime Text 插件開發(fā) - RunSnippet & JumpTo

# ?? Readme Fist
學(xué)習(xí) Sublime Text 插件開發(fā),請(qǐng)參考 [Sublime API 探索](https://github.com/jimboyeah/run-snippet/blob/master/APIs.md)
了解 RunSnippetCommand 或 JumpTo 插件請(qǐng)繼續(xù)閱讀本文。
快捷鍵配置文件 RunSnippet\Default.sublime-keymap

## ? RunSnippetCommand 插件
作為一個(gè)重度 Sublime Text 用戶,掌握 Plugin-host 插件機(jī)制及插件開發(fā)是非常必要的,有些稀奇古怪的想法功能都可以實(shí)現(xiàn)。
在 MD 文檔中執(zhí)行 Python 代碼片段,比如 MD 文檔中有以下代碼片段,按注解提示配置好插件上下文菜單,保持光標(biāo)在代碼塊上,按 F6 就可以執(zhí)行:

可以在 Packages 目錄執(zhí)行以下命令安裝 RunSnippet 插件:
? ? git clone git@github.com/jimboyeah/run-snippet.git
Sublime Text 4 插件宿主支持 Python 3.3、3.8,但在 Packages 目錄安裝的插件默認(rèn)是 Plugin-Host 3.3,某些 Python 3.8 新功能不能使用。
RunSnippetCommand 插件實(shí)現(xiàn)代碼,以下是基于 Python 3.8 的語法,可以根據(jù) Sublime 選擇器實(shí)現(xiàn)更多語言的支持,包括 C/C++,只需要配置好編譯器待調(diào)用即可。
## ? JumpTo ...
使用 SublimeText 閱讀文檔和寫作是日?;顒?dòng),特別是最近在閱讀 [CPython](https://github.com/python/cpython) 以及 C# 相關(guān)開源代碼及文檔,Sublime 提供的跳轉(zhuǎn)工具非常強(qiáng)大,因?yàn)闀?huì)對(duì)代碼文件進(jìn)行符號(hào)索引,所以在已經(jīng)建立索引的工程上,直接按 F12 就可以跳轉(zhuǎn)到光標(biāo)所在的符號(hào)定義上,對(duì)于 URL 地址,也可以通過右角菜單打開瀏覽器進(jìn)行訪問。
? ? git clone git@github.com:python/cpython
? ? git clone git@github.com:python/devguide
? ? git clone git@github.com:python/peps
不足的是,文檔中有非常多的相對(duì)引用,這些引用都不能直接跳轉(zhuǎn)到對(duì)應(yīng)的文件中,需要通過 Ctrl+P 手動(dòng)輸入文件名間接跳轉(zhuǎn),對(duì)于大量文件查詢來說,這是極其差的體驗(yàn)。
例如,C# 規(guī)范文檔就包含許多文件,還有 .NET Core 和 ASP.NET Core 的參考文檔中有大量示范代碼文件的引用:
? ? - [ §1](csharp/standard/scope.md)? Scope
? ? - [ §2](csharp/standard/normative-references.md)? Normative references
? ? - [ §3](csharp/standard/terms-and-definitions.md)? Terms and definitions
? ? - [ §4](csharp/standard/general-description.md)? General description
? ? - [ §5](csharp/standard/conformance.md)? Conformance
? ? - [ §6](csharp/standard/lexical-structure.md)? Lexical structure
? ? - [ §7](csharp/standard/basic-concepts.md)? Basic concepts
? ? - [ §8](csharp/standard/types.md)? Types
? ? - [ §9](csharp/standard/variables.md)? Variables
? ? - [§10](csharp/standard/conversions.md)? Conversions
? ? - [§11](csharp/standard/expressions.md)? Expressions
? ? - [§12](csharp/standard/statements.md)? Statements
? ? - [§13](csharp/standard/namespaces.md)? Namespaces
? ? - [§14](csharp/standard/classes.md)? Classes
? ? - [§15](csharp/standard/structs.md)? Structs
? ? - [§16](csharp/standard/arrays.md)? Arrays
? ? - [§17](csharp/standard/interfaces.md)? Interfaces
? ? - [§18](csharp/standard/enums.md)? Enums
? ? - [§19](csharp/standard/delegates.md)? Delegates
? ? - [§20](csharp/standard/exceptions.md)? Exceptions
? ? - [§21](csharp/standard/attributes.md)? Attributes
? ? - [§22](csharp/standard/unsafe-code.md)? Unsafe code
? ? - [ §A](csharp/standard/grammar.md)? Grammar
? ? - [ §B](csharp/standard/portability-issues.md)? Portability issues
? ? - [ §C](csharp/standard/standard-library.md)? Standard library
? ? - [ §D](csharp/standard/documentation-comments.md)? Documentation comments
? ? - [ §E](csharp/standard/bibliography.md)? Bibliography
為此,有了開發(fā) JumpTo 插件的想法,這是一個(gè)可以極高地提升文檔閱讀效率的插件,你值得擁有。
預(yù)期支持跳轉(zhuǎn)地址格式如下:
- [x] 帶括號(hào)的文件 (scope.md)
- [x] 帶引號(hào)的文件 'scope.md' 或 "scope.md"
- [x] 帶前導(dǎo)符號(hào)且使用空格分隔的文件路徑
- [ ] 帶 # 的設(shè)置的行號(hào) (scope.md#LINE_NO)
- [ ] 帶 # 的設(shè)置的標(biāo)簽 (scope.md#ANCHRO)
因?yàn)?Sublime 文件跳轉(zhuǎn)有個(gè)臨時(shí)狀態(tài),文件并沒有完全確定打開,此時(shí)按方向鍵及回車之外的鍵,都會(huì)撤消文件的打開。所以,帶標(biāo)簽的自動(dòng)定位還需要尋求其它解決辦法。
可跳轉(zhuǎn)的內(nèi)容示范:
? ? - language-reference\builtin-types\value-types.md
? ? - language-reference/builtin-types/value-types.md
? ? - [`is` expression](operators/is.md)
? ? # csharp\fundamentals\functional\pattern-matching.md
? ? :::code language="csharp" source="Program.cs" ID="NullableCheck":::
默認(rèn)按 Shift 點(diǎn)擊內(nèi)容進(jìn)行跳轉(zhuǎn),配置熱鍵使用更方便。實(shí)現(xiàn)中使用了 on_text_command 事件,它可以獲取鼠標(biāo)點(diǎn)擊坐標(biāo),但沒有找到相應(yīng)的 API 將坐標(biāo)轉(zhuǎn)換為 Text Point。
Sublime 使用 Ctrl 和 Alt 兩個(gè)控制鍵分別用于增減選區(qū),所以不太好直接使用它們。同時(shí) Shift 也被用來做字符擴(kuò)展選擇,但是還是免強(qiáng)可用,最好還是配置按鍵觸發(fā),默認(rèn)配置 F9 觸發(fā)。
使用到正則字符串處理具,參考 CPython 文檔:
? ? +-- Doc\howto
? ? |? ?? -- regex.rst? ? ? ? ? => Regular Expression HOWTO
? ? +-- Doc\library
? ? |? ?? - re.rst? ? ? ? ? ? ? => `re` --- Regular expression operations
JumpTo 插件參考代碼:
import re
import sublime_api as sapi
from sublime import *
from sublime_plugin import *
from datetime import datetime

# ?? .Net Core Sources
如何閱讀 .Net Core 源代碼?
第一手資源是官方文檔倉庫:
- https://github.com/dotnet/docs
- https://github.com/dotnet/standard
- https://github.com/dotnet/csharpstandard
- https://github.com/dotnet/dotnet-api-docs
- https://github.com/dotnet/AspNetCore.Docs
快捷源查詢 .NET Framework 代碼可以使用 Reference Source https://referencesource.microsoft.com/
.Net Core 有三個(gè)核心開源代碼倉庫,外加一個(gè) ASP.NET Core 和 Roslyn,項(xiàng)目容量如下:

|? ? ? ? Projects? ? ? ? |? ? ?Repository? ? |? Size? | Files? | Folders |
|------------------------|-------------------|--------|--------|---------|
| .NET Runtime? ? ? ? ? ?| runtime-6.0.4? ? ?| 811 MB | 56,191 | 9,565? ?|
| .NET Core Runtime? ? ? | coreclr-3.1.24? ? | 529 MB | 30,146 | 6,180? ?|
| .NET Core Libraries? ? | corefx-3.1.24? ? ?| 201 MB | 20,880 | 2,930? ?|
| .NET Compiler Platform | roslyn-4.0.1? ? ? | 304 MB | 16,576 | 3,061? ?|
| ASP.NET Core? ? ? ? ? ?| aspnetcore-3.1.24 | 113 MB | 12,505 | 2,830? ?|
? .NET Runtime (Runtime)?
? ? git clone https://github.com/dotnet/runtime
? ? git clone git@github.com:dotnet/runtime.git
.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
This repo contains the code to build the .NET runtime, libraries and shared host (dotnet) installers for all supported platforms, as well as the sources to .NET runtime and libraries.
? .NET Core Libraries (CoreFX)?
? ? git clone https://github.com/dotnet/corefx
? ? git clone git@github.com:dotnet/corefx.git
This repo is used for servicing PR's for .NET Core 2.1 and 3.1. Please visit us at https://github.com/dotnet/runtime
This repo contains the library implementation (called "CoreFX") for .NET Core. It includes System.Collections, System.IO, System.Xml, and many other components.?
? .NET Core Runtime (CoreCLR)?
? ? git clone https://github.com/dotnet/coreclr
? ? git clone git@github.com:dotnet/coreclr.git
CoreCLR is the runtime for .NET Core. It includes the garbage collector, JIT compiler, primitive data types and low-level classes.
This repo contains the runtime implementation for .NET Core. It includes RyuJIT, the .NET GC, and many other components. Runtime-specific library code (System.Private.CoreLib) lives in the CoreCLR repo. It needs to be built and versioned in tandem with the runtime.
The rest of CoreFX is agnostic of runtime-implementation and can be run on any compatible .NET runtime (e.g. CoreRT).
? ASP.NET Core (AspNetCore)?
? ? git clone https://github.com/dotnet/aspnetcore
? ? git clone git@github.com:dotnet/aspnetcore.git
ASP.NET Core 容量是最小的,但項(xiàng)目實(shí)在還是太大,有 566 csproj 文件,包含 251 單元工程測(cè)試在內(nèi),26 個(gè)解決方案 .sln 文件也不少。在 .\src 目錄下面有很多子目錄,每一個(gè)子目錄都是一個(gè)子項(xiàng)目,每個(gè)子項(xiàng)目中都包含了一個(gè)解決方案。此外,還好些 MSBuild 工程文件,.debproj .javaproj .npmproj .pkgproj .proj .rpmproj .vcxproj .wixproj,這么多的工程類型是因?yàn)?ASP.NET Core 是基于 Web 的編程,涉及前端 JavaScript 腳本,NodeJS 以及 Java 等開發(fā)環(huán)境。
? Roslyn .NET compiler (roslyn)?
? ? git clone https://github.com/dotnet/roslyn
? ? git clone git@github.com:dotnet/roslyn.git
The .NET Compiler Platform, the Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
Roslyn is the open-source implementation of both the C# and Visual Basic compilers with an API surface for building code analysis tools.
Roslyn 項(xiàng)目是 .NET 開源編譯器,.NET 平臺(tái)程序的執(zhí)行模型的不同階段有兩個(gè)不同的編譯器:一個(gè)叫 Roslyn 編譯器,負(fù)責(zé)把 C# 和 VB 代碼編譯為程序集;另一個(gè)叫 RyuJIT 編譯器,負(fù)責(zé)把程序集中的 IL 中間語言代碼編譯為機(jī)器碼。
最初 C# 語言的編譯器是用 C++ 編寫的,后來微軟推出了一個(gè)新的用 C# 自身編寫的編譯器:Roslyn,它屬于自舉編譯器,即編譯器用自身語言來實(shí)現(xiàn)自己。Roslyn 支持 C# 和 Visual Basic 代碼編譯,并提供豐富的代碼分析 API,可以用它來做代碼生成器。