Skip to content

Commit

Permalink
refactor: logFunction 参数调整
Browse files Browse the repository at this point in the history
  • Loading branch information
ygqygq2 committed Jan 29, 2024
1 parent 6418672 commit 8b0ae14
Show file tree
Hide file tree
Showing 12 changed files with 177 additions and 124 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Properties:
| ------------------------------------ | --------------------------------------------------------------------------------------------------------------------------- | ------------------------------- | -------------- |
| Custom Prefix | The prefix of the log message | logMessagePrefix | `🚀` |
| Custom Suffix | The suffix of the log message | logMessageSuffix | `:` |
| Custom Log Function | Custom log function to use in the inserted log message | logFunction | `''` |
| Custom Log Function | Custom log function to use in the inserted log message | logFunction | `{}` |
| Delimiter Inside Message | The delimiter that will separate the different log message elements (file name, line number, class, function, and variable) | delimiterInsideMessage | `~` |
| Quote | Double quotes ("") or single quotes ('') | quote | `"` |
| Add Semicolon In The End | Whether to put a semicolon at the end of the log message or not by language | addSemicolonInTheEnd | `true` |
Expand Down
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,11 @@
"description": "Double quotes, single quotes or backtick"
},
"turboConsoleLog.logFunction": {
"type": "string",
"default": "",
"description": "Specify a log function"
"type": "object",
"default": {
"php": "echo"
},
"description": "Specify log function for language"
}
}
},
Expand Down Expand Up @@ -146,7 +148,7 @@
"publish": "vsce publish --no-dependencies",
"test": "vscode-test",
"test:suite": "ts-node -r tsconfig-paths/register src/test/runTests.ts",
"test:unit": "mocha -r ts-node/register -r tsconfig-paths/register 'src/test/unit/**/*.test.ts'"
"test:unit": "mocha -p src/test/tsconfig.json -r ts-node/register -r tsconfig-paths/register \"src/test/unit/**/*.test.ts\""
},
"devDependencies": {
"@types/chai": "^4.3.11",
Expand All @@ -170,7 +172,6 @@
"lodash": "^4.17.21",
"mocha": "^10.2.0",
"prettier": "^3.2.4",
"ts-mocha": "^10.0.0",
"ts-node": "^10.9.2",
"tsconfig-paths": "^4.2.0",
"typescript": "^5.3.3"
Expand Down
95 changes: 0 additions & 95 deletions pnpm-lock.yaml

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

10 changes: 5 additions & 5 deletions src/debug-message/DebugMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { Position, TextDocument, TextEditorEdit } from 'vscode';
import { closingContextLine } from '@/utils';
import { omit } from 'lodash';
import { BracketType, ExtensionProperties, Message } from '../typings';
import { LanguageProcessorUnion } from './LanguageProcessor';
import { LanguageProcessor } from './types';

// 导出抽象类DebugMessage
export abstract class DebugMessage {
// 行代码处理
languageProcessor: LanguageProcessorUnion;
languageProcessor: LanguageProcessor;
// 构造函数
constructor(languageProcessor: LanguageProcessorUnion) {
constructor(languageProcessor: LanguageProcessor) {
this.languageProcessor = languageProcessor;
}

Expand Down Expand Up @@ -67,7 +67,7 @@ export abstract class DebugMessage {
}

export class GeneralDebugMessage extends DebugMessage {
constructor(languageProcessor: LanguageProcessorUnion) {
constructor(languageProcessor: LanguageProcessor) {
super(languageProcessor);
}
private baseDebuggingMsg(
Expand Down Expand Up @@ -145,7 +145,7 @@ export class GeneralDebugMessage extends DebugMessage {
if (!logFunctionByLanguageId) {
return this.languageProcessor.getPrintStatement(content, semicolon);
}
return `${logFunctionByLanguageId}(${content})${semicolon}`;
return this.languageProcessor.getPrintStatement(content, logFunctionByLanguageId, semicolon);
}

/**
Expand Down
35 changes: 23 additions & 12 deletions src/debug-message/LanguageProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@ export class GeneralLanguageProcessor implements LanguageProcessor {

// 根据 languageId 获取对应的 logFunction
getLogFunction(logFunction: ExtensionProperties['logFunction']): string {
if (Array.isArray(logFunction)) {
const logFunctionObj = logFunction.find((obj) =>
Object.prototype.hasOwnProperty.call(obj, this.languageId),
);
const logFunctionValue = logFunctionObj ? logFunctionObj[this.languageId] : undefined;
return logFunctionValue || '';
if (typeof logFunction === 'object') {
return logFunction[this.languageId] || '';
}
return '';
}
Expand All @@ -24,7 +20,7 @@ export class GeneralLanguageProcessor implements LanguageProcessor {
case 'python':
return 'print';
case 'go':
return 'fmt.println';
return 'fmt.Println';
case 'java':
return 'System.out.println';
case 'php':
Expand Down Expand Up @@ -75,13 +71,27 @@ export class GeneralLanguageProcessor implements LanguageProcessor {
return match;
});
}
return `${printFunction} ${escapedVariableName}`;
return `${printFunction} ${escapedVariableName};`;
}
case 'perl': {
const count = (variableName.match(/\$/g) || []).length; // 统计字符串中的 $ 符号个数
let escapedVariableName = variableName;
if (count >= 2 && count % 2 === 0) {
const halfCount = count / 2;
let escapedCount = 0;
escapedVariableName = variableName.replace(/\$/g, (match) => {
if (escapedCount < halfCount) {
escapedCount++;
return '\\$';
}
return match;
});
}
return `${printFunction} ${escapedVariableName};\n`;
}
case 'ruby':
case 'shellscript':
return `${printFunction} ${variableName}`;
case 'perl':
return `${printFunction} ${variableName}\n`;
default:
return `${printFunction}(${variableName})`;
}
Expand Down Expand Up @@ -119,9 +129,10 @@ export class GeneralLanguageProcessor implements LanguageProcessor {
return ' + ';
case 'python':
case 'ruby':
case 'perl':
case 'shellscript':
return ' ';
case 'perl':
return ' . " " . ';
case 'php':
return ' . ';
default:
Expand All @@ -138,9 +149,9 @@ export class GeneralLanguageProcessor implements LanguageProcessor {
case 'csharp':
case 'python':
case 'swift':
case 'perl':
case 'ruby':
return `${variableName}`;
case 'perl':
case 'php':
if (variableName.includes('$')) {
return variableName;
Expand Down
9 changes: 8 additions & 1 deletion src/debug-message/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { ExtensionProperties } from '@/typings';

export interface LanguageProcessor {
getLogFunction(logFunction: ExtensionProperties['logFunction']): string;
getPrintString(): string;
getPrintStatement(variableName: string, semicolon?: string): string;
getPrintStatement(
variableName: string,
logFunctionByLanguageId?: string,
semicolon?: string,
): string;
getSingleLineCommentSymbol(): string;
getConcatenatedString(): string;
variableToString(variableName: string): string;
Expand Down
7 changes: 7 additions & 0 deletions src/test/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "../../tsconfig",
"compilerOptions": {
"typeRoots": ["../../node_modules/@types", "./typings"],
},
"include": ["./unit/**/*.test.ts"],
}
8 changes: 8 additions & 0 deletions src/test/typings/global/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import * as Chai from 'chai';

declare global {
interface Window {
expect: Chai.ExpectStatic;
}
const expect: Chai.ExpectStatic;
}
Empty file.
Loading

0 comments on commit 8b0ae14

Please sign in to comment.