diff --git a/output/data.json b/output/data.json index 735f46e..f6071a0 100644 --- a/output/data.json +++ b/output/data.json @@ -1 +1 @@ -{"3":["Improper Capitalized Primitive Type"],"4":["Improper Capitalized Primitive Type"],"12":["Code Smell: Consider refactoring to reduce method length"],"15":["Improper Capitalized Primitive Type","Improper Capitalized Primitive Type"],"16":["Improper Capitalized Primitive Type","Improper Capitalized Primitive Type"],"17":["Improper Capitalized Primitive Type"],"31":["Code Smell: Consider refactoring to reduce method length"],"50":["Improper Capitalized Primitive Type"]} \ No newline at end of file +{"3":["Improper Capitalized Primitive Type"],"4":["Improper Capitalized Primitive Type"],"12":["Code Smell: Consider refactoring to reduce method length"],"15":["Improper Capitalized Primitive Type","Improper Capitalized Primitive Type"],"16":["Improper Capitalized Primitive Type","Improper Capitalized Primitive Type"],"17":["Improper Capitalized Primitive Type"],"30":["Code smell found: Long Parameters in method public static int addNumbers (int a, int b, int c, int d){"],"38":["Code Smell: Consider refactoring to reduce method length"],"57":["Improper Capitalized Primitive Type"]} \ No newline at end of file diff --git a/package.json b/package.json index 2475643..1b7853c 100644 --- a/package.json +++ b/package.json @@ -92,6 +92,10 @@ { "command": "extension.longMethod", "title": "findLongMethod" + }, + { + "command": "extension.longParameters", + "title": "findLongParameters" } ], "menus": { diff --git a/src/extension.ts b/src/extension.ts index 7df4241..7032e36 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -12,6 +12,7 @@ import { findDuplicateCode } from './findDuplicateCode'; import { findLowercaseEnums } from './findLowercaseEnums'; import { findConstantCap} from './findConstantCap'; import { longMethod } from './longMethod'; +import { longParameters } from './longParameters'; const myMap: Map = new Map(); const filePath = 'C:\\Users\\Will\\Desktop\\cw\\gradeFast-1.0\\output\\data.json'; @@ -45,6 +46,14 @@ export function activate(context: vscode.ExtensionContext) { const disposable6 = vscode.commands.registerCommand('extension.findConstantCap', findConstantCap); + const disposable7 = vscode.commands.registerCommand('extension.longMethod', () => { + longMethod(myMap); + } ); + + const disposable8 = vscode.commands.registerCommand('extension.longParameters', () => { + longParameters(myMap); + } ); + const commentController = vscode.comments.createCommentController('comment-sample', 'Comment API Sample'); context.subscriptions.push(commentController); @@ -55,9 +64,5 @@ export function activate(context: vscode.ExtensionContext) { } }; - const disposable7 = vscode.commands.registerCommand('extension.longMethod', () => { - longMethod(myMap); - } ); - NoteCommentController.registerCommands(context); } diff --git a/src/longParameters.ts b/src/longParameters.ts new file mode 100644 index 0000000..14ff947 --- /dev/null +++ b/src/longParameters.ts @@ -0,0 +1,29 @@ +import * as vscode from 'vscode'; + +export function longParameters(myMap: Map): Map { + let editor = vscode.window.activeTextEditor; + if (!editor) { + vscode.window.showErrorMessage('No active text editor.'); + return myMap; // Early return if no editor + } + + let text = editor.document.getText(); + // Updated regex to match Java method declarations with 3 or more parameters and exclude main method and methods with 1 or 2 parameters + let methodRegex = /(\b(?:public|private|protected|static|\s)+[\w<>]+\s+(\w+)\s*\(([^)]+)\)\s*{)/g; + let match; + while ((match = methodRegex.exec(text))) { + let methodName = match[1]; + let parameters = match[3].split(',').map(param => param.trim()); + if (methodName !== 'main' && parameters.length >= 3) { + let lineIndex = editor.document.positionAt(match.index).line; + let errorMessage = `Code smell found: Long Parameters in method ${methodName}`; + if (!myMap.has(lineIndex)) { + myMap.set(lineIndex, [errorMessage]); + } else { + myMap.get(lineIndex)?.push(errorMessage); + } + } + } + + return myMap; +} \ No newline at end of file