欢迎光临散文网 会员登陆 & 注册

Sublime Text 插件开发 - RunSnippet & JumpTo

2022-04-16 09:51 作者:紧果呗  | 我要投稿

# 🚩 Readme Fist

学习 Sublime Text 插件开发,请参考 [Sublime API 探索](https://github.com/jimboyeah/run-snippet/blob/master/APIs.md)

了解 RunSnippetCommand 或 JumpTo 插件请继续阅读本文。

快捷键配置文件 RunSnippet\Default.sublime-keymap

## ⚡ RunSnippetCommand 插件

作为一个重度 Sublime Text 用户,掌握 Plugin-host 插件机制及插件开发是非常必要的,有些稀奇古怪的想法功能都可以实现。


在 MD 文档中执行 Python 代码片段,比如 MD 文档中有以下代码片段,按注解提示配置好插件上下文菜单,保持光标在代码块上,按 F6 就可以执行:

可以在 Packages 目录执行以下命令安装 RunSnippet 插件:

    git clone git@github.com/jimboyeah/run-snippet.git

Sublime Text 4 插件宿主支持 Python 3.3、3.8,但在 Packages 目录安装的插件默认是 Plugin-Host 3.3,某些 Python 3.8 新功能不能使用。

RunSnippetCommand 插件实现代码,以下是基于 Python 3.8 的语法,可以根据 Sublime 选择器实现更多语言的支持,包括 C/C++,只需要配置好编译器待调用即可。


## ⚡ JumpTo ...


使用 SublimeText 阅读文档和写作是日常活动,特别是最近在阅读 [CPython](https://github.com/python/cpython) 以及 C# 相关开源代码及文档,Sublime 提供的跳转工具非常强大,因为会对代码文件进行符号索引,所以在已经建立索引的工程上,直接按 F12 就可以跳转到光标所在的符号定义上,对于 URL 地址,也可以通过右角菜单打开浏览器进行访问。


    git clone git@github.com:python/cpython

    git clone git@github.com:python/devguide

    git clone git@github.com:python/peps


不足的是,文档中有非常多的相对引用,这些引用都不能直接跳转到对应的文件中,需要通过 Ctrl+P 手动输入文件名间接跳转,对于大量文件查询来说,这是极其差的体验。

例如,C# 规范文档就包含许多文件,还有 .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

为此,有了开发 JumpTo 插件的想法,这是一个可以极高地提升文档阅读效率的插件,你值得拥有。

预期支持跳转地址格式如下:

- [x] 带括号的文件 (scope.md)

- [x] 带引号的文件 'scope.md' 或 "scope.md"

- [x] 带前导符号且使用空格分隔的文件路径

- [ ] 带 # 的设置的行号 (scope.md#LINE_NO)

- [ ] 带 # 的设置的标签 (scope.md#ANCHRO)

因为 Sublime 文件跳转有个临时状态,文件并没有完全确定打开,此时按方向键及回车之外的键,都会撤消文件的打开。所以,带标签的自动定位还需要寻求其它解决办法。


可跳转的内容示范:

    - 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":::


默认按 Shift 点击内容进行跳转,配置热键使用更方便。实现中使用了 on_text_command 事件,它可以获取鼠标点击坐标,但没有找到相应的 API 将坐标转换为 Text Point。

Sublime 使用 Ctrl 和 Alt 两个控制键分别用于增减选区,所以不太好直接使用它们。同时 Shift 也被用来做字符扩展选择,但是还是免强可用,最好还是配置按键触发,默认配置 F9 触发。

使用到正则字符串处理具,参考 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 有三个核心开源代码仓库,外加一个 ASP.NET Core 和 Roslyn,项目容量如下:

.Net Core 核心开源代码仓库

|        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 容量是最小的,但项目实在还是太大,有 566 csproj 文件,包含 251 单元工程测试在内,26 个解决方案 .sln 文件也不少。在 .\src 目录下面有很多子目录,每一个子目录都是一个子项目,每个子项目中都包含了一个解决方案。此外,还好些 MSBuild 工程文件,.debproj .javaproj .npmproj .pkgproj .proj .rpmproj .vcxproj .wixproj,这么多的工程类型是因为 ASP.NET Core 是基于 Web 的编程,涉及前端 JavaScript 脚本,NodeJS 以及 Java 等开发环境。


✅ 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 项目是 .NET 开源编译器,.NET 平台程序的执行模型的不同阶段有两个不同的编译器:一个叫 Roslyn 编译器,负责把 C# 和 VB 代码编译为程序集;另一个叫 RyuJIT 编译器,负责把程序集中的 IL 中间语言代码编译为机器码。


最初 C# 语言的编译器是用 C++ 编写的,后来微软推出了一个新的用 C# 自身编写的编译器:Roslyn,它属于自举编译器,即编译器用自身语言来实现自己。Roslyn 支持 C# 和 Visual Basic 代码编译,并提供丰富的代码分析 API,可以用它来做代码生成器。




Sublime Text 插件开发 - RunSnippet & JumpTo的评论 (共 条)

分享到微博请遵守国家法律