VS Code API
约 926 字大约 3 分钟
macvscodeapi
2025-01-03
1. API
1.1. window
Namespace for dealing with the current window of the editor. That is visible and active editors, as well as, UI elements to show messages, selections, and asking for user input.
type vscode.window = {
activeColorTheme: ColorTheme
activeNotebookEditor: NotebookEditor | undefined
activeTerminal: Terminal | undefined
// type: TextEditor | undefined
// desc: 当前活动的编辑器或未定义。活动编辑器是当前具有焦点的编辑器,如果没有焦点,则是最近更改了输入的编辑器。
activeTextEditor: {
// type: TextDocument
// desc: Represents a text document, such as a source file. Text documents have lines and knowledge about an underlying resource like a file.
// 代表文本文档,例如源文件。文本文档具有有关文件之类的基础资源的行和知识。
document: {
eol: EndOfLine
fileName: string
isClosed: boolean
isDirty: boolean
isUntitled: boolean
languageId: string
lineCount: number
uri: Uri
version: number
getText(range?: Range): string
getWordRangeAtPosition(position: Position, regex?: RegExp): Range
lineAt(line: number): TextLine
lineAt(position: Position): TextLine
offsetAt(position: Position): number
positionAt(offset: number): Position
save(): Thenable<boolean> // 保存
validatePosition(position: Position): Position
validateRange(range: Range): Range
}
options: TextEditorOptions
// 编辑器内选中的文本
selection: Selection
selections: readonly Selection[]
viewColumn: ViewColumn
visibleRanges: readonly Range[]
edit(callback: (editBuilder: TextEditorEdit) => void, options?: {undoStopAfter: boolean, undoStopBefore: boolean}): Thenable<boolean>
hide(): void
insertSnippet(snippet: SnippetString, location?: Range | Position | readonly Range[] | readonly Position[], options?: {undoStopAfter: boolean, undoStopBefore: boolean}): Thenable<boolean>
revealRange(range: Range, revealType?: TextEditorRevealType): void
setDecorations(decorationType: TextEditorDecorationType, rangesOrOptions: readonly Range[] | readonly DecorationOptions[]): void
show(column?: ViewColumn): void
}
state: WindowState
tabGroups: TabGroups
terminals: readonly Terminal[]
visibleNotebookEditors: readonly NotebookEditor[]
visibleTextEditors: readonly TextEditor[]
}1.2. WorkspaceEdit
A workspace edit is a collection of textual and files changes for multiple resources and documents.
Use the applyEdit-function to apply a workspace edit.
const MyWorkspaceEdit = new vscode.WorkspaceEdit();
MyWorkspaceEdit.createFile(uri: Uri, options?: {contents: Uint8Array | DataTransferFile, ignoreIfExists: boolean, overwrite: boolean}, metadata?: WorkspaceEditEntryMetadata): void
MyWorkspaceEdit.delete(uri: Uri, range: Range, metadata?: WorkspaceEditEntryMetadata): void
MyWorkspaceEdit.deleteFile(uri: Uri, options?: {ignoreIfNotExists: boolean, recursive: boolean}, metadata?: WorkspaceEditEntryMetadata): void
MyWorkspaceEdit.entries(): Array<[Uri, TextEdit[]]>
MyWorkspaceEdit.get(uri: Uri): TextEdit[]
MyWorkspaceEdit.has(uri: Uri): boolean
// Insert the given text at the given position.
// 在指定的位置插入给定文本。
MyWorkspaceEdit.insert(uri: Uri, position: Position, newText: string, metadata?: WorkspaceEditEntryMetadata): void
MyWorkspaceEdit.renameFile(oldUri: Uri, newUri: Uri, options?: {ignoreIfExists: boolean, overwrite: boolean}, metadata?: WorkspaceEditEntryMetadata): void
MyWorkspaceEdit.replace(uri: Uri, range: Range, newText: string, metadata?: WorkspaceEditEntryMetadata): void
MyWorkspaceEdit.set(uri: Uri, edits: ReadonlyArray<TextEdit | SnippetTextEdit>): void
MyWorkspaceEdit.set(uri: Uri, edits: ReadonlyArray<[TextEdit | SnippetTextEdit, WorkspaceEditEntryMetadata]>): void
MyWorkspaceEdit.set(uri: Uri, edits: readonly NotebookEdit[]): void
MyWorkspaceEdit.set(uri: Uri, edits: ReadonlyArray<[NotebookEdit, WorkspaceEditEntryMetadata]>): void1.3. TextEditorEdit
A complex edit that will be applied in one transaction on a TextEditor. This holds a description of the edits and if the edits are valid (i.e. no overlapping regions, document was not changed in the meantime, etc.) they can be applied on a document associated with a text editor.
delete(location: Range | Selection): void
insert(location: Position, value: string): void
replace(location: Range | Position | Selection, value: string): void
setEndOfLine(endOfLine: EndOfLine): void2. 设置 setting 的配置项
package.json
{
"contributes": {
"configuration": {
"title": "RainTool",
"properties": {
"raintool.apiKey": {
"type": "string",
"default": "",
"description": "TinyPNG API Key"
}
}
}
}
}index.ts
// 从配置中获取 TinyPNG API key
const config = vscode.workspace.getConfiguration('raintool');
const apiKey = config.get<string>('apiKey');3. 右键菜单
官方文档:contributes.menus
Currently extension writers can contribute to:
- commandPalette - global Command Palette(全局命令面板)
- comments/comment/title - Comments title menu bar(评论标题菜单栏)
- comments/comment/context - Comments context menu(评论上下文菜单)
- comments/commentThread/title - Comments thread title menu bar
- comments/commentThread/context- Comments thread context menu
- debug/callstack/context - Debug Call Stack view context menu˜
- debug/callstack/context group inline - Debug Call Stack view inline actions
- debug/toolBar - Debug view toolbar
- debug/variables/context - Debug Variables view context menu
- editor/context - editor context menu(编辑器上下文菜单)
- editor/lineNumber/context - editor line number context menu
- editor/title - editor title menu bar
- editor/title/context - editor title context menu
- editor/title/run - Run submenu on the editor title menu bar
- explorer/context - Explorer view context menu
- extension/context - Extensions view context menu
- file/newFile - New File item in the File menu and Welcome page
- interactive/toolbar - Interactive Window toolbar
- interactive/cell/title - Interactive Window cell title menu bar
- notebook/toolbar - notebook toolbar
- notebook/cell/title - notebook cell title menu bar
- notebook/cell/execute - notebook cell execution menu
- scm/title - SCM title menu
- scm/resourceGroup/context - SCM resource groups menus
- scm/resourceFolder/context - SCM resource folders menus
- scm/resourceState/context - SCM resources menus
- scm/change/title - SCM change title menus
- scm/sourceControl- SCM source control menu
- terminal/context - terminal context menu
- terminal/title/context - terminal title context menu
- testing/item/context - Test Explorer item context menu
- testing/item/gutter - menu for a gutter decoration for a test item
- timeline/title - Timeline view title menu bar
- timeline/item/context - Timeline view item context menu
- touchBar - macOS Touch Bar
- view/title - View title menu
- view/item/context - View item context menu
- webview/context - any webview context menu
- Any contributed submenu
example: package.json
{
"contributes": {
"menus": {
// 编辑器上下文菜单
"editor/context": [
{
"when": "",
"command": "rain-tool.sort",
"alt": "hello world",
"group": "RainGroup@1"
}
],
// 资源管理器视图上下文菜单
"explorer/context": [
{
"when": "explorerResourceIsFolder",
"command": "rain-tool.compressFolderImgs",
"group": "RainGroup@2"
}
]
}
}
}