diff --git a/package.json b/package.json index ddc23f6..20eca14 100644 --- a/package.json +++ b/package.json @@ -65,6 +65,22 @@ "when": "false" } ], + "configuration": { + "type": "object", + "title": "OAK Assistant", + "properties": { + "oak-assistant.i18n": { + "type": "boolean", + "default": true, + "description": "启用国际化相关功能" + }, + "oak-assistant.entityJump": { + "type": "boolean", + "default": true, + "description": "启用实体跳转功能" + } + } + }, "menus": { "explorer/context": [ { diff --git a/src/extension.ts b/src/extension.ts index c4d60e6..7734d73 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -145,6 +145,7 @@ export async function activate(context: vscode.ExtensionContext) { activateOakLocale(context); activateOakComponentPropsLinkProvider(context); commonCommands.activate(context); + entityProviders.activate(context); context.subscriptions.push( helloOak, reload, @@ -156,7 +157,6 @@ export async function activate(context: vscode.ExtensionContext) { oakPathCompletion.oakPathCompletion, oakPathCompletion.oakPathDocumentLinkProvider, ...oakPathHighlighter, - entityProviders.documentLinkProvider ); createFileWatcher(context); } catch (error) { diff --git a/src/plugins/entityJump.ts b/src/plugins/entityJump.ts index 0edc9f9..fe1d3f7 100644 --- a/src/plugins/entityJump.ts +++ b/src/plugins/entityJump.ts @@ -1,44 +1,76 @@ import * as vscode from 'vscode'; import { entityConfig } from '../utils/entities'; -const entityProviders = { - documentLinkProvider: vscode.languages.registerDocumentLinkProvider( - { scheme: 'file' }, - { - provideDocumentLinks( - document: vscode.TextDocument - ): vscode.DocumentLink[] { - const links: vscode.DocumentLink[] = []; - const regex = /[\$]?entity[:=]\s*(['"])([a-zA-Z0-9_\s]+)\1[,\n]/g; - const text = document.getText(); - let match; - while ((match = regex.exec(text)) !== null) { - const entityName = match[2]; - if (!entityConfig.entityNameList.includes(entityName)) { - continue; - } - const start = document.positionAt(match.index); - const end = document.positionAt( - match.index + match[0].length - ); - const range = new vscode.Range(start, end); - const uri = vscode.Uri.parse( - `command:oak-entities.jumpToDefinition?${encodeURIComponent( - JSON.stringify({ entityName }) - )}` - ); - const link = new vscode.DocumentLink(range, uri); - link.tooltip = `跳转到定义: ${entityName}`; - links.push(link); - } - return links; - }, - } - ), +let provider: vscode.Disposable | undefined; +const entityProviders = { + activate: (context: vscode.ExtensionContext) => { + const enabled = vscode.workspace + .getConfiguration('oak-assistant') + .get('entityJump'); + if (!enabled) { + console.log('Entity Jump is disabled'); + return; + } + provider = vscode.languages.registerDocumentLinkProvider( + { scheme: 'file' }, + { + provideDocumentLinks( + document: vscode.TextDocument + ): vscode.DocumentLink[] { + const links: vscode.DocumentLink[] = []; + const regex = + /[\$]?entity[:=]\s*(['"])([a-zA-Z0-9_\s]+)\1[,\n]/g; + const text = document.getText(); + let match; + while ((match = regex.exec(text)) !== null) { + const entityName = match[2]; + if (!entityConfig.entityNameList.includes(entityName)) { + continue; + } + const start = document.positionAt(match.index); + const end = document.positionAt( + match.index + match[0].length + ); + const range = new vscode.Range(start, end); + const uri = vscode.Uri.parse( + `command:oak-entities.jumpToDefinition?${encodeURIComponent( + JSON.stringify({ entityName }) + )}` + ); + const link = new vscode.DocumentLink(range, uri); + link.tooltip = `跳转到定义: ${entityName}`; + links.push(link); + } + return links; + }, + } + ); + context.subscriptions.push(provider); + }, dispose() { - this.documentLinkProvider.dispose(); + provider?.dispose(); }, }; +// 监控配置修改 +vscode.workspace.onDidChangeConfiguration((e) => { + if (e.affectsConfiguration('oak-assistant.entityJump')) { + // 提示重新加载 + vscode.window + .showInformationMessage( + '配置已更新,是否重新加载以应用更改?', + '是', + '否' + ) + .then((selection) => { + if (selection === '是') { + vscode.commands.executeCommand( + 'workbench.action.reloadWindow' + ); + } + }); + } +}); + export default entityProviders; diff --git a/src/plugins/oakLocale.ts b/src/plugins/oakLocale.ts index 0f7e38d..386c7b1 100644 --- a/src/plugins/oakLocale.ts +++ b/src/plugins/oakLocale.ts @@ -1,3 +1,4 @@ +import { getAvailableKeys } from './../utils/locales'; import { join } from 'path'; import { getCachedLocaleItemByKey, @@ -294,6 +295,13 @@ const addLocaleToData = ( }; export function activateOakLocale(context: vscode.ExtensionContext) { + const enabledI18n = vscode.workspace + .getConfiguration('oak-assistant') + .get('i18n'); + if (!enabledI18n) { + console.log('i18n 相关功能已禁用'); + return; + } context.subscriptions.push(oakLocalesProvider); context.subscriptions.push(documentChangeListener); context.subscriptions.push(documentOpenListener); @@ -314,6 +322,26 @@ export function deactivateOakLocale() { addLocaleCommand.dispose(); } +// 监控配置项修改 +vscode.workspace.onDidChangeConfiguration((event) => { + if (event.affectsConfiguration('oak-assistant.i18n')) { + // 提示重新加载 + vscode.window + .showInformationMessage( + '配置已更新,是否重新加载以应用更改?', + '是', + '否' + ) + .then((selection) => { + if (selection === '是') { + vscode.commands.executeCommand( + 'workbench.action.reloadWindow' + ); + } + }); + } +}); + onEntityLoaded(() => { // 第一次加载完先激活一次 if (vscode.window.activeTextEditor) {