Skip to content

Commit

Permalink
chore(vscode): move saving time code to features/
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Apr 27, 2023
1 parent ec1a1ef commit 9344e23
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 49 deletions.
2 changes: 2 additions & 0 deletions packages/vscode-vue/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import * as lsp from 'vscode-languageclient';
import * as componentMeta from './features/componentMeta';
import * as doctor from './features/doctor';
import * as nameCasing from './features/nameCasing';
import * as savingTime from './features/savingTime';
import * as splitEditors from './features/splitEditors';
import { config } from './config';

Expand Down Expand Up @@ -85,6 +86,7 @@ async function doActivate(context: vscode.ExtensionContext, createLc: CreateLang
activateServerMaxOldSpaceSizeChange();
activateRestartRequest();
activateClientRequests();
context.subscriptions.push(...savingTime.register());

splitEditors.register(context, syntacticClient);
doctor.register(context, semanticClient);
Expand Down
56 changes: 56 additions & 0 deletions packages/vscode-vue/src/features/savingTime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import * as vscode from 'vscode';
import { config } from '../config';

export function register() {

let start: number | undefined;
let isSavingMultiple = false;

return [
vscode.workspace.onWillSaveTextDocument((e) => {
if (e.document.languageId !== 'vue') {
return;
}
if (start !== undefined) {
isSavingMultiple = true;
}
start = Date.now();
}),
vscode.workspace.onDidSaveTextDocument(async () => {

if (isSavingMultiple) {
isSavingMultiple = false;
start = undefined;
}

if (start === undefined) {
return;
}

const time = Date.now() - start;
start = undefined;

if (config.codeActions.enabled && time > config.codeActions.savingTimeLimit) {
const options = [
'Disable codeActions',
'Increase saveTimeLimit',
'What is this?',
];;
const result = await vscode.window.showInformationMessage(
`Saving time is too long. (${time} ms > ${config.codeActions.savingTimeLimit} ms), `,
...options,
);
if (result === options[0]) {
config.update('codeActions.enabled', false);
vscode.window.showInformationMessage('Code Actions is disabled. (You can enable it in .vscode/settings.json)');
}
else if (result === options[1]) {
vscode.commands.executeCommand('workbench.action.openSettings2', { query: 'vue.codeActions.savingTimeLimit' });
}
else if (result === options[2]) {
vscode.env.openExternal(vscode.Uri.parse('https://github.com/vuejs/language-tools/discussions/2740'));
}
}
}),
];
}
49 changes: 0 additions & 49 deletions packages/vscode-vue/src/nodeClientMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,55 +26,6 @@ export function activate(context: vscode.ExtensionContext) {
}
});

let start: number | undefined;
let isSavingMultiple = false;

vscode.workspace.onWillSaveTextDocument((e) => {
if (e.document.languageId !== 'vue') {
return;
}
if (start !== undefined) {
isSavingMultiple = true;
}
start = Date.now();
});
vscode.workspace.onDidSaveTextDocument(async () => {

if (isSavingMultiple) {
isSavingMultiple = false;
start = undefined;
}

if (start === undefined) {
return;
}

const time = Date.now() - start;
start = undefined;

if (config.codeActions.enabled && time > config.codeActions.savingTimeLimit) {
const options = [
'Disable codeActions',
'Increase saveTimeLimit',
'What is this?',
];;
const result = await vscode.window.showInformationMessage(
`Saving time is too long. (${time} ms > ${config.codeActions.savingTimeLimit} ms), `,
...options,
);
if (result === options[0]) {
config.update('codeActions.enabled', false);
vscode.window.showInformationMessage('Code Actions is disabled. (You can enable it in .vscode/settings.json)');
}
else if (result === options[1]) {
vscode.commands.executeCommand('workbench.action.openSettings2', { query: 'vue.codeActions.savingTimeLimit' });
}
else if (result === options[2]) {
vscode.env.openExternal(vscode.Uri.parse('https://github.com/vuejs/language-tools/discussions/2740'));
}
}
});

return commonActivate(context, (
id,
name,
Expand Down

0 comments on commit 9344e23

Please sign in to comment.