From b3a40d637dbccceb33276b6dfbc98d256756e99f Mon Sep 17 00:00:00 2001 From: lvjx7 Date: Thu, 1 Sep 2022 16:39:15 +0800 Subject: [PATCH] feat: progress statusbar --- src/extension.ts | 73 +++++++++++++++++++----------------------------- 1 file changed, 29 insertions(+), 44 deletions(-) diff --git a/src/extension.ts b/src/extension.ts index 5504096..b2a2a7b 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,13 +1,13 @@ -import { type ExtensionContext, Position, ProgressLocation, SnippetString, TextEdit, TextEditor, commands, window, workspace } from 'vscode' +import { ExtensionContext, Position, ProgressLocation, SnippetString, StatusBarAlignment, StatusBarItem, commands, window, workspace } from 'vscode' import log from './log' import { ESLint } from 'eslint' import { getTextBylines } from './utils' -let eslint: ESLint | null +let eslint: ESLint +let statusBarItem: StatusBarItem export function activate(context: ExtensionContext) { - log('eslint-disabled activated!') eslint = new ESLint({ @@ -21,7 +21,11 @@ export function activate(context: ExtensionContext) { }, }) - context.subscriptions.push(...disposes) + statusBarItem = window.createStatusBarItem(StatusBarAlignment.Left) + + context.subscriptions.push(...disposes, statusBarItem) + + log('eslint-disabled initialized!') } // this method is called when your extension is deactivated @@ -40,40 +44,26 @@ const disposes = [ return } - const lineRuleIdsMap = await window.withProgress({ - cancellable: false, - location: ProgressLocation.Notification, - title: 'Progress Notification', - }, async (progress, token) => { - - log('Start linting hole file content...') - let count = 5 - progress.report({ increment: 5, message: 'Start linting hole file content...' }) - - const fakeIncrement = setInterval(() => { - progress.report({ increment: count++, message: 'Still going...' }) - if (count == 95) clearInterval(fakeIncrement) - }, 1000) - - const results = await eslint!.lintFiles(activeTextEditor.document.uri.fsPath) - - log('Linting finish...') - clearInterval(fakeIncrement) - progress.report({ increment: 99, message: 'Linting finish.' }) - - const lineRuleIdsMap = results?.[0].messages.reduce((preValue, item) => { - if (!item.ruleId) return preValue - if (!preValue[item.line]) { - preValue[item.line] = [ item.ruleId ] - } else { - preValue[item.line] = [ ...preValue[item.line], item.ruleId ] - } - return preValue - }, {} as Record) ?? {} - - // eslint-disable-next-line no-promise-executor-return - return new Promise(resolve => setTimeout(() => resolve(lineRuleIdsMap), 500)) - }) + log('Start linting hole file content...') + statusBarItem.text = '$(loading~spin) Start linting hole file content...' + statusBarItem.show() + + const results = await eslint.lintFiles(activeTextEditor.document.uri.fsPath) + + log('Linting finish.') + // eslint-disable-next-line require-atomic-updates + statusBarItem.text = '$(check) Linting finish.' + setTimeout(() => statusBarItem.hide(), 2222) + + const lineRuleIdsMap = results?.[0].messages.reduce((preValue, item) => { + if (!item.ruleId) return preValue + if (!preValue[item.line]) { + preValue[item.line] = [ item.ruleId ] + } else { + preValue[item.line] = [ ...preValue[item.line], item.ruleId ] + } + return preValue + }, {} as Record) ?? {} if (!Object.keys(lineRuleIdsMap).length) { log('Everything is good.') @@ -117,11 +107,6 @@ const disposes = [ // hello world commands.registerCommand('eslint-disable.helloWorld', () => { - const p = window.showInformationMessage('Hello World from eslint-disable!!!') - void p.then(r => { - return r - }) - // ... + void window.showInformationMessage('Hello World from eslint-disable!!!') }), ] -