From 923b73e3e0a6a02cb536de8ffaf878b0eead8095 Mon Sep 17 00:00:00 2001 From: Enigma-Soul Date: Tue, 23 Jun 2026 20:23:34 +0800 Subject: [PATCH] =?UTF-8?q?style:=20prettier=20=E7=BB=9F=E4=B8=80=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E5=B9=B6=E7=AE=80=E5=8C=96=20needLogin?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - prettier 统一代码格式(换行重排) - 简化 needLogin:用 Promise.resolve + finally 取代两个重复的 then 回调 纯格式与等价重构,无逻辑变更。 Co-Authored-By: Claude Fable 5 --- README.md | 1 - src/__mocks__/vscode.ts | 23 +++++++++++++++++++---- src/commands/traininglist/index.ts | 6 +++++- src/features/login/auth.ts | 6 +++++- src/features/myArticle/fsProvider.ts | 3 +-- src/features/record.ts | 5 ++++- src/features/solution.ts | 5 ++++- src/utils/api.ts | 11 +++++++---- src/utils/shared.ts | 5 ++++- src/utils/showTrainDetails.ts | 14 ++++++++------ src/utils/tagManager.ts | 12 +++++++++--- src/utils/uiUtils.ts | 18 +++++++++++------- webview/utils/tags.ts | 10 ++++++++-- 13 files changed, 85 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index 45c6020d..714374a2 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,6 @@ > 在 issue 或者 QQ 群中提问 wiki 中有的内容,极有可能被无视或者攻击。 - ## 完成功能 - 查看题目 diff --git a/src/__mocks__/vscode.ts b/src/__mocks__/vscode.ts index 2292976c..9cd04fee 100644 --- a/src/__mocks__/vscode.ts +++ b/src/__mocks__/vscode.ts @@ -7,7 +7,11 @@ const vscode = { showQuickPick: () => Promise.resolve(undefined), showOpenDialog: () => Promise.resolve(undefined), createWebviewPanel: () => ({ - webview: { html: '', onDidReceiveMessage: () => ({ dispose: () => {} }), postMessage: () => {} }, + webview: { + html: '', + onDidReceiveMessage: () => ({ dispose: () => {} }), + postMessage: () => {} + }, onDidDispose: () => {}, dispose: () => {} }), @@ -31,8 +35,16 @@ const vscode = { workspaceFolders: undefined }, Uri: { - file: (path: string) => ({ fsPath: path, scheme: 'file', toString: () => path }), - parse: (uri: string) => ({ fsPath: uri, scheme: 'file', toString: () => uri }) + file: (path: string) => ({ + fsPath: path, + scheme: 'file', + toString: () => path + }), + parse: (uri: string) => ({ + fsPath: uri, + scheme: 'file', + toString: () => uri + }) }, ViewColumn: { One: 1, Two: 2, Three: 3, Active: -1 }, env: { @@ -41,7 +53,10 @@ const vscode = { }, EventEmitter: class { listeners: unknown[] = []; - event = (listener: unknown) => { this.listeners.push(listener); return { dispose: () => {} }; }; + event = (listener: unknown) => { + this.listeners.push(listener); + return { dispose: () => {} }; + }; fire() {} dispose() {} }, diff --git a/src/commands/traininglist/index.ts b/src/commands/traininglist/index.ts index 480739fc..47ec0719 100644 --- a/src/commands/traininglist/index.ts +++ b/src/commands/traininglist/index.ts @@ -3,7 +3,11 @@ import * as vscode from 'vscode'; import { searchTrainingdetail, searchTraininglist } from '@/utils/api'; import { getResourceFilePath } from '@/utils/html'; import { showTrainDetails } from '@/utils/showTrainDetails'; -import { getUsernameColor, getUserSvg, getWebviewViewColumn } from '@/utils/workspaceUtils'; +import { + getUsernameColor, + getUserSvg, + getWebviewViewColumn +} from '@/utils/workspaceUtils'; export default new SuperCommand({ onCommand: 'traininglist', diff --git a/src/features/login/auth.ts b/src/features/login/auth.ts index e940d363..2e082f22 100644 --- a/src/features/login/auth.ts +++ b/src/features/login/auth.ts @@ -43,7 +43,11 @@ export default class LuoguAuthProvider clientID: await genClientID(), name: '' }); - vscode.commands.executeCommand('setContext', 'luoguLoginStatus', false); + vscode.commands.executeCommand( + 'setContext', + 'luoguLoginStatus', + false + ); finishlock(); return; } diff --git a/src/features/myArticle/fsProvider.ts b/src/features/myArticle/fsProvider.ts index dd6fdadd..49649be1 100644 --- a/src/features/myArticle/fsProvider.ts +++ b/src/features/myArticle/fsProvider.ts @@ -88,8 +88,7 @@ export default class myArticleFsProvider try { const articleData = (await getArticle(uri.query)).data; const res = articleData.article; - if (!articleData.canEdit) - throw vscode.FileSystemError.NoPermissions(uri); + if (!articleData.canEdit) throw vscode.FileSystemError.NoPermissions(uri); if (res.promoteStatus === 2) if ( (await vscode.window.showWarningMessage( diff --git a/src/features/record.ts b/src/features/record.ts index 2360cbbc..67b7f4bb 100644 --- a/src/features/record.ts +++ b/src/features/record.ts @@ -2,7 +2,10 @@ import * as vscode from 'vscode'; import { fetchRecords, fetchResult } from '@/utils/api'; import { getDistFilePath } from '@/utils/html'; import { createWebsocket, WebsocketSchema } from '@/utils/websocket'; -import { processAxiosError, getWebviewViewColumn } from '@/utils/workspaceUtils'; +import { + processAxiosError, + getWebviewViewColumn +} from '@/utils/workspaceUtils'; import { RecordData } from 'luogu-api'; import { MessageTypes } from '@w/views/record/data'; diff --git a/src/features/solution.ts b/src/features/solution.ts index 88e4ab47..22905815 100644 --- a/src/features/solution.ts +++ b/src/features/solution.ts @@ -2,7 +2,10 @@ import * as vscode from 'vscode'; import type { ArticleDetails, List } from 'luogu-api'; import { getSolution, voteArticle } from '@/utils/api'; import { getDistFilePath } from '@/utils/html'; -import { processAxiosError, getWebviewViewColumn } from '@/utils/workspaceUtils'; +import { + processAxiosError, + getWebviewViewColumn +} from '@/utils/workspaceUtils'; import useWebviewResponseHandle from '@/utils/webviewResponse'; import ArticleData from '@/model/article'; diff --git a/src/utils/api.ts b/src/utils/api.ts index 7d5b8931..70354f95 100644 --- a/src/utils/api.ts +++ b/src/utils/api.ts @@ -747,10 +747,13 @@ interface TagsResponse { export const fetchLuoguTags = async (): Promise => { try { - const res = await axios.get('https://www.luogu.com.cn/_lfe/tags/zh-CN', { - myInterceptors_notCheckCookie: true, - myInterceptors_cookie: null - }); + const res = await axios.get( + 'https://www.luogu.com.cn/_lfe/tags/zh-CN', + { + myInterceptors_notCheckCookie: true, + myInterceptors_cookie: null + } + ); return res.data; } catch (err) { throw new Error('获取标签数据失败', { cause: err }); diff --git a/src/utils/shared.ts b/src/utils/shared.ts index bb108c57..f353ae31 100644 --- a/src/utils/shared.ts +++ b/src/utils/shared.ts @@ -274,7 +274,10 @@ export const formatTime = ( const lens = matched.length === 1 ? 3 : matched.length; fmt = fmt.replace( matched, - ('00' + o[k]).slice(('' + o[k]).length - 1, ('' + o[k]).length - 1 + lens) + ('00' + o[k]).slice( + ('' + o[k]).length - 1, + ('' + o[k]).length - 1 + lens + ) ); } else { fmt = fmt.replace( diff --git a/src/utils/showTrainDetails.ts b/src/utils/showTrainDetails.ts index 63a887d8..6dc08b66 100644 --- a/src/utils/showTrainDetails.ts +++ b/src/utils/showTrainDetails.ts @@ -40,12 +40,14 @@ const getDifficultyStatus = (difficulty: number) => { }; const getTagsStatus = async (tagIds: number[]) => { const resolved = await tagManager.getTags(tagIds); - return resolved.map((tag, i) => { - const id = tagIds[i]; - const name = tag?.name ?? `未知标签(${id})`; - const color = tag?.color ?? '#000000'; - return `${name} `; - }).join(''); + return resolved + .map((tag, i) => { + const id = tagIds[i]; + const name = tag?.name ?? `未知标签(${id})`; + const color = tag?.color ?? '#000000'; + return `${name} `; + }) + .join(''); }; export class TrainDetals { public title: string; diff --git a/src/utils/tagManager.ts b/src/utils/tagManager.ts index 149de4b3..761043ae 100644 --- a/src/utils/tagManager.ts +++ b/src/utils/tagManager.ts @@ -19,7 +19,9 @@ class TagManager { const cached = globalThis.__luoguTagsCache; const cacheTime = globalThis.__luoguTagsCacheTime; if (cached && cacheTime && Date.now() - cacheTime < CACHE_DURATION) { - this.tags = new Map(Object.entries(cached).map(([k, v]) => [Number(k), v as Tag])); + this.tags = new Map( + Object.entries(cached).map(([k, v]) => [Number(k), v as Tag]) + ); return true; } return false; @@ -36,7 +38,10 @@ class TagManager { try { const res = await fetchLuoguTags(); const typeColors = new Map( - res.types.map(t => [t.id, isColorKey(t.color) ? ColorPalette[t.color] : '#000000']) + res.types.map(t => [ + t.id, + isColorKey(t.color) ? ColorPalette[t.color] : '#000000' + ]) ); this.tags.clear(); this.types = typeColors; @@ -57,7 +62,8 @@ class TagManager { } private async ensureLoaded(): Promise { - if (this.tags.size > 0 && Date.now() - this.lastFetch < CACHE_DURATION) return; + if (this.tags.size > 0 && Date.now() - this.lastFetch < CACHE_DURATION) + return; if (!this.loadFromCache()) await this.fetchTags(); } diff --git a/src/utils/uiUtils.ts b/src/utils/uiUtils.ts index b654b614..a5221b5f 100644 --- a/src/utils/uiUtils.ts +++ b/src/utils/uiUtils.ts @@ -109,11 +109,15 @@ let needLoginShown = false; export const needLogin = () => { if (needLoginShown) return; needLoginShown = true; - vscode.window.showErrorMessage('未登录', '登录').then(async c => { - if (c) vscode.commands.executeCommand('luogu.signin'); - }).then(() => { - setTimeout(() => { needLoginShown = false; }, 5000); - }, () => { - setTimeout(() => { needLoginShown = false; }, 5000); - }); + // showErrorMessage 返回 Thenable 而非 Promise(无 finally),需包装为真正的 Promise + Promise.resolve(vscode.window.showErrorMessage('未登录', '登录')) + .then(async c => { + if (c) vscode.commands.executeCommand('luogu.signin'); + }) + // 无论用户是否点击,5 秒后都重置标记以允许再次提示 + .finally(() => { + setTimeout(() => { + needLoginShown = false; + }, 5000); + }); }; diff --git a/webview/utils/tags.ts b/webview/utils/tags.ts index dfed5ec6..1cb615b0 100644 --- a/webview/utils/tags.ts +++ b/webview/utils/tags.ts @@ -1,11 +1,17 @@ const tagsMap: Record = {}; const tagsJson = document.getElementById('luogu-tags')?.innerText; if (tagsJson) { - for (const t of JSON.parse(tagsJson) as { id: number; name: string; color: string }[]) { + for (const t of JSON.parse(tagsJson) as { + id: number; + name: string; + color: string; + }[]) { tagsMap[t.id] = { name: t.name, color: t.color }; } } -export function getTag(id: number): { name: string; color: string } | undefined { +export function getTag( + id: number +): { name: string; color: string } | undefined { return tagsMap[id]; }