From bc1e80502e7c654a7498f6b2149535003f9a9c44 Mon Sep 17 00:00:00 2001 From: ygqygq2 Date: Fri, 29 Mar 2024 11:41:54 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E5=87=A0=E4=B8=AA?= =?UTF-8?q?=E8=AF=AD=E8=A8=80=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: ygqygq2 --- README.md | 19 +++++++++++++++++ package.json | 5 ++++- pnpm-lock.yaml | 9 ++++++++ src/commands/commentAllLogMessages.ts | 4 +++- src/commands/deleteAllLogMessages.ts | 7 ++++--- src/commands/displayLogMessage.ts | 4 +++- src/commands/uncommentAllLogMessages.ts | 8 +++++-- src/commands/updateLineNumAllLogMessages.ts | 8 +++++-- src/constants.ts | 1 + src/error/CustomErrorCode.enum.ts | 21 +++++++++++++++++++ src/error/index.ts | 4 ++++ src/extension.ts | 9 +++++++- src/test/suite/displayLogMessage.test.ts | 9 ++++++++ .../commands/commentAllLogMessages.spec.ts | 15 ++++++++++--- .../commands/deleteAllLogMessages.spec.ts | 15 ++++++++++--- .../unit/commands/displayLogMessage.spec.ts | 15 ++++++++++--- .../commands/uncommentAllLogMessages.spec.ts | 15 ++++++++++--- .../updateLineNumAllLogMessages.spec.ts | 15 ++++++++++--- 18 files changed, 157 insertions(+), 26 deletions(-) create mode 100644 src/constants.ts create mode 100644 src/error/CustomErrorCode.enum.ts create mode 100644 src/error/index.ts diff --git a/README.md b/README.md index c5851ab..9a117df 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,25 @@ Properties: | Insert Empty Line Before Log Message | Whether to insert an empty line before the log message or not | insertEmptyLineBeforeLogMessage | `false` | | Insert Empty Line After Log Message | Whether to insert an empty line after the log message or not | insertEmptyLineAfterLogMessage | `false` | +## Support languages + +- javascript +- typescript +- php +- csharp +- c++ +- rust +- kotlin +- scala +- python +- ruby +- perl +- go +- java +- swift +- shellscript +- lua + ## License MIT Copyright © Turbo Console Log diff --git a/package.json b/package.json index 10e3eb9..a6d5e34 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "turbo-print-log", "displayName": "Turbo Console Log for more language", "description": "选中变量,根据不同语言增加一行变量打印日志", - "version": "3.0.5", + "version": "3.0.6", "publisher": "ygqygq2", "engines": { "vscode": "^1.85.0" @@ -204,5 +204,8 @@ "publisherDisplayName": "ygqygq2", "publisherId": "e0b1421e-635c-639a-85a0-8b271dcb3d11", "isPreReleaseVersion": false + }, + "dependencies": { + "@ygqygq2/vscode-log": "^0.0.3" } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 86e480d..1e716fd 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -4,6 +4,11 @@ settings: autoInstallPeers: true excludeLinksFromLockfile: false +dependencies: + '@ygqygq2/vscode-log': + specifier: ^0.0.3 + version: 0.0.3 + devDependencies: '@types/chai': specifier: ^4.3.11 @@ -1158,6 +1163,10 @@ packages: - supports-color dev: true + /@ygqygq2/vscode-log@0.0.3: + resolution: {integrity: sha512-QcmmOJ2+T1QBTTKdtlCGQQ/qNUaMRKHKEtckf0B2V/aafZKnoRZfnMWQRNpaZwDVSakSqkb48vJJlusw9/T9vw==} + dev: false + /acorn-jsx@5.3.2(acorn@8.11.3): resolution: {integrity: sha512-rq9s+JNhf0IChjtDXxllJ7g41oZk5SlXtp0LHwyA5cejwn7vKmKp4pPri6YEePv2PU65sAsegbXtIinmDFDXgQ==} peerDependencies: diff --git a/src/commands/commentAllLogMessages.ts b/src/commands/commentAllLogMessages.ts index 5d797fb..abef826 100644 --- a/src/commands/commentAllLogMessages.ts +++ b/src/commands/commentAllLogMessages.ts @@ -1,6 +1,7 @@ +import { logger } from '@/extension'; +import { instanceDebugMessage } from '@/utils/instanceDebugMessage'; import * as vscode from 'vscode'; import { Command, ExtensionProperties, Message } from '../typings'; -import { instanceDebugMessage } from '@/utils/instanceDebugMessage'; export function commentAllLogMessagesCommand(): Command { return { @@ -41,6 +42,7 @@ export function commentAllLogMessagesCommand(): Command { // 在编辑器中添加注释 const singleLineCommentSymbol = debugMessage.getSingleLineCommentSymbol(); + logger.info('Comment debug log'); editor.edit((editBuilder) => { logMessages.forEach(({ spaces, lines }) => { lines.forEach((line: vscode.Range) => { diff --git a/src/commands/deleteAllLogMessages.ts b/src/commands/deleteAllLogMessages.ts index 5289ca6..bc0efa9 100644 --- a/src/commands/deleteAllLogMessages.ts +++ b/src/commands/deleteAllLogMessages.ts @@ -1,6 +1,7 @@ +import { logger } from '@/extension'; +import { instanceDebugMessage } from '@/utils/instanceDebugMessage'; import * as vscode from 'vscode'; import { Command, ExtensionProperties, Message } from '../typings'; -import { instanceDebugMessage } from '@/utils/instanceDebugMessage'; // 导出一个函数,用于删除所有日志消息 export function deleteAllLogMessagesCommand(): Command { @@ -62,9 +63,9 @@ export function deleteAllLogMessagesCommand(): Command { }) .then((success) => { if (success) { - vscode.window.showInformationMessage('TurboConsoleLog: Delete debug log successes.'); + logger.info('Delete debug log success.'); } else { - vscode.window.showErrorMessage('TurboConsoleLog: Delete debug log failed.'); + logger.info('Delete debug log failed.'); } }); }, diff --git a/src/commands/displayLogMessage.ts b/src/commands/displayLogMessage.ts index 92f7656..2953cc9 100644 --- a/src/commands/displayLogMessage.ts +++ b/src/commands/displayLogMessage.ts @@ -1,7 +1,8 @@ +import { logger } from '@/extension'; +import { instanceDebugMessage } from '@/utils/instanceDebugMessage'; import * as vscode from 'vscode'; import { Command, ExtensionProperties } from '../typings'; import { getTabSize } from '../utils/getTabSize'; -import { instanceDebugMessage } from '@/utils/instanceDebugMessage'; // 插入调试日志/更新调试日志行号 export function displayLogMessageCommand(): Command { @@ -45,6 +46,7 @@ export function displayLogMessageCommand(): Command { // 如果选择文本不为空 if (selectedVar.trim().length !== 0) { // 使用编辑器编辑 + logger.info('Insert debug log'); await editor.edit((editBuilder) => { // 调用debugMessage.insertMessage函数 debugMessage.insertMessage( diff --git a/src/commands/uncommentAllLogMessages.ts b/src/commands/uncommentAllLogMessages.ts index 60ce259..326bda7 100644 --- a/src/commands/uncommentAllLogMessages.ts +++ b/src/commands/uncommentAllLogMessages.ts @@ -1,6 +1,7 @@ +import { logger } from '@/extension'; +import { instanceDebugMessage } from '@/utils/instanceDebugMessage'; import * as vscode from 'vscode'; import { Command, ExtensionProperties, Message } from '../typings'; -import { instanceDebugMessage } from '@/utils/instanceDebugMessage'; export function uncommentAllLogMessagesCommand(): Command { return { @@ -21,7 +22,9 @@ export function uncommentAllLogMessagesCommand(): Command { const document: vscode.TextDocument = editor.document; // 检测所有日志消息 - const logFunctionByLanguageId = debugMessage?.getLanguageProcessor().getLogFunction(logFunction); + const logFunctionByLanguageId = debugMessage + ?.getLanguageProcessor() + .getLogFunction(logFunction); const logMessages: Message[] = debugMessage.detectAll( document, logFunctionByLanguageId, @@ -32,6 +35,7 @@ export function uncommentAllLogMessagesCommand(): Command { // 遍历所有日志消息,并删除注释 const singleLineCommentSymbol = debugMessage.getSingleLineCommentSymbol(); const regex = new RegExp(`${singleLineCommentSymbol}`, 'g'); + logger.info('Uncomment debug log'); editor.edit((editBuilder) => { logMessages.forEach(({ spaces, lines }) => { lines.forEach((line: vscode.Range) => { diff --git a/src/commands/updateLineNumAllLogMessages.ts b/src/commands/updateLineNumAllLogMessages.ts index 230b473..f7c4289 100644 --- a/src/commands/updateLineNumAllLogMessages.ts +++ b/src/commands/updateLineNumAllLogMessages.ts @@ -1,6 +1,7 @@ +import { logger } from '@/extension'; +import { instanceDebugMessage } from '@/utils/instanceDebugMessage'; import * as vscode from 'vscode'; import { Command, ExtensionProperties, Message } from '../typings'; -import { instanceDebugMessage } from '@/utils/instanceDebugMessage'; export function updateLineNumAllLogMessagesCommand(): Command { return { @@ -27,7 +28,9 @@ export function updateLineNumAllLogMessagesCommand(): Command { } // 检测所有日志消息 - const logFunctionByLanguageId = debugMessage?.getLanguageProcessor().getLogFunction(logFunction); + const logFunctionByLanguageId = debugMessage + ?.getLanguageProcessor() + .getLogFunction(logFunction); const logMessages: Message[] = debugMessage.detectAll( document, logFunctionByLanguageId, @@ -37,6 +40,7 @@ export function updateLineNumAllLogMessagesCommand(): Command { // 遍历所有日志消息,并更新行号 const oldLineNum = new RegExp(`:(\\d+) ${delimiterInsideMessage}`); + logger.info('Update debug log line number'); editor.edit((editBuilder) => { logMessages.forEach(({ spaces, lines }) => { lines.forEach((line: vscode.Range) => { diff --git a/src/constants.ts b/src/constants.ts new file mode 100644 index 0000000..af9ea5d --- /dev/null +++ b/src/constants.ts @@ -0,0 +1 @@ +export const CHANNEL_TITLE = 'Turbo Console Log'; diff --git a/src/error/CustomErrorCode.enum.ts b/src/error/CustomErrorCode.enum.ts new file mode 100644 index 0000000..b89a1fc --- /dev/null +++ b/src/error/CustomErrorCode.enum.ts @@ -0,0 +1,21 @@ +import { createErrorCodeMessages, ErrorCodeMessage } from '@ygqygq2/vscode-log'; + +export enum CustomErrorCode { + // 运行相关错误 (1000 - 1009) + RUN_ERROR = 1000, +} + +const extensionPrefix = ''; + +const execCustomErrorCodeMessage = { + [CustomErrorCode.RUN_ERROR]: `${extensionPrefix}Run error.`, +}; + +export const customErrorCodeMessages: ErrorCodeMessage = { + ...execCustomErrorCodeMessage, +}; + +export const { errorCodeEnum, errorCodeMessages } = createErrorCodeMessages( + CustomErrorCode, + customErrorCodeMessages, +); diff --git a/src/error/index.ts b/src/error/index.ts new file mode 100644 index 0000000..b5fe365 --- /dev/null +++ b/src/error/index.ts @@ -0,0 +1,4 @@ +import { errorCodeEnum as ErrorCode, errorCodeMessages } from './CustomErrorCode.enum'; + +export { CustomError } from '@ygqygq2/vscode-log'; +export { ErrorCode, errorCodeMessages }; diff --git a/src/extension.ts b/src/extension.ts index a1ac3ef..241e945 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,6 +1,13 @@ +import { Logger } from '@ygqygq2/vscode-log'; import * as vscode from 'vscode'; -import { Command, ExtensionProperties } from './typings'; import { getAllCommands } from './commands/'; +import { CHANNEL_TITLE } from './constants'; +import { CustomError, errorCodeMessages } from './error'; +import { Command, ExtensionProperties } from './typings'; + +CustomError.configure(errorCodeMessages); +Logger.configure(vscode.window, CHANNEL_TITLE); +export const logger = Logger.getInstance(); // 导出一个函数,用于激活插件 export function activate(): void { diff --git a/src/test/suite/displayLogMessage.test.ts b/src/test/suite/displayLogMessage.test.ts index 31e64f1..f91aea5 100644 --- a/src/test/suite/displayLogMessage.test.ts +++ b/src/test/suite/displayLogMessage.test.ts @@ -12,6 +12,15 @@ import { displayLogMessageCommand } from '@/commands/displayLogMessage'; import { ExtensionProperties } from '@/typings'; vi.mock('vscode'); +vi.mock('@/extension', () => ({ + logger: { + trace: vi.fn(), + debug: vi.fn(), + info: vi.fn(), + warn: vi.fn(), + error: vi.fn(), + }, +})); describe('displayLogMessageCommand', () => { let mockEditor: TextEditor | undefined; diff --git a/src/test/unit/commands/commentAllLogMessages.spec.ts b/src/test/unit/commands/commentAllLogMessages.spec.ts index 36255b8..c6011e5 100644 --- a/src/test/unit/commands/commentAllLogMessages.spec.ts +++ b/src/test/unit/commands/commentAllLogMessages.spec.ts @@ -1,3 +1,6 @@ +import { commentAllLogMessagesCommand } from '@/commands/commentAllLogMessages'; +import { ExtensionProperties } from '@/typings'; +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; import { Position, Range, @@ -7,11 +10,17 @@ import { TextEditorEdit, window, } from 'vscode'; -import { describe, expect, it, vi, beforeEach, afterEach } from 'vitest'; -import { commentAllLogMessagesCommand } from '@/commands/commentAllLogMessages'; -import { ExtensionProperties } from '@/typings'; vi.mock('vscode'); +vi.mock('@/extension', () => ({ + logger: { + trace: vi.fn(), + debug: vi.fn(), + info: vi.fn(), + warn: vi.fn(), + error: vi.fn(), + }, +})); describe('commentAllLogMessagesCommand', () => { let mockEditor: TextEditor | undefined; diff --git a/src/test/unit/commands/deleteAllLogMessages.spec.ts b/src/test/unit/commands/deleteAllLogMessages.spec.ts index a947292..0eddfc7 100644 --- a/src/test/unit/commands/deleteAllLogMessages.spec.ts +++ b/src/test/unit/commands/deleteAllLogMessages.spec.ts @@ -1,3 +1,6 @@ +import { deleteAllLogMessagesCommand } from '@/commands/deleteAllLogMessages'; +import { ExtensionProperties } from '@/typings'; +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; import { Position, Range, @@ -7,11 +10,17 @@ import { TextEditorEdit, window, } from 'vscode'; -import { describe, expect, it, vi, beforeEach, afterEach } from 'vitest'; -import { deleteAllLogMessagesCommand } from '@/commands/deleteAllLogMessages'; -import { ExtensionProperties } from '@/typings'; vi.mock('vscode'); +vi.mock('@/extension', () => ({ + logger: { + trace: vi.fn(), + debug: vi.fn(), + info: vi.fn(), + warn: vi.fn(), + error: vi.fn(), + }, +})); describe('deleteAllLogMessagesCommand', () => { let mockEditor: TextEditor | undefined; diff --git a/src/test/unit/commands/displayLogMessage.spec.ts b/src/test/unit/commands/displayLogMessage.spec.ts index 31e64f1..549b3fd 100644 --- a/src/test/unit/commands/displayLogMessage.spec.ts +++ b/src/test/unit/commands/displayLogMessage.spec.ts @@ -1,3 +1,6 @@ +import { displayLogMessageCommand } from '@/commands/displayLogMessage'; +import { ExtensionProperties } from '@/typings'; +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; import { Position, Range, @@ -7,11 +10,17 @@ import { TextEditorEdit, window, } from 'vscode'; -import { describe, expect, it, vi, beforeEach, afterEach } from 'vitest'; -import { displayLogMessageCommand } from '@/commands/displayLogMessage'; -import { ExtensionProperties } from '@/typings'; vi.mock('vscode'); +vi.mock('@/extension', () => ({ + logger: { + trace: vi.fn(), + debug: vi.fn(), + info: vi.fn(), + warn: vi.fn(), + error: vi.fn(), + }, +})); describe('displayLogMessageCommand', () => { let mockEditor: TextEditor | undefined; diff --git a/src/test/unit/commands/uncommentAllLogMessages.spec.ts b/src/test/unit/commands/uncommentAllLogMessages.spec.ts index 2e47ecb..c762e1a 100644 --- a/src/test/unit/commands/uncommentAllLogMessages.spec.ts +++ b/src/test/unit/commands/uncommentAllLogMessages.spec.ts @@ -1,3 +1,6 @@ +import { uncommentAllLogMessagesCommand } from '@/commands/uncommentAllLogMessages'; +import { ExtensionProperties } from '@/typings'; +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; import { Position, Range, @@ -7,11 +10,17 @@ import { TextEditorEdit, window, } from 'vscode'; -import { describe, expect, it, vi, beforeEach, afterEach } from 'vitest'; -import { uncommentAllLogMessagesCommand } from '@/commands/uncommentAllLogMessages'; -import { ExtensionProperties } from '@/typings'; vi.mock('vscode'); +vi.mock('@/extension', () => ({ + logger: { + trace: vi.fn(), + debug: vi.fn(), + info: vi.fn(), + warn: vi.fn(), + error: vi.fn(), + }, +})); describe('uncommentAllLogMessagesCommand', () => { let mockEditor: TextEditor | undefined; diff --git a/src/test/unit/commands/updateLineNumAllLogMessages.spec.ts b/src/test/unit/commands/updateLineNumAllLogMessages.spec.ts index 973eeeb..14c9e1c 100644 --- a/src/test/unit/commands/updateLineNumAllLogMessages.spec.ts +++ b/src/test/unit/commands/updateLineNumAllLogMessages.spec.ts @@ -1,3 +1,6 @@ +import { updateLineNumAllLogMessagesCommand } from '@/commands/updateLineNumAllLogMessages'; +import { ExtensionProperties } from '@/typings'; +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; import { Position, Range, @@ -7,11 +10,17 @@ import { TextEditorEdit, window, } from 'vscode'; -import { describe, expect, it, vi, beforeEach, afterEach } from 'vitest'; -import { updateLineNumAllLogMessagesCommand } from '@/commands/updateLineNumAllLogMessages'; -import { ExtensionProperties } from '@/typings'; vi.mock('vscode'); +vi.mock('@/extension', () => ({ + logger: { + trace: vi.fn(), + debug: vi.fn(), + info: vi.fn(), + warn: vi.fn(), + error: vi.fn(), + }, +})); describe('updateLineNumAllLogMessagesCommand', () => { let mockEditor: TextEditor | undefined;