mirror of
https://github.com/microsoft/vscode-extension-samples.git
synced 2026-06-13 07:10:26 +08:00
58 lines
1.9 KiB
TypeScript
58 lines
1.9 KiB
TypeScript
/*---------------------------------------------------------
|
|
* Copyright (C) Microsoft Corporation. All rights reserved.
|
|
*--------------------------------------------------------*/
|
|
|
|
import * as vscode from 'vscode';
|
|
|
|
export function activate(context: vscode.ExtensionContext) {
|
|
context.subscriptions.push(
|
|
vscode.languages.registerCodeActionsProvider('markdown', new Emojizer(), {
|
|
providedCodeActionKinds: Emojizer.providedCodeActionKinds
|
|
}));
|
|
}
|
|
|
|
/**
|
|
* Provides code actions for converting :) to an smiley emoji.
|
|
*/
|
|
export class Emojizer implements vscode.CodeActionProvider {
|
|
|
|
public static readonly providedCodeActionKinds = [
|
|
vscode.CodeActionKind.QuickFix
|
|
];
|
|
|
|
public provideCodeActions(document: vscode.TextDocument, range: vscode.Range): vscode.CodeAction[] | undefined {
|
|
if (!this.isAtStartOfSmiley(document, range)) {
|
|
return;
|
|
}
|
|
|
|
const replaceWithSmileyCatFix = this.createFix(document, range, '😺');
|
|
|
|
const replaceWithSmileyFix = this.createFix(document, range, '😀');
|
|
// Marking a single fix as `preferred` means that users can apply it with a
|
|
// single keyboard shortcut using the `Auto Fix` command.
|
|
replaceWithSmileyFix.isPreferred = true;
|
|
|
|
const replaceWithSmileyHankyFix = this.createFix(document, range, '💩');
|
|
|
|
return [
|
|
replaceWithSmileyCatFix,
|
|
replaceWithSmileyFix,
|
|
replaceWithSmileyHankyFix
|
|
];
|
|
}
|
|
|
|
private isAtStartOfSmiley(document: vscode.TextDocument, range: vscode.Range) {
|
|
const start = range.start;
|
|
const line = document.lineAt(start.line);
|
|
return line.text[start.character] === ':' && line.text[start.character + 1] === ')';
|
|
}
|
|
|
|
private createFix(document: vscode.TextDocument, range: vscode.Range, emoji: string): vscode.CodeAction {
|
|
const fix = new vscode.CodeAction(`Convert to ${emoji}`, vscode.CodeActionKind.QuickFix);
|
|
fix.edit = new vscode.WorkspaceEdit();
|
|
fix.edit.replace(document.uri, new vscode.Range(range.start, range.start.translate(0, 2)), emoji);
|
|
return fix;
|
|
}
|
|
}
|
|
|