Skip to content

Commit

Permalink
feat: 增加几个语言支持
Browse files Browse the repository at this point in the history
Co-authored-by: ygqygq2 <[email protected]>
  • Loading branch information
ygqygq2 committed Mar 29, 2024
1 parent 54c0dbf commit bc1e805
Show file tree
Hide file tree
Showing 18 changed files with 157 additions and 26 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 &copy; Turbo Console Log
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -204,5 +204,8 @@
"publisherDisplayName": "ygqygq2",
"publisherId": "e0b1421e-635c-639a-85a0-8b271dcb3d11",
"isPreReleaseVersion": false
},
"dependencies": {
"@ygqygq2/vscode-log": "^0.0.3"
}
}
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion src/commands/commentAllLogMessages.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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) => {
Expand Down
7 changes: 4 additions & 3 deletions src/commands/deleteAllLogMessages.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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.');
}
});
},
Expand Down
4 changes: 3 additions & 1 deletion src/commands/displayLogMessage.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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(
Expand Down
8 changes: 6 additions & 2 deletions src/commands/uncommentAllLogMessages.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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,
Expand All @@ -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) => {
Expand Down
8 changes: 6 additions & 2 deletions src/commands/updateLineNumAllLogMessages.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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,
Expand All @@ -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) => {
Expand Down
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const CHANNEL_TITLE = 'Turbo Console Log';
21 changes: 21 additions & 0 deletions src/error/CustomErrorCode.enum.ts
Original file line number Diff line number Diff line change
@@ -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,
);
4 changes: 4 additions & 0 deletions src/error/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { errorCodeEnum as ErrorCode, errorCodeMessages } from './CustomErrorCode.enum';

export { CustomError } from '@ygqygq2/vscode-log';
export { ErrorCode, errorCodeMessages };
9 changes: 8 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
9 changes: 9 additions & 0 deletions src/test/suite/displayLogMessage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
15 changes: 12 additions & 3 deletions src/test/unit/commands/commentAllLogMessages.spec.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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;
Expand Down
15 changes: 12 additions & 3 deletions src/test/unit/commands/deleteAllLogMessages.spec.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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;
Expand Down
15 changes: 12 additions & 3 deletions src/test/unit/commands/displayLogMessage.spec.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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;
Expand Down
15 changes: 12 additions & 3 deletions src/test/unit/commands/uncommentAllLogMessages.spec.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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;
Expand Down
15 changes: 12 additions & 3 deletions src/test/unit/commands/updateLineNumAllLogMessages.spec.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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;
Expand Down

0 comments on commit bc1e805

Please sign in to comment.