VS Code 整活:100行代码写一个悬浮翻译插件

本文介绍了如何利用100行代码在VS Code中实现一个悬浮翻译插件,通过分析现有插件源码,讲解了初始化配置、触发翻译和显示悬停等关键步骤,适合初学者和插件开发者学习。
摘要由CSDN通过智能技术生成

前言

要说哪个插件对效率提升最大,可能各有推荐,各有千秋。但我要说对初学者,以及英文有亿点点差的同学来讲:翻译,是日常开发中必不可少的一环。在下找过N个VSCode 翻译插件 发现一个神器:

f8ddfdeb199869cc6240f4ca95232eba.png

VSCode 插件:Google Translate Extension

这款插件不像其它要么需科学上网,要么强绑定快捷键的憨憨插件,有个最直观的功能:选中悬浮翻译

115cfd075b09b8af7ef990d57ea0db29.gif

啊这...也太作弊了吧,编程能力+1s。

f6c77eb78c62eae5c311a09706f6991a.png

因为这两年都在从事VSCode二次/插件 开发,这个功能深得我心,于是想捣腾一下,看看能不能写轮眼学习一番

b4f3d16718b7063900b25807eff6f0e5.png

撒话不说,开干!

1. 代码目录分析

c78a398dac8493331a3f680f877e1e57.png

其中代码逻辑都在src目录里。

command.js //全部命令
extension.js //插件入口文件
tranlate.js //翻译工具函数

先从入口文件extension.js出发:

// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
const command = require('./command')

function activate(context) {
    console.log('Congratulations, your extension "vsc-google-translate" is now active!');

    command.initSetting(context);

    context.subscriptions.push(command.hoverDisposable);
    context.subscriptions.push(command.tranDisposable);
    context.subscriptions.push(command.switchDisposable);
    context.subscriptions.push(command.copyDisposable);
    context.subscriptions.push(command.replaceDisposable);
    context.subscriptions.push(command.canDisposable);
    context.subscriptions.push(command.switchLangDisposable);
    conte
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值