From 5c74ce41888d1d5b8d6a5152b9363f4e08d3eee4 Mon Sep 17 00:00:00 2001 From: Quatro Designs Date: Sun, 20 Feb 2022 11:27:20 -0500 Subject: [PATCH 1/7] Added support for C# and Unity 3D debug messages. feat: Added string to extension properties called language. refactor: msg to take a language parameter. refactor: index.ts to declare debuggingMsg then craft message off of user selection. refactor: extension.ts constructor to pass in the language selection. --- package.json | 10 +++ src/debug-message/index.ts | 1 + src/debug-message/js/index.ts | 107 ++++++++++++++++++++++------ src/entities/extensionProperties.ts | 1 + src/extension.ts | 3 + 5 files changed, 99 insertions(+), 23 deletions(-) diff --git a/package.json b/package.json index a83a2ef..be816b4 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,16 @@ "type": "object", "title": "Turbo Console Log Configuration", "properties": { + "turboConsoleLog.language": { + "type": "string", + "enum": [ + "JS", + "C#", + "Unity3D" + ], + "default": "JS", + "description": "Javascript, C# (Console.WriteLine), or Unity3D (Debug.Log)" + }, "turboConsoleLog.wrapLogMessage": { "type": "boolean", "default": false, diff --git a/src/debug-message/index.ts b/src/debug-message/index.ts index 7b72012..15e9c95 100644 --- a/src/debug-message/index.ts +++ b/src/debug-message/index.ts @@ -9,6 +9,7 @@ export abstract class DebugMessage { this.lineCodeProcessing = lineCodeProcessing; } abstract msg( + language: string, textEditor: TextEditorEdit, document: TextDocument, selectedVar: string, diff --git a/src/debug-message/js/index.ts b/src/debug-message/js/index.ts index 9819e01..f4cca32 100644 --- a/src/debug-message/js/index.ts +++ b/src/debug-message/js/index.ts @@ -9,6 +9,7 @@ export class JSDebugMessage extends DebugMessage { super(lineCodeProcessing); } msg( + language: string, textEditor: TextEditorEdit, document: TextDocument, selectedVar: string, @@ -55,30 +56,90 @@ export class JSDebugMessage extends DebugMessage { ) { logMessagePrefix = `${delemiterInsideMessage} `; } - const debuggingMsg: string = `console.log(${quote}${logMessagePrefix}${ - logMessagePrefix.length !== 0 && - logMessagePrefix !== `${delemiterInsideMessage} ` - ? ` ${delemiterInsideMessage} ` - : "" - }${ - includeFileNameAndLineNum - ? `file: ${fileName} ${delemiterInsideMessage} line ${ - lineOfLogMsg + 1 - } ${delemiterInsideMessage} ` - : "" - }${ - insertEnclosingClass - ? classThatEncloseTheVar.length > 0 - ? `${classThatEncloseTheVar} ${delemiterInsideMessage} ` - : `` - : "" - }${ - insertEnclosingFunction - ? funcThatEncloseTheVar.length > 0 - ? `${funcThatEncloseTheVar} ${delemiterInsideMessage} ` + + var debuggingMsg: string = ""; + if(language == "JS") + { + debuggingMsg = `console.log(${quote}${logMessagePrefix}${ + logMessagePrefix.length !== 0 && + logMessagePrefix !== `${delemiterInsideMessage} ` + ? ` ${delemiterInsideMessage} ` + : "" + }${ + includeFileNameAndLineNum + ? `file: ${fileName} ${delemiterInsideMessage} line ${ + lineOfLogMsg + 1 + } ${delemiterInsideMessage} ` + : "" + }${ + insertEnclosingClass + ? classThatEncloseTheVar.length > 0 + ? `${classThatEncloseTheVar} ${delemiterInsideMessage} ` + : `` + : "" + }${ + insertEnclosingFunction + ? funcThatEncloseTheVar.length > 0 + ? `${funcThatEncloseTheVar} ${delemiterInsideMessage} ` + : "" + : "" + }${selectedVar}${quote}, ${selectedVar})${semicolon}`; + } + else if(language == "C#") + { + debuggingMsg = `Console.WriteLine(${quote}${logMessagePrefix}${ + logMessagePrefix.length !== 0 && + logMessagePrefix !== `${delemiterInsideMessage} ` + ? ` ${delemiterInsideMessage} ` + : "" + }${ + includeFileNameAndLineNum + ? `file: ${fileName} ${delemiterInsideMessage} line ${ + lineOfLogMsg + 1 + } ${delemiterInsideMessage} ` + : "" + }${ + insertEnclosingClass + ? classThatEncloseTheVar.length > 0 + ? `${classThatEncloseTheVar} ${delemiterInsideMessage} ` + : `` : "" - : "" - }${selectedVar}${quote}, ${selectedVar})${semicolon}`; + }${ + insertEnclosingFunction + ? funcThatEncloseTheVar.length > 0 + ? `${funcThatEncloseTheVar} ${delemiterInsideMessage} ` + : "" + : "" + }${selectedVar} {0}${quote}, ${selectedVar});`; + } + else if(language == "Unity3D") + { + debuggingMsg = `Debug.Log(${quote}${logMessagePrefix}${ + logMessagePrefix.length !== 0 && + logMessagePrefix !== `${delemiterInsideMessage} ` + ? ` ${delemiterInsideMessage} ` + : "" + }${ + includeFileNameAndLineNum + ? `file: ${fileName} ${delemiterInsideMessage} line ${ + lineOfLogMsg + 1 + } ${delemiterInsideMessage} ` + : "" + }${ + insertEnclosingClass + ? classThatEncloseTheVar.length > 0 + ? `${classThatEncloseTheVar} ${delemiterInsideMessage} ` + : `` + : "" + }${ + insertEnclosingFunction + ? funcThatEncloseTheVar.length > 0 + ? `${funcThatEncloseTheVar} ${delemiterInsideMessage} ` + : "" + : "" + }${selectedVar} : ${quote} \+ ${selectedVar});`; + } + if (wrapLogMessage) { // 16 represents the length of console.log(""); const wrappingMsg: string = `console.log(${quote}${logMessagePrefix} ${"-".repeat( diff --git a/src/entities/extensionProperties.ts b/src/entities/extensionProperties.ts index 2b16c34..8b5941d 100644 --- a/src/entities/extensionProperties.ts +++ b/src/entities/extensionProperties.ts @@ -1,4 +1,5 @@ export type ExtensionProperties = { + language: string; wrapLogMessage: boolean; logMessagePrefix: string; addSemicolonInTheEnd: boolean; diff --git a/src/extension.ts b/src/extension.ts index 343de66..c0c9002 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -35,6 +35,7 @@ export function activate(context: vscode.ExtensionContext) { selectedVar ); jsDebugMessage.msg( + properties.language, editBuilder, document, selectedVar, @@ -156,6 +157,7 @@ export function deactivate() {} function getExtensionProperties( workspaceConfig: vscode.WorkspaceConfiguration ) { + const language = workspaceConfig.language; const wrapLogMessage = workspaceConfig.wrapLogMessage || false; const logMessagePrefix = workspaceConfig.logMessagePrefix ? workspaceConfig.logMessagePrefix @@ -168,6 +170,7 @@ function getExtensionProperties( const includeFileNameAndLineNum = workspaceConfig.includeFileNameAndLineNum || false; const extensionProperties: ExtensionProperties = { + language, wrapLogMessage, logMessagePrefix, addSemicolonInTheEnd, From b27b0f6573dfcd011229b005bcc9b60fa862e176 Mon Sep 17 00:00:00 2001 From: Quatro Designs Date: Sun, 20 Feb 2022 14:36:49 -0500 Subject: [PATCH 2/7] Updated functionality to allow for comment all and delete all to function on all languages feat: Created method to create the regex for the command for the specific language. refactor: Added Parameter to detectAll to pass in the language refactor: Regex statement to catch all types of endings. --- src/debug-message/index.ts | 3 ++- src/debug-message/js/index.ts | 26 ++++++++++++++++++++++---- src/extension.ts | 9 ++++++--- 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/src/debug-message/index.ts b/src/debug-message/index.ts index 15e9c95..e9edba1 100644 --- a/src/debug-message/index.ts +++ b/src/debug-message/index.ts @@ -38,7 +38,8 @@ export abstract class DebugMessage { document: TextDocument, tabSize: number, delemiterInsideMessage: string, - quote: string + quote: string, + language: string ): Message[]; abstract enclosingBlockName( document: TextDocument, diff --git a/src/debug-message/js/index.ts b/src/debug-message/js/index.ts index f4cca32..f7381a6 100644 --- a/src/debug-message/js/index.ts +++ b/src/debug-message/js/index.ts @@ -137,7 +137,7 @@ export class JSDebugMessage extends DebugMessage { ? `${funcThatEncloseTheVar} ${delemiterInsideMessage} ` : "" : "" - }${selectedVar} : ${quote} \+ ${selectedVar});`; + }${selectedVar}${quote} \+ ${selectedVar});`; } if (wrapLogMessage) { @@ -598,17 +598,31 @@ export class JSDebugMessage extends DebugMessage { } return ""; } + getLogCommand(language: string): RegExp + { + if(language == "JS") + return /console\.log\(/; + else if(language == "C#") + return /Console\.WriteLine\(/; + else if(language == "Unity3D") + return /Debug\.Log\(/; + else + return /console\.log\(/; + } detectAll( document: TextDocument, tabSize: number, delemiterInsideMessage: string, - quote: string + quote: string, + language: string ): Message[] { const documentNbrOfLines: number = document.lineCount; const logMessages: Message[] = []; for (let i = 0; i < documentNbrOfLines; i++) { - const turboConsoleLogMessage: RegExp = /console\.log\(/; + console.log("Line ", i, " Line Contains ", document.lineAt(i).text); + const turboConsoleLogMessage: RegExp = this.getLogCommand(language); if (turboConsoleLogMessage.test(document.lineAt(i).text)) { + console.log("Line is Debug Message"); const logMessage: Message = { spaces: "", lines: [], @@ -623,16 +637,20 @@ export class JSDebugMessage extends DebugMessage { for (let j = i; j <= closedParenthesisLine; j++) { msg += document.lineAt(j).text; logMessage.lines.push(document.lineAt(j).rangeIncludingLineBreak); + console.log("Adding multiline") } if ( new RegExp( - `${delemiterInsideMessage}[a-zA-Z0-9]+${quote},(//)?[a-zA-Z0-9]+` + `${delemiterInsideMessage}{1}[a-zA-Z0-9]+[0-9{}]*${quote}(,|\\+)[a-zA-Z0-9.]+\\){1}` ).test(msg.replace(/\s/g, "")) ) { logMessages.push(logMessage); + console.log("Adding message") } } } + console.log("🚀 ~ file: index.ts ~ line 651 ~ JSDebugMessage ~ logMessages : ", logMessages.length); return logMessages; } + } diff --git a/src/extension.ts b/src/extension.ts index c0c9002..ed681a9 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -73,7 +73,8 @@ export function activate(context: vscode.ExtensionContext) { document, tabSize, properties.delimiterInsideMessage, - properties.quote + properties.quote, + properties.language ); editor.edit((editBuilder) => { logMessages.forEach(({ spaces, lines }) => { @@ -106,7 +107,8 @@ export function activate(context: vscode.ExtensionContext) { document, tabSize, properties.delimiterInsideMessage, - properties.quote + properties.quote, + properties.language ); editor.edit((editBuilder) => { logMessages.forEach(({ spaces, lines }) => { @@ -139,7 +141,8 @@ export function activate(context: vscode.ExtensionContext) { document, tabSize, properties.delimiterInsideMessage, - properties.quote + properties.quote, + properties.language ); editor.edit((editBuilder) => { logMessages.forEach(({ lines }) => { From e66b199862dffe5fb8908934aff293e36dec23ac Mon Sep 17 00:00:00 2001 From: Quatro Designs Date: Sun, 20 Feb 2022 17:33:18 -0500 Subject: [PATCH 3/7] Refactored into methods refactor: Inline language log creation into method. refactor: Added space into Unity Debug.Log message for better readability. --- src/debug-message/js/index.ts | 160 +++++++++++++++------------------- 1 file changed, 72 insertions(+), 88 deletions(-) diff --git a/src/debug-message/js/index.ts b/src/debug-message/js/index.ts index f7381a6..34ae549 100644 --- a/src/debug-message/js/index.ts +++ b/src/debug-message/js/index.ts @@ -57,88 +57,10 @@ export class JSDebugMessage extends DebugMessage { logMessagePrefix = `${delemiterInsideMessage} `; } - var debuggingMsg: string = ""; - if(language == "JS") - { - debuggingMsg = `console.log(${quote}${logMessagePrefix}${ - logMessagePrefix.length !== 0 && - logMessagePrefix !== `${delemiterInsideMessage} ` - ? ` ${delemiterInsideMessage} ` - : "" - }${ - includeFileNameAndLineNum - ? `file: ${fileName} ${delemiterInsideMessage} line ${ - lineOfLogMsg + 1 - } ${delemiterInsideMessage} ` - : "" - }${ - insertEnclosingClass - ? classThatEncloseTheVar.length > 0 - ? `${classThatEncloseTheVar} ${delemiterInsideMessage} ` - : `` - : "" - }${ - insertEnclosingFunction - ? funcThatEncloseTheVar.length > 0 - ? `${funcThatEncloseTheVar} ${delemiterInsideMessage} ` - : "" - : "" - }${selectedVar}${quote}, ${selectedVar})${semicolon}`; - } - else if(language == "C#") - { - debuggingMsg = `Console.WriteLine(${quote}${logMessagePrefix}${ - logMessagePrefix.length !== 0 && - logMessagePrefix !== `${delemiterInsideMessage} ` - ? ` ${delemiterInsideMessage} ` - : "" - }${ - includeFileNameAndLineNum - ? `file: ${fileName} ${delemiterInsideMessage} line ${ - lineOfLogMsg + 1 - } ${delemiterInsideMessage} ` - : "" - }${ - insertEnclosingClass - ? classThatEncloseTheVar.length > 0 - ? `${classThatEncloseTheVar} ${delemiterInsideMessage} ` - : `` - : "" - }${ - insertEnclosingFunction - ? funcThatEncloseTheVar.length > 0 - ? `${funcThatEncloseTheVar} ${delemiterInsideMessage} ` - : "" - : "" - }${selectedVar} {0}${quote}, ${selectedVar});`; - } - else if(language == "Unity3D") - { - debuggingMsg = `Debug.Log(${quote}${logMessagePrefix}${ - logMessagePrefix.length !== 0 && - logMessagePrefix !== `${delemiterInsideMessage} ` - ? ` ${delemiterInsideMessage} ` - : "" - }${ - includeFileNameAndLineNum - ? `file: ${fileName} ${delemiterInsideMessage} line ${ - lineOfLogMsg + 1 - } ${delemiterInsideMessage} ` - : "" - }${ - insertEnclosingClass - ? classThatEncloseTheVar.length > 0 - ? `${classThatEncloseTheVar} ${delemiterInsideMessage} ` - : `` - : "" - }${ - insertEnclosingFunction - ? funcThatEncloseTheVar.length > 0 - ? `${funcThatEncloseTheVar} ${delemiterInsideMessage} ` - : "" - : "" - }${selectedVar}${quote} \+ ${selectedVar});`; - } + const debuggingMsg: string = this.getDebugMessage(language, quote, logMessagePrefix, + delemiterInsideMessage, includeFileNameAndLineNum, fileName, + lineOfLogMsg, insertEnclosingClass, classThatEncloseTheVar, + insertEnclosingFunction, funcThatEncloseTheVar, selectedVar, semicolon); if (wrapLogMessage) { // 16 represents the length of console.log(""); @@ -619,10 +541,8 @@ export class JSDebugMessage extends DebugMessage { const documentNbrOfLines: number = document.lineCount; const logMessages: Message[] = []; for (let i = 0; i < documentNbrOfLines; i++) { - console.log("Line ", i, " Line Contains ", document.lineAt(i).text); const turboConsoleLogMessage: RegExp = this.getLogCommand(language); if (turboConsoleLogMessage.test(document.lineAt(i).text)) { - console.log("Line is Debug Message"); const logMessage: Message = { spaces: "", lines: [], @@ -637,7 +557,6 @@ export class JSDebugMessage extends DebugMessage { for (let j = i; j <= closedParenthesisLine; j++) { msg += document.lineAt(j).text; logMessage.lines.push(document.lineAt(j).rangeIncludingLineBreak); - console.log("Adding multiline") } if ( new RegExp( @@ -645,12 +564,77 @@ export class JSDebugMessage extends DebugMessage { ).test(msg.replace(/\s/g, "")) ) { logMessages.push(logMessage); - console.log("Adding message") } } } - console.log("🚀 ~ file: index.ts ~ line 651 ~ JSDebugMessage ~ logMessages : ", logMessages.length); return logMessages; } - + getDebugMessage( + language: string, + quote: string, + logMessagePrefix: string, + delemiterInsideMessage: string, + includeFileNameAndLineNum: boolean, + fileName: string, + lineOfLogMsg: number, + insertEnclosingClass: boolean, + classThatEncloseTheVar: string, + insertEnclosingFunction: boolean, + funcThatEncloseTheVar: string, + selectedVar: string, + semicolon: string): string + { + var debuggingMsg: string = ""; + if (language == "JS") { + debuggingMsg = `console.log(${quote}${logMessagePrefix}${logMessagePrefix.length !== 0 && + logMessagePrefix !== `${delemiterInsideMessage} ` + ? ` ${delemiterInsideMessage} ` + : ""}${includeFileNameAndLineNum + ? `file: ${fileName} ${delemiterInsideMessage} line ${lineOfLogMsg + 1} ${delemiterInsideMessage} ` + : ""}${insertEnclosingClass + ? classThatEncloseTheVar.length > 0 + ? `${classThatEncloseTheVar} ${delemiterInsideMessage} ` + : `` + : ""}${insertEnclosingFunction + ? funcThatEncloseTheVar.length > 0 + ? `${funcThatEncloseTheVar} ${delemiterInsideMessage} ` + : "" + : ""}${selectedVar}${quote}, ${selectedVar})${semicolon}`; + } + else if (language == "C#") { + debuggingMsg = `Console.WriteLine(${quote}${logMessagePrefix}${logMessagePrefix.length !== 0 && + logMessagePrefix !== `${delemiterInsideMessage} ` + ? ` ${delemiterInsideMessage} ` + : ""}${includeFileNameAndLineNum + ? `file: ${fileName} ${delemiterInsideMessage} line ${lineOfLogMsg + 1} ${delemiterInsideMessage} ` + : ""}${insertEnclosingClass + ? classThatEncloseTheVar.length > 0 + ? `${classThatEncloseTheVar} ${delemiterInsideMessage} ` + : `` + : ""}${insertEnclosingFunction + ? funcThatEncloseTheVar.length > 0 + ? `${funcThatEncloseTheVar} ${delemiterInsideMessage} ` + : "" + : ""}${selectedVar} {0}${quote}, ${selectedVar});`; + } + else if (language == "Unity3D") { + debuggingMsg = `Debug.Log(${quote}${logMessagePrefix}${logMessagePrefix.length !== 0 && + logMessagePrefix !== `${delemiterInsideMessage} ` + ? ` ${delemiterInsideMessage} ` + : ""}${includeFileNameAndLineNum + ? `file: ${fileName} ${delemiterInsideMessage} line ${lineOfLogMsg + 1} ${delemiterInsideMessage} ` + : ""}${insertEnclosingClass + ? classThatEncloseTheVar.length > 0 + ? `${classThatEncloseTheVar} ${delemiterInsideMessage} ` + : `` + : ""}${insertEnclosingFunction + ? funcThatEncloseTheVar.length > 0 + ? `${funcThatEncloseTheVar} ${delemiterInsideMessage} ` + : "" + : ""}${selectedVar} ${quote} \+ ${selectedVar});`; + } + return debuggingMsg; + } } + + From 5ce26d5d72016aa6f4127c1bb1bdac4709a3b4f6 Mon Sep 17 00:00:00 2001 From: Quatro Designs Date: Tue, 22 Feb 2022 16:35:49 -0500 Subject: [PATCH 4/7] Refactored code duplication in debug message creation. refactor: getDebugMessage into getCommand for the intro and getProlog for the exit, removing the duplicated center section of the code. refactor: Created getCommand method, that returns the proper Debug command for the language. refactor: Created getProlog method. Returns the ending for the language, always including semi-colons for C# variants --- src/debug-message/js/index.ts | 59 ++++++++++++++++++++++++++++++++--- 1 file changed, 55 insertions(+), 4 deletions(-) diff --git a/src/debug-message/js/index.ts b/src/debug-message/js/index.ts index 34ae549..a8caae0 100644 --- a/src/debug-message/js/index.ts +++ b/src/debug-message/js/index.ts @@ -57,10 +57,30 @@ export class JSDebugMessage extends DebugMessage { logMessagePrefix = `${delemiterInsideMessage} `; } - const debuggingMsg: string = this.getDebugMessage(language, quote, logMessagePrefix, - delemiterInsideMessage, includeFileNameAndLineNum, fileName, - lineOfLogMsg, insertEnclosingClass, classThatEncloseTheVar, - insertEnclosingFunction, funcThatEncloseTheVar, selectedVar, semicolon); + const debuggingMsg: string = this.getCommand(language) + `${quote}${logMessagePrefix}${ + logMessagePrefix.length !== 0 && + logMessagePrefix !== `${delemiterInsideMessage} ` + ? ` ${delemiterInsideMessage} ` + : "" + }${ + includeFileNameAndLineNum + ? `file: ${fileName} ${delemiterInsideMessage} line ${ + lineOfLogMsg + 1 + } ${delemiterInsideMessage} ` + : "" + }${ + insertEnclosingClass + ? classThatEncloseTheVar.length > 0 + ? `${classThatEncloseTheVar} ${delemiterInsideMessage} ` + : `` + : "" + }${ + insertEnclosingFunction + ? funcThatEncloseTheVar.length > 0 + ? `${funcThatEncloseTheVar} ${delemiterInsideMessage} ` + : "" + : "" + }` + this.getProlog(language, quote, selectedVar, semicolon); if (wrapLogMessage) { // 16 represents the length of console.log(""); @@ -569,6 +589,37 @@ export class JSDebugMessage extends DebugMessage { } return logMessages; } + + getCommand(language: string): string + { + var debuggingMsg: string = ""; + if (language == "JS") { + debuggingMsg = "console.log("; + } + else if (language == "C#") { + debuggingMsg = "Console.WriteLine("; + } + else if (language == "Unity3D") { + debuggingMsg = "Debug.Log("; + } + return debuggingMsg; + } + + getProlog(language: string, quote: string, selectedVar: string, semicolon: string): string + { + var prolog : string = ""; + if (language == "JS") { + prolog = ` ${selectedVar}${quote}, ${selectedVar})${semicolon}`; + } + else if (language == "C#") { + prolog = ` ${selectedVar} {0}${quote}, ${selectedVar});`; + } + else if (language == "Unity3D") { + prolog = ` ${selectedVar} ${quote} \+ ${selectedVar});`; + } + return prolog; + } + getDebugMessage( language: string, quote: string, From 9565217ba08f557f6cba580ced7adcf9d26681c3 Mon Sep 17 00:00:00 2001 From: George Rowland Date: Fri, 11 Mar 2022 08:09:08 -0500 Subject: [PATCH 5/7] Refactored Other Logs into Extension classes feat: CSUnity DebugMessage Class. feat: CS Console.WriteLine DebugMessage Class. feat: Updated Extension.js to utilize new debugging messages. Uses contra/contra variance to call base methods on the DebugMessage variant. refactor: Removed Language property from the extension and get language from editor. feat: Added Line code processing extension class for C#. --- src/debug-message/cs/index.ts | 571 +++++++++++++++++++++++++++ src/debug-message/csunity/index.ts | 571 +++++++++++++++++++++++++++ src/debug-message/index.ts | 2 - src/debug-message/js/index.ts | 135 +------ src/entities/extensionProperties.ts | 2 +- src/extension.ts | 89 ++++- src/line-code-processing/cs/index.ts | 79 ++++ 7 files changed, 1304 insertions(+), 145 deletions(-) create mode 100644 src/debug-message/cs/index.ts create mode 100644 src/debug-message/csunity/index.ts create mode 100644 src/line-code-processing/cs/index.ts diff --git a/src/debug-message/cs/index.ts b/src/debug-message/cs/index.ts new file mode 100644 index 0000000..12551ad --- /dev/null +++ b/src/debug-message/cs/index.ts @@ -0,0 +1,571 @@ +import * as vscode from "vscode"; +import { TextDocument, TextEditorEdit, TextLine } from "vscode"; +import { DebugMessage } from ".."; +import { BlockType, LocElement, Message } from "../../entities"; +import { LineCodeProcessing } from "../../line-code-processing"; + +export class CSDebugMessage extends DebugMessage { + constructor(lineCodeProcessing: LineCodeProcessing) { + super(lineCodeProcessing); + } + msg( + textEditor: TextEditorEdit, + document: TextDocument, + selectedVar: string, + lineOfSelectedVar: number, + wrapLogMessage: boolean, + logMessagePrefix: string, + quote: string, + addSemicolonInTheEnd: boolean, + insertEnclosingClass: boolean, + insertEnclosingFunction: boolean, + delemiterInsideMessage: string, + includeFileNameAndLineNum: boolean, + tabSize: number + ): void { + const classThatEncloseTheVar: string = this.enclosingBlockName( + document, + lineOfSelectedVar, + "class" + ); + const funcThatEncloseTheVar: string = this.enclosingBlockName( + document, + lineOfSelectedVar, + "function" + ); + const lineOfLogMsg: number = this.line( + document, + lineOfSelectedVar, + selectedVar + ); + const spacesBeforeMsg: string = this.spacesBefore( + document, + lineOfSelectedVar, + tabSize + ); + const semicolon: string = addSemicolonInTheEnd ? ";" : ""; + const fileName = document.fileName.includes("/") + ? document.fileName.split("/")[document.fileName.split("/").length - 1] + : document.fileName.split("\\")[document.fileName.split("\\").length - 1]; + if ( + !includeFileNameAndLineNum && + !insertEnclosingFunction && + !insertEnclosingClass && + logMessagePrefix.length === 0 + ) { + logMessagePrefix = `${delemiterInsideMessage} `; + } + + const debuggingMsg: string = `Console.WriteLine(${quote}${logMessagePrefix}${logMessagePrefix.length !== 0 && + logMessagePrefix !== `${delemiterInsideMessage} ` + ? ` ${delemiterInsideMessage} ` + : ""}${includeFileNameAndLineNum + ? `file: ${fileName} ${delemiterInsideMessage} line ${lineOfLogMsg + 1} ${delemiterInsideMessage} ` + : ""}${insertEnclosingClass + ? classThatEncloseTheVar.length > 0 + ? `${classThatEncloseTheVar} ${delemiterInsideMessage} ` + : `` + : ""}${insertEnclosingFunction + ? funcThatEncloseTheVar.length > 0 + ? `${funcThatEncloseTheVar} ${delemiterInsideMessage} ` + : "" + : ""}{0}${quote}, ${selectedVar});`; + + if (wrapLogMessage) { + // 21 represents the length of console.log(""); + const wrappingMsg: string = `Console.WriteLine(${quote}${logMessagePrefix} ${"-".repeat( + debuggingMsg.length - 21 + )}${quote})${semicolon}`; + textEditor.insert( + new vscode.Position( + lineOfLogMsg >= document.lineCount + ? document.lineCount + : lineOfLogMsg, + 0 + ), + `${ + lineOfLogMsg === document.lineCount ? "\n" : "" + }${spacesBeforeMsg}${wrappingMsg}\n${spacesBeforeMsg}${debuggingMsg}\n${spacesBeforeMsg}${wrappingMsg}\n` + ); + } + const previousMsgLogLine = document.lineAt(lineOfLogMsg - 1); + if (/\){.*}/.test(previousMsgLogLine.text.replace(/\s/g, ""))) { + const textBeforeClosedFunctionParenthesis = + previousMsgLogLine.text.split(")")[0]; + textEditor.delete(previousMsgLogLine.rangeIncludingLineBreak); + textEditor.insert( + new vscode.Position( + lineOfLogMsg >= document.lineCount + ? document.lineCount + : lineOfLogMsg, + 0 + ), + `${textBeforeClosedFunctionParenthesis}){\n${ + lineOfLogMsg === document.lineCount ? "\n" : "" + }${spacesBeforeMsg}${debuggingMsg}\n${spacesBeforeMsg}}\n` + ); + } else { + textEditor.insert( + new vscode.Position( + lineOfLogMsg >= document.lineCount + ? document.lineCount + : lineOfLogMsg, + 0 + ), + `${ + lineOfLogMsg === document.lineCount ? "\n" : "" + }${spacesBeforeMsg}${debuggingMsg}\n` + ); + } + } + line( + document: TextDocument, + selectionLine: number, + selectedVar: string + ): number { + if (selectionLine === document.lineCount - 1) { + return selectionLine; + } + const multilineParenthesisVariableLine = this.getMultiLineVariableLine( + document, + selectionLine, + LocElement.Parenthesis + ); + const multilineBracesVariableLine = this.getMultiLineVariableLine( + document, + selectionLine, + LocElement.Braces + ); + let currentLineText: string = document.lineAt(selectionLine).text; + let nextLineText: string = document + .lineAt(selectionLine + 1) + .text.replace(/\s/g, ""); + if ( + this.lineCodeProcessing.isObjectLiteralAssignedToVariable( + `${currentLineText}\n${nextLineText}` + ) + ) { + return this.objectLiteralLine(document, selectionLine); + } else if ( + this.lineCodeProcessing.isFunctionAssignedToVariable(`${currentLineText}`) + ) { + if (currentLineText.split("=")[0].includes(selectedVar)) { + return this.functionAssignmentLine(document, selectionLine); + } else { + return this.functionOpenedBraceLine(document, selectionLine) + 1; + } + } else if ( + this.lineCodeProcessing.isObjectFunctionCall( + `${currentLineText}\n${nextLineText}` + ) + ) { + return this.objectFunctionCallLine(document, selectionLine, selectedVar); + } else if ( + this.lineCodeProcessing.isArrayAssignedToVariable( + `${currentLineText}\n${currentLineText}` + ) + ) { + return this.arrayLine(document, selectionLine); + } else if ( + this.lineCodeProcessing.isValueAssignedToVariable( + `${currentLineText}\n${currentLineText}` + ) + ) { + return multilineParenthesisVariableLine !== null && + this.lineText(document, multilineParenthesisVariableLine - 1).includes( + "{" + ) + ? multilineParenthesisVariableLine + : selectionLine + 1; + } else if (this.lineCodeProcessing.isFunctionDeclaration(currentLineText)) { + if ( + multilineParenthesisVariableLine !== null && + this.lineText(document, multilineParenthesisVariableLine - 1).includes( + "{" + ) + ) { + return multilineParenthesisVariableLine; + } else { + const lineOfOpenedBrace = this.functionOpenedBraceLine( + document, + selectionLine + ); + if (lineOfOpenedBrace !== -1) { + return lineOfOpenedBrace + 1; + } + } + } else if (/`/.test(currentLineText)) { + return this.templateStringLine(document, selectionLine); + } else if ( + multilineParenthesisVariableLine !== null && + this.lineText(document, multilineParenthesisVariableLine - 1).includes( + "{" + ) + ) { + return multilineParenthesisVariableLine; + } else if (multilineBracesVariableLine !== null) { + return multilineBracesVariableLine; + } else if (currentLineText.trim().startsWith("return")) { + return selectionLine; + } + return selectionLine + 1; + } + private objectLiteralLine( + document: TextDocument, + selectionLine: number + ): number { + let currentLineText: string = document.lineAt(selectionLine).text; + let nbrOfOpenedBrackets: number = (currentLineText.match(/{/g) || []) + .length; + let nbrOfClosedBrackets: number = (currentLineText.match(/}/g) || []) + .length; + let currentLineNum: number = selectionLine + 1; + while (currentLineNum < document.lineCount) { + const currentLineText: string = document.lineAt(currentLineNum).text; + nbrOfOpenedBrackets += (currentLineText.match(/{/g) || []).length; + nbrOfClosedBrackets += (currentLineText.match(/}/g) || []).length; + currentLineNum++; + if (nbrOfOpenedBrackets === nbrOfClosedBrackets) { + break; + } + } + return nbrOfClosedBrackets === nbrOfOpenedBrackets + ? currentLineNum + : selectionLine + 1; + } + private objectFunctionCallLine( + document: TextDocument, + selectionLine: number, + selectedVar: string + ): number { + let currentLineText: string = document.lineAt(selectionLine).text; + let nextLineText: string = document + .lineAt(selectionLine + 1) + .text.replace(/\s/g, ""); + if ( + /\((\s*)$/.test(currentLineText.split(selectedVar)[0]) || + /,(\s*)$/.test(currentLineText.split(selectedVar)[0]) + ) { + return selectionLine + 1; + } + let totalOpenedParenthesis = 0; + let totalClosedParenthesis = 0; + const { openedElementOccurrences, closedElementOccurrences } = + this.locOpenedClosedElementOccurrences( + currentLineText, + LocElement.Parenthesis + ); + totalOpenedParenthesis += openedElementOccurrences; + totalClosedParenthesis += closedElementOccurrences; + let currentLineNum = selectionLine + 1; + if ( + totalOpenedParenthesis !== totalClosedParenthesis || + currentLineText.endsWith(".") || + nextLineText.trim().startsWith(".") + ) { + while (currentLineNum < document.lineCount) { + currentLineText = document.lineAt(currentLineNum).text; + const { openedElementOccurrences, closedElementOccurrences } = + this.locOpenedClosedElementOccurrences( + currentLineText, + LocElement.Parenthesis + ); + totalOpenedParenthesis += openedElementOccurrences; + totalClosedParenthesis += closedElementOccurrences; + if (currentLineNum === document.lineCount - 1) { + break; + } + nextLineText = document.lineAt(currentLineNum + 1).text; + currentLineNum++; + if ( + totalOpenedParenthesis === totalClosedParenthesis && + !currentLineText.endsWith(".") && + !nextLineText.trim().startsWith(".") + ) { + break; + } + } + } + return totalOpenedParenthesis === totalClosedParenthesis + ? currentLineNum + : selectionLine + 1; + } + private functionAssignmentLine( + document: TextDocument, + selectionLine: number + ): number { + const currentLineText = document.lineAt(selectionLine).text; + if (/{/.test(currentLineText)) { + return ( + this.closingElementLine(document, selectionLine, LocElement.Braces) + 1 + ); + } else { + const closedParenthesisLine = this.closingElementLine( + document, + selectionLine, + LocElement.Parenthesis + ); + return ( + this.closingElementLine( + document, + closedParenthesisLine, + LocElement.Braces + ) + 1 + ); + } + } + private templateStringLine( + document: TextDocument, + selectionLine: number + ): number { + let currentLineText: string = document.lineAt(selectionLine).text; + let currentLineNum: number = selectionLine + 1; + let nbrOfBackticks: number = (currentLineText.match(/`/g) || []).length; + while (currentLineNum < document.lineCount) { + const currentLineText: string = document.lineAt(currentLineNum).text; + nbrOfBackticks += (currentLineText.match(/`/g) || []).length; + if (nbrOfBackticks % 2 === 0) { + break; + } + currentLineNum++; + } + return nbrOfBackticks % 2 === 0 ? currentLineNum + 1 : selectionLine + 1; + } + private arrayLine(document: TextDocument, selectionLine: number): number { + let currentLineText: string = document.lineAt(selectionLine).text; + let nbrOfOpenedBrackets: number = (currentLineText.match(/\[/g) || []) + .length; + let nbrOfClosedBrackets: number = (currentLineText.match(/\]/g) || []) + .length; + let currentLineNum: number = selectionLine + 1; + if (nbrOfOpenedBrackets !== nbrOfClosedBrackets) { + while (currentLineNum < document.lineCount) { + const currentLineText: string = document.lineAt(currentLineNum).text; + nbrOfOpenedBrackets += (currentLineText.match(/\[/g) || []).length; + nbrOfClosedBrackets += (currentLineText.match(/\]/g) || []).length; + currentLineNum++; + if (nbrOfOpenedBrackets === nbrOfClosedBrackets) { + break; + } + } + } + return nbrOfOpenedBrackets === nbrOfClosedBrackets + ? currentLineNum + : selectionLine + 1; + } + // Line for a variable which is in multiline context (function paramter, or deconstructred object) + private getMultiLineVariableLine( + document: TextDocument, + lineNum: number, + blockType: LocElement + ): number | null { + let currentLineNum = lineNum - 1; + let nbrOfOpenedBlockType: number = 0; + let nbrOfClosedBlockType: number = 1; // Closing parenthesis + while (currentLineNum >= 0) { + const currentLineText: string = document.lineAt(currentLineNum).text; + const currentLineParenthesis = this.locOpenedClosedElementOccurrences( + currentLineText, + blockType + ); + nbrOfOpenedBlockType += currentLineParenthesis.openedElementOccurrences; + nbrOfClosedBlockType += currentLineParenthesis.closedElementOccurrences; + if (nbrOfOpenedBlockType === nbrOfClosedBlockType) { + return this.closingElementLine(document, currentLineNum, blockType) + 1; + } + currentLineNum--; + } + return null; + } + private functionOpenedBraceLine(docuemt: TextDocument, line: number) { + let nbrOfOpenedBraces = 0; + let nbrOfClosedBraces = 0; + while (line < docuemt.lineCount) { + const { openedElementOccurrences, closedElementOccurrences } = + this.locOpenedClosedElementOccurrences( + this.lineText(docuemt, line), + LocElement.Braces + ); + nbrOfOpenedBraces += openedElementOccurrences; + nbrOfClosedBraces += closedElementOccurrences; + if ( + nbrOfOpenedBraces - nbrOfClosedBraces === 1 || + nbrOfOpenedBraces - nbrOfClosedBraces === 0 + ) { + return line; + } + line++; + } + return -1; + } + spacesBefore(document: TextDocument, line: number, tabSize: number): string { + const currentLine: TextLine = document.lineAt(line); + const currentLineTextChars: string[] = currentLine.text.split(""); + if ( + (!this.lineCodeProcessing.isFunctionAssignedToVariable( + currentLine.text + ) && + this.lineCodeProcessing.doesContainsNamedFunctionDeclaration( + currentLine.text + )) || + this.lineCodeProcessing.doesContainsBuiltInFunction(currentLine.text) || + this.lineCodeProcessing.doesContainClassDeclaration(currentLine.text) + ) { + const nextLine: TextLine = document.lineAt(line + 1); + const nextLineTextChars: string[] = nextLine.text.split(""); + if (nextLineTextChars.filter((char) => char !== " ").length !== 0) { + if ( + nextLine.firstNonWhitespaceCharacterIndex > + currentLine.firstNonWhitespaceCharacterIndex + ) { + if ( + nextLineTextChars[nextLine.firstNonWhitespaceCharacterIndex - 1] === + "\t" + ) { + return " ".repeat( + nextLine.firstNonWhitespaceCharacterIndex * tabSize + ); + } else { + return " ".repeat(nextLine.firstNonWhitespaceCharacterIndex); + } + } else { + if ( + currentLineTextChars[ + currentLine.firstNonWhitespaceCharacterIndex - 1 + ] === "\t" + ) { + return " ".repeat( + currentLine.firstNonWhitespaceCharacterIndex * tabSize + ); + } else { + return " ".repeat(currentLine.firstNonWhitespaceCharacterIndex); + } + } + } else { + if ( + currentLineTextChars[ + currentLine.firstNonWhitespaceCharacterIndex - 1 + ] === "\t" + ) { + return " ".repeat( + currentLine.firstNonWhitespaceCharacterIndex * tabSize + ); + } else { + return " ".repeat(currentLine.firstNonWhitespaceCharacterIndex); + } + } + } else { + if ( + currentLineTextChars[ + currentLine.firstNonWhitespaceCharacterIndex - 1 + ] === "\t" + ) { + return " ".repeat( + currentLine.firstNonWhitespaceCharacterIndex * tabSize + ); + } else { + return " ".repeat(currentLine.firstNonWhitespaceCharacterIndex); + } + } + } + enclosingBlockName( + document: TextDocument, + lineOfSelectedVar: number, + blockType: BlockType + ): string { + let currentLineNum: number = lineOfSelectedVar; + while (currentLineNum >= 0) { + const currentLineText: string = document.lineAt(currentLineNum).text; + switch (blockType) { + case "class": + if ( + this.lineCodeProcessing.doesContainClassDeclaration(currentLineText) + ) { + if ( + lineOfSelectedVar > currentLineNum && + lineOfSelectedVar < + this.closingElementLine( + document, + currentLineNum, + LocElement.Braces + ) + ) { + return `${this.lineCodeProcessing.getClassName(currentLineText)}`; + } + } + break; + case "function": + if ( + this.lineCodeProcessing.doesContainsNamedFunctionDeclaration( + currentLineText + ) && + !this.lineCodeProcessing.doesContainsBuiltInFunction( + currentLineText + ) + ) { + if ( + lineOfSelectedVar >= currentLineNum && + lineOfSelectedVar < + this.closingElementLine( + document, + currentLineNum, + LocElement.Braces + ) + ) { + if ( + this.lineCodeProcessing.getFunctionName(currentLineText) + .length !== 0 + ) { + return `${this.lineCodeProcessing.getFunctionName( + currentLineText + )}`; + } + return ""; + } + } + break; + } + currentLineNum--; + } + return ""; + } + detectAll( + document: TextDocument, + tabSize: number, + delemiterInsideMessage: string, + quote: string, + ): Message[] { + const documentNbrOfLines: number = document.lineCount; + const logMessages: Message[] = []; + for (let i = 0; i < documentNbrOfLines; i++) { + const turboConsoleLogMessage: RegExp = /Console\.WriteLine\(/; + if (turboConsoleLogMessage.test(document.lineAt(i).text)) { + const logMessage: Message = { + spaces: "", + lines: [], + }; + logMessage.spaces = this.spacesBefore(document, i, tabSize); + const closedParenthesisLine = this.closingElementLine( + document, + i, + LocElement.Parenthesis + ); + let msg = ""; + for (let j = i; j <= closedParenthesisLine; j++) { + msg += document.lineAt(j).text; + logMessage.lines.push(document.lineAt(j).rangeIncludingLineBreak); + } + if ( + new RegExp( + `${delemiterInsideMessage}{1}[a-zA-Z0-9]+[0-9{}]*${quote}(,|\\+)[a-zA-Z0-9.]+\\){1}` + ).test(msg.replace(/\s/g, "")) + ) { + logMessages.push(logMessage); + } + } + } + return logMessages; + } +} + + diff --git a/src/debug-message/csunity/index.ts b/src/debug-message/csunity/index.ts new file mode 100644 index 0000000..a8fd496 --- /dev/null +++ b/src/debug-message/csunity/index.ts @@ -0,0 +1,571 @@ +import * as vscode from "vscode"; +import { TextDocument, TextEditorEdit, TextLine } from "vscode"; +import { DebugMessage } from ".."; +import { BlockType, LocElement, Message } from "../../entities"; +import { LineCodeProcessing } from "../../line-code-processing"; + +export class CSUnityDebugMessage extends DebugMessage { + constructor(lineCodeProcessing: LineCodeProcessing) { + super(lineCodeProcessing); + } + msg( + textEditor: TextEditorEdit, + document: TextDocument, + selectedVar: string, + lineOfSelectedVar: number, + wrapLogMessage: boolean, + logMessagePrefix: string, + quote: string, + addSemicolonInTheEnd: boolean, + insertEnclosingClass: boolean, + insertEnclosingFunction: boolean, + delemiterInsideMessage: string, + includeFileNameAndLineNum: boolean, + tabSize: number + ): void { + const classThatEncloseTheVar: string = this.enclosingBlockName( + document, + lineOfSelectedVar, + "class" + ); + const funcThatEncloseTheVar: string = this.enclosingBlockName( + document, + lineOfSelectedVar, + "function" + ); + const lineOfLogMsg: number = this.line( + document, + lineOfSelectedVar, + selectedVar + ); + const spacesBeforeMsg: string = this.spacesBefore( + document, + lineOfSelectedVar, + tabSize + ); + const semicolon: string = addSemicolonInTheEnd ? ";" : ""; + const fileName = document.fileName.includes("/") + ? document.fileName.split("/")[document.fileName.split("/").length - 1] + : document.fileName.split("\\")[document.fileName.split("\\").length - 1]; + if ( + !includeFileNameAndLineNum && + !insertEnclosingFunction && + !insertEnclosingClass && + logMessagePrefix.length === 0 + ) { + logMessagePrefix = `${delemiterInsideMessage} `; + } + + const debuggingMsg: string = `Debug.Log(${quote}${logMessagePrefix}${logMessagePrefix.length !== 0 && + logMessagePrefix !== `${delemiterInsideMessage} ` + ? ` ${delemiterInsideMessage} ` + : ""}${includeFileNameAndLineNum + ? `file: ${fileName} ${delemiterInsideMessage} line ${lineOfLogMsg + 1} ${delemiterInsideMessage} ` + : ""}${insertEnclosingClass + ? classThatEncloseTheVar.length > 0 + ? `${classThatEncloseTheVar} ${delemiterInsideMessage} ` + : `` + : ""}${insertEnclosingFunction + ? funcThatEncloseTheVar.length > 0 + ? `${funcThatEncloseTheVar} ${delemiterInsideMessage} ` + : "" + : ""} ${quote} + ${selectedVar});`; + + if (wrapLogMessage) { + // 21 represents the length of console.log(""); + const wrappingMsg: string = `Debug.Log(${quote}${logMessagePrefix} ${"-".repeat( + debuggingMsg.length - 21 + )}${quote})${semicolon}`; + textEditor.insert( + new vscode.Position( + lineOfLogMsg >= document.lineCount + ? document.lineCount + : lineOfLogMsg, + 0 + ), + `${ + lineOfLogMsg === document.lineCount ? "\n" : "" + }${spacesBeforeMsg}${wrappingMsg}\n${spacesBeforeMsg}${debuggingMsg}\n${spacesBeforeMsg}${wrappingMsg}\n` + ); + } + const previousMsgLogLine = document.lineAt(lineOfLogMsg - 1); + if (/\){.*}/.test(previousMsgLogLine.text.replace(/\s/g, ""))) { + const textBeforeClosedFunctionParenthesis = + previousMsgLogLine.text.split(")")[0]; + textEditor.delete(previousMsgLogLine.rangeIncludingLineBreak); + textEditor.insert( + new vscode.Position( + lineOfLogMsg >= document.lineCount + ? document.lineCount + : lineOfLogMsg, + 0 + ), + `${textBeforeClosedFunctionParenthesis}){\n${ + lineOfLogMsg === document.lineCount ? "\n" : "" + }${spacesBeforeMsg}${debuggingMsg}\n${spacesBeforeMsg}}\n` + ); + } else { + textEditor.insert( + new vscode.Position( + lineOfLogMsg >= document.lineCount + ? document.lineCount + : lineOfLogMsg, + 0 + ), + `${ + lineOfLogMsg === document.lineCount ? "\n" : "" + }${spacesBeforeMsg}${debuggingMsg}\n` + ); + } + } + line( + document: TextDocument, + selectionLine: number, + selectedVar: string + ): number { + if (selectionLine === document.lineCount - 1) { + return selectionLine; + } + const multilineParenthesisVariableLine = this.getMultiLineVariableLine( + document, + selectionLine, + LocElement.Parenthesis + ); + const multilineBracesVariableLine = this.getMultiLineVariableLine( + document, + selectionLine, + LocElement.Braces + ); + let currentLineText: string = document.lineAt(selectionLine).text; + let nextLineText: string = document + .lineAt(selectionLine + 1) + .text.replace(/\s/g, ""); + if ( + this.lineCodeProcessing.isObjectLiteralAssignedToVariable( + `${currentLineText}\n${nextLineText}` + ) + ) { + return this.objectLiteralLine(document, selectionLine); + } else if ( + this.lineCodeProcessing.isFunctionAssignedToVariable(`${currentLineText}`) + ) { + if (currentLineText.split("=")[0].includes(selectedVar)) { + return this.functionAssignmentLine(document, selectionLine); + } else { + return this.functionOpenedBraceLine(document, selectionLine) + 1; + } + } else if ( + this.lineCodeProcessing.isObjectFunctionCall( + `${currentLineText}\n${nextLineText}` + ) + ) { + return this.objectFunctionCallLine(document, selectionLine, selectedVar); + } else if ( + this.lineCodeProcessing.isArrayAssignedToVariable( + `${currentLineText}\n${currentLineText}` + ) + ) { + return this.arrayLine(document, selectionLine); + } else if ( + this.lineCodeProcessing.isValueAssignedToVariable( + `${currentLineText}\n${currentLineText}` + ) + ) { + return multilineParenthesisVariableLine !== null && + this.lineText(document, multilineParenthesisVariableLine - 1).includes( + "{" + ) + ? multilineParenthesisVariableLine + : selectionLine + 1; + } else if (this.lineCodeProcessing.isFunctionDeclaration(currentLineText)) { + if ( + multilineParenthesisVariableLine !== null && + this.lineText(document, multilineParenthesisVariableLine - 1).includes( + "{" + ) + ) { + return multilineParenthesisVariableLine; + } else { + const lineOfOpenedBrace = this.functionOpenedBraceLine( + document, + selectionLine + ); + if (lineOfOpenedBrace !== -1) { + return lineOfOpenedBrace + 1; + } + } + } else if (/`/.test(currentLineText)) { + return this.templateStringLine(document, selectionLine); + } else if ( + multilineParenthesisVariableLine !== null && + this.lineText(document, multilineParenthesisVariableLine - 1).includes( + "{" + ) + ) { + return multilineParenthesisVariableLine; + } else if (multilineBracesVariableLine !== null) { + return multilineBracesVariableLine; + } else if (currentLineText.trim().startsWith("return")) { + return selectionLine; + } + return selectionLine + 1; + } + private objectLiteralLine( + document: TextDocument, + selectionLine: number + ): number { + let currentLineText: string = document.lineAt(selectionLine).text; + let nbrOfOpenedBrackets: number = (currentLineText.match(/{/g) || []) + .length; + let nbrOfClosedBrackets: number = (currentLineText.match(/}/g) || []) + .length; + let currentLineNum: number = selectionLine + 1; + while (currentLineNum < document.lineCount) { + const currentLineText: string = document.lineAt(currentLineNum).text; + nbrOfOpenedBrackets += (currentLineText.match(/{/g) || []).length; + nbrOfClosedBrackets += (currentLineText.match(/}/g) || []).length; + currentLineNum++; + if (nbrOfOpenedBrackets === nbrOfClosedBrackets) { + break; + } + } + return nbrOfClosedBrackets === nbrOfOpenedBrackets + ? currentLineNum + : selectionLine + 1; + } + private objectFunctionCallLine( + document: TextDocument, + selectionLine: number, + selectedVar: string + ): number { + let currentLineText: string = document.lineAt(selectionLine).text; + let nextLineText: string = document + .lineAt(selectionLine + 1) + .text.replace(/\s/g, ""); + if ( + /\((\s*)$/.test(currentLineText.split(selectedVar)[0]) || + /,(\s*)$/.test(currentLineText.split(selectedVar)[0]) + ) { + return selectionLine + 1; + } + let totalOpenedParenthesis = 0; + let totalClosedParenthesis = 0; + const { openedElementOccurrences, closedElementOccurrences } = + this.locOpenedClosedElementOccurrences( + currentLineText, + LocElement.Parenthesis + ); + totalOpenedParenthesis += openedElementOccurrences; + totalClosedParenthesis += closedElementOccurrences; + let currentLineNum = selectionLine + 1; + if ( + totalOpenedParenthesis !== totalClosedParenthesis || + currentLineText.endsWith(".") || + nextLineText.trim().startsWith(".") + ) { + while (currentLineNum < document.lineCount) { + currentLineText = document.lineAt(currentLineNum).text; + const { openedElementOccurrences, closedElementOccurrences } = + this.locOpenedClosedElementOccurrences( + currentLineText, + LocElement.Parenthesis + ); + totalOpenedParenthesis += openedElementOccurrences; + totalClosedParenthesis += closedElementOccurrences; + if (currentLineNum === document.lineCount - 1) { + break; + } + nextLineText = document.lineAt(currentLineNum + 1).text; + currentLineNum++; + if ( + totalOpenedParenthesis === totalClosedParenthesis && + !currentLineText.endsWith(".") && + !nextLineText.trim().startsWith(".") + ) { + break; + } + } + } + return totalOpenedParenthesis === totalClosedParenthesis + ? currentLineNum + : selectionLine + 1; + } + private functionAssignmentLine( + document: TextDocument, + selectionLine: number + ): number { + const currentLineText = document.lineAt(selectionLine).text; + if (/{/.test(currentLineText)) { + return ( + this.closingElementLine(document, selectionLine, LocElement.Braces) + 1 + ); + } else { + const closedParenthesisLine = this.closingElementLine( + document, + selectionLine, + LocElement.Parenthesis + ); + return ( + this.closingElementLine( + document, + closedParenthesisLine, + LocElement.Braces + ) + 1 + ); + } + } + private templateStringLine( + document: TextDocument, + selectionLine: number + ): number { + let currentLineText: string = document.lineAt(selectionLine).text; + let currentLineNum: number = selectionLine + 1; + let nbrOfBackticks: number = (currentLineText.match(/`/g) || []).length; + while (currentLineNum < document.lineCount) { + const currentLineText: string = document.lineAt(currentLineNum).text; + nbrOfBackticks += (currentLineText.match(/`/g) || []).length; + if (nbrOfBackticks % 2 === 0) { + break; + } + currentLineNum++; + } + return nbrOfBackticks % 2 === 0 ? currentLineNum + 1 : selectionLine + 1; + } + private arrayLine(document: TextDocument, selectionLine: number): number { + let currentLineText: string = document.lineAt(selectionLine).text; + let nbrOfOpenedBrackets: number = (currentLineText.match(/\[/g) || []) + .length; + let nbrOfClosedBrackets: number = (currentLineText.match(/\]/g) || []) + .length; + let currentLineNum: number = selectionLine + 1; + if (nbrOfOpenedBrackets !== nbrOfClosedBrackets) { + while (currentLineNum < document.lineCount) { + const currentLineText: string = document.lineAt(currentLineNum).text; + nbrOfOpenedBrackets += (currentLineText.match(/\[/g) || []).length; + nbrOfClosedBrackets += (currentLineText.match(/\]/g) || []).length; + currentLineNum++; + if (nbrOfOpenedBrackets === nbrOfClosedBrackets) { + break; + } + } + } + return nbrOfOpenedBrackets === nbrOfClosedBrackets + ? currentLineNum + : selectionLine + 1; + } + // Line for a variable which is in multiline context (function paramter, or deconstructred object) + private getMultiLineVariableLine( + document: TextDocument, + lineNum: number, + blockType: LocElement + ): number | null { + let currentLineNum = lineNum - 1; + let nbrOfOpenedBlockType: number = 0; + let nbrOfClosedBlockType: number = 1; // Closing parenthesis + while (currentLineNum >= 0) { + const currentLineText: string = document.lineAt(currentLineNum).text; + const currentLineParenthesis = this.locOpenedClosedElementOccurrences( + currentLineText, + blockType + ); + nbrOfOpenedBlockType += currentLineParenthesis.openedElementOccurrences; + nbrOfClosedBlockType += currentLineParenthesis.closedElementOccurrences; + if (nbrOfOpenedBlockType === nbrOfClosedBlockType) { + return this.closingElementLine(document, currentLineNum, blockType) + 1; + } + currentLineNum--; + } + return null; + } + private functionOpenedBraceLine(docuemt: TextDocument, line: number) { + let nbrOfOpenedBraces = 0; + let nbrOfClosedBraces = 0; + while (line < docuemt.lineCount) { + const { openedElementOccurrences, closedElementOccurrences } = + this.locOpenedClosedElementOccurrences( + this.lineText(docuemt, line), + LocElement.Braces + ); + nbrOfOpenedBraces += openedElementOccurrences; + nbrOfClosedBraces += closedElementOccurrences; + if ( + nbrOfOpenedBraces - nbrOfClosedBraces === 1 || + nbrOfOpenedBraces - nbrOfClosedBraces === 0 + ) { + return line; + } + line++; + } + return -1; + } + spacesBefore(document: TextDocument, line: number, tabSize: number): string { + const currentLine: TextLine = document.lineAt(line); + const currentLineTextChars: string[] = currentLine.text.split(""); + if ( + (!this.lineCodeProcessing.isFunctionAssignedToVariable( + currentLine.text + ) && + this.lineCodeProcessing.doesContainsNamedFunctionDeclaration( + currentLine.text + )) || + this.lineCodeProcessing.doesContainsBuiltInFunction(currentLine.text) || + this.lineCodeProcessing.doesContainClassDeclaration(currentLine.text) + ) { + const nextLine: TextLine = document.lineAt(line + 1); + const nextLineTextChars: string[] = nextLine.text.split(""); + if (nextLineTextChars.filter((char) => char !== " ").length !== 0) { + if ( + nextLine.firstNonWhitespaceCharacterIndex > + currentLine.firstNonWhitespaceCharacterIndex + ) { + if ( + nextLineTextChars[nextLine.firstNonWhitespaceCharacterIndex - 1] === + "\t" + ) { + return " ".repeat( + nextLine.firstNonWhitespaceCharacterIndex * tabSize + ); + } else { + return " ".repeat(nextLine.firstNonWhitespaceCharacterIndex); + } + } else { + if ( + currentLineTextChars[ + currentLine.firstNonWhitespaceCharacterIndex - 1 + ] === "\t" + ) { + return " ".repeat( + currentLine.firstNonWhitespaceCharacterIndex * tabSize + ); + } else { + return " ".repeat(currentLine.firstNonWhitespaceCharacterIndex); + } + } + } else { + if ( + currentLineTextChars[ + currentLine.firstNonWhitespaceCharacterIndex - 1 + ] === "\t" + ) { + return " ".repeat( + currentLine.firstNonWhitespaceCharacterIndex * tabSize + ); + } else { + return " ".repeat(currentLine.firstNonWhitespaceCharacterIndex); + } + } + } else { + if ( + currentLineTextChars[ + currentLine.firstNonWhitespaceCharacterIndex - 1 + ] === "\t" + ) { + return " ".repeat( + currentLine.firstNonWhitespaceCharacterIndex * tabSize + ); + } else { + return " ".repeat(currentLine.firstNonWhitespaceCharacterIndex); + } + } + } + enclosingBlockName( + document: TextDocument, + lineOfSelectedVar: number, + blockType: BlockType + ): string { + let currentLineNum: number = lineOfSelectedVar; + while (currentLineNum >= 0) { + const currentLineText: string = document.lineAt(currentLineNum).text; + switch (blockType) { + case "class": + if ( + this.lineCodeProcessing.doesContainClassDeclaration(currentLineText) + ) { + if ( + lineOfSelectedVar > currentLineNum && + lineOfSelectedVar < + this.closingElementLine( + document, + currentLineNum, + LocElement.Braces + ) + ) { + return `${this.lineCodeProcessing.getClassName(currentLineText)}`; + } + } + break; + case "function": + if ( + this.lineCodeProcessing.doesContainsNamedFunctionDeclaration( + currentLineText + ) && + !this.lineCodeProcessing.doesContainsBuiltInFunction( + currentLineText + ) + ) { + if ( + lineOfSelectedVar >= currentLineNum && + lineOfSelectedVar < + this.closingElementLine( + document, + currentLineNum, + LocElement.Braces + ) + ) { + if ( + this.lineCodeProcessing.getFunctionName(currentLineText) + .length !== 0 + ) { + return `${this.lineCodeProcessing.getFunctionName( + currentLineText + )}`; + } + return ""; + } + } + break; + } + currentLineNum--; + } + return ""; + } + detectAll( + document: TextDocument, + tabSize: number, + delemiterInsideMessage: string, + quote: string, + ): Message[] { + const documentNbrOfLines: number = document.lineCount; + const logMessages: Message[] = []; + for (let i = 0; i < documentNbrOfLines; i++) { + const turboConsoleLogMessage: RegExp = /Debug\.Log\(/; + if (turboConsoleLogMessage.test(document.lineAt(i).text)) { + const logMessage: Message = { + spaces: "", + lines: [], + }; + logMessage.spaces = this.spacesBefore(document, i, tabSize); + const closedParenthesisLine = this.closingElementLine( + document, + i, + LocElement.Parenthesis + ); + let msg = ""; + for (let j = i; j <= closedParenthesisLine; j++) { + msg += document.lineAt(j).text; + logMessage.lines.push(document.lineAt(j).rangeIncludingLineBreak); + } + if ( + new RegExp( + `${delemiterInsideMessage}{1}[a-zA-Z0-9]+[0-9{}]*${quote}(,|\\+)[a-zA-Z0-9.]+\\){1}` + ).test(msg.replace(/\s/g, "")) + ) { + logMessages.push(logMessage); + } + } + } + return logMessages; + } +} + + diff --git a/src/debug-message/index.ts b/src/debug-message/index.ts index e9edba1..74a8809 100644 --- a/src/debug-message/index.ts +++ b/src/debug-message/index.ts @@ -9,7 +9,6 @@ export abstract class DebugMessage { this.lineCodeProcessing = lineCodeProcessing; } abstract msg( - language: string, textEditor: TextEditorEdit, document: TextDocument, selectedVar: string, @@ -39,7 +38,6 @@ export abstract class DebugMessage { tabSize: number, delemiterInsideMessage: string, quote: string, - language: string ): Message[]; abstract enclosingBlockName( document: TextDocument, diff --git a/src/debug-message/js/index.ts b/src/debug-message/js/index.ts index a8caae0..ce184fa 100644 --- a/src/debug-message/js/index.ts +++ b/src/debug-message/js/index.ts @@ -9,7 +9,6 @@ export class JSDebugMessage extends DebugMessage { super(lineCodeProcessing); } msg( - language: string, textEditor: TextEditorEdit, document: TextDocument, selectedVar: string, @@ -57,30 +56,20 @@ export class JSDebugMessage extends DebugMessage { logMessagePrefix = `${delemiterInsideMessage} `; } - const debuggingMsg: string = this.getCommand(language) + `${quote}${logMessagePrefix}${ - logMessagePrefix.length !== 0 && + const debuggingMsg: string = `console.log(${quote}${logMessagePrefix}${logMessagePrefix.length !== 0 && logMessagePrefix !== `${delemiterInsideMessage} ` - ? ` ${delemiterInsideMessage} ` + ? ` ${delemiterInsideMessage} ` + : ""}${includeFileNameAndLineNum + ? `file: ${fileName} ${delemiterInsideMessage} line ${lineOfLogMsg + 1} ${delemiterInsideMessage} ` + : ""}${insertEnclosingClass + ? classThatEncloseTheVar.length > 0 + ? `${classThatEncloseTheVar} ${delemiterInsideMessage} ` + : `` + : ""}${insertEnclosingFunction + ? funcThatEncloseTheVar.length > 0 + ? `${funcThatEncloseTheVar} ${delemiterInsideMessage} ` : "" - }${ - includeFileNameAndLineNum - ? `file: ${fileName} ${delemiterInsideMessage} line ${ - lineOfLogMsg + 1 - } ${delemiterInsideMessage} ` - : "" - }${ - insertEnclosingClass - ? classThatEncloseTheVar.length > 0 - ? `${classThatEncloseTheVar} ${delemiterInsideMessage} ` - : `` - : "" - }${ - insertEnclosingFunction - ? funcThatEncloseTheVar.length > 0 - ? `${funcThatEncloseTheVar} ${delemiterInsideMessage} ` - : "" - : "" - }` + this.getProlog(language, quote, selectedVar, semicolon); + : ""}${selectedVar}${quote}, ${selectedVar})${semicolon}`; if (wrapLogMessage) { // 16 represents the length of console.log(""); @@ -556,12 +545,11 @@ export class JSDebugMessage extends DebugMessage { tabSize: number, delemiterInsideMessage: string, quote: string, - language: string ): Message[] { const documentNbrOfLines: number = document.lineCount; const logMessages: Message[] = []; for (let i = 0; i < documentNbrOfLines; i++) { - const turboConsoleLogMessage: RegExp = this.getLogCommand(language); + const turboConsoleLogMessage: RegExp = /console\.log\(/; if (turboConsoleLogMessage.test(document.lineAt(i).text)) { const logMessage: Message = { spaces: "", @@ -589,103 +577,6 @@ export class JSDebugMessage extends DebugMessage { } return logMessages; } - - getCommand(language: string): string - { - var debuggingMsg: string = ""; - if (language == "JS") { - debuggingMsg = "console.log("; - } - else if (language == "C#") { - debuggingMsg = "Console.WriteLine("; - } - else if (language == "Unity3D") { - debuggingMsg = "Debug.Log("; - } - return debuggingMsg; - } - - getProlog(language: string, quote: string, selectedVar: string, semicolon: string): string - { - var prolog : string = ""; - if (language == "JS") { - prolog = ` ${selectedVar}${quote}, ${selectedVar})${semicolon}`; - } - else if (language == "C#") { - prolog = ` ${selectedVar} {0}${quote}, ${selectedVar});`; - } - else if (language == "Unity3D") { - prolog = ` ${selectedVar} ${quote} \+ ${selectedVar});`; - } - return prolog; - } - - getDebugMessage( - language: string, - quote: string, - logMessagePrefix: string, - delemiterInsideMessage: string, - includeFileNameAndLineNum: boolean, - fileName: string, - lineOfLogMsg: number, - insertEnclosingClass: boolean, - classThatEncloseTheVar: string, - insertEnclosingFunction: boolean, - funcThatEncloseTheVar: string, - selectedVar: string, - semicolon: string): string - { - var debuggingMsg: string = ""; - if (language == "JS") { - debuggingMsg = `console.log(${quote}${logMessagePrefix}${logMessagePrefix.length !== 0 && - logMessagePrefix !== `${delemiterInsideMessage} ` - ? ` ${delemiterInsideMessage} ` - : ""}${includeFileNameAndLineNum - ? `file: ${fileName} ${delemiterInsideMessage} line ${lineOfLogMsg + 1} ${delemiterInsideMessage} ` - : ""}${insertEnclosingClass - ? classThatEncloseTheVar.length > 0 - ? `${classThatEncloseTheVar} ${delemiterInsideMessage} ` - : `` - : ""}${insertEnclosingFunction - ? funcThatEncloseTheVar.length > 0 - ? `${funcThatEncloseTheVar} ${delemiterInsideMessage} ` - : "" - : ""}${selectedVar}${quote}, ${selectedVar})${semicolon}`; - } - else if (language == "C#") { - debuggingMsg = `Console.WriteLine(${quote}${logMessagePrefix}${logMessagePrefix.length !== 0 && - logMessagePrefix !== `${delemiterInsideMessage} ` - ? ` ${delemiterInsideMessage} ` - : ""}${includeFileNameAndLineNum - ? `file: ${fileName} ${delemiterInsideMessage} line ${lineOfLogMsg + 1} ${delemiterInsideMessage} ` - : ""}${insertEnclosingClass - ? classThatEncloseTheVar.length > 0 - ? `${classThatEncloseTheVar} ${delemiterInsideMessage} ` - : `` - : ""}${insertEnclosingFunction - ? funcThatEncloseTheVar.length > 0 - ? `${funcThatEncloseTheVar} ${delemiterInsideMessage} ` - : "" - : ""}${selectedVar} {0}${quote}, ${selectedVar});`; - } - else if (language == "Unity3D") { - debuggingMsg = `Debug.Log(${quote}${logMessagePrefix}${logMessagePrefix.length !== 0 && - logMessagePrefix !== `${delemiterInsideMessage} ` - ? ` ${delemiterInsideMessage} ` - : ""}${includeFileNameAndLineNum - ? `file: ${fileName} ${delemiterInsideMessage} line ${lineOfLogMsg + 1} ${delemiterInsideMessage} ` - : ""}${insertEnclosingClass - ? classThatEncloseTheVar.length > 0 - ? `${classThatEncloseTheVar} ${delemiterInsideMessage} ` - : `` - : ""}${insertEnclosingFunction - ? funcThatEncloseTheVar.length > 0 - ? `${funcThatEncloseTheVar} ${delemiterInsideMessage} ` - : "" - : ""}${selectedVar} ${quote} \+ ${selectedVar});`; - } - return debuggingMsg; - } } diff --git a/src/entities/extensionProperties.ts b/src/entities/extensionProperties.ts index 8b5941d..39c2933 100644 --- a/src/entities/extensionProperties.ts +++ b/src/entities/extensionProperties.ts @@ -1,5 +1,5 @@ export type ExtensionProperties = { - language: string; + unity3d: boolean; wrapLogMessage: boolean; logMessagePrefix: string; addSemicolonInTheEnd: boolean; diff --git a/src/extension.ts b/src/extension.ts index ed681a9..97c89f1 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,13 +1,24 @@ import * as vscode from "vscode"; -import { DebugMessage } from "./debug-message"; -import { JSDebugMessage } from "./debug-message/js"; import { ExtensionProperties, Message } from "./entities"; + +import { DebugMessage } from "./debug-message"; import { LineCodeProcessing } from "./line-code-processing"; + +import { JSDebugMessage } from "./debug-message/js"; import { JSLineCodeProcessing } from "./line-code-processing/js"; +import { CSDebugMessage } from "./debug-message/cs"; +import { CSLineCodeProcessing } from "./line-code-processing/cs"; +import { CSUnityDebugMessage } from "./debug-message/csunity"; +import { runInThisContext } from "vm"; + +var lineCodeProcessing: LineCodeProcessing; +var debugMessage: DebugMessage; + export function activate(context: vscode.ExtensionContext) { - const jsLineCodeProcessing: LineCodeProcessing = new JSLineCodeProcessing(); - const jsDebugMessage: DebugMessage = new JSDebugMessage(jsLineCodeProcessing); + lineCodeProcessing = getLineCodeProcessing() + debugMessage = getDebugMessage(lineCodeProcessing); + // Insert debug message vscode.commands.registerCommand( "turboConsoleLog.displayLogMessage", @@ -17,6 +28,7 @@ export function activate(context: vscode.ExtensionContext) { if (!editor) { return; } + updateLanguage(); const tabSize: number | string = getTabSize(editor.options.tabSize); const document: vscode.TextDocument = editor.document; const config: vscode.WorkspaceConfiguration = @@ -29,13 +41,12 @@ export function activate(context: vscode.ExtensionContext) { // Check if the selection line is not the last one in the document and the selected variable is not empty if (selectedVar.trim().length !== 0) { await editor.edit((editBuilder) => { - const logMessageLine = jsDebugMessage.line( + const logMessageLine = debugMessage.line( document, lineOfSelectedVar, selectedVar ); - jsDebugMessage.msg( - properties.language, + debugMessage.msg( editBuilder, document, selectedVar, @@ -69,12 +80,11 @@ export function activate(context: vscode.ExtensionContext) { const config: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("turboConsoleLog"); const properties: ExtensionProperties = getExtensionProperties(config); - const logMessages: Message[] = jsDebugMessage.detectAll( + const logMessages: Message[] = debugMessage.detectAll( document, tabSize, properties.delimiterInsideMessage, - properties.quote, - properties.language + properties.quote ); editor.edit((editBuilder) => { logMessages.forEach(({ spaces, lines }) => { @@ -103,12 +113,11 @@ export function activate(context: vscode.ExtensionContext) { const config: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("turboConsoleLog"); const properties: ExtensionProperties = getExtensionProperties(config); - const logMessages: Message[] = jsDebugMessage.detectAll( + const logMessages: Message[] = debugMessage.detectAll( document, tabSize, properties.delimiterInsideMessage, - properties.quote, - properties.language + properties.quote ); editor.edit((editBuilder) => { logMessages.forEach(({ spaces, lines }) => { @@ -137,12 +146,11 @@ export function activate(context: vscode.ExtensionContext) { const config: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("turboConsoleLog"); const properties: ExtensionProperties = getExtensionProperties(config); - const logMessages: Message[] = jsDebugMessage.detectAll( + const logMessages: Message[] = debugMessage.detectAll( document, tabSize, properties.delimiterInsideMessage, - properties.quote, - properties.language + properties.quote ); editor.edit((editBuilder) => { logMessages.forEach(({ lines }) => { @@ -160,7 +168,7 @@ export function deactivate() {} function getExtensionProperties( workspaceConfig: vscode.WorkspaceConfiguration ) { - const language = workspaceConfig.language; + const unity3d = workspaceConfig.unity3d || false; const wrapLogMessage = workspaceConfig.wrapLogMessage || false; const logMessagePrefix = workspaceConfig.logMessagePrefix ? workspaceConfig.logMessagePrefix @@ -170,10 +178,9 @@ function getExtensionProperties( const insertEnclosingFunction = workspaceConfig.insertEnclosingFunction; const quote = workspaceConfig.quote || '"'; const delimiterInsideMessage = workspaceConfig.delimiterInsideMessage || "~"; - const includeFileNameAndLineNum = - workspaceConfig.includeFileNameAndLineNum || false; + const includeFileNameAndLineNum = workspaceConfig.includeFileNameAndLineNum || false; const extensionProperties: ExtensionProperties = { - language, + unity3d, wrapLogMessage, logMessagePrefix, addSemicolonInTheEnd, @@ -195,3 +202,45 @@ function getTabSize(tabSize: string | number | undefined): number { return 4; } } + +function updateLanguage() { + console.log(vscode.window.activeTextEditor?.document.languageId); + lineCodeProcessing = getLineCodeProcessing(); + debugMessage = getDebugMessage(lineCodeProcessing); +} + +function getLineCodeProcessing(){ + if(vscode.window.activeTextEditor?.document.languageId == "csharp") + { + return new CSLineCodeProcessing(); + } + else if(vscode.window.activeTextEditor?.document.languageId == "TypeScript" || vscode.window.activeTextEditor?.document.languageId == "javascript") + { + return new JSLineCodeProcessing(); + } + else + { + return new JSLineCodeProcessing(); + } +} + +function getDebugMessage(lcp :LineCodeProcessing) { + const config: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("turboConsoleLog"); + const properties: ExtensionProperties = getExtensionProperties(config); + + if(vscode.window.activeTextEditor?.document.languageId == "csharp") + { + if(properties.unity3d) + return new CSUnityDebugMessage(lcp); + else + return new CSDebugMessage(lcp); + } + else if(vscode.window.activeTextEditor?.document.languageId == "typescript" || vscode.window.activeTextEditor?.document.languageId == "javascript") + { + return new JSDebugMessage(lcp); + } + else + { + return new JSDebugMessage(lcp); + } +} diff --git a/src/line-code-processing/cs/index.ts b/src/line-code-processing/cs/index.ts new file mode 100644 index 0000000..19c9d8d --- /dev/null +++ b/src/line-code-processing/cs/index.ts @@ -0,0 +1,79 @@ +import { LineCodeProcessing } from ".."; + +export class CSLineCodeProcessing implements LineCodeProcessing { + isValueAssignedToVariable(loc: string): boolean { + return /.*=.*/.test(loc) && !/=>/.test(loc); + } + isObjectLiteralAssignedToVariable(loc: string): boolean { + const locWithoutWhiteSpaces = loc.replace(/\s/g, ""); + return /(const|let|var)[a-zA-Z0-9]*.*={.+:.+/.test(locWithoutWhiteSpaces); + } + isArrayAssignedToVariable(loc: string): boolean { + const locWithoutWhiteSpaces = loc.replace(/\s/g, ""); + return /(const|let|var).*=\[.*/.test(locWithoutWhiteSpaces); + } + doesContainClassDeclaration(loc: string): boolean { + return /class(\s+).*{/.test(loc); + } + getClassName(loc: string): string { + if (this.doesContainClassDeclaration(loc)) { + return loc.split("class ")[1].trim().split(" ")[0].replace("{", ""); + } else { + return ""; + } + } + doesContainsBuiltInFunction(loc: string): boolean { + const locWithoutWhiteSpaces = loc.replace(/\s/g, ""); + return /(if|switch|while|for|catch|do)\(.*\)/.test(locWithoutWhiteSpaces); + } + doesContainsNamedFunctionDeclaration(loc: string): boolean { + const locWithoutFunctionKeyword = loc.replace("function", ""); + const regularNamedFunctionRegex = new RegExp( + /\s*[a-zA-Z0-9]+\s*\(.*\):?.*{/ + ); + const regularFunctionAssignedToVariableRegex = new RegExp( + /(const|let|var)(\s*)[a-zA-Z0-9]*\s*=(\s*)\(.*\)(\s*){/ + ); + const arrowFunctionAssignedToVariableRegex = new RegExp( + /(const|let|var)(\s*)[a-zA-Z0-9]*\s*=(\s*)\(.*\):?\s*[a-zA-Z0-9]*(\s*)=>(\s*){/ + ); + return ( + regularNamedFunctionRegex.test(locWithoutFunctionKeyword) || + regularFunctionAssignedToVariableRegex.test(locWithoutFunctionKeyword) || + arrowFunctionAssignedToVariableRegex.test(loc) + ); + } + isFunctionAssignedToVariable(loc: string): boolean { + return /(const|let|var)(\s*)[a-zA-Z0-9]*\s*=.*\(.*/.test(loc); + } + isFunctionDeclaration(loc: string): boolean { + const locWithoutWhiteSpaces = loc.replace(/\s/g, ""); + return ( + /.*\(.*/.test(locWithoutWhiteSpaces) || /=>/.test(locWithoutWhiteSpaces) + ); + } + isObjectFunctionCall(loc: string): boolean { + const locWithoutWhiteSpaces = loc.replace(/\s/g, ""); + return /([a-zA-Z0-9]+\.){1,}/.test(locWithoutWhiteSpaces); + } + getFunctionName(loc: string): string { + if (this.doesContainsNamedFunctionDeclaration(loc)) { + if (/(const|let|var)(\s*)[a-zA-Z0-9]*\s*=/.test(loc)) { + return loc + .split("=")[0] + .replace(/export |module.exports |const |var |let |=|(\s*)/g, ""); + } else if (/function(\s+)/.test(loc)) { + return loc.split("function ")[1].split("(")[0].replace(/(\s*)/g, ""); + } else { + return loc + .split(/\(.*\)/)[0] + .replace( + /async |static |public |private |protected |export |default |(\s*)/g, + "" + ); + } + } else { + return ""; + } + } +} From ecfea2f2a87c7d23cdcc0aaccee7161622ec77e2 Mon Sep 17 00:00:00 2001 From: George Rowland Date: Fri, 11 Mar 2022 08:10:27 -0500 Subject: [PATCH 6/7] Added Checkbox for C# Unity3D feat: New extension setting to change Console.WriteLine to Debug.Log --- package.json | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index be816b4..c6d0b76 100644 --- a/package.json +++ b/package.json @@ -23,15 +23,10 @@ "type": "object", "title": "Turbo Console Log Configuration", "properties": { - "turboConsoleLog.language": { - "type": "string", - "enum": [ - "JS", - "C#", - "Unity3D" - ], - "default": "JS", - "description": "Javascript, C# (Console.WriteLine), or Unity3D (Debug.Log)" + "turboConsoleLog.unity3d": { + "type": "boolean", + "default": false, + "description": "Debug messages as Debug.Log for Unity 3d C#." }, "turboConsoleLog.wrapLogMessage": { "type": "boolean", From e717a21aed51cb2ff361f6be6231cfeb9c3acd52 Mon Sep 17 00:00:00 2001 From: George Rowland Date: Sat, 12 Mar 2022 09:00:31 -0500 Subject: [PATCH 7/7] Updated C# and C#-Unity to porperly detect all. refactor: CS debug-message regex refactor: CS unity debug-message regex --- src/debug-message/cs/index.ts | 2 +- src/debug-message/csunity/index.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/debug-message/cs/index.ts b/src/debug-message/cs/index.ts index 12551ad..523b617 100644 --- a/src/debug-message/cs/index.ts +++ b/src/debug-message/cs/index.ts @@ -557,7 +557,7 @@ export class CSDebugMessage extends DebugMessage { } if ( new RegExp( - `${delemiterInsideMessage}{1}[a-zA-Z0-9]+[0-9{}]*${quote}(,|\\+)[a-zA-Z0-9.]+\\){1}` + `${delemiterInsideMessage}{1}{[0-9]{1}}",[a-zA-Z0-9_-]\\);` ).test(msg.replace(/\s/g, "")) ) { logMessages.push(logMessage); diff --git a/src/debug-message/csunity/index.ts b/src/debug-message/csunity/index.ts index a8fd496..a401e4a 100644 --- a/src/debug-message/csunity/index.ts +++ b/src/debug-message/csunity/index.ts @@ -557,7 +557,7 @@ export class CSUnityDebugMessage extends DebugMessage { } if ( new RegExp( - `${delemiterInsideMessage}{1}[a-zA-Z0-9]+[0-9{}]*${quote}(,|\\+)[a-zA-Z0-9.]+\\){1}` + `${delemiterInsideMessage}{1}"\\+[a-zA-Z0-9_-]\\);` ).test(msg.replace(/\s/g, "")) ) { logMessages.push(logMessage);